Search in sources :

Example 51 with CallTarget

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

the class LoopNodeTest method testLoopCountReportingInCallTarget.

@Test
public void testLoopCountReportingInCallTarget() {
    final GuestLanguageNode node = new GuestLanguageNode() {

        @Override
        public Object execute(VirtualFrame frame) {
            int[] data = new int[] { 1, 2, 3, 4, 5 };
            try {
                int sum = 0;
                for (int i = 0; i < data.length; i++) {
                    sum += data[i];
                }
                return sum;
            } finally {
                LoopNode.reportLoopCount(this, data.length);
            }
        }
    };
    RootNode root = new RootNode(null) {

        @Override
        public Object execute(VirtualFrame frame) {
            return node.execute(frame);
        }
    };
    CallTarget target = Truffle.getRuntime().createCallTarget(root);
    for (int i = 0; i < 1000; i++) {
        Assert.assertEquals(15, target.call());
    }
}
Also used : VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) RootNode(com.oracle.truffle.api.nodes.RootNode) CallTarget(com.oracle.truffle.api.CallTarget) Test(org.junit.Test)

Example 52 with CallTarget

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

the class ArgumentsTest method test.

@Test
public void test() {
    TruffleRuntime runtime = Truffle.getRuntime();
    TestRootNode rootNode = new TestRootNode(new TestArgumentNode[] { new TestArgumentNode(0), new TestArgumentNode(1) });
    CallTarget target = runtime.createCallTarget(rootNode);
    Object result = target.call(new Object[] { 20, 22 });
    Assert.assertEquals(42, result);
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget) TruffleRuntime(com.oracle.truffle.api.TruffleRuntime) Test(org.junit.Test)

Example 53 with CallTarget

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

the class AssumptionInvalidationTest method test.

@Test
public void test() throws InterruptedException {
    TruffleRuntime runtime = Truffle.getRuntime();
    Assumption assumption = runtime.createAssumption("propagated assumption invalidation");
    CountingNode countingNode = new CountingNode(assumption);
    TestRootNode countingRootNode = new TestRootNode(countingNode);
    final CallTarget countingTarget = runtime.createCallTarget(countingRootNode);
    Thread thread = new Thread(new Runnable() {

        public void run() {
            countingTarget.call();
        }
    });
    thread.start();
    // Give a chance for CountingNode.execute to OSR compile
    Thread.sleep(100);
    assumption.invalidate();
    thread.join(100);
    assertEquals("Thread ought to be notified of invalidation in reasonable time.", false, thread.isAlive());
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget) TruffleRuntime(com.oracle.truffle.api.TruffleRuntime) Assumption(com.oracle.truffle.api.Assumption) Test(org.junit.Test)

Example 54 with CallTarget

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

the class ChildNodeTest method test.

@Test
public void test() {
    TruffleRuntime runtime = Truffle.getRuntime();
    TestChildNode leftChild = new TestChildNode();
    TestChildNode rightChild = new TestChildNode();
    TestRootNode rootNode = new TestRootNode(leftChild, rightChild);
    CallTarget target = runtime.createCallTarget(rootNode);
    assertEquals(rootNode, leftChild.getParent());
    assertEquals(rootNode, rightChild.getParent());
    Iterator<Node> iterator = rootNode.getChildren().iterator();
    assertEquals(leftChild, iterator.next());
    assertEquals(rightChild, iterator.next());
    assertFalse(iterator.hasNext());
    Object result = target.call();
    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)

Example 55 with CallTarget

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

the class PolyglotContextImpl method eval.

@Override
public Value eval(String languageId, Object sourceImpl) {
    PolyglotLanguage language = requirePublicLanguage(languageId);
    Object prev = enter();
    PolyglotLanguageContext languageContext = contexts[language.index];
    try {
        languageContext.checkAccess(null);
        com.oracle.truffle.api.source.Source source = (com.oracle.truffle.api.source.Source) sourceImpl;
        CallTarget target = languageContext.parseCached(source);
        Object result = target.call(PolyglotImpl.EMPTY_ARGS);
        if (source.isInteractive()) {
            printResult(languageContext, result);
        }
        return languageContext.toHostValue(result);
    } catch (Throwable e) {
        throw PolyglotImpl.wrapGuestException(languageContext, e);
    } finally {
        leave(prev);
    }
}
Also used : CallTarget(com.oracle.truffle.api.CallTarget)

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