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));
}
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);
}
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]);
}
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;
}
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);
}
Aggregations