Search in sources :

Example 31 with Context

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();
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.MultiThreadedLanguage.LanguageContext) Function(java.util.function.Function) Semaphore(java.util.concurrent.Semaphore) Env(com.oracle.truffle.api.TruffleLanguage.Env) PolyglotException(org.graalvm.polyglot.PolyglotException) Engine(org.graalvm.polyglot.Engine) Test(org.junit.Test)

Example 32 with Context

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();
}
Also used : Context(org.graalvm.polyglot.Context) ProxyExecutable(org.graalvm.polyglot.proxy.ProxyExecutable) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) PolyglotException(org.graalvm.polyglot.PolyglotException) Test(org.junit.Test)

Example 33 with Context

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;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) ProxyArray(org.graalvm.polyglot.proxy.ProxyArray) Value(org.graalvm.polyglot.Value) Test(org.junit.Test)

Example 34 with Context

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;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) Proxy(org.graalvm.polyglot.proxy.Proxy) Test(org.junit.Test)

Example 35 with Context

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;
    });
}
Also used : LanguageContext(com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) ProxyNativeObject(org.graalvm.polyglot.proxy.ProxyNativeObject) Test(org.junit.Test)

Aggregations

Context (org.graalvm.polyglot.Context)185 Test (org.junit.Test)148 Value (org.graalvm.polyglot.Value)58 TruffleContext (com.oracle.truffle.api.TruffleContext)56 Env (com.oracle.truffle.api.TruffleLanguage.Env)41 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)37 Engine (org.graalvm.polyglot.Engine)32 ArrayList (java.util.ArrayList)29 PolyglotException (org.graalvm.polyglot.PolyglotException)24 Source (org.graalvm.polyglot.Source)22 Path (java.nio.file.Path)21 TruffleFile (com.oracle.truffle.api.TruffleFile)20 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)20 CEntryPoint (org.graalvm.nativeimage.c.function.CEntryPoint)19 CEntryPointContext (org.graalvm.nativeimage.c.function.CEntryPointContext)19 LanguageContext (com.oracle.truffle.api.test.polyglot.ContextAPITestLanguage.LanguageContext)17 IOException (java.io.IOException)14 Debugger (com.oracle.truffle.api.debug.Debugger)13 EventContext (com.oracle.truffle.api.instrumentation.EventContext)13 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)12