use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLMain method executeSource.
private static int executeSource(Source source, InputStream in, PrintStream out, Map<String, String> options) {
Context context;
try {
context = Context.newBuilder(SL).in(in).out(out).options(options).build();
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
return 1;
}
out.println("== running on " + context.getEngine());
try {
Value result = context.eval(source);
if (context.getBindings(SL).getMember("main") == null) {
System.err.println("No function main() defined in SL source file.");
return 1;
}
if (!result.isNull()) {
out.println(result.toString());
}
return 0;
} catch (PolyglotException ex) {
if (ex.isInternalError()) {
// for internal errors we print the full stack trace
ex.printStackTrace();
} else {
System.err.println(ex.getMessage());
}
return 1;
} finally {
context.close();
}
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLDebugTest method testStackInterop.
@Test
public void testStackInterop() {
final Source stackSource = slCode("function fac(n, multiply) {\n" + " if (n <= 1) {\n" + " debugger;\n" + " return 1;\n" + " }\n" + " return multiply.multiply(n, fac, n - 1);\n" + "}\n");
Context context = Context.create("sl");
context.eval(stackSource);
Value fac = context.getBindings("sl").getMember("fac");
Object multiply = new Multiply();
Debugger debugger = context.getEngine().getInstruments().get("debugger").lookup(Debugger.class);
boolean[] done = new boolean[1];
try (DebuggerSession session = debugger.startSession((event) -> {
Iterator<DebugStackFrame> sfIt = event.getStackFrames().iterator();
assertTrue(sfIt.hasNext());
DebugStackFrame dsf = sfIt.next();
assertEquals("fac", dsf.getName());
assertEquals(3, dsf.getSourceSection().getStartLine());
assertFalse(dsf.isInternal());
int numStacksAt6 = 10 - 1;
int numInteropStacks = 0;
for (int i = 0; i < numStacksAt6; ) {
assertTrue(sfIt.hasNext());
dsf = sfIt.next();
boolean inFac = dsf.getName() != null && !dsf.isInternal();
if (inFac) {
// Frame in fac function
assertEquals("fac", dsf.getName());
assertEquals(6, dsf.getSourceSection().getStartLine());
assertFalse(dsf.isInternal());
i++;
} else {
// Frame in an interop method, internal
assertNull(dsf.getSourceSection());
assertTrue(dsf.isInternal());
numInteropStacks++;
}
}
// There were at least as many interop internal frames as frames in fac function:
assertTrue("numInteropStacks = " + numInteropStacks, numInteropStacks >= numStacksAt6);
// Some more internal frames remain
while (sfIt.hasNext()) {
dsf = sfIt.next();
assertNull(dsf.getSourceSection());
assertTrue(dsf.isInternal());
}
done[0] = true;
})) {
Assert.assertNotNull(session);
Value ret = fac.execute(10, multiply);
assertNumber(ret.asLong(), 3628800L);
}
assertTrue(done[0]);
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLInstrumentTest method testReplaceNodeReturnValue.
/**
* This test demonstrates that it's possible to easily replace a return value of any node using
* {@link ExecutionEventListener#onUnwind(com.oracle.truffle.api.instrumentation.EventContext, com.oracle.truffle.api.frame.VirtualFrame, java.lang.Object)}
* .
*/
@Test
public void testReplaceNodeReturnValue() throws Exception {
String code = "function main() {\n" + " a = new();\n" + " b = a.rp1;\n" + " return b;\n" + "}\n";
final Source source = Source.newBuilder("sl", code, "testing").build();
SourceSection ss = DebuggerTester.getSourceImpl(source).createSection(24, 5);
Context context = Context.create();
NewReplacedInstrument replaced = context.getEngine().getInstruments().get("testNewNodeReplaced").lookup(NewReplacedInstrument.class);
replaced.attachAt(ss);
Value ret = context.eval(source);
assertEquals("Replaced Value", ret.toString());
}
use of org.graalvm.polyglot.Context 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.Context in project graal by oracle.
the class SLInstrumentTest method testRedoIO.
/**
* Test that we reenter a node whose execution was interrupted. Unwind just the one node off.
*/
@Test
public void testRedoIO() throws Throwable {
String code = "function main() {\n" + " a = readln();\n" + " return a;\n" + "}\n";
final Source ioWait = Source.newBuilder("sl", code, "testing").build();
final TestRedoIO[] redoIOPtr = new TestRedoIO[1];
InputStream strIn = new ByteArrayInputStream("O.K.".getBytes());
InputStream delegateInputStream = new InputStream() {
@Override
public int read() throws IOException {
synchronized (SLInstrumentTest.class) {
// Block reading before we do unwind:
if (redoIOPtr[0].beforePop) {
redoIOPtr[0].inRead.release();
try {
SLInstrumentTest.class.wait();
} catch (InterruptedException ex) {
throw new RuntimeInterruptedException();
}
}
}
return strIn.read();
}
};
Engine engine = Engine.newBuilder().in(delegateInputStream).build();
TestRedoIO redoIO = engine.getInstruments().get("testRedoIO").lookup(TestRedoIO.class);
redoIOPtr[0] = redoIO;
redoIO.inRead.drainPermits();
Context context = Context.newBuilder().engine(engine).build();
Value ret = context.eval(ioWait);
assertEquals("O.K.", ret.asString());
assertFalse(redoIO.beforePop);
}
Aggregations