Search in sources :

Example 26 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class ThreadsActivationListenerTest method testExceptionImpl.

private void testExceptionImpl(Runnable body, boolean throwOnEnter, boolean throwOnLeave) {
    setupEnv(Context.create(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return RootNode.createConstantNode(42).getCallTarget();
        }
    });
    ThreadsActivationListener listener = new ThreadsActivationListener() {

        @Override
        public void onEnterThread(TruffleContext c) {
            if (throwOnEnter) {
                CompilerDirectives.transferToInterpreterAndInvalidate();
                throw new RuntimeException();
            }
        }

        @Override
        public void onLeaveThread(TruffleContext c) {
            if (throwOnLeave) {
                CompilerDirectives.transferToInterpreterAndInvalidate();
                throw new RuntimeException();
            }
        }
    };
    EventBinding<? extends ThreadsActivationListener> binding = instrumentEnv.getInstrumenter().attachThreadsActivationListener(listener);
    try {
        body.run();
    } finally {
        binding.dispose();
    }
}
Also used : ThreadsActivationListener(com.oracle.truffle.api.instrumentation.ThreadsActivationListener) CallTarget(com.oracle.truffle.api.CallTarget) TruffleContext(com.oracle.truffle.api.TruffleContext) PolyglotException(org.graalvm.polyglot.PolyglotException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage)

Example 27 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class ThreadsActivationListenerTest method testInternalContext.

@Test
public void testInternalContext() {
    setupEnv(Context.create(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return RootNode.createConstantNode(42).getCallTarget();
        }
    });
    Context c0 = this.context;
    TruffleContext tc0 = this.languageEnv.getContext();
    c0.enter();
    TruffleContext ic0 = this.languageEnv.newContextBuilder().build();
    TruffleContext ic0CreatorHandle = ic0;
    Object prev = ic0.enter(null);
    // look language handle on the context it is not the same as
    // the creator handle. The creator handle can be closed.
    ic0 = LanguageContext.get(null).getEnv().getContext();
    ic0.leave(null, prev);
    c0.leave();
    List<TruffleContext> entered = new ArrayList<>();
    List<TruffleContext> left = new ArrayList<>();
    EventBinding<?> binding = instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {

        @TruffleBoundary
        public void onEnterThread(TruffleContext c) {
            entered.add(c);
        }

        @TruffleBoundary
        public void onLeaveThread(TruffleContext c) {
            left.add(c);
        }
    });
    assertList(entered);
    assertList(left);
    assertSame(tc0, ic0.getParent());
    assertList(entered);
    assertList(left);
    prev = ic0.enter(null);
    assertList(entered, ic0);
    assertList(left);
    ic0.leave(null, prev);
    assertList(entered, ic0);
    assertList(left, ic0);
    prev = tc0.enter(null);
    assertList(entered, ic0, tc0);
    assertList(left, ic0);
    Object prev1 = ic0.enter(null);
    assertList(entered, ic0, tc0, ic0);
    assertList(left, ic0);
    ic0.leave(null, prev1);
    assertList(entered, ic0, tc0, ic0);
    assertList(left, ic0, ic0);
    tc0.leave(null, prev);
    assertList(entered, ic0, tc0, ic0);
    assertList(left, ic0, ic0, tc0);
    binding.dispose();
    prev = tc0.enter(null);
    prev1 = ic0.enter(null);
    ic0.leave(null, prev1);
    tc0.leave(null, prev);
    assertList(entered, ic0, tc0, ic0);
    assertList(left, ic0, ic0, tc0);
    ic0CreatorHandle.close();
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) Context(org.graalvm.polyglot.Context) CallTarget(com.oracle.truffle.api.CallTarget) TruffleContext(com.oracle.truffle.api.TruffleContext) ArrayList(java.util.ArrayList) PolyglotException(org.graalvm.polyglot.PolyglotException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) ThreadsActivationListener(com.oracle.truffle.api.instrumentation.ThreadsActivationListener) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 28 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class ThreadsActivationListenerTest method testSingleContext.

@Test
public void testSingleContext() {
    setupEnv(Context.create(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return RootNode.createConstantNode(42).getCallTarget();
        }
    });
    List<TruffleContext> entered = new ArrayList<>();
    List<TruffleContext> left = new ArrayList<>();
    EventBinding<?> binding = instrumentEnv.getInstrumenter().attachThreadsActivationListener(new ThreadsActivationListener() {

        @TruffleBoundary
        public void onEnterThread(TruffleContext c) {
            entered.add(c);
        }

        @TruffleBoundary
        public void onLeaveThread(TruffleContext c) {
            left.add(c);
        }
    });
    assertList(entered);
    assertList(left);
    context.enter();
    TruffleContext to = languageEnv.getContext();
    assertList(entered, to);
    assertList(left);
    context.leave();
    assertList(entered, to);
    assertList(left, to);
    context.enter();
    assertList(entered, to, to);
    assertList(left, to);
    context.enter();
    assertList(entered, to, to, to);
    assertList(left, to);
    context.leave();
    assertList(entered, to, to, to);
    assertList(left, to, to);
    context.leave();
    assertList(entered, to, to, to);
    assertList(left, to, to, to);
    context.eval(ProxyLanguage.ID, "");
    assertList(entered, to, to, to, to);
    assertList(left, to, to, to, to);
    binding.dispose();
    context.enter();
    context.leave();
    assertList(entered, to, to, to, to);
    assertList(left, to, to, to, to);
}
Also used : ThreadsActivationListener(com.oracle.truffle.api.instrumentation.ThreadsActivationListener) CallTarget(com.oracle.truffle.api.CallTarget) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) TruffleContext(com.oracle.truffle.api.TruffleContext) ArrayList(java.util.ArrayList) PolyglotException(org.graalvm.polyglot.PolyglotException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 29 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class TruffleContextTest method testParallelForceClose.

@Test
public void testParallelForceClose() throws InterruptedException {
    setupEnv(Context.newBuilder().allowAllAccess(true).option("engine.TriggerUncaughtExceptionHandlerForCancel", "true").build(), new ProxyLanguage() {

        @Override
        protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
            return true;
        }
    });
    TruffleContext tc = languageEnv.newContextBuilder().build();
    List<Thread> threads = new ArrayList<>();
    List<AtomicReference<Throwable>> exceptions = new ArrayList<>();
    Semaphore waitUntilStart = new Semaphore(0);
    for (int i = 0; i < 100; i++) {
        Thread t = languageEnv.createThread(() -> {
            com.oracle.truffle.api.source.Source s = com.oracle.truffle.api.source.Source.newBuilder(InstrumentationTestLanguage.ID, "EXPRESSION", "").build();
            CallTarget target = LanguageContext.get(null).getEnv().parsePublic(s);
            while (true) {
                target.call();
                // at least one thread should have started execution
                waitUntilStart.release();
            }
        }, tc);
        AtomicReference<Throwable> exception = new AtomicReference<>();
        t.setUncaughtExceptionHandler((thread, e) -> {
            exception.set(e);
        });
        exceptions.add(exception);
        t.start();
        threads.add(t);
    }
    // 10s ought to be enough for anybody
    if (!waitUntilStart.tryAcquire(10000, TimeUnit.MILLISECONDS)) {
        for (AtomicReference<Throwable> e : exceptions) {
            if (e.get() != null) {
                throw new AssertionError(e.get());
            }
        }
        throw new AssertionError("failed to wait for execution");
    }
    assertFalse(tc.isClosed());
    for (int i = 0; i < threads.size(); i++) {
        assertNull(exceptions.get(i).get());
    }
    tc.closeCancelled(null, "testreason");
    for (int i = 0; i < threads.size(); i++) {
        threads.get(i).join();
    }
    for (int i = 0; i < threads.size(); i++) {
        Throwable e = exceptions.get(i).get();
        assertNotNull(e);
        assertEquals(getCancelExecutionClass(), e.getClass());
        assertEquals("testreason", e.getMessage());
        assertTrue(tc.isClosed());
    }
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget) TruffleContext(com.oracle.truffle.api.TruffleContext) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) TruffleSafepoint(com.oracle.truffle.api.TruffleSafepoint) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) LanguageSPIOrderTest(com.oracle.truffle.api.test.polyglot.LanguageSPIOrderTest) Test(org.junit.Test)

Example 30 with ProxyLanguage

use of com.oracle.truffle.api.test.polyglot.ProxyLanguage in project graal by oracle.

the class VariablesScopeTest method createDefaultScopeLanguage.

private static void createDefaultScopeLanguage(Context context) {
    ProxyLanguage language = new ProxyLanguage() {

        @Override
        protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
            return new RootNode(ProxyLanguage.get(null)) {

                @Node.Child
                private DefaultRootBlockNode block = insert(new DefaultRootBlockNode());

                @Override
                protected boolean isInstrumentable() {
                    return true;
                }

                @Override
                public Object execute(VirtualFrame frame) {
                    return block.execute(frame);
                }
            }.getCallTarget();
        }
    };
    ProxyLanguage.setDelegate(language);
    context.initialize(ProxyLanguage.ID);
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) RootNode(com.oracle.truffle.api.nodes.RootNode) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage)

Aggregations

ProxyLanguage (com.oracle.truffle.api.test.polyglot.ProxyLanguage)64 Test (org.junit.Test)47 CallTarget (com.oracle.truffle.api.CallTarget)32 AbstractPolyglotTest (com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)28 PolyglotException (org.graalvm.polyglot.PolyglotException)26 RootNode (com.oracle.truffle.api.nodes.RootNode)21 Source (org.graalvm.polyglot.Source)21 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)20 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)20 Context (org.graalvm.polyglot.Context)17 TruffleContext (com.oracle.truffle.api.TruffleContext)16 Before (org.junit.Before)16 TruffleLanguage (com.oracle.truffle.api.TruffleLanguage)14 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)13 CountDownLatch (java.util.concurrent.CountDownLatch)11 InteropLibrary (com.oracle.truffle.api.interop.InteropLibrary)10 ArrayList (java.util.ArrayList)10 ExecutionException (java.util.concurrent.ExecutionException)10 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)9 ExecutorService (java.util.concurrent.ExecutorService)9