use of org.graalvm.polyglot.Value in project graal by oracle.
the class LanguageSPITest method testPolyglotBindings.
@Test
public void testPolyglotBindings() {
ProxyLanguage.setDelegate(new ProxyLanguage() {
@Override
protected CallTarget parse(ParsingRequest request) throws Exception {
return Truffle.getRuntime().createCallTarget(new RootNode(languageInstance) {
@Override
public Object execute(VirtualFrame frame) {
return getCurrentContext(ProxyLanguage.class).env.getPolyglotBindings();
}
});
}
});
Context c = Context.create();
Value languageBindings = c.eval(ProxyLanguage.ID, "");
Value polyglotBindings = c.getPolyglotBindings();
polyglotBindings.putMember("foo", "bar");
assertEquals("bar", polyglotBindings.getMember("foo").asString());
assertEquals("bar", languageBindings.getMember("foo").asString());
languageBindings.putMember("baz", "42");
assertEquals("42", polyglotBindings.getMember("baz").asString());
assertEquals("42", languageBindings.getMember("baz").asString());
ValueAssert.assertValue(c, polyglotBindings);
ValueAssert.assertValue(c, languageBindings);
c.close();
}
use of org.graalvm.polyglot.Value in project graal by oracle.
the class MultiThreadedLanguageTest method evalAndWait.
/**
* Method evals code on another thread and waits until the function is finished evaluation. The
* future then waits until Future#get is invoked to complete the operation.
*/
private static Completable<Value> evalAndWait(ExecutorService executor, Context context, boolean manualComplete, CountDownLatch latch, Function<Env, Object> f) {
Semaphore waitInEval = manualComplete ? new Semaphore(0) : null;
Future<Value> valueResult = executor.<Value>submit(() -> {
try {
return eval(context, (env) -> {
Object result = f.apply(env);
try {
if (latch != null) {
latch.countDown();
}
if (waitInEval != null) {
waitInEval.acquire();
}
} catch (InterruptedException e) {
// cancelled
}
return result;
});
} catch (Throwable e) {
if (latch != null) {
latch.countDown();
}
throw e;
}
});
return new Completable<>(valueResult, waitInEval);
}
use of org.graalvm.polyglot.Value 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.Value in project graal by oracle.
the class ProxyAPITest method testProxy.
@Test
public void testProxy() {
Proxy proxy = new Proxy() {
};
Value value = context.asValue(proxy);
assertTrue(value.isProxyObject());
assertSame(proxy, value.asProxyObject());
assertUnsupported(value, PROXY_OBJECT);
}
use of org.graalvm.polyglot.Value 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());
}
}
Aggregations