Mercurial > hg > truffle
diff truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/RootNode.java @ 22135:e70b20f4bb00
Implementing API for Java/Truffle interop. Based around JavaInterop.asJavaObject and JavaInterop.asTruffleObject methods. Connected to TruffleVM via Symbol.as(Class) wrapper. Verified by extended TCK.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Mon, 07 Sep 2015 17:07:20 +0200 |
parents | 78c3d3d8d86e |
children | d5f809827bf1 3aad794eec0e |
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/RootNode.java Fri Sep 04 16:41:38 2015 +0200 +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/RootNode.java Mon Sep 07 17:07:20 2015 +0200 @@ -154,7 +154,7 @@ * stack) without prior knowledge of the language it has come from. * * Used for instance to determine the language of a <code>RootNode<code>: - * + * * <pre> * <code> * rootNode.getExecutionContext().getLanguageShortName(); @@ -194,4 +194,33 @@ */ public void applyInstrumentation() { } + + /** + * Helper method to create a root node that always returns the same value. Certain operations + * (expecially {@link com.oracle.api.truffle.api.interop inter-operability} API) require return + * of stable {@link RootNode root nodes}. To simplify creation of such nodes, here is a factory + * method that can create {@link RootNode} that returns always the same value. + * + * @param constant the constant to return + * @return root node returning the constant + */ + public static RootNode createConstantNode(Object constant) { + return new Constant(constant); + } + + private static final class Constant extends RootNode { + + private final Object value; + + public Constant(Object value) { + super(TruffleLanguage.class, null, null); + this.value = value; + } + + @Override + public Object execute(VirtualFrame vf) { + return value; + } + } + }