use of org.graalvm.polyglot.Source in project graal by oracle.
the class PolyglotEngineWithJavaScript method 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);
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class PolyglotEngineWithJavaScript method callJavaScriptClassFactoryFromJava.
public void callJavaScriptClassFactoryFromJava() {
Source src = Source.newBuilder("js", "" + "(function() {\n" + " class JSIncrementor {\n" + " constructor(init) {\n" + " this.value = init;\n" + " }\n" + " inc() {\n" + " return ++this.value;\n" + " }\n" + " dec() {\n" + " return --this.value;\n" + " }\n" + " }\n" + " return function(init) {\n" + " return new JSIncrementor(init);\n" + " }\n" + "})\n", "Incrementor.js").buildLiteral();
// Evaluate JavaScript function definition
Value jsFunction = context.eval(src);
// Execute the JavaScript function
Value jsFactory = jsFunction.execute();
// Execute the JavaScript factory to create Java objects
Incrementor initFive = jsFactory.execute(5).as(Incrementor.class);
Incrementor initTen = jsFactory.execute(10).as(Incrementor.class);
initFive.inc();
assertEquals("Now at seven", 7, initFive.inc());
initTen.dec();
assertEquals("Now at eight", 8, initTen.dec());
initTen.dec();
assertEquals("Values are the same", initFive.value(), initTen.value());
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class PolyglotEngineWithRuby method callRubyFunctionFromJava.
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));
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class InstrumentationTest method testQueryTags2.
/*
* Test behavior of queryTags when used with languages
*/
@Test
public void testQueryTags2() throws IOException {
Instrument instrument = engine.getInstruments().get("testIsNodeTaggedWith1");
assureEnabled(instrument);
TestIsNodeTaggedWith1.expressionNode = null;
TestIsNodeTaggedWith1.statementNode = null;
TestIsNodeTaggedWith1Language.instrumenter = null;
Source otherLanguageSource = Source.create("testIsNodeTaggedWith1-lang", "STATEMENT(EXPRESSION)");
run(otherLanguageSource);
Instrumenter instrumenter = TestIsNodeTaggedWith1Language.instrumenter;
Node languageExpression = TestIsNodeTaggedWith1.expressionNode;
Node languageStatement = TestIsNodeTaggedWith1.statementNode;
assertTags(instrumenter.queryTags(languageExpression), InstrumentationTestLanguage.EXPRESSION);
assertTags(instrumenter.queryTags(languageStatement), InstrumentationTestLanguage.STATEMENT);
TestIsNodeTaggedWith1.expressionNode = null;
TestIsNodeTaggedWith1.statementNode = null;
run("EXPRESSION");
// fail if called with nodes from a different language
Node otherLanguageExpression = TestIsNodeTaggedWith1.expressionNode;
try {
instrumenter.queryTags(otherLanguageExpression);
Assert.fail();
} catch (IllegalArgumentException e) {
}
}
use of org.graalvm.polyglot.Source in project graal by oracle.
the class InstrumentationTest method testLanguageInitializedOnly.
@Test
public void testLanguageInitializedOnly() throws Exception {
Source initSource = Source.create(InstrumentationTestLanguage.ID, "STATEMENT(EXPRESSION, EXPRESSION)");
setupEngine(initSource, false);
Instrument instrument = engine.getInstruments().get("testLangInitialized");
// Events during language initialization phase are excluded:
TestLangInitialized.initializationEvents = false;
TestLangInitialized service = instrument.lookup(TestLangInitialized.class);
run("LOOP(2, STATEMENT())");
assertEquals("[FunctionRootNode, true, LoopNode, true, StatementNode, true, StatementNode, true]", service.getEnteredNodes());
engine.close();
engine = null;
}
Aggregations