use of org.graalvm.polyglot.proxy.ProxyExecutable in project graal by oracle.
the class PolyglotExceptionTest method testExceptionWrapping.
@Test
public void testExceptionWrapping() {
Context context = Context.create();
Context otherContext = Context.create();
CauseErrorTruffleObject causeError = new CauseErrorTruffleObject();
causeError.thrownError = new TestGuestError();
Value throwError = context.asValue(causeError);
Value throwErrorOtherContext = otherContext.asValue(causeError);
try {
throwError.execute();
Assert.fail();
} catch (PolyglotException e) {
Assert.assertEquals(e.getMessage(), "MyError");
Assert.assertTrue(e.isGuestException());
}
Value verifyError = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
try {
throwError.execute();
Assert.fail();
} catch (PolyglotException e) {
Assert.assertEquals(e.getMessage(), "MyError");
Assert.assertTrue(e.isGuestException());
throw e;
}
return null;
}
});
try {
verifyError.execute();
Assert.fail();
} catch (PolyglotException e) {
Assert.assertEquals(e.getMessage(), "MyError");
Assert.assertTrue(e.isGuestException());
}
// if the exception was thrown by a different context it will be treated
// as a host exception.
Value verifyErrorOtherContext = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
try {
throwErrorOtherContext.execute();
} catch (PolyglotException e) {
Assert.assertEquals(e.getMessage(), "MyError");
Assert.assertTrue(e.isGuestException());
throw e;
}
return null;
}
});
try {
verifyErrorOtherContext.execute();
Assert.fail();
} catch (PolyglotException e) {
// assert that polyglot exception was not unboxed if from other context
Assert.assertTrue(e.asHostException() instanceof PolyglotException);
PolyglotException polyglot = (PolyglotException) e.asHostException();
Assert.assertEquals(polyglot.getMessage(), "MyError");
Assert.assertTrue(polyglot.isGuestException());
}
context.close();
otherContext.close();
}
use of org.graalvm.polyglot.proxy.ProxyExecutable in project graal by oracle.
the class ProxyAPITest method testExceptionFrames.
@Test
public void testExceptionFrames() {
Value innerInner = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
throw new RuntimeException("foobar");
}
});
Value inner = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
return innerInner.execute();
}
});
Value outer = context.asValue(new ProxyExecutable() {
public Object execute(Value... arguments) {
return inner.execute();
}
});
try {
outer.execute();
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isHostException());
assertTrue(e.asHostException() instanceof RuntimeException);
assertEquals("foobar", e.getMessage());
Iterator<StackFrame> frameIterator = e.getPolyglotStackTrace().iterator();
StackFrame frame;
for (int i = 0; i < 6; i++) {
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("execute", frame.toHostFrame().getMethodName());
}
frame = frameIterator.next();
assertTrue(frame.isHostFrame());
assertEquals("testExceptionFrames", frame.toHostFrame().getMethodName());
}
}
use of org.graalvm.polyglot.proxy.ProxyExecutable 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.proxy.ProxyExecutable in project sulong by graalvm.
the class LLVMInteropTest method test010.
@Test
public void test010() {
try (Runner runner = new Runner("interop010")) {
runner.export(new ProxyExecutable() {
@Override
public Object execute(Value... t) {
return t[0].asLong() + t[1].asLong();
}
}, "foreign");
Assert.assertEquals(42, runner.run());
}
}
use of org.graalvm.polyglot.proxy.ProxyExecutable in project sulong by graalvm.
the class LLVMInteropTest method test012.
@Test
public void test012() {
try (Runner runner = new Runner("interop012")) {
runner.export(new ProxyExecutable() {
@Override
public Object execute(Value... t) {
Assert.assertEquals("argument count", 2, t.length);
return t[0].asDouble() + t[1].asDouble();
}
}, "foreign");
Assert.assertEquals(42.0, runner.run(), 0.1);
}
}
Aggregations