Search in sources :

Example 76 with Source

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);
}
Also used : Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source)

Example 77 with Source

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());
}
Also used : Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source)

Example 78 with Source

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));
}
Also used : Value(org.graalvm.polyglot.Value) Source(org.graalvm.polyglot.Source)

Example 79 with Source

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) {
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) DirectCallNode(com.oracle.truffle.api.nodes.DirectCallNode) ExecutableNode(com.oracle.truffle.api.nodes.ExecutableNode) Node(com.oracle.truffle.api.nodes.Node) ExecutionEventNode(com.oracle.truffle.api.instrumentation.ExecutionEventNode) InstrumentableNode(com.oracle.truffle.api.instrumentation.InstrumentableNode) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Instrumenter(com.oracle.truffle.api.instrumentation.Instrumenter) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 80 with Source

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;
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Aggregations

Source (org.graalvm.polyglot.Source)196 Test (org.junit.Test)165 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)103 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)95 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)51 Value (org.graalvm.polyglot.Value)31 Context (org.graalvm.polyglot.Context)25 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)21 DebugValue (com.oracle.truffle.api.debug.DebugValue)20 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)14 Instrument (org.graalvm.polyglot.Instrument)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 SourceSection (com.oracle.truffle.api.source.SourceSection)12 EventContext (com.oracle.truffle.api.instrumentation.EventContext)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Engine (org.graalvm.polyglot.Engine)9 ArrayList (java.util.ArrayList)8 PolyglotException (org.graalvm.polyglot.PolyglotException)8 SuspensionFilter (com.oracle.truffle.api.debug.SuspensionFilter)7 AbstractInstrumentationTest (com.oracle.truffle.api.instrumentation.test.AbstractInstrumentationTest)7