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());
}
}
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);
}
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());
}
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);
}
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);
}
}
Aggregations