Search in sources :

Example 36 with ProxyLanguage

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

the class RewriteDuringCompilationTest method testCompilation.

private void testCompilation(BaseNode testedCode, LoopNode loopNode, DetectInvalidCodeNode nodeToRewrite, int rewriteCount, int maxDelayBeforeRewrite) throws IOException, InterruptedException, ExecutionException {
    // DetectInvalidCodeNode.invalidTwice does not work with multi-tier
    // code can remain active of another tier with local invalidation.
    setupEnv(Context.newBuilder().allowExperimentalOptions(true).option("engine.MultiTier", "false"), new ProxyLanguage() {

        private CallTarget target;

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

                    @Node.Child
                    private volatile BaseNode child = testedCode;

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

                    @Override
                    public SourceSection getSourceSection() {
                        return source.createSection(1);
                    }
                }.getCallTarget();
            }
            return target;
        }
    });
    AtomicReference<DetectInvalidCodeNode> nodeToRewriteReference = new AtomicReference<>(nodeToRewrite);
    Random rnd = new Random();
    CountDownLatch nodeRewritingLatch = new CountDownLatch(1);
    List<Object> callTargetsToCheck = new ArrayList<>();
    rewriting = true;
    ExecutorService executor = Executors.newFixedThreadPool(1);
    Future<?> future = executor.submit(() -> {
        try {
            for (int i = 1; i <= rewriteCount && rewriting; i++) {
                try {
                    Thread.sleep(rnd.nextInt(maxDelayBeforeRewrite));
                    nodeRewritingLatch.await();
                } catch (InterruptedException ie) {
                }
                if (loopNode != null) {
                    Object loopNodeCallTarget = ((OptimizedOSRLoopNode) loopNode).getCompiledOSRLoop();
                    if (loopNodeCallTarget != null) {
                        callTargetsToCheck.add(loopNodeCallTarget);
                    }
                }
                DetectInvalidCodeNode previousNode = nodeToRewriteReference.get();
                DetectInvalidCodeNode newNode = new DetectInvalidCodeNode();
                nodeToRewriteReference.set(newNode);
                previousNode.replace(newNode);
                previousNode.valid = false;
            }
        } finally {
            rewriting = false;
        }
    });
    Source source = Source.newBuilder(ProxyLanguage.ID, "", "DummySource").build();
    try {
        for (; ; ) {
            context.eval(source);
            nodeRewritingLatch.countDown();
            if (!rewriting) {
                break;
            }
        }
    } finally {
        rewriting = false;
        future.get();
        executor.shutdownNow();
        executor.awaitTermination(100, TimeUnit.SECONDS);
    }
    for (Object callTarget : callTargetsToCheck) {
        Assert.assertFalse("Obsolete loop call target is still valid", ((OptimizedCallTarget) callTarget).isValid());
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) OptimizedCallTarget(org.graalvm.compiler.truffle.runtime.OptimizedCallTarget) CallTarget(com.oracle.truffle.api.CallTarget) ArrayList(java.util.ArrayList) Source(org.graalvm.polyglot.Source) Random(java.util.Random) SourceSection(com.oracle.truffle.api.source.SourceSection) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) FrameSlotTypeException(com.oracle.truffle.api.frame.FrameSlotTypeException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) OptimizedOSRLoopNode(org.graalvm.compiler.truffle.runtime.OptimizedOSRLoopNode) ExecutorService(java.util.concurrent.ExecutorService)

Example 37 with ProxyLanguage

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

the class HostExceptionTest method before.

@Before
public void before() {
    context = Context.newBuilder().allowAllAccess(true).build();
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected LanguageContext createContext(Env contextEnv) {
            env = contextEnv;
            return super.createContext(contextEnv);
        }

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            RootNode rootNode;
            switch(request.getSource().getCharacters().toString()) {
                case "catcher":
                    rootNode = new CatcherRootNode();
                    break;
                case "runner":
                    rootNode = new RunnerRootNode();
                    break;
                case "rethrower":
                    rootNode = new RethrowerRootNode();
                    break;
                default:
                    throw new IllegalArgumentException();
            }
            return RootNode.createConstantNode(new CatcherObject(rootNode.getCallTarget())).getCallTarget();
        }
    });
    context.initialize(ProxyLanguage.ID);
    context.enter();
    assertNotNull(env);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) Env(com.oracle.truffle.api.TruffleLanguage.Env) LanguageContext(com.oracle.truffle.api.test.polyglot.ProxyLanguage.LanguageContext) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) ArityException(com.oracle.truffle.api.interop.ArityException) NoSuchElementException(java.util.NoSuchElementException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Before(org.junit.Before)

Example 38 with ProxyLanguage

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

the class GR31558 method before.

@Before
public void before() {
    HostAccess.Builder hostAccessBuilder = // 
    HostAccess.newBuilder().allowAccessAnnotatedBy(// 
    HostAccess.Export.class).allowPublicAccess(// 
    false).allowArrayAccess(// 
    true).allowIterableAccess(// 
    true).allowIteratorAccess(// 
    true).allowImplementationsAnnotatedBy(FunctionalInterface.class);
    Context.Builder contextBuilder = Context.newBuilder();
    contextBuilder.allowHostAccess(hostAccessBuilder.build());
    setupEnv(contextBuilder, new ProxyLanguage() {

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            String src = request.getSource().getCharacters().toString();
            RootCallTarget invokeTestApi;
            if ("testFunction".equals(src)) {
                invokeTestApi = new RootNode(ProxyLanguage.get(null)) {

                    @Override
                    public Object execute(VirtualFrame frame) {
                        try {
                            InteropLibrary interop = InteropLibrary.getUncached();
                            Object test = frame.getArguments()[0];
                            interop.invokeMember(test, "testFunction", "hi", new ArgumentsCollectorFunction());
                            if (interop.isMemberExisting(test, "testMap")) {
                                interop.invokeMember(test, "testFunction", "hi", new ArgumentsCollectorFunction(false, false, false, true));
                            }
                            if (interop.isMemberExisting(test, "testMapEntry")) {
                                interop.invokeMember(test, "testMapEntry", "hi", new ArgumentsCollectorFunction(false, false, true, false));
                            }
                            if (interop.isMemberExisting(test, "testList")) {
                                interop.invokeMember(test, "testList", "hi", new ArgumentsCollectorFunction(false, false, true, false));
                            }
                            if (interop.isMemberExisting(test, "testIterator")) {
                                interop.invokeMember(test, "testIterator", "hi", new ArgumentsCollectorFunction(false, true, false, false));
                            }
                            if (interop.isMemberExisting(test, "testIterable")) {
                                interop.invokeMember(test, "testIterable", "hi", new ArgumentsCollectorFunction(true, false, false, false));
                            }
                            return "success";
                        } catch (UnsupportedMessageException | UnknownIdentifierException | ArityException | UnsupportedTypeException e) {
                            CompilerDirectives.transferToInterpreter();
                            throw new AssertionError(e);
                        }
                    }
                }.getCallTarget();
            } else {
                throw new IllegalArgumentException(src);
            }
            return RootNode.createConstantNode(new HostExceptionTest.CatcherObject(invokeTestApi)).getCallTarget();
        }
    });
}
Also used : Context(org.graalvm.polyglot.Context) RootNode(com.oracle.truffle.api.nodes.RootNode) RootCallTarget(com.oracle.truffle.api.RootCallTarget) CallTarget(com.oracle.truffle.api.CallTarget) StopIterationException(com.oracle.truffle.api.interop.StopIterationException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) ArityException(com.oracle.truffle.api.interop.ArityException) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) HostAccess(org.graalvm.polyglot.HostAccess) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) RootCallTarget(com.oracle.truffle.api.RootCallTarget) Before(org.junit.Before)

Example 39 with ProxyLanguage

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

the class TruffleExceptionTest method testExceptionFromCreateContext.

@Test
public void testExceptionFromCreateContext() {
    String message = "Failed to create";
    ExceptionType type = ExceptionType.EXIT;
    assertFails(() -> setupEnv(Context.create(), new ProxyLanguage() {

        @Override
        protected LanguageContext createContext(Env env) {
            throw new TruffleExceptionImpl(message, null, type, null);
        }
    }), PolyglotException.class, (pe) -> {
        Assert.assertEquals(message, pe.getMessage());
        Assert.assertTrue(pe.isExit());
        Assert.assertFalse(pe.isInternalError());
        Assert.assertEquals(0, pe.getExitStatus());
        Assert.assertNull(pe.getGuestObject());
    });
}
Also used : ExceptionType(com.oracle.truffle.api.interop.ExceptionType) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

Example 40 with ProxyLanguage

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

the class TruffleExceptionTest method testTruffleException.

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

        @Override
        protected CallTarget parse(TruffleLanguage.ParsingRequest request) throws Exception {
            return createAST(AbstractTruffleException.class, languageInstance, (n) -> new TruffleExceptionImpl("Test exception", n), false);
        }
    });
    verifyingHandler.expect(BlockNode.Kind.TRY, BlockNode.Kind.CATCH, BlockNode.Kind.FINALLY);
    context.eval(ProxyLanguage.ID, "Test");
}
Also used : Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) ExportLibrary(com.oracle.truffle.api.library.ExportLibrary) DirectCallNode(com.oracle.truffle.api.nodes.DirectCallNode) PolyglotException(org.graalvm.polyglot.PolyglotException) TruffleBoundary(com.oracle.truffle.api.CompilerDirectives.TruffleBoundary) InteropException(com.oracle.truffle.api.interop.InteropException) TruffleLogger(com.oracle.truffle.api.TruffleLogger) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) TruffleStackTrace(com.oracle.truffle.api.TruffleStackTrace) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) CompilerDirectives(com.oracle.truffle.api.CompilerDirectives) BranchProfile(com.oracle.truffle.api.profiles.BranchProfile) InteropLibrary(com.oracle.truffle.api.interop.InteropLibrary) RootNode(com.oracle.truffle.api.nodes.RootNode) SourceSection(com.oracle.truffle.api.source.SourceSection) EnumSet(java.util.EnumSet) TruffleTestAssumptions(com.oracle.truffle.tck.tests.TruffleTestAssumptions) Before(org.junit.Before) PrintWriter(java.io.PrintWriter) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Node(com.oracle.truffle.api.nodes.Node) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) RootNodeTest.verifyStackTraceElementGuestObject(com.oracle.truffle.api.test.RootNodeTest.verifyStackTraceElementGuestObject) StringWriter(java.io.StringWriter) Set(java.util.Set) Test(org.junit.Test) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) CallTarget(com.oracle.truffle.api.CallTarget) LogRecord(java.util.logging.LogRecord) ControlFlowException(com.oracle.truffle.api.nodes.ControlFlowException) Consumer(java.util.function.Consumer) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest) List(java.util.List) ExceptionType(com.oracle.truffle.api.interop.ExceptionType) ExplodeLoop(com.oracle.truffle.api.nodes.ExplodeLoop) ExportMessage(com.oracle.truffle.api.library.ExportMessage) TruffleStackTraceElement(com.oracle.truffle.api.TruffleStackTraceElement) Truffle(com.oracle.truffle.api.Truffle) Queue(java.util.Queue) Pattern(java.util.regex.Pattern) Context(org.graalvm.polyglot.Context) ArrayDeque(java.util.ArrayDeque) Handler(java.util.logging.Handler) Assert(org.junit.Assert) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) Collections(java.util.Collections) CallTarget(com.oracle.truffle.api.CallTarget) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) AbstractTruffleException(com.oracle.truffle.api.exception.AbstractTruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) InteropException(com.oracle.truffle.api.interop.InteropException) InvalidArrayIndexException(com.oracle.truffle.api.interop.InvalidArrayIndexException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) ControlFlowException(com.oracle.truffle.api.nodes.ControlFlowException) ProxyLanguage(com.oracle.truffle.api.test.polyglot.ProxyLanguage) Test(org.junit.Test) AbstractPolyglotTest(com.oracle.truffle.api.test.polyglot.AbstractPolyglotTest)

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