use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class EvalLauncher method printVersions.
private static void printVersions() {
Engine engine = Engine.create();
System.out.println("GraalVM Polyglot Engine Version " + engine.getVersion());
System.out.println("Installed Languages: ");
for (Language language : engine.getLanguages().values()) {
System.out.printf(" %-10s: %-10s Version %s%n", language.getId(), language.getName(), language.getVersion());
}
System.out.println("Installed Instruments: ");
for (Instrument instrument : engine.getInstruments().values()) {
System.out.printf(" %-10s: %-10s Version %s%n", instrument.getId(), instrument.getName(), instrument.getVersion());
}
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class SLInstrumentTest method testLexicalScopes.
@Test
public void testLexicalScopes() throws Exception {
String code = "function test(n) {\n" + // 2
" a = 1;\n" + " if (a > 0) {\n" + " b = 10;\n" + // 5
" println(b);\n" + " }\n" + " if (a == 1) {\n" + " b = 20;\n" + " a = 0;\n" + // 10
" c = 1;\n" + " if (b > 0) {\n" + " a = 4;\n" + " b = 5;\n" + " c = 6;\n" + // 15
" d = 7;\n" + " println(d);\n" + " }\n" + " }\n" + " println(b);\n" + // 20
" println(a);\n" + "}\n" + "function main() {\n" + " test(\"n_n\");\n" + "}";
Source source = Source.newBuilder("sl", code, "testing").build();
List<Throwable> throwables;
try (Engine engine = Engine.newBuilder().out(new java.io.OutputStream() {
// null output stream
@Override
public void write(int b) throws IOException {
}
}).build()) {
Instrument envInstr = engine.getInstruments().get("testEnvironmentHandlerInstrument");
TruffleInstrument.Env env = envInstr.lookup(Environment.class).env;
throwables = new ArrayList<>();
env.getInstrumenter().attachExecutionEventListener(SourceSectionFilter.newBuilder().lineIn(1, source.getLineCount()).build(), new ExecutionEventListener() {
@Override
public void onEnter(EventContext context, VirtualFrame frame) {
Node node = context.getInstrumentedNode();
Iterable<Scope> lexicalScopes = env.findLocalScopes(node, null);
Iterable<Scope> dynamicScopes = env.findLocalScopes(node, frame);
try {
verifyLexicalScopes(lexicalScopes, dynamicScopes, context.getInstrumentedSourceSection().getStartLine(), frame.materialize());
} catch (ThreadDeath t) {
throw t;
} catch (Throwable t) {
CompilerDirectives.transferToInterpreter();
PrintStream lsErr = System.err;
lsErr.println("Line = " + context.getInstrumentedSourceSection().getStartLine());
lsErr.println("Node = " + node + ", class = " + node.getClass().getName());
t.printStackTrace(lsErr);
throwables.add(t);
}
}
@Override
public void onReturnValue(EventContext context, VirtualFrame frame, Object result) {
}
@Override
public void onReturnExceptional(EventContext context, VirtualFrame frame, Throwable exception) {
}
});
Context.newBuilder().engine(engine).build().eval(source);
}
assertTrue(throwables.toString(), throwables.isEmpty());
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class SLInstrumentTest method testOutput.
@Test
public void testOutput() throws IOException {
String code = "function main() {\n" + " f = fac(5);\n" + " println(f);\n" + "}\n" + "function fac(n) {\n" + " println(n);\n" + " if (n <= 1) {\n" + // break
" return 1;\n" + " }\n" + " return n * fac(n - 1);\n" + "}\n";
String fullOutput = "5\n4\n3\n2\n1\n120\n";
String fullLines = "[5, 4, 3, 2, 1, 120]";
// Pure exec:
Source source = Source.newBuilder("sl", code, "testing").build();
ByteArrayOutputStream engineOut = new ByteArrayOutputStream();
Engine engine = Engine.newBuilder().out(engineOut).build();
Context context = Context.newBuilder().engine(engine).build();
context.eval(source);
String engineOutput = fullOutput;
assertEquals(engineOutput, engineOut.toString());
// Check output
Instrument outInstr = engine.getInstruments().get("testEnvironmentHandlerInstrument");
TruffleInstrument.Env env = outInstr.lookup(Environment.class).env;
ByteArrayOutputStream consumedOut = new ByteArrayOutputStream();
EventBinding<ByteArrayOutputStream> outputConsumerBinding = env.getInstrumenter().attachOutConsumer(consumedOut);
assertEquals(0, consumedOut.size());
context.eval(source);
BufferedReader fromOutReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(consumedOut.toByteArray())));
engineOutput = engineOutput + fullOutput;
assertEquals(engineOutput, engineOut.toString());
assertTrue(fromOutReader.ready());
assertEquals(fullLines, readLinesList(fromOutReader));
// Check two output readers
ByteArrayOutputStream consumedOut2 = new ByteArrayOutputStream();
EventBinding<ByteArrayOutputStream> outputConsumerBinding2 = env.getInstrumenter().attachOutConsumer(consumedOut2);
assertEquals(0, consumedOut2.size());
context.eval(source);
fromOutReader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(consumedOut.toByteArray())));
BufferedReader fromOutReader2 = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(consumedOut2.toByteArray())));
engineOutput = engineOutput + fullOutput;
assertEquals(engineOutput, engineOut.toString());
assertTrue(fromOutReader.ready());
assertTrue(fromOutReader2.ready());
String fullLines2x = fullLines.substring(0, fullLines.length() - 1) + ", " + fullLines.substring(1);
assertEquals(fullLines2x, readLinesList(fromOutReader));
assertEquals(fullLines, readLinesList(fromOutReader2));
// One output reader closes, the other still receives the output
outputConsumerBinding.dispose();
consumedOut.reset();
consumedOut2.reset();
context.eval(source);
engineOutput = engineOutput + fullOutput;
assertEquals(engineOutput, engineOut.toString());
assertEquals(0, consumedOut.size());
assertTrue(consumedOut2.size() > 0);
fromOutReader2 = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(consumedOut2.toByteArray())));
assertEquals(fullLines, readLinesList(fromOutReader2));
// Remaining closes and pure exec successful:
consumedOut2.reset();
outputConsumerBinding2.dispose();
context.eval(source);
engineOutput = engineOutput + fullOutput;
assertEquals(engineOutput, engineOut.toString());
assertEquals(0, consumedOut.size());
assertEquals(0, consumedOut2.size());
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class InstrumentationTest method testDefaultId.
/*
* Test that metadata is properly propagated to Instrument handles.
*/
@Test
public void testDefaultId() {
Instrument descriptor1 = engine.getInstruments().get(MetadataInstrument2.class.getSimpleName());
Assert.assertEquals("", descriptor1.getName());
Assert.assertEquals("", descriptor1.getVersion());
Assert.assertEquals(MetadataInstrument2.class.getSimpleName(), descriptor1.getId());
Assert.assertFalse(isInitialized(descriptor1));
}
use of org.graalvm.polyglot.Instrument in project graal by oracle.
the class InstrumentationTest method testMetadata.
/*
* Test that metadata is properly propagated to Instrument handles.
*/
@Test
public void testMetadata() {
Instrument instrumentHandle1 = engine.getInstruments().get("testMetadataType1");
Assert.assertEquals("name", instrumentHandle1.getName());
Assert.assertEquals("version", instrumentHandle1.getVersion());
Assert.assertEquals("testMetadataType1", instrumentHandle1.getId());
Assert.assertFalse(isInitialized(instrumentHandle1));
Assert.assertFalse(isCreated(instrumentHandle1));
}
Aggregations