use of org.graalvm.polyglot.Context in project graal by oracle.
the class PolyglotNativeAPI method polyglot_create_function.
@CEntryPoint(name = "polyglot_create_function")
public static PolyglotStatus polyglot_create_function(IsolateThread isolate_thread, PolyglotContextPointer polyglot_context, PolyglotCallbackPointer callback, VoidPointer data, PolyglotValuePointerPointer value) {
return withHandledErrors(() -> {
Context c = ObjectHandles.getGlobal().get(polyglot_context);
ProxyExecutable executable = (Value... arguments) -> {
ObjectHandle[] handleArgs = new ObjectHandle[arguments.length];
for (int i = 0; i < arguments.length; i++) {
handleArgs[i] = createHandle(arguments[i]);
}
PolyglotCallbackInfo cbInfo = (PolyglotCallbackInfo) createHandle(new PolyglotCallbackInfoInternal(handleArgs, data));
try {
PolyglotValuePointer result = callback.invoke(CEntryPointContext.getCurrentIsolateThread(), cbInfo);
CallbackException ce = exceptionsTL.get();
if (ce != null) {
exceptionsTL.remove();
throw ce;
} else {
return PolyglotNativeAPI.fetchHandle(result);
}
} finally {
PolyglotCallbackInfoInternal info = fetchHandle(cbInfo);
for (ObjectHandle arg : info.arguments) {
freeHandle(arg);
}
freeHandle(cbInfo);
}
};
value.write(createHandle(c.asValue(executable)));
});
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class CPUSamplerSnippets method example.
@SuppressWarnings("unused")
public void example() {
// @formatter:off
// BEGIN: CPUSamplerSnippets#example
Context context = Context.create();
CPUSampler sampler = CPUSampler.find(context.getEngine());
sampler.setCollecting(true);
context.eval("...", "...");
sampler.setCollecting(false);
sampler.close();
// Read information about the roots of the tree.
for (ProfilerNode<CPUSampler.Payload> node : sampler.getRootNodes()) {
final String rootName = node.getRootName();
final int selfHitCount = node.getPayload().getSelfHitCount();
// ...
}
// END: CPUSamplerSnippets#example
// @formatter:on
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class CPUTracerSnippets method example.
@SuppressWarnings("unused")
public void example() {
// @formatter:off
// BEGIN: CPUTracerSnippets#example
Context context = Context.create();
CPUTracer tracer = CPUTracer.find(context.getEngine());
tracer.setCollecting(true);
context.eval("...", "...");
tracer.setCollecting(false);
tracer.close();
// Read information about execution counts of elements.
for (CPUTracer.Payload p : tracer.getPayloads()) {
final String rootName = p.getRootName();
final long count = p.getCount();
}
// END: CPUTracerSnippets#example
// @formatter:on
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLInstrumentTest method testChangeArgumentsOnReenter.
/**
* Test that we can alter function arguments on reenter.
*/
@Test
public void testChangeArgumentsOnReenter() throws Exception {
String code = "function main() {\n" + " y = fce(0, 10000);\n" + " return y;\n" + "}\n" + "function fce(x, z) {\n" + " y = 2 * x;\n" + " if (y < z) {\n" + " print(\"A bad error.\");\n" + " return 0 - 1;\n" + " } else {\n" + " return y;\n" + " }\n" + "}\n";
final Source source = Source.newBuilder("sl", code, "testing").build();
Context context = Context.create();
IncreaseArgOnErrorInstrument incOnError = context.getEngine().getInstruments().get("testIncreaseArgumentOnError").lookup(IncreaseArgOnErrorInstrument.class);
incOnError.attachOn("A bad error");
Value ret = context.eval(source);
assertEquals(10000, ret.asInt());
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class SLInstrumentTest method testEarlyReturn.
/**
* Test that we can forcibly return early from call nodes with an arbitrary value.
*/
@Test
public void testEarlyReturn() throws Exception {
String code = "function main() {\n" + " a = 10;\n" + " b = a;\n" + " // Let fce() warm up and specialize:\n" + " while (a == b && a < 100000) {\n" + " a = fce(a);\n" + " b = b + 1;\n" + " }\n" + " c = a;\n" + " // Run fce() and alter it's return type in an instrument:\n" + " c = fce(c);\n" + " return c;\n" + "}\n" + "function fce(x) {\n" + " return x + 1;\n" + "}\n";
final Source source = Source.newBuilder("sl", code, "testing").build();
ByteArrayOutputStream engineOut = new ByteArrayOutputStream();
Engine engine = Engine.newBuilder().err(engineOut).build();
Context context = Context.newBuilder().engine(engine).build();
// No instrument:
Value ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(100001L, ret.asLong());
EarlyReturnInstrument earlyReturn = context.getEngine().getInstruments().get("testEarlyReturn").lookup(EarlyReturnInstrument.class);
earlyReturn.fceCode = "fce(a)";
earlyReturn.returnValue = 200000L;
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(200001L, ret.asLong());
earlyReturn.returnValue = "Hello!";
ret = context.eval(source);
assertFalse(ret.isNumber());
assertTrue(ret.isString());
assertEquals("Hello!1", ret.asString());
// Specialize to long again:
earlyReturn.fceCode = "<>";
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(100001L, ret.asLong());
earlyReturn.fceCode = "fce(a)";
earlyReturn.returnValue = new BigInteger("-42");
boolean interopFailure;
try {
context.eval(source);
interopFailure = false;
} catch (PolyglotException err) {
interopFailure = true;
}
assertTrue(interopFailure);
earlyReturn.returnValue = new SLBigNumber(new BigInteger("-42"));
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(-41L, ret.asLong());
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = Boolean.TRUE;
ret = context.eval(source);
assertTrue(ret.isBoolean());
assertEquals(Boolean.TRUE, ret.asBoolean());
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = -42.42;
ret = context.eval(source);
assertTrue(ret.isNumber());
assertEquals(-42.42, ret.asDouble(), 1e-8);
earlyReturn.fceCode = "fce(c)";
earlyReturn.returnValue = "Hello!";
ret = context.eval(source);
assertTrue(ret.isString());
assertEquals("Hello!", ret.asString());
}
Aggregations