Search in sources :

Example 36 with CallTarget

use of com.oracle.truffle.api.CallTarget in project graal by oracle.

the class ImplicitCastTest method testImplicitCastExecute2.

@Test
public void testImplicitCastExecute2() {
    ExampleArgumentNode[] args = ExampleNode.createArguments(2);
    CallTarget target = ExampleNode.createTarget(ImplicitCastExecuteNodeGen.create(args));
    Assert.assertEquals("s2", target.call(1L, 1L));
    Assert.assertEquals(0, args[0].longInvocationCount);
    Assert.assertEquals(0, args[1].longInvocationCount);
    Assert.assertEquals(1, args[0].genericInvocationCount);
    Assert.assertEquals(1, args[1].genericInvocationCount);
    Assert.assertEquals("s2", target.call(1L, 1L));
    Assert.assertEquals(1, args[0].longInvocationCount);
    Assert.assertEquals(1, args[1].longInvocationCount);
    Assert.assertEquals(2, args[0].genericInvocationCount);
    Assert.assertEquals(2, args[1].genericInvocationCount);
    Assert.assertEquals("s2", target.call(1L, 1L));
    Assert.assertEquals(2, args[0].longInvocationCount);
    Assert.assertEquals(2, args[1].longInvocationCount);
    Assert.assertEquals(3, args[0].genericInvocationCount);
    Assert.assertEquals(3, args[1].genericInvocationCount);
    Assert.assertEquals(0, args[0].doubleInvocationCount);
    Assert.assertEquals(0, args[1].doubleInvocationCount);
    Assert.assertEquals(0, args[0].intInvocationCount);
    Assert.assertEquals(0, args[1].intInvocationCount);
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget) ExampleArgumentNode(com.oracle.truffle.api.dsl.test.examples.ExampleNode.ExampleArgumentNode) Test(org.junit.Test)

Example 37 with CallTarget

use of com.oracle.truffle.api.CallTarget in project graal by oracle.

the class LanguageSPITest method testErrorInParse.

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

        @Override
        protected CallTarget parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest request) throws Exception {
            throw new RuntimeException();
        }
    });
    Context c = Context.create();
    c.initialize(ProxyLanguage.ID);
    testFails(() -> c.eval(ProxyLanguage.ID, "t0"));
    testFails(() -> c.eval(ProxyLanguage.ID, "t1"));
    c.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) CallTarget(com.oracle.truffle.api.CallTarget) RootCallTarget(com.oracle.truffle.api.RootCallTarget) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleException(com.oracle.truffle.api.TruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 38 with CallTarget

use of com.oracle.truffle.api.CallTarget in project graal by oracle.

the class LanguageSPITest method testPolyglotBindingsMultiThreaded.

@Test
public void testPolyglotBindingsMultiThreaded() throws InterruptedException, ExecutionException, TimeoutException {
    ProxyLanguage.setDelegate(new ProxyLanguage() {

        @Override
        protected boolean isThreadAccessAllowed(Thread thread, boolean singleThreaded) {
            return true;
        }

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return Truffle.getRuntime().createCallTarget(new RootNode(languageInstance) {

                @Override
                public Object execute(VirtualFrame frame) {
                    return getCurrentContext(ProxyLanguage.class).env.getPolyglotBindings();
                }
            });
        }
    });
    Context c = Context.create();
    ExecutorService service = Executors.newFixedThreadPool(20);
    Value languageBindings = c.eval(ProxyLanguage.ID, "");
    Value polyglotBindings = c.getPolyglotBindings();
    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < 2000; i++) {
        futures.add(service.submit(() -> {
            polyglotBindings.putMember("foo", "bar");
            assertEquals("bar", polyglotBindings.getMember("foo").asString());
            assertEquals("bar", languageBindings.getMember("foo").asString());
            languageBindings.putMember("baz", "42");
            assertEquals("42", polyglotBindings.getMember("baz").asString());
            assertEquals("42", languageBindings.getMember("baz").asString());
        }));
    }
    for (Future<?> future : futures) {
        future.get(100000, TimeUnit.MILLISECONDS);
    }
    service.shutdown();
    service.awaitTermination(100000, TimeUnit.MILLISECONDS);
    c.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) RootCallTarget(com.oracle.truffle.api.RootCallTarget) ArrayList(java.util.ArrayList) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleException(com.oracle.truffle.api.TruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) ExecutionException(java.util.concurrent.ExecutionException) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) ExecutorService(java.util.concurrent.ExecutorService) Value(org.graalvm.polyglot.Value) Future(java.util.concurrent.Future) Test(org.junit.Test)

Example 39 with CallTarget

use of com.oracle.truffle.api.CallTarget in project graal by oracle.

the class LanguageSPITest method testPolyglotBindingsPreserveLanguage.

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

        @Override
        protected CallTarget parse(ParsingRequest request) throws Exception {
            return Truffle.getRuntime().createCallTarget(new RootNode(languageInstance) {

                @Override
                public Object execute(VirtualFrame frame) {
                    Object bindings = getCurrentContext(ProxyLanguage.class).env.getPolyglotBindings();
                    try {
                        ForeignAccess.sendWrite(Message.WRITE.createNode(), (TruffleObject) bindings, "exportedValue", "convertOnToString");
                    } catch (UnknownIdentifierException | UnsupportedTypeException | UnsupportedMessageException e) {
                        throw new AssertionError(e);
                    }
                    return bindings;
                }
            });
        }

        @Override
        protected String toString(LanguageContext context, Object value) {
            if (value.equals("convertOnToString")) {
                return "myStringToString";
            }
            return super.toString(context, value);
        }
    });
    Context c = Context.create();
    c.eval(ProxyLanguage.ID, "");
    assertEquals("Make sure language specific toString was invoked.", "myStringToString", c.getPolyglotBindings().getMember("exportedValue").toString());
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) RootCallTarget(com.oracle.truffle.api.RootCallTarget) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleException(com.oracle.truffle.api.TruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) ExecutionException(java.util.concurrent.ExecutionException) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Example 40 with CallTarget

use of com.oracle.truffle.api.CallTarget in project graal by oracle.

the class LanguageSPITest method testErrorInFindMetaObject.

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

        @Override
        protected Object findMetaObject(LanguageContext context, Object value) {
            throw new RuntimeException();
        }

        @Override
        protected CallTarget parse(com.oracle.truffle.api.TruffleLanguage.ParsingRequest request) throws Exception {
            return Truffle.getRuntime().createCallTarget(RootNode.createConstantNode(42));
        }
    });
    Context c = Context.create();
    Value v = c.eval(ProxyLanguage.ID, "");
    testFails(() -> v.getMetaObject());
    testFails(() -> v.getMetaObject());
    c.close();
}
Also used : Context(org.graalvm.polyglot.Context) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TruffleContext(com.oracle.truffle.api.TruffleContext) CallTarget(com.oracle.truffle.api.CallTarget) RootCallTarget(com.oracle.truffle.api.RootCallTarget) LanguageContext(com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext) TimeoutException(java.util.concurrent.TimeoutException) UnsupportedMessageException(com.oracle.truffle.api.interop.UnsupportedMessageException) TruffleException(com.oracle.truffle.api.TruffleException) PolyglotException(org.graalvm.polyglot.PolyglotException) UnsupportedTypeException(com.oracle.truffle.api.interop.UnsupportedTypeException) UnknownIdentifierException(com.oracle.truffle.api.interop.UnknownIdentifierException) ExecutionException(java.util.concurrent.ExecutionException) Value(org.graalvm.polyglot.Value) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) Test(org.junit.Test)

Aggregations

CallTarget (com.oracle.truffle.api.CallTarget)128 Test (org.junit.Test)102 TestHelper.createCallTarget (com.oracle.truffle.api.dsl.test.TestHelper.createCallTarget)50 RootCallTarget (com.oracle.truffle.api.RootCallTarget)26 RootNode (com.oracle.truffle.api.nodes.RootNode)17 TruffleRuntime (com.oracle.truffle.api.TruffleRuntime)15 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)10 UnsupportedSpecializationException (com.oracle.truffle.api.dsl.UnsupportedSpecializationException)9 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)9 Node (com.oracle.truffle.api.nodes.Node)9 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)9 TruffleContext (com.oracle.truffle.api.TruffleContext)6 LanguageContext (com.oracle.truffle.api.test.polyglot.LanguageSPITestLanguage.LanguageContext)6 Context (org.graalvm.polyglot.Context)6 Theory (org.junit.experimental.theories.Theory)6 TruffleException (com.oracle.truffle.api.TruffleException)5 UnknownIdentifierException (com.oracle.truffle.api.interop.UnknownIdentifierException)5 UnsupportedMessageException (com.oracle.truffle.api.interop.UnsupportedMessageException)5 UnsupportedTypeException (com.oracle.truffle.api.interop.UnsupportedTypeException)5 Source (com.oracle.truffle.api.source.Source)5