Child nodes in arrays

Often, it is necessary for a dynamic number of children to be present in a node (e.g. a block of code with multiple operations, or an array initializer in the guest language). This can be done with arrays, for which the code is fairly straightforward:

package com.wordpress.hextruffle.tests;


public class ChildrenNodesDemo {

	static class StringConcatenatorTestRootNode extends RootNode {

		private final TestChildNode[] children;

		public StringConcatenatorTestRootNode(TestChildNode[] children) {
			this.children = children;

		public Object execute(VirtualFrame frame) {
			String sum = "";
			for (TestChildNode child : children) {
				sum += child.execute();
			return sum;

	static class TestChildNode extends Node {
		private final String s;

		public TestChildNode(String s) {
			this.s = s;

		public String execute() {
			return s;

	public static void main(String[] args) {
		TruffleRuntime runtime = Truffle.getRuntime();
	        TestChildNode firstChild = new TestChildNode("Hello");
	        TestChildNode secondChild = new TestChildNode(", ");
	        TestChildNode thirdChild = new TestChildNode("world");
	        TestChildNode forthChild = new TestChildNode("!");
	        StringConcatenatorTestRootNode root = new StringConcatenatorTestRootNode(new TestChildNode[]{firstChild,secondChild,thirdChild,forthChild});
	        CallTarget target = runtime.createCallTarget(root);

The important points to note here is that the array itself is final. Any changes to the array need to occur by changing the contents, and not by reassignment. The @Children annotation is also required for Truffle to properly handle the array, especially later on where nodes might be changing during runtime.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s