Search in sources :

Example 16 with RootNode

use of com.oracle.truffle.api.nodes.RootNode 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 17 with RootNode

use of com.oracle.truffle.api.nodes.RootNode in project graal by oracle.

the class LanguageSPITest method testPolyglotBindings.

@Test
public void testPolyglotBindings() {
    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) {
                    return getCurrentContext(ProxyLanguage.class).env.getPolyglotBindings();
                }
            });
        }
    });
    Context c = Context.create();
    Value languageBindings = c.eval(ProxyLanguage.ID, "");
    Value polyglotBindings = c.getPolyglotBindings();
    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());
    ValueAssert.assertValue(c, polyglotBindings);
    ValueAssert.assertValue(c, languageBindings);
    c.close();
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) 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) Value(org.graalvm.polyglot.Value) 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 18 with RootNode

use of com.oracle.truffle.api.nodes.RootNode in project graal by oracle.

the class ReplaceTest method test.

@Test
public void test() {
    TruffleRuntime runtime = Truffle.getRuntime();
    UnresolvedNode leftChild = new UnresolvedNode("20");
    UnresolvedNode rightChild = new UnresolvedNode("22");
    TestRootNode rootNode = new TestRootNode(new ValueNode[] { leftChild, rightChild });
    CallTarget target = runtime.createCallTarget(rootNode);
    assertEquals(rootNode, leftChild.getParent());
    assertEquals(rootNode, rightChild.getParent());
    Iterator<Node> iterator = rootNode.getChildren().iterator();
    Assert.assertEquals(leftChild, iterator.next());
    Assert.assertEquals(rightChild, iterator.next());
    Assert.assertFalse(iterator.hasNext());
    Object result = target.call();
    assertEquals(42, result);
    assertEquals(42, target.call());
    iterator = rootNode.getChildren().iterator();
    Assert.assertEquals(ResolvedNode.class, iterator.next().getClass());
    Assert.assertEquals(ResolvedNode.class, iterator.next().getClass());
    Assert.assertFalse(iterator.hasNext());
    iterator = rootNode.getChildren().iterator();
    Assert.assertEquals(rootNode, iterator.next().getParent());
    Assert.assertEquals(rootNode, iterator.next().getParent());
    Assert.assertFalse(iterator.hasNext());
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget) Node(com.oracle.truffle.api.nodes.Node) RootNode(com.oracle.truffle.api.nodes.RootNode) TruffleRuntime(com.oracle.truffle.api.TruffleRuntime) Test(org.junit.Test)

Example 19 with RootNode

use of com.oracle.truffle.api.nodes.RootNode in project graal by oracle.

the class TruffleRuntimeTest method testCreateCallTarget.

@Test
public void testCreateCallTarget() {
    RootNode rootNode = createTestRootNode(null);
    RootCallTarget target = runtime.createCallTarget(rootNode);
    assertNotNull(target);
    assertEquals(target.call(), 42);
    assertSame(rootNode, target.getRootNode());
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) RootCallTarget(com.oracle.truffle.api.RootCallTarget) Test(org.junit.Test)

Example 20 with RootNode

use of com.oracle.truffle.api.nodes.RootNode in project graal by oracle.

the class ChildrenNodesTest method test.

@Test
public void test() {
    TruffleRuntime runtime = Truffle.getRuntime();
    TestChildNode firstChild = new TestChildNode();
    TestChildNode secondChild = new TestChildNode();
    TestRootNode rootNode = new TestRootNode(new TestChildNode[] { firstChild, secondChild });
    CallTarget target = runtime.createCallTarget(rootNode);
    Assert.assertEquals(rootNode, firstChild.getParent());
    Assert.assertEquals(rootNode, secondChild.getParent());
    Iterator<Node> iterator = rootNode.getChildren().iterator();
    Assert.assertEquals(firstChild, iterator.next());
    Assert.assertEquals(secondChild, iterator.next());
    Assert.assertFalse(iterator.hasNext());
    Object result = target.call();
    Assert.assertEquals(42, result);
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget) Node(com.oracle.truffle.api.nodes.Node) RootNode(com.oracle.truffle.api.nodes.RootNode) TruffleRuntime(com.oracle.truffle.api.TruffleRuntime) Test(org.junit.Test)

Aggregations

RootNode (com.oracle.truffle.api.nodes.RootNode)86 Test (org.junit.Test)36 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)24 Node (com.oracle.truffle.api.nodes.Node)23 CallTarget (com.oracle.truffle.api.CallTarget)16 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)16 RootCallTarget (com.oracle.truffle.api.RootCallTarget)12 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)12 RootTestNode (org.graalvm.compiler.truffle.test.nodes.RootTestNode)9 Source (com.oracle.truffle.api.source.Source)8 AbstractTestNode (org.graalvm.compiler.truffle.test.nodes.AbstractTestNode)8 TruffleRuntime (com.oracle.truffle.api.TruffleRuntime)7 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)6 SourceSection (com.oracle.truffle.api.source.SourceSection)6 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)5 ArrayList (java.util.ArrayList)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 TruffleContext (com.oracle.truffle.api.TruffleContext)3 TruffleException (com.oracle.truffle.api.TruffleException)3 TruffleLanguage (com.oracle.truffle.api.TruffleLanguage)3