Search in sources :

Example 1 with LanguageInfo

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

the class ValueLanguageTest method testValueLanguage.

@Test
public void testValueLanguage() {
    Source source1 = Source.create(ValuesLanguage1.ID, "i=10\n" + "s=test\n" + "a=null\n" + "b={}\n" + "b.a={}\n" + "b.j=100\n" + "b.k=200\n");
    Source source2 = Source.create(ValuesLanguage2.ID, "j=20\n" + "s=test2\n" + "d=null\n" + "e={}\n" + "b.c={}\n" + "e.d={}\n" + "e.k=200\n");
    try (DebuggerSession session = startSession()) {
        Breakpoint bp1 = Breakpoint.newBuilder(getSourceImpl(source1)).lineIs(7).build();
        session.install(bp1);
        startEval(source1);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            DebugValue value = frame.getScope().getDeclaredValue("i");
            assertNull(value.getOriginalLanguage());
            assertEquals("L1:10", value.as(String.class));
            value = frame.getScope().getDeclaredValue("s");
            assertNull(value.getOriginalLanguage());
            assertEquals("L1:test", value.as(String.class));
            value = frame.getScope().getDeclaredValue("a");
            assertNull(value.getOriginalLanguage());
            assertEquals("null", value.as(String.class));
            value = frame.getScope().getDeclaredValue("b");
            LanguageInfo lang = value.getOriginalLanguage();
            assertNotNull(lang);
            assertEquals(ValuesLanguage1.NAME, lang.getName());
            assertEquals("{a={}, j=100}", value.as(String.class));
            event.prepareContinue();
        });
        expectDone();
        Breakpoint bp2 = Breakpoint.newBuilder(getSourceImpl(source2)).lineIs(7).build();
        session.install(bp2);
        startEval(source2);
        expectSuspended((SuspendedEvent event) -> {
            DebugStackFrame frame = event.getTopStackFrame();
            DebugValue value = frame.getScope().getDeclaredValue("j");
            assertNull(value.getOriginalLanguage());
            assertEquals("L2:20", value.as(String.class));
            value = frame.getScope().getDeclaredValue("s");
            assertNull(value.getOriginalLanguage());
            assertEquals("L2:test2", value.as(String.class));
            value = frame.getScope().getDeclaredValue("e");
            LanguageInfo lang2 = value.getOriginalLanguage();
            assertNotNull(lang2);
            assertEquals(ValuesLanguage2.NAME, lang2.getName());
            assertEquals("{d={}}", value.as(String.class));
            value = frame.getScope().getDeclaredValue("b");
            LanguageInfo lang1 = value.getOriginalLanguage();
            assertNotNull(lang1);
            assertNotEquals(lang1, lang2);
            assertEquals(ValuesLanguage1.NAME, lang1.getName());
            // info from current lang2:
            assertEquals("Object", value.as(String.class));
            assertEquals("L2:Object", value.getMetaObject().as(String.class));
            // info from original lang1:
            value = value.asInLanguage(lang1);
            assertEquals("{a={}, j=100, k=200, c={}}", value.as(String.class));
            assertEquals("L1:Map", value.getMetaObject().as(String.class));
            assertEquals("L2:Map", value.getMetaObject().asInLanguage(lang2).as(String.class));
            // Properties are always in the original language:
            value = frame.getScope().getDeclaredValue("b");
            DebugValue a = value.getProperties().iterator().next();
            assertEquals(lang1, a.getOriginalLanguage());
            Iterator<DebugValue> it = value.getProperties().iterator();
            it.next();
            it.next();
            it.next();
            DebugValue c = it.next();
            assertEquals(lang2, c.getOriginalLanguage());
            value = value.asInLanguage(lang2);
            a = value.getProperties().iterator().next();
            assertEquals(lang1, a.getOriginalLanguage());
            it = value.getProperties().iterator();
            it.next();
            it.next();
            it.next();
            c = it.next();
            assertEquals(lang2, c.getOriginalLanguage());
            value = frame.getScope().getDeclaredValue("j");
            assertNull(value.getSourceLocation());
            value = value.asInLanguage(lang1);
            assertEquals("L1:20", value.as(String.class));
            assertNull(value.getSourceLocation());
            value = frame.getScope().getDeclaredValue("d");
            assertEquals("null", value.as(String.class));
            value = value.asInLanguage(lang1);
            assertEquals("null", value.as(String.class));
            value = frame.getScope().getDeclaredValue("e");
            assertEquals(getSourceImpl(source2).createSection(4, 3, 2), value.getSourceLocation());
            value = value.asInLanguage(lang1);
            assertNull(value.getSourceLocation());
            event.prepareContinue();
        });
        expectDone();
    }
}
Also used : DebugStackFrame(com.oracle.truffle.api.debug.DebugStackFrame) Breakpoint(com.oracle.truffle.api.debug.Breakpoint) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) DebuggerSession(com.oracle.truffle.api.debug.DebuggerSession) DebugValue(com.oracle.truffle.api.debug.DebugValue) SuspendedEvent(com.oracle.truffle.api.debug.SuspendedEvent) Source(org.graalvm.polyglot.Source) Test(org.junit.Test)

Example 2 with LanguageInfo

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

the class DebugStackFrame method wrapHeapValue.

DebugValue wrapHeapValue(Object result) {
    LanguageInfo language;
    RootNode root = findCurrentRoot();
    if (root != null) {
        language = root.getLanguageInfo();
    } else {
        language = null;
    }
    return new HeapValue(event.getSession().getDebugger(), language, null, result);
}
Also used : LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) RootNode(com.oracle.truffle.api.nodes.RootNode) HeapValue(com.oracle.truffle.api.debug.DebugValue.HeapValue)

Example 3 with LanguageInfo

use of com.oracle.truffle.api.nodes.LanguageInfo 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 4 with LanguageInfo

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

the class PolyglotExceptionFrame method createGuest.

static PolyglotExceptionFrame createGuest(PolyglotExceptionImpl exception, TruffleStackTraceElement frame, boolean first) {
    if (frame == null) {
        return null;
    }
    RootNode targetRoot = frame.getTarget().getRootNode();
    if (targetRoot.isInternal()) {
        return null;
    }
    LanguageInfo info = targetRoot.getLanguageInfo();
    if (info == null) {
        return null;
    }
    PolyglotEngineImpl engine = exception.context.getEngine();
    PolyglotLanguage language = engine.idToLanguage.get(info.getId());
    String rootName = targetRoot.getName();
    SourceSection location;
    Node callNode = frame.getLocation();
    if (callNode != null) {
        com.oracle.truffle.api.source.SourceSection section = callNode.getEncapsulatingSourceSection();
        if (section != null) {
            Source source = engine.getAPIAccess().newSource(language.getId(), section.getSource());
            location = engine.getAPIAccess().newSourceSection(source, section);
        } else {
            location = null;
        }
    } else {
        location = first ? exception.getSourceLocation() : null;
    }
    return new PolyglotExceptionFrame(exception, language, location, rootName, false, null);
}
Also used : RootNode(com.oracle.truffle.api.nodes.RootNode) Node(com.oracle.truffle.api.nodes.Node) RootNode(com.oracle.truffle.api.nodes.RootNode) Source(org.graalvm.polyglot.Source) LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) SourceSection(org.graalvm.polyglot.SourceSection)

Example 5 with LanguageInfo

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

the class UnwindInstrumentationReturnSnippets method parseInContext.

/**
 * Evaluates source of (potentially different) language using the current context. The names of
 * arguments are parameters for the resulting {#link CallTarget} that allow the
 * <code>source</code> to reference the actual parameters passed to
 * {@link CallTarget#call(java.lang.Object...)}.
 *
 * @param source the source to evaluate
 * @param argumentNames the names of {@link CallTarget#call(java.lang.Object...)} arguments that
 *            can be referenced from the source
 * @return the call target representing the parsed result
 * @throws IOException if the parsing or evaluation fails for some reason
 * @since 0.12
 * @deprecated Use
 *             {@link TruffleInstrument.Env#parseInline(com.oracle.truffle.api.source.Source, com.oracle.truffle.api.nodes.Node, com.oracle.truffle.api.frame.MaterializedFrame)}
 *             with {@link #getInstrumentedNode()} instead.
 */
@Deprecated
public CallTarget parseInContext(Source source, String... argumentNames) throws IOException {
    Node instrumentedNode = getInstrumentedNode();
    LanguageInfo languageInfo = instrumentedNode.getRootNode().getLanguageInfo();
    if (languageInfo == null) {
        throw new IllegalArgumentException("No language available for given node.");
    }
    Env env = AccessorInstrumentHandler.engineAccess().getEnvForInstrument(languageInfo);
    return AccessorInstrumentHandler.langAccess().parse(env, source, instrumentedNode, argumentNames);
}
Also used : LanguageInfo(com.oracle.truffle.api.nodes.LanguageInfo) Node(com.oracle.truffle.api.nodes.Node) RootNode(com.oracle.truffle.api.nodes.RootNode) Env(com.oracle.truffle.api.TruffleLanguage.Env)

Aggregations

LanguageInfo (com.oracle.truffle.api.nodes.LanguageInfo)14 RootNode (com.oracle.truffle.api.nodes.RootNode)6 TruffleLanguage (com.oracle.truffle.api.TruffleLanguage)3 TruffleInstrument (com.oracle.truffle.api.instrumentation.TruffleInstrument)3 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)3 Node (com.oracle.truffle.api.nodes.Node)3 Test (org.junit.Test)3 Env (com.oracle.truffle.api.TruffleLanguage.Env)2 DebugValue (com.oracle.truffle.api.debug.DebugValue)2 IOException (java.io.IOException)2 Source (org.graalvm.polyglot.Source)2 CallTarget (com.oracle.truffle.api.CallTarget)1 InstrumentInfo (com.oracle.truffle.api.InstrumentInfo)1 Scope (com.oracle.truffle.api.Scope)1 Breakpoint (com.oracle.truffle.api.debug.Breakpoint)1 DebugStackFrame (com.oracle.truffle.api.debug.DebugStackFrame)1 HeapValue (com.oracle.truffle.api.debug.DebugValue.HeapValue)1 DebuggerSession (com.oracle.truffle.api.debug.DebuggerSession)1 SuspendedEvent (com.oracle.truffle.api.debug.SuspendedEvent)1 VirtualFrame (com.oracle.truffle.api.frame.VirtualFrame)1