Search in sources :

Example 11 with Instrument

use of org.graalvm.polyglot.Instrument 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 12 with Instrument

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

the class SourceListenerTest method testLoadSourceNoRootSection.

@Test
public void testLoadSourceNoRootSection() throws Exception {
    Instrument instrument = engine.getInstruments().get("testLoadExecuteSource");
    TestLoadExecuteSource impl = instrument.lookup(TestLoadExecuteSource.class);
    impl.attachLoad();
    testNoRootSectionImpl(impl);
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Example 13 with Instrument

use of org.graalvm.polyglot.Instrument 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 14 with Instrument

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

the class InstrumentationTest method testLanguageInitializedOrNotAppend.

@Test
public void testLanguageInitializedOrNotAppend() throws Exception {
    Source initSource = Source.create(InstrumentationTestLanguage.ID, "STATEMENT(EXPRESSION, EXPRESSION)");
    setupEngine(initSource, true);
    Instrument instrument = engine.getInstruments().get("testLangInitialized");
    // Events during language initialization phase are prepended and appended:
    TestLangInitialized.initializationEvents = true;
    TestLangInitialized service = instrument.lookup(TestLangInitialized.class);
    run("LOOP(2, STATEMENT())");
    assertEquals("[FunctionRootNode, false, StatementNode, false, ExpressionNode, false, ExpressionNode, false, " + "FunctionRootNode, true, LoopNode, true, StatementNode, true, StatementNode, true, FunctionRootNode, true, StatementNode, true, ExpressionNode, true, ExpressionNode, 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)

Example 15 with Instrument

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

the class InstrumentationTest method queryInstrumentsBeforeUseAndObtainSpecialService.

@Test
public void queryInstrumentsBeforeUseAndObtainSpecialService() throws Exception {
    engine = Engine.newBuilder().err(err).build();
    Runnable start = null;
    for (Instrument instr : engine.getInstruments().values()) {
        Runnable r = instr.lookup(Runnable.class);
        if (r != null) {
            start = r;
            start.run();
            assertTrue("Now enabled: " + instr, isCreated(instr));
        }
    }
    assertNotNull("At least one Runnable found", start);
}
Also used : Instrument(org.graalvm.polyglot.Instrument) TruffleInstrument(com.oracle.truffle.api.instrumentation.TruffleInstrument) Test(org.junit.Test)

Aggregations

Instrument (org.graalvm.polyglot.Instrument)46 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)37 Test (org.junit.Test)33 Source (org.graalvm.polyglot.Source)14 Context (org.graalvm.polyglot.Context)12 TruffleContext (com.oracle.truffle.api.TruffleContext)11 Engine (org.graalvm.polyglot.Engine)10 EventContext (com.oracle.truffle.api.instrumentation.EventContext)7 ArrayList (java.util.ArrayList)5 Node (com.oracle.truffle.api.nodes.Node)4 RootNode (com.oracle.truffle.api.nodes.RootNode)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Language (org.graalvm.polyglot.Language)3 ExecutionEventNode (com.oracle.truffle.api.instrumentation.ExecutionEventNode)2 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)2 Instrumenter (com.oracle.truffle.api.instrumentation.Instrumenter)2 DirectCallNode (com.oracle.truffle.api.nodes.DirectCallNode)2 ExecutableNode (com.oracle.truffle.api.nodes.ExecutableNode)2 List (java.util.List)2 OptionDescriptor (org.graalvm.options.OptionDescriptor)2