/** * * * @since 1.0 */ public static Source create(String language, CharSequence source) { return newBuilder(language, source, "Unnamed").buildLiteral(); }
/** * Shortcut for creating a source object from a language and char sequence. The given characters * must not mutate after they were accessed for the first time. * * @since 1.0 */ public static Source create(String language, CharSequence source) { return newBuilder(language, source, "Unnamed").buildLiteral(); }
@Override public Source getSource() throws IOException { if (source == null) { source = Source.newBuilder(getLanguage(), new File(file)).build(); } return source; }
public void callRFunctionFromJava() { Source src = Source.newBuilder("R", "qbinom", "qbinom.R").buildLiteral(); BinomQuantile func = engine.eval(src).as(BinomQuantile.class); assertEquals(4, func.qbinom(0.37, 10, 0.5)); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithR#callRFunctionFromJava
public void accessFieldsOfJavaObject() { Source src = Source.newBuilder("js", "" + "(function(t) {\n" + " return 3600 * t.hours + 60 * t.minutes + t.seconds;\n" + "})\n", "MomentToSeconds.js").buildLiteral(); final Moment javaMoment = new Moment(6, 30, 10); // Evaluate the JavaScript function definition Value jsFunction = context.eval(src); // Execute the JavaScript function, passing a Java object argument Value jsSeconds = jsFunction.execute(javaMoment); // Convert foreign object result to desired Java type int seconds = jsSeconds.as(Number.class).intValue(); assertEquals(3600 * 6 + 30 * 60 + 10, seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#accessFieldsOfJavaObject
public void accessFieldsOfJavaObject() { Source src = Source.newBuilder("js", "" + "(function(t) {\n" + " return 3600 * t.hours + 60 * t.minutes + t.seconds;\n" + "})\n", "MomentToSeconds.js").buildLiteral(); final Moment javaMoment = new Moment(6, 30, 10); // Evaluate the JavaScript function definition Value jsFunction = context.eval(src); // Execute the JavaScript function, passing a Java object argument Value jsSeconds = jsFunction.execute(javaMoment); // Convert foreign object result to desired Java type int seconds = jsSeconds.as(Number.class).intValue(); assertEquals(3600 * 6 + 30 * 60 + 10, seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#accessFieldsOfJavaObject
public void callRFunctionFromJava() { Source src = Source.newBuilder("R", "qbinom", "qbinom.R").buildLiteral(); BinomQuantile func = engine.eval(src).as(BinomQuantile.class); assertEquals(4, func.qbinom(0.37, 10, 0.5)); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithR#callRFunctionFromJava
public void accessFieldsOfJavaObjectWithConverter() { Source src = Source.newBuilder("js", "" + "(function(t) {\n" + " return 3600 * t.hours + 60 * t.minutes + t.seconds;\n" + "})\n", "MomentToSeconds.js").buildLiteral(); final Moment javaMoment = new Moment(6, 30, 10); // Evaluate the JavaScript function definition final Value jsFunction = context.eval(src); // Convert the function to desired Java type MomentConverter converter = jsFunction.as(MomentConverter.class); // Execute the JavaScript function as a Java foreign function int seconds = converter.toSeconds(javaMoment); assertEquals(3600 * 6 + 30 * 60 + 10, seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#accessFieldsOfJavaObjectWithConverter
public void accessFieldsOfJavaObjectWithConverter() { Source src = Source.newBuilder("js", "" + "(function(t) {\n" + " return 3600 * t.hours + 60 * t.minutes + t.seconds;\n" + "})\n", "MomentToSeconds.js").buildLiteral(); final Moment javaMoment = new Moment(6, 30, 10); // Evaluate the JavaScript function definition final Value jsFunction = context.eval(src); // Convert the function to desired Java type MomentConverter converter = jsFunction.as(MomentConverter.class); // Execute the JavaScript function as a Java foreign function int seconds = converter.toSeconds(javaMoment); assertEquals(3600 * 6 + 30 * 60 + 10, seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#accessFieldsOfJavaObjectWithConverter
public void accessJavaScriptArrayWithTypedElementsFromJava() { Source src = Source.newBuilder("js", "" + "(function() {\n" + " class Point {\n" + " constructor(x, y) {\n" + " this.x = x;\n" + " this.y = y;\n" + " }\n" + " }\n" + " return [ new Point(30, 15), new Point(5, 7) ];\n" + "})\n", "ArrayOfPoints.js").buildLiteral(); // Evaluate the JavaScript function definition Value jsFunction = context.eval(src); // Create Java-typed access to the JavaScript function PointProvider pointProvider = jsFunction.as(PointProvider.class); // Invoke the JavaScript function to generate points List<Point> points = pointProvider.createPoints(); assertEquals("Two points", 2, points.size()); Point first = points.get(0); assertEquals(30, first.x()); assertEquals(15, first.y()); Point second = points.get(1); assertEquals(5, second.x()); assertEquals(7, second.y()); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#accessJavaScriptArrayWithTypedElementsFromJava
public void accessJavaScriptArrayWithTypedElementsFromJava() { Source src = Source.newBuilder("js", "" + "(function() {\n" + " class Point {\n" + " constructor(x, y) {\n" + " this.x = x;\n" + " this.y = y;\n" + " }\n" + " }\n" + " return [ new Point(30, 15), new Point(5, 7) ];\n" + "})\n", "ArrayOfPoints.js").buildLiteral(); // Evaluate the JavaScript function definition Value jsFunction = context.eval(src); // Create Java-typed access to the JavaScript function PointProvider pointProvider = jsFunction.as(PointProvider.class); // Invoke the JavaScript function to generate points List<Point> points = pointProvider.createPoints(); assertEquals("Two points", 2, points.size()); Point first = points.get(0); assertEquals(30, first.x()); assertEquals(15, first.y()); Point second = points.get(1); assertEquals(5, second.x()); assertEquals(7, second.y()); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#accessJavaScriptArrayWithTypedElementsFromJava
@Override public Value eval(String script, boolean literal) { final Source source = Source .newBuilder("js", script, "<shell>") .interactive(literal) .buildLiteral(); if (literal) { try { return context.eval(source); } catch (PolyglotException e) { // in this special case we wrap and hide the polyglot type // so we can keep a contract outside graal throw new ScriptException(e, e.isIncompleteSource(), e.isExit()); } } else { return context.eval(source); } }
public void callRubyFunctionFromJava() { Source src = Source.newBuilder("ruby", "proc { |a, b|\n" + " a * b" + "}", "mul.rb").buildLiteral(); // Evaluate Ruby function definition Value rbFunction = context.eval(src); // Create Java access to Ruby function Multiplier mul = rbFunction.as(Multiplier.class); assertEquals(42, mul.multiply(6, 7)); assertEquals(144, mul.multiply(12, 12)); assertEquals(256, mul.multiply(32, 8)); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithRuby#callRubyFunctionFromJava
public void createJavaScriptFactoryForJavaClass() { Source src = Source.newBuilder("js", "" + "(function(Moment) {\n" + " return function(h, m, s) {\n" + " return new Moment(h, m, s);\n" + " };\n" + "})\n", "ConstructMoment.js").buildLiteral(); // Evaluate the JavaScript function definition final Value jsFunction = context.eval(src); // Create a JavaScript factory for the provided Java class final Value jsFactory = jsFunction.execute(Moment.class); // Convert the JavaScript factory to a Java foreign function MomentFactory momentFactory = jsFactory.as(MomentFactory.class); final Moment javaMoment = momentFactory.create(6, 30, 10); assertEquals("Hours", 6, javaMoment.hours); assertEquals("Minutes", 30, javaMoment.minutes); assertEquals("Seconds", 10, javaMoment.seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#createJavaScriptFactoryForJavaClass
public void callJavaScriptFunctionFromJava() { Source src = Source.newBuilder("js", "(function (a, b) {\n" + " return a * b;\n" + "})\n", "mul.js").buildLiteral(); // Evaluate JavaScript function definition Value jsFunction = context.eval(src); // Create Java access to JavaScript function Multiplier mul = jsFunction.as(Multiplier.class); assertEquals(42, mul.multiply(6, 7)); assertEquals(144, mul.multiply(12, 12)); assertEquals(256, mul.multiply(32, 8)); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#callJavaScriptFunctionFromJava
public void callJavaScriptFunctionFromJava() { Source src = Source.newBuilder("js", "(function (a, b) {\n" + " return a * b;\n" + "})\n", "mul.js").buildLiteral(); // Evaluate JavaScript function definition Value jsFunction = context.eval(src); // Create Java access to JavaScript function Multiplier mul = jsFunction.as(Multiplier.class); assertEquals(42, mul.multiply(6, 7)); assertEquals(144, mul.multiply(12, 12)); assertEquals(256, mul.multiply(32, 8)); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#callJavaScriptFunctionFromJava
public void callRubyFunctionFromJava() { Source src = Source.newBuilder("ruby", "proc { |a, b|\n" + " a * b" + "}", "mul.rb").buildLiteral(); // Evaluate Ruby function definition Value rbFunction = context.eval(src); // Create Java access to Ruby function Multiplier mul = rbFunction.as(Multiplier.class); assertEquals(42, mul.multiply(6, 7)); assertEquals(144, mul.multiply(12, 12)); assertEquals(256, mul.multiply(32, 8)); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithRuby#callRubyFunctionFromJava
public void createJavaScriptFactoryForJavaClass() { Source src = Source.newBuilder("js", "" + "(function(Moment) {\n" + " return function(h, m, s) {\n" + " return new Moment(h, m, s);\n" + " };\n" + "})\n", "ConstructMoment.js").buildLiteral(); // Evaluate the JavaScript function definition final Value jsFunction = context.eval(src); // Create a JavaScript factory for the provided Java class final Value jsFactory = jsFunction.execute(Moment.class); // Convert the JavaScript factory to a Java foreign function MomentFactory momentFactory = jsFactory.as(MomentFactory.class); final Moment javaMoment = momentFactory.create(6, 30, 10); assertEquals("Hours", 6, javaMoment.hours); assertEquals("Minutes", 30, javaMoment.minutes); assertEquals("Seconds", 10, javaMoment.seconds); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#createJavaScriptFactoryForJavaClass
public void callJavaScriptFunctionsWithSharedStateFromJava() { Source src = Source.newBuilder("js", "" + "(function() {\n" + " var seconds = 0;\n" + " function addTime(h, m, s) {\n" + " seconds += 3600 * h;\n" + " seconds += 60 * m;\n" + " seconds += s;\n" + " }\n" + " function time() {\n" + " return seconds;\n" + " }\n" + " return {\n" + " 'addTime': addTime,\n" + " 'timeInSeconds': time\n" + " }\n" + "})\n", "CountSeconds.js").buildLiteral(); // Evaluate JavaScript function definition Value jsFunction = context.eval(src); // Execute the JavaScript function Value jsObject = jsFunction.execute(); // Create Java access to the JavaScript object Counter counter = jsObject.as(Counter.class); counter.addTime(6, 30, 0); counter.addTime(9, 0, 0); counter.addTime(12, 5, 30); assertEquals(99330, counter.timeInSeconds()); } // END: com.oracle.truffle.tck.impl.PolyglotEngineWithJavaScript#callJavaScriptFunctionsWithSharedStateFromJava
public void callJavaScriptClassFactoryFromJava() { Source src = Source.newBuilder("js", "" + "(function() {\n" + " class JSIncrementor {\n" +