Search in sources :

Example 21 with ProxyLanguage

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

the class LegacyTruffleExceptionTest method testLegacyCancelException.

@Test
public void testLegacyCancelException() {
    setupEnv(createContext(verifyingHandler), new ProxyLanguage() {

        @Override
        protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
            return createAST(LegacyTruffleExceptionTest.class, languageInstance, (n) -> {
                LegacyCancelException exception = new LegacyCancelException(n);
                LangObject exceptionObject = new LangObject(exception);
                exception.setExceptionObject(exceptionObject);
                return exceptionObject;
            }, false);
        }
    });
    verifyingHandler.expect(BlockNode.Kind.TRY);
    assertFails(() -> context.eval(ProxyLanguage.ID, "Test"), PolyglotException.class, (e) -> {
        Assert.assertTrue(e.isCancelled());
        Assert.assertNotNull(e.getGuestObject());
    });
}
Also used : TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Node(com.oracle.truffle.api.nodes.Node) BeforeClass(org.junit.BeforeClass) ExportLibrary(com.oracle.truffle.api.library.ExportLibrary) PolyglotException(org.graalvm.polyglot.PolyglotException) ThrowNode(com.oracle.truffle.api.test.TruffleExceptionTest.ThrowNode) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) Test(org.junit.Test) CallTarget(com.oracle.truffle.api.CallTarget) BlockNode(com.oracle.truffle.api.test.TruffleExceptionTest.BlockNode) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) TestRootNode(com.oracle.truffle.api.test.TruffleExceptionTest.TestRootNode) ExceptionType(com.oracle.truffle.api.interop.ExceptionType) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) ExportMessage(com.oracle.truffle.api.library.ExportMessage) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleExceptionTest.createContext(com.oracle.truffle.api.test.TruffleExceptionTest.createContext) Context(org.graalvm.polyglot.Context) Assert(org.junit.Assert) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) TruffleTestAssumptions(com.oracle.truffle.tck.tests.TruffleTestAssumptions) TruffleExceptionTest.createAST(com.oracle.truffle.api.test.TruffleExceptionTest.createAST) Before(org.junit.Before) CallTarget(com.oracle.truffle.api.CallTarget) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 22 with ProxyLanguage

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

the class InstrumentationTest method testAsynchronousStacks3.

@Test
public void testAsynchronousStacks3() {
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return new AsyncRootNode(language, 0).getCallTarget();
        }

        class AsyncRootNode extends RootNode {

            private final TruffleLanguage<?> language;

            private final int level;

            @Node.Child
            private AsyncNode child;

            AsyncRootNode(TruffleLanguage<?> language, int level) {
                super(language, createFrameDescriptor(1));
                this.language = language;
                this.level = level;
                this.child = new AsyncNode(level);
            }

            @Override
            public Object execute(VirtualFrame frame) {
                storeInvokedTarget(frame.materialize());
                child.execute(frame);
                return 42;
            }

            @TruffleBoundary
            private void storeInvokedTarget(MaterializedFrame frame) {
                CallTarget callTarget = Truffle.getRuntime().iterateFrames((f) -> f.getCallTarget());
                frame.setObject(TARGET_SLOT, callTarget);
            }

            @Override
            public SourceSection getSourceSection() {
                return com.oracle.truffle.api.source.Source.newBuilder(ID, Integer.toString(level), "level").build().createSection(1);
            }

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

            @Override
            protected List<TruffleStackTraceElement> findAsynchronousFrames(Frame frame) {
                assertSame(this.getFrameDescriptor(), frame.getFrameDescriptor());
                AsyncRootNode invoker = new AsyncRootNode(language, level + 1);
                RootCallTarget invokerTarget = invoker.getCallTarget();
                Frame invokerFrame = Truffle.getRuntime().createMaterializedFrame(new Object[] { level + 1 }, invoker.getFrameDescriptor());
                TruffleStackTraceElement element = TruffleStackTraceElement.create(invoker.child, invokerTarget, invokerFrame);
                return Collections.singletonList(element);
            }
        }
    });
    AtomicReference<List<TruffleStackTraceElement>> asyncStack = new AtomicReference<>();
    instrumentEnv.getInstrumenter().attachExecutionEventListener(SourceSectionFilter.ANY, new ExecutionEventListener() {

        @Override
        public void onEnter(EventContext ctx, VirtualFrame frame) {
            assertNull(asyncStack.get());
            asyncStack.set(TruffleStackTrace.getAsynchronousStackTrace(ctx.getInstrumentedNode().getRootNode().getCallTarget(), frame));
        }

        @Override
        public void onReturnValue(EventContext ctx, VirtualFrame frame, Object result) {
        }

        @Override
        public void onReturnExceptional(EventContext ctx, VirtualFrame frame, Throwable exception) {
        }
    });
    int ret = context.eval(ProxyLanguage.ID, "").asInt();
    assertEquals(42, ret);
    List<TruffleStackTraceElement> stack = asyncStack.get();
    int numTestLevels = 10;
    for (int i = 1; i < numTestLevels; i++) {
        assertEquals(1, stack.size());
        TruffleStackTraceElement element = stack.get(0);
        assertEquals(i, ((AsyncNode) element.getLocation()).getLevel());
        assertEquals(Integer.toString(i), element.getTarget().getRootNode().getSourceSection().getCharacters());
        stack = TruffleStackTrace.getAsynchronousStackTrace(element.getTarget(), element.getFrame());
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) Frame(com.oracle.truffle.api.frame.Frame) MaterializedFrame(com.oracle.truffle.api.frame.MaterializedFrame) TruffleStackTraceElement(com.oracle.truffle.api.TruffleStackTraceElement) CallTarget(com.oracle.truffle.api.CallTarget) RootCallTarget(com.oracle.truffle.api.RootCallTarget) AtomicReference(java.util.concurrent.atomic.AtomicReference) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) IOException(java.io.IOException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) ExecutionEventListener(com.oracle.truffle.api.instrumentation.ExecutionEventListener) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) EventContext(com.oracle.truffle.api.instrumentation.EventContext) List(java.util.List) ArrayList(java.util.ArrayList) MaterializedFrame(com.oracle.truffle.api.frame.MaterializedFrame) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) RootCallTarget(com.oracle.truffle.api.RootCallTarget) Test(org.junit.Test)

Example 23 with ProxyLanguage

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

the class TruffleContextTest method testCancellingUncaughtExceptionHandler.

@Test
public void testCancellingUncaughtExceptionHandler() {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    setupEnv(Context.newBuilder().allowAllAccess(true).err(out).build(), new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) {
            RootNode rootNode;
            String command = request.getSource().getCharacters().toString();
            switch(command) {
                case "controller":
                    rootNode = new ControllerNode(languageInstance);
                    break;
                case "worker":
                    rootNode = new WorkerNode(languageInstance);
                    break;
                default:
                    throw CompilerDirectives.shouldNotReachHere("Unknown request: " + command);
            }
            return rootNode.getCallTarget();
        }
    });
    context.eval(Source.newBuilder(ProxyLanguage.ID, "controller", "test").buildLiteral());
    assertFalse(out.toString().contains(getCancelExecutionClass().getName()));
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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 24 with ProxyLanguage

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

the class TruffleContextTest method testInitializeMultiContextForInnerContext.

@Test
public void testInitializeMultiContextForInnerContext() {
    AtomicBoolean multiContextInitialized = new AtomicBoolean(false);
    setupEnv(Context.newBuilder().engine(Engine.create()).build(), new ProxyLanguage() {

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

        @Override
        protected void initializeMultipleContexts() {
            multiContextInitialized.set(true);
        }

        @Override
        protected boolean areOptionsCompatible(OptionValues firstOptions, OptionValues newOptions) {
            return true;
        }
    });
    TruffleContext ic = languageEnv.newContextBuilder().initializeCreatorContext(true).build();
    assertTrue(multiContextInitialized.get());
    ic.close();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OptionValues(org.graalvm.options.OptionValues) CallTarget(com.oracle.truffle.api.CallTarget) TruffleContext(com.oracle.truffle.api.TruffleContext) InteropException(com.oracle.truffle.api.interop.InteropException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) ArityException(com.oracle.truffle.api.interop.ArityException) ExecutionException(java.util.concurrent.ExecutionException) 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 25 with ProxyLanguage

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

the class SourceListenerTest method testNoMaterializationOnSourceListener.

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

        @Override
        protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
            com.oracle.truffle.api.source.Source source = request.getSource();
            return new RootNode(languageInstance) {

                @Node.Child
                private NeverMaterializedNode child = new NeverMaterializedNode();

                @Override
                public Object execute(VirtualFrame frame) {
                    return child.execute(frame);
                }

                @Override
                public SourceSection getSourceSection() {
                    return source.createSection(1);
                }
            }.getCallTarget();
        }
    });
    context.eval(Source.create(ProxyLanguage.ID, "a"));
    instrumentEnv.getInstrumenter().attachLoadSourceListener(SourceFilter.ANY, s -> {
    }, true);
    instrumentEnv.getInstrumenter().attachExecuteSourceListener(SourceFilter.ANY, s -> {
    }, true);
    context.eval(Source.create(ProxyLanguage.ID, "b"));
    // Verify that it fails when materialized:
    try {
        instrumentEnv.getInstrumenter().attachLoadSourceSectionListener(SourceSectionFilter.ANY, e -> {
        }, true);
        Assert.fail();
    } catch (IllegalStateException ex) {
    // O.K.
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) PolyglotException(org.graalvm.polyglot.PolyglotException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Source(org.graalvm.polyglot.Source) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) Test(org.junit.Test)

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