Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/ChildrenNodesTest.java @ 7267:a4b84ba6dc2e
Introduction of the Truffle API for efficient implementation of dynamic languages on top of the Graal VM. New projects com.oracle.truffle.api for the API definition and com.oracle.truffle.api.test for API tests and documentation.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Tue, 18 Dec 2012 15:33:55 +0100 |
parents | |
children | 5e3d1a68664e |
comparison
equal
deleted
inserted
replaced
7259:494d99e07614 | 7267:a4b84ba6dc2e |
---|---|
1 /* | |
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.api.test; | |
24 | |
25 import java.util.*; | |
26 | |
27 import org.junit.*; | |
28 | |
29 import com.oracle.truffle.api.*; | |
30 import com.oracle.truffle.api.frame.*; | |
31 import com.oracle.truffle.api.nodes.*; | |
32 | |
33 /** | |
34 * <h3>Creating an Array of Children Nodes</h3> | |
35 * | |
36 * <p> | |
37 * An array of children nodes can be used as a field in a parent node. The field has to be annotated with | |
38 * {@link com.oracle.truffle.api.nodes.Node.Children} and must be declared private and final. Before assigning the field | |
39 * in the parent node constructor, {@link Node#adoptChildren} must be called in order to update the parent pointers in | |
40 * the child nodes. After filling the array with its first values, it must never be changed. It is only possible to call | |
41 * {@link Node#replace} on a child node. | |
42 * </p> | |
43 * | |
44 * <p> | |
45 * The next part of the Truffle API introduction is at {@link com.oracle.truffle.api.test.FinalFieldTest}. | |
46 * </p> | |
47 */ | |
48 public class ChildrenNodesTest { | |
49 | |
50 @Test | |
51 public void test() { | |
52 TruffleRuntime runtime = Truffle.getRuntime(); | |
53 TestChildNode firstChild = new TestChildNode(); | |
54 TestChildNode secondChild = new TestChildNode(); | |
55 TestRootNode rootNode = new TestRootNode(new TestChildNode[]{firstChild, secondChild}); | |
56 Assert.assertEquals(rootNode, firstChild.getParent()); | |
57 Assert.assertEquals(rootNode, secondChild.getParent()); | |
58 Iterator<Node> iterator = rootNode.getChildren().iterator(); | |
59 Assert.assertEquals(firstChild, iterator.next()); | |
60 Assert.assertEquals(secondChild, iterator.next()); | |
61 Assert.assertFalse(iterator.hasNext()); | |
62 CallTarget target = runtime.createCallTarget(rootNode); | |
63 Object result = target.call(); | |
64 Assert.assertEquals(42, result); | |
65 } | |
66 | |
67 class TestRootNode extends RootNode { | |
68 | |
69 @Children private final TestChildNode[] children; | |
70 | |
71 public TestRootNode(TestChildNode[] children) { | |
72 this.children = adoptChildren(children); | |
73 } | |
74 | |
75 @Override | |
76 public Object execute(VirtualFrame frame) { | |
77 int sum = 0; | |
78 for (int i = 0; i < children.length; ++i) { | |
79 sum += children[i].execute(); | |
80 } | |
81 return sum; | |
82 } | |
83 } | |
84 | |
85 class TestChildNode extends Node { | |
86 public int execute() { | |
87 return 21; | |
88 } | |
89 } | |
90 } | |
91 |