use of org.graalvm.polyglot.Context in project graal by oracle.
the class MultiThreadedLanguageTest method testAsssertionIfThreadStillActive.
@Test
public void testAsssertionIfThreadStillActive() throws InterruptedException {
MultiThreadedLanguage.isThreadAccessAllowed = (req) -> {
return true;
};
Engine engine = Engine.create();
Context context = Context.newBuilder().allowCreateThread(true).engine(engine).build();
Semaphore wait = new Semaphore(0);
Thread returnThread = eval(context, new Function<Env, Object>() {
public Object apply(Env env) {
Semaphore waitForEnter = new Semaphore(0);
Thread t = env.createThread(() -> {
try {
waitForEnter.release();
wait.acquire();
} catch (InterruptedException e) {
}
});
t.start();
try {
waitForEnter.acquire();
} catch (InterruptedException e) {
}
return t;
}
}).asHostObject();
try {
engine.close();
Assert.fail();
} catch (PolyglotException e) {
assertTrue(e.isInternalError());
assertTrue(e.getMessage().contains("The language did not complete all polyglot threads but should have"));
}
wait.release(1);
returnThread.join();
engine.close();
}
use of org.graalvm.polyglot.Context 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.Context in project graal by oracle.
the class ProxySPITest method testArrayProxy.
@Test
public void testArrayProxy() throws Throwable {
Context context = Context.create();
final int size = 42;
ProxyArray proxyOuter = new ProxyArray() {
int[] array = new int[size];
{
Arrays.fill(array, 42);
}
public Object get(long index) {
return array[(int) index];
}
public void set(long index, Value value) {
array[(int) index] = value.asInt();
}
public long getSize() {
return size;
}
};
eval(context, proxyOuter, (proxyInner) -> {
assertEquals(size, Message.GET_SIZE, proxyInner);
for (int i = 0; i < size; i++) {
assertEquals(42, Message.READ, proxyInner, i);
}
for (int i = 0; i < size; i++) {
assertEquals(41, Message.WRITE, proxyInner, i, 41);
}
for (int i = 0; i < size; i++) {
assertEquals(41, Message.READ, proxyInner, i);
}
assertUnknownIdentifier(Message.READ, proxyInner, 42);
assertUnknownIdentifier(Message.READ, proxyInner, -1);
assertUnknownIdentifier(Message.READ, proxyInner, Integer.MAX_VALUE);
assertUnknownIdentifier(Message.READ, proxyInner, Integer.MIN_VALUE);
assertEquals(true, Message.HAS_SIZE, proxyInner);
assertEquals(EXISTING_KEY, Message.KEY_INFO, proxyInner, 41);
assertEquals(NO_KEY, Message.KEY_INFO, proxyInner, 42);
assertUnsupported(Message.createExecute(0), proxyInner);
assertUnsupported(Message.AS_POINTER, proxyInner);
assertEquals(false, Message.HAS_KEYS, proxyInner);
assertEmpty(Message.KEYS, proxyInner);
assertUnsupported(Message.READ, proxyInner, "");
assertUnsupported(Message.WRITE, proxyInner, "");
assertUnsupported(Message.TO_NATIVE, proxyInner);
assertUnsupported(Message.UNBOX, proxyInner);
assertUnsupported(Message.createInvoke(0), proxyInner);
assertUnsupported(Message.createExecute(0), proxyInner);
assertUnsupported(Message.createNew(0), proxyInner);
assertEquals(false, Message.IS_BOXED, proxyInner);
assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
assertEquals(false, Message.IS_INSTANTIABLE, proxyInner);
assertEquals(false, Message.IS_NULL, proxyInner);
assertEquals(false, Message.IS_POINTER, proxyInner);
assertEquals(0, Message.KEY_INFO, proxyInner);
return null;
});
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ProxySPITest method testSimpleProxy.
@Test
public void testSimpleProxy() throws Throwable {
Context context = Context.create();
Proxy proxyOuter = new Proxy() {
};
eval(context, proxyOuter, (proxyInner) -> {
assertUnsupported(Message.createExecute(0), proxyInner);
assertUnsupported(Message.AS_POINTER, proxyInner);
assertUnsupported(Message.GET_SIZE, proxyInner);
assertEmpty(Message.KEYS, proxyInner);
assertUnsupported(Message.READ, proxyInner);
assertUnsupported(Message.WRITE, proxyInner);
assertUnsupported(Message.REMOVE, proxyInner);
assertUnsupported(Message.TO_NATIVE, proxyInner);
assertUnsupported(Message.UNBOX, proxyInner);
assertUnsupported(Message.createInvoke(0), proxyInner);
assertUnsupported(Message.createExecute(0), proxyInner);
assertUnsupported(Message.createNew(0), proxyInner);
assertEquals(false, Message.IS_BOXED, proxyInner);
assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
assertEquals(false, Message.IS_INSTANTIABLE, proxyInner);
assertEquals(false, Message.IS_NULL, proxyInner);
assertEquals(false, Message.HAS_KEYS, proxyInner);
assertEquals(false, Message.HAS_SIZE, proxyInner);
assertEquals(false, Message.IS_POINTER, proxyInner);
assertEquals(0, Message.KEY_INFO, proxyInner);
return null;
});
}
use of org.graalvm.polyglot.Context in project graal by oracle.
the class ProxySPITest method testProxyNativeObject.
@Test
public void testProxyNativeObject() throws Throwable {
Context context = Context.create();
ProxyNativeObject proxyOuter = new ProxyNativeObject() {
public long asPointer() {
return 42;
}
};
eval(context, proxyOuter, (proxyInner) -> {
assertEquals(true, Message.IS_POINTER, proxyInner);
assertEquals(42L, Message.AS_POINTER, proxyInner);
assertUnsupported(Message.createExecute(0), proxyInner);
assertUnsupported(Message.UNBOX, proxyInner);
assertUnsupported(Message.GET_SIZE, proxyInner);
assertEmpty(Message.KEYS, proxyInner);
assertUnsupported(Message.READ, proxyInner);
assertUnsupported(Message.WRITE, proxyInner);
assertUnsupported(Message.TO_NATIVE, proxyInner);
assertUnsupported(Message.UNBOX, proxyInner);
assertUnsupported(Message.createInvoke(0), proxyInner);
assertUnsupported(Message.createExecute(0), proxyInner);
assertUnsupported(Message.createNew(0), proxyInner);
assertEquals(false, Message.IS_EXECUTABLE, proxyInner);
assertEquals(false, Message.IS_INSTANTIABLE, proxyInner);
assertEquals(false, Message.IS_NULL, proxyInner);
assertEquals(false, Message.HAS_KEYS, proxyInner);
assertEquals(false, Message.HAS_SIZE, proxyInner);
assertEquals(false, Message.IS_BOXED, proxyInner);
assertEquals(0, Message.KEY_INFO, proxyInner);
return null;
});
}
Aggregations