use of org.graalvm.polyglot.Instrument 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) {
}
}
use of org.graalvm.polyglot.Instrument 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;
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class InstrumentationTest method testAllocation.
@Test
public void testAllocation() throws Exception {
Instrument instrument = engine.getInstruments().get("testAllocation");
assureEnabled(instrument);
TestAllocation allocation = instrument.lookup(TestAllocation.class);
run("LOOP(3, VARIABLE(a, 10))");
engine.close();
engine = null;
assertEquals("[W 4 null, A 4 10, W 4 null, A 4 10, W 4 null, A 4 10]", allocation.getAllocations());
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class InstrumentationTest method testAccessInstruments.
@Test
public void testAccessInstruments() {
Instrument instrument = engine.getInstruments().get("testAccessInstruments");
TestAccessInstruments access = instrument.lookup(TestAccessInstruments.class);
InstrumentInfo info = access.env.getInstruments().get("testAccessInstruments");
assertNotNull(info);
assertEquals("testAccessInstruments", info.getId());
assertEquals("name", info.getName());
assertEquals("version", info.getVersion());
try {
access.env.lookup(info, TestAccessInstruments.class);
fail();
} catch (IllegalArgumentException e) {
// expected
}
TestAccessInstrumentsOther.initializedCount = 0;
InstrumentInfo other = access.env.getInstruments().get("testAccessInstrumentsOther");
assertNotNull(other);
assertEquals("testAccessInstrumentsOther", other.getId());
assertEquals("otherName", other.getName());
assertEquals("otherVersion", other.getVersion());
assertEquals(0, TestAccessInstrumentsOther.initializedCount);
// invalid service, should not trigger onCreate
assertNull(access.env.lookup(other, Object.class));
assertEquals(0, TestAccessInstrumentsOther.initializedCount);
// valide service, should trigger onCreate
assertNotNull(access.env.lookup(other, TestAccessInstrumentsOther.class));
assertEquals(1, TestAccessInstrumentsOther.initializedCount);
}
use of org.graalvm.polyglot.Instrument 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);
}
Aggregations