Search in sources :

Example 81 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class SourceListenerTest method testLoadExecuteSourceImpl.

private void testLoadExecuteSourceImpl(boolean load, int runTimes) throws IOException {
    int initialQueryCount = InstrumentationTestLanguage.getRootSourceSectionQueryCount();
    Instrument instrument = context.getEngine().getInstruments().get("testLoadExecuteSource");
    Source source1 = lines("STATEMENT(EXPRESSION, EXPRESSION)");
    // running the same source multiple times should not have any effect on the test result.
    for (int i = 0; i < runTimes; i++) {
        run(source1);
    }
    Assert.assertEquals("unexpected getSourceSection calls without source listeners", initialQueryCount, InstrumentationTestLanguage.getRootSourceSectionQueryCount());
    TestLoadExecuteSource impl = instrument.lookup(TestLoadExecuteSource.class);
    assertTrue("Lookup of registered service enables the instrument", isCreated(instrument));
    if (load) {
        impl.attachLoad();
    } else {
        impl.attachExecute();
    }
    Source source2 = lines("ROOT(DEFINE(f1, STATEMENT(EXPRESSION)), DEFINE(f2, STATEMENT)," + "BLOCK(CALL(f1), CALL(f2)))");
    for (int i = 0; i < runTimes; i++) {
        run(source2);
    }
    Assert.assertNotEquals("expecting getSourceSection calls because of source listeners", initialQueryCount, InstrumentationTestLanguage.getRootSourceSectionQueryCount());
    assertEvents(impl.onlyNewEvents, source2);
    assertEvents(impl.allEvents, source1, source2);
    // Load an internal source
    Source source3 = Source.newBuilder(InstrumentationTestLanguage.ID, "STATEMENT", "test").internal(true).build();
    for (int i = 0; i < runTimes; i++) {
        run(source3);
    }
    assertEvents(impl.onlyNewEvents, source2, source3);
    assertEvents(impl.allEvents, source1, source2, source3);
    assertEvents(impl.allNotInternalEvents, source1, source2);
    // Disable the instrument by closing the engine.
    engine.close();
    engine = null;
    engine = getEngine();
    Source source4 = lines("STATEMENT(EXPRESSION, EXPRESSION, EXPRESSION)");
    for (int i = 0; i < runTimes; i++) {
        run(source4);
    }
    assertEvents(impl.onlyNewEvents, source2, source3);
    assertEvents(impl.allEvents, source1, source2, source3);
    instrument = engine.getInstruments().get("testLoadExecuteSource");
    impl = instrument.lookup(TestLoadExecuteSource.class);
    if (load) {
        impl.attachLoad();
    } else {
        impl.attachExecute();
    }
    assertEvents(impl.onlyNewEvents);
    assertEvents(impl.allEvents, source4);
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Source(org.graalvm.polyglot.Source)

Example 82 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class SourceListenerTest method testBindingDisposalImpl.

private void testBindingDisposalImpl(boolean load) throws Exception {
    Instrument instrument = engine.getInstruments().get("testBindingDisposal");
    TestBindingDisposal impl = instrument.lookup(TestBindingDisposal.class);
    impl.doAttach(load);
    Source source1 = lines("STATEMENT");
    run(source1);
    assertEvents(impl.onlyNewEvents, source1);
    assertEvents(impl.allEvents, source1);
    impl.onlyNewBinding.dispose();
    impl.allBinding.dispose();
    // No new events received after bindings are disposed
    com.oracle.truffle.api.source.Source source2a = com.oracle.truffle.api.source.Source.newBuilder("line2a").mimeType("mime").name("NoName2a").build();
    com.oracle.truffle.api.source.Source source2b = com.oracle.truffle.api.source.Source.newBuilder("line2b").mimeType("mime").name("NoName2b").build();
    Node node2a = new SourceSectionFilterTest.SourceSectionNode(source2a.createSection(1));
    Node node2b = new SourceSectionFilterTest.SourceSectionNode(source2b.createSection(1));
    RootNode root2 = SourceSectionFilterTest.createRootNode(engine, null, Boolean.FALSE, node2a, node2b);
    Truffle.getRuntime().createCallTarget(root2).call();
    assertEvents(impl.onlyNewEvents, source1);
    assertEvents(impl.allEvents, source1);
    // Clear and reattach
    impl.onlyNewEvents.clear();
    impl.allEvents.clear();
    impl.doAttach(load);
    Source source3 = lines("VARIABLE(a, 10)");
    run(source3);
    assertEvents(impl.onlyNewEvents, source3);
    assertEvents(impl.allEvents, getSourceImpl(source1), source2a, source2b, getSourceImpl(source3));
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node) RootNode(com.oracle.truffle.api.nodes.RootNode) Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Source(org.graalvm.polyglot.Source)

Example 83 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class SourceSectionListenerTest method testLoadSourceSectionImpl.

private void testLoadSourceSectionImpl(int runTimes) throws IOException {
    Instrument instrument = engine.getInstruments().get("testLoadSourceSection1");
    SourceSection[] sourceSections1 = sections("STATEMENT(EXPRESSION, EXPRESSION)", "STATEMENT(EXPRESSION, EXPRESSION)", "EXPRESSION");
    final SourceSectionFilter statementFilter = SourceSectionFilter.newBuilder().tagIs(StandardTags.StatementTag.class).build();
    final SourceSectionFilter exprFilter = SourceSectionFilter.newBuilder().tagIs(InstrumentationTestLanguage.EXPRESSION).build();
    Source source1 = sourceSections1[0].getSource();
    for (int i = 0; i < runTimes; i++) {
        run(source1);
    }
    assureEnabled(instrument);
    TestLoadSourceSection1 impl = instrument.lookup(TestLoadSourceSection1.class);
    assertSections(impl.query(SourceSectionFilter.ANY), sourceSections1);
    assertSections(impl.query(statementFilter), sourceSections1[0]);
    assertSections(impl.query(exprFilter), sourceSections1[2], sourceSections1[3]);
    SourceSection[] sourceSections2 = sections("STATEMENT(EXPRESSION)", "STATEMENT(EXPRESSION)", "EXPRESSION");
    Source source2 = sourceSections2[0].getSource();
    for (int i = 0; i < runTimes; i++) {
        run(source2);
    }
    assertEvents(impl.allEvents, merge(sourceSections1, sourceSections2));
    assertEvents(impl.onlyNewEvents, sourceSections2);
    assertEvents(impl.onlyStatements, sourceSections1[0], sourceSections2[0]);
    assertEvents(impl.onlyExpressions, sourceSections1[2], sourceSections1[3], sourceSections2[2]);
    assertSections(impl.query(SourceSectionFilter.ANY), merge(sourceSections1, sourceSections2));
    assertSections(impl.query(statementFilter), sourceSections1[0], sourceSections2[0]);
    assertSections(impl.query(exprFilter), sourceSections1[2], sourceSections1[3], sourceSections2[2]);
    // disables the instrument
    engine.close();
    engine = null;
    engine = getEngine();
    SourceSection[] sourceSections3 = sections("STATEMENT(EXPRESSION, EXPRESSION, EXPRESSION)", "STATEMENT(EXPRESSION, EXPRESSION, EXPRESSION)", "EXPRESSION");
    Source source3 = sourceSections3[0].getSource();
    for (int i = 0; i < runTimes; i++) {
        run(source3);
    }
    assertEvents(impl.allEvents, merge(sourceSections1, sourceSections2));
    assertEvents(impl.onlyNewEvents, sourceSections2);
    assertEvents(impl.onlyStatements, sourceSections1[0], sourceSections2[0]);
    assertEvents(impl.onlyExpressions, sourceSections1[2], sourceSections1[3], sourceSections2[2]);
    assertSections(impl.query(SourceSectionFilter.ANY), merge(sourceSections1, sourceSections2));
    assertSections(impl.query(statementFilter), sourceSections1[0], sourceSections2[0]);
    assertEvents(impl.onlyExpressions, sourceSections1[2], sourceSections1[3], sourceSections2[2]);
    instrument = engine.getInstruments().get("testLoadSourceSection1");
    assureEnabled(instrument);
    // new instrument needs update
    impl = instrument.lookup(TestLoadSourceSection1.class);
    assertEvents(impl.onlyNewEvents);
    assertEvents(impl.allEvents, sourceSections3);
    assertEvents(impl.onlyStatements, sourceSections3[0]);
    assertEvents(impl.onlyExpressions, sourceSections3[2], sourceSections3[3], sourceSections3[4]);
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) SourceSection(org.graalvm.polyglot.SourceSection) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) Source(org.graalvm.polyglot.Source)

Example 84 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class SourceSectionListenerTest method sections.

private SourceSection[] sections(String code, String... match) throws IOException {
    Source source = Source.newBuilder(InstrumentationTestLanguage.ID, code, "sourceSectionTest").build();
    List<SourceSection> sections = new ArrayList<>();
    sections.add(createSection(source, 0, code.length()));
    for (String matchExpression : match) {
        int index = -1;
        while ((index = code.indexOf(matchExpression, index + 1)) != -1) {
            sections.add(createSection(source, index, matchExpression.length()));
        }
    }
    return sections.toArray(new SourceSection[0]);
}
Also used : ArrayList(java.util.ArrayList) SourceSection(org.graalvm.polyglot.SourceSection) Source(org.graalvm.polyglot.Source)

Example 85 with Source

use of org.graalvm.polyglot.Source in project graal by oracle.

the class InstrumentableNodeTest method testLateMaterializeSyntax.

/*
     * First instrument statements and then instrument expressions to test materialization at
     * locations where there is already a wrapper.
     */
@Test
public void testLateMaterializeSyntax() {
    Source source = createSource("MATERIALIZE_CHILD_EXPRESSION");
    SourceSectionFilter filter;
    filter = SourceSectionFilter.newBuilder().tagIs(StandardTags.StatementTag.class).build();
    instrumenter.attachExecutionEventFactory(filter, null, factory);
    execute(source);
    assertOn(ENTER, (e) -> {
        assertTrue(e.context.getInstrumentedNode() instanceof MaterializeChildExpressionNode);
    });
    assertOn(RETURN_VALUE, (e) -> {
        assertTrue(e.context.getInstrumentedNode() instanceof MaterializeChildExpressionNode);
    });
    assertAllEventsConsumed();
    filter = SourceSectionFilter.newBuilder().tagIs(StandardTags.ExpressionTag.class).build();
    instrumenter.attachExecutionEventFactory(filter, null, factory);
    execute(source);
    assertOn(ENTER, (e) -> {
        assertTrue(e.context.getInstrumentedNode() instanceof MaterializedChildExpressionNode);
    });
    assertOn(ENTER, (e) -> {
        assertTrue(e.context.getInstrumentedNode() instanceof ExpressionNode);
    });
    assertOn(RETURN_VALUE, (e) -> {
        assertTrue(e.context.getInstrumentedNode() instanceof ExpressionNode);
    });
    assertOn(RETURN_VALUE, (e) -> {
        assertTrue(e.context.getInstrumentedNode() instanceof MaterializedChildExpressionNode);
    });
}
Also used : MaterializeChildExpressionNode(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.MaterializeChildExpressionNode) ExpressionNode(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.ExpressionNode) MaterializeChildExpressionNode(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.MaterializeChildExpressionNode) MaterializedChildExpressionNode(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.MaterializedChildExpressionNode) MaterializedChildExpressionNode(com.oracle.truffle.api.instrumentation.test.InstrumentationTestLanguage.MaterializedChildExpressionNode) SourceSectionFilter(com.oracle.truffle.api.instrumentation.SourceSectionFilter) Source(org.graalvm.polyglot.Source) StandardTags(com.oracle.truffle.api.instrumentation.StandardTags) 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