comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ArgumentsTest.java @ 14991:64dcb92ee75a

Truffle: Change signature for Truffle calls from (PackedFrame, Arguments) to (Object[]).
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sun, 06 Apr 2014 17:46:24 +0200
parents a08b8694f556
children
comparison
equal deleted inserted replaced
14989:a0dbb3628f2a 14991:64dcb92ee75a
28 import com.oracle.truffle.api.frame.*; 28 import com.oracle.truffle.api.frame.*;
29 import com.oracle.truffle.api.nodes.*; 29 import com.oracle.truffle.api.nodes.*;
30 30
31 /** 31 /**
32 * <h3>Passing Arguments</h3> 32 * <h3>Passing Arguments</h3>
33 * 33 *
34 * <p> 34 * <p>
35 * A guest language can pass its own custom arguments when invoking a Truffle method by creating a 35 * When invoking a call target with {@link CallTarget#call(Object[])}, arguments can be passed. A
36 * subclass of {@link Arguments}. When invoking a call target with 36 * Truffle node can access the arguments passed into the Truffle method by using
37 * {@link CallTarget#call(Arguments)}, the arguments can be passed. A Truffle node can access the 37 * {@link VirtualFrame#getArguments}.
38 * arguments passed into the Truffle method by using {@link VirtualFrame#getArguments}.
39 * </p> 38 * </p>
40 * 39 *
41 * <p> 40 * <p>
42 * The arguments class should only contain fields that are declared as final. This allows the 41 * The arguments class should only contain fields that are declared as final. This allows the
43 * Truffle runtime to improve optimizations around guest language method calls. Also, the arguments 42 * Truffle runtime to improve optimizations around guest language method calls. Also, the arguments
44 * object must never be stored into a field. It should be created immediately before invoking 43 * object array must never be stored into a field. It should be created immediately before invoking
45 * {@link CallTarget#call(Arguments)} and no longer be accessed afterwards. 44 * {@link CallTarget#call(Object[])} and no longer be accessed afterwards.
46 * </p> 45 * </p>
47 * 46 *
48 * <p> 47 * <p>
49 * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.FrameTest} 48 * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.FrameTest}
50 * . 49 * .
51 * </p> 50 * </p>
52 */ 51 */
55 @Test 54 @Test
56 public void test() { 55 public void test() {
57 TruffleRuntime runtime = Truffle.getRuntime(); 56 TruffleRuntime runtime = Truffle.getRuntime();
58 TestRootNode rootNode = new TestRootNode(new TestArgumentNode[]{new TestArgumentNode(0), new TestArgumentNode(1)}); 57 TestRootNode rootNode = new TestRootNode(new TestArgumentNode[]{new TestArgumentNode(0), new TestArgumentNode(1)});
59 CallTarget target = runtime.createCallTarget(rootNode); 58 CallTarget target = runtime.createCallTarget(rootNode);
60 Object result = target.call(new TestArguments(20, 22)); 59 Object result = target.call(new Object[]{20, 22});
61 Assert.assertEquals(42, result); 60 Assert.assertEquals(42, result);
62 }
63
64 private static class TestArguments extends Arguments {
65
66 final int[] values;
67
68 TestArguments(int... values) {
69 this.values = values;
70 }
71 } 61 }
72 62
73 private static class TestRootNode extends RootNode { 63 private static class TestRootNode extends RootNode {
74 64
75 @Children private final TestArgumentNode[] children; 65 @Children private final TestArgumentNode[] children;
97 super(null); 87 super(null);
98 this.index = index; 88 this.index = index;
99 } 89 }
100 90
101 int execute(VirtualFrame frame) { 91 int execute(VirtualFrame frame) {
102 return frame.getArguments(TestArguments.class).values[index]; 92 return (Integer) frame.getArguments()[index];
103 } 93 }
104 } 94 }
105 } 95 }