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