Search in sources :

Example 26 with RootNode

use of com.oracle.truffle.api.nodes.RootNode in project graal by oracle.

the class EngineTest method testLanguageAccess.

@Test
public void testLanguageAccess() {
    final PolyglotEngine.Builder builder = createBuilderInternal();
    ForkingLanguageChannel channel = new ForkingLanguageChannel(builder::build);
    PolyglotEngine vm = builder.config(ForkingLanguage.MIME_TYPE, "channel", channel).build();
    vm.eval(Source.newBuilder("").name("").mimeType(ForkingLanguage.MIME_TYPE).build()).get();
    RootNode root = new RootNode(channel.language) {

        @Override
        public Object execute(VirtualFrame frame) {
            return null;
        }
    };
    try {
        // no access using a TruffleLanguage hack
        root.getLanguage(TruffleLanguage.class);
        fail();
    } catch (ClassCastException e) {
    }
    Class<?> oClass = Object.class;
    Class<? extends TruffleLanguage> lang = (Class<? extends TruffleLanguage>) oClass;
    try {
        // no access using a TruffleLanguage class cast
        root.getLanguage(lang);
        fail();
    } catch (ClassCastException e) {
    }
    oClass = SecretInterfaceType.class;
    Class<? extends TruffleLanguage> secretInterface = (Class<? extends TruffleLanguage>) oClass;
    try {
        // no access using secret interface
        root.getLanguage(secretInterface);
        fail();
    } catch (ClassCastException e) {
    }
    // this should work as expected
    assertNotNull(root.getLanguage(ForkingLanguage.class));
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) VirtualFrame(com.oracle.truffle.api.frame.VirtualFrame) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage) Test(org.junit.Test)

Example 27 with RootNode

use of com.oracle.truffle.api.nodes.RootNode in project graal by oracle.

the class DefaultScope method lexicalScope.

static Iterable<Scope> lexicalScope(Node node, Frame frame) {
    RootNode root = node.getRootNode();
    String name = root.getName();
    if (name == null) {
        name = "local";
    }
    return Collections.singletonList(Scope.newBuilder(name, getVariables(root, frame)).node(root).arguments(getArguments(frame)).build());
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode)

Example 28 with RootNode

use of com.oracle.truffle.api.nodes.RootNode in project graal by oracle.

the class JSONHelper method dumpNewNode.

/**
 * @since 0.8 or earlier
 */
public static void dumpNewNode(Node newNode) {
    if (AstJsonDumpBuilder != null) {
        String language = "";
        RootNode root = newNode.getRootNode();
        if (root != null) {
            TruffleLanguage<?> clazz = root.getLanguage(TruffleLanguage.class);
            if (clazz != null) {
                language = clazz.getClass().getName();
            }
        }
        AstJsonDumpBuilder.append("{ \"action\": \"createNode\", \"newId\": \"" + getID(newNode) + "\", \"type\": \"" + getType(newNode) + "\", \"description\": \"" + newNode.getDescription() + "\", \"language\": \"" + language + "\"" + " },\n");
    }
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode)

Example 29 with RootNode

use of com.oracle.truffle.api.nodes.RootNode in project graal by oracle.

the class Scope method findScopes.

/**
 * Find a list of scopes enclosing the given {@link Node node}. There is at least one scope
 * provided, that corresponds to the enclosing function. The iteration order corresponds with
 * the scope nesting, from the inner-most to the outer-most. The scopes contain variables valid
 * at the provided node.
 * <p>
 * In general, there can be a different list of scopes with different variables and arguments
 * returned for different {@link Frame} instances, as scopes may depend on runtime information.
 * Known lexical scopes are returned when <code>frame</code> argument is <code>null</code>.
 *
 * @param instrumentEnv the instrument environment that is used to
 *            {@link com.oracle.truffle.api.instrumentation.TruffleInstrument.Env#lookup(com.oracle.truffle.api.nodes.LanguageInfo, java.lang.Class)
 *            look-up} the {@link ScopeProvider} service.
 * @param node a node to get the enclosing scopes for. The node needs to be inside a
 *            {@link RootNode} associated with a language.
 * @param frame The current frame the node is in, or <code>null</code> for lexical access when
 *            the program is not running, or is not suspended at the node's location.
 * @return an {@link Iterable} providing list of scopes from the inner-most to the outer-most.
 * @see ScopeProvider#findScope(java.lang.Object, com.oracle.truffle.api.nodes.Node,
 *      com.oracle.truffle.api.frame.Frame)
 * @since 0.26
 * @deprecated Use
 *             {@link TruffleInstrument.Env#findLocalScopes(com.oracle.truffle.api.nodes.Node, com.oracle.truffle.api.frame.Frame)}
 *             instead.
 */
@Deprecated
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Iterable<Scope> findScopes(Env instrumentEnv, Node node, Frame frame) {
    RootNode rootNode = node.getRootNode();
    if (rootNode == null) {
        throw new IllegalArgumentException("The node " + node + " does not have a RootNode.");
    }
    LanguageInfo languageInfo = rootNode.getLanguageInfo();
    if (languageInfo == null) {
        throw new IllegalArgumentException("The root node " + rootNode + " does not have a language associated.");
    }
    ScopeProvider scopeProvider = instrumentEnv.lookup(languageInfo, ScopeProvider.class);
    com.oracle.truffle.api.metadata.ScopeProvider.AbstractScope ascope;
    if (scopeProvider != null) {
        TruffleLanguage.Env env = ScopeAccessor.engine().getEnvForInstrument(languageInfo);
        Object context = ScopeAccessor.langs().getContext(env);
        ascope = scopeProvider.findScope(context, node, frame);
    } else {
        ascope = new DefaultScopeVariables(node);
    }
    return ascope.toIterable();
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) TruffleObject(com.oracle.truffle.api.interop.TruffleObject) TruffleLanguage(com.oracle.truffle.api.TruffleLanguage)

Example 30 with RootNode

use of com.oracle.truffle.api.nodes.RootNode in project graal by oracle.

the class SLInstrumentTest method checkBlock.

private static void checkBlock(Scope ls) {
    assertEquals("block", ls.getName());
    // Test that ls.getNode() does not return the current root node, it ought to be a block node
    Node node = ls.getNode();
    assertNotNull(node);
    assertFalse(node.getClass().getName(), node instanceof RootNode);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) RootNode(com.oracle.truffle.api.nodes.RootNode) ProbeNode(com.oracle.truffle.api.instrumentation.ProbeNode) Node(com.oracle.truffle.api.nodes.Node)

Aggregations

RootNode (com.oracle.truffle.api.nodes.RootNode)86 Test (org.junit.Test)36 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)24 Node (com.oracle.truffle.api.nodes.Node)23 CallTarget (com.oracle.truffle.api.CallTarget)16 OptimizedCallTarget (org.graalvm.compiler.truffle.runtime.OptimizedCallTarget)16 RootCallTarget (com.oracle.truffle.api.RootCallTarget)12 FrameDescriptor (com.oracle.truffle.api.frame.FrameDescriptor)12 RootTestNode (org.graalvm.compiler.truffle.test.nodes.RootTestNode)9 Source (com.oracle.truffle.api.source.Source)8 AbstractTestNode (org.graalvm.compiler.truffle.test.nodes.AbstractTestNode)8 TruffleRuntime (com.oracle.truffle.api.TruffleRuntime)7 TruffleBoundary (com.oracle.truffle.api.CompilerDirectives.TruffleBoundary)6 SourceSection (com.oracle.truffle.api.source.SourceSection)6 LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)5 ArrayList (java.util.ArrayList)5 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)4 TruffleContext (com.oracle.truffle.api.TruffleContext)3 TruffleException (com.oracle.truffle.api.TruffleException)3 TruffleLanguage (com.oracle.truffle.api.TruffleLanguage)3