use of com.oracle.truffle.api.Scope in project graal by oracle.
the class DebugStackFrame method getScope.
/**
* Get the current inner-most scope. The scope remain valid as long as the current stack frame
* remains valid.
* <p>
* Use {@link DebuggerSession#getTopScope(java.lang.String)} to get scopes with global validity.
* <p>
* This method is not thread-safe and will throw an {@link IllegalStateException} if called on
* another thread than it was created with.
*
* @return the scope, or <code>null</code> when no language is associated with this frame
* location, or when no local scope exists.
* @since 0.26
*/
public DebugScope getScope() {
verifyValidState(false);
SuspendedContext context = getContext();
RootNode root = findCurrentRoot();
if (root == null) {
return null;
}
Node node;
if (currentFrame == null) {
node = context.getInstrumentedNode();
} else {
node = currentFrame.getCallNode();
}
if (node.getRootNode().getLanguageInfo() == null) {
// no language, no scopes
return null;
}
Debugger debugger = event.getSession().getDebugger();
MaterializedFrame frame = findTruffleFrame();
Iterable<Scope> scopes = debugger.getEnv().findLocalScopes(node, frame);
Iterator<Scope> it = scopes.iterator();
if (!it.hasNext()) {
return null;
}
return new DebugScope(it.next(), it, debugger, event, frame, root);
}
use of com.oracle.truffle.api.Scope in project graal by oracle.
the class SLInstrumentTest method verifyLexicalScopes.
@CompilerDirectives.TruffleBoundary
private static void verifyLexicalScopes(Iterable<Scope> lexicalScopes, Iterable<Scope> dynamicScopes, int line, MaterializedFrame frame) {
int depth = 0;
switch(line) {
case 1:
break;
case 2:
for (Scope ls : lexicalScopes) {
// Test that ls.getNode() returns the current root node:
checkRootNode(ls, "test", frame);
TruffleObject arguments = (TruffleObject) ls.getArguments();
checkVars(arguments, "n", null);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "n", null);
depth++;
}
assertEquals("LexicalScope depth", 1, depth);
depth = 0;
for (Scope ls : dynamicScopes) {
// Test that ls.getNode() returns the current root node:
checkRootNode(ls, "test", frame);
TruffleObject arguments = (TruffleObject) ls.getArguments();
checkVars(arguments, "n", "n_n");
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "n", "n_n");
depth++;
}
assertEquals("DynamicScope depth", 1, depth);
break;
case 3:
case 7:
case 19:
case 20:
for (Scope ls : lexicalScopes) {
checkRootNode(ls, "test", frame);
TruffleObject arguments = (TruffleObject) ls.getArguments();
checkVars(arguments, "n", null);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "n", null, "a", null);
depth++;
}
assertEquals("LexicalScope depth", 1, depth);
depth = 0;
for (Scope ls : dynamicScopes) {
checkRootNode(ls, "test", frame);
TruffleObject arguments = (TruffleObject) ls.getArguments();
checkVars(arguments, "n", "n_n");
TruffleObject variables = (TruffleObject) ls.getVariables();
long aVal = (line < 19) ? 1L : 4L;
checkVars(variables, "n", "n_n", "a", aVal);
depth++;
}
assertEquals("DynamicScope depth", 1, depth);
break;
case 4:
case 8:
for (Scope ls : lexicalScopes) {
if (depth == 0) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables);
assertNull(ls.getArguments());
} else {
checkRootNode(ls, "test", frame);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "n", null, "a", null);
TruffleObject arguments = (TruffleObject) ls.getArguments();
checkVars(arguments, "n", null);
}
depth++;
}
assertEquals("LexicalScope depth", 2, depth);
depth = 0;
for (Scope ls : dynamicScopes) {
if (depth == 0) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables);
assertNull(ls.getArguments());
} else {
checkRootNode(ls, "test", frame);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "n", "n_n", "a", 1L);
TruffleObject arguments = (TruffleObject) ls.getArguments();
checkVars(arguments, "n", "n_n");
}
depth++;
}
assertEquals("DynamicScope depth", 2, depth);
break;
case 5:
case 9:
case 10:
for (Scope ls : lexicalScopes) {
if (depth == 0) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "b", null);
assertNull(ls.getArguments());
} else {
checkRootNode(ls, "test", frame);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "n", null, "a", null);
TruffleObject arguments = (TruffleObject) ls.getArguments();
checkVars(arguments, "n", null);
}
depth++;
}
assertEquals("LexicalScope depth", 2, depth);
depth = 0;
for (Scope ls : dynamicScopes) {
if (depth == 0) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
long bVal = (line == 5) ? 10L : 20L;
checkVars(variables, "b", bVal);
assertNull(ls.getArguments());
} else {
checkRootNode(ls, "test", frame);
TruffleObject variables = (TruffleObject) ls.getVariables();
long aVal = (line == 10) ? 0L : 1L;
checkVars(variables, "n", "n_n", "a", aVal);
TruffleObject arguments = (TruffleObject) ls.getArguments();
checkVars(arguments, "n", "n_n");
}
depth++;
}
assertEquals("DynamicScope depth", 2, depth);
break;
case 11:
for (Scope ls : lexicalScopes) {
if (depth == 0) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "b", null, "c", null);
assertNull(ls.getArguments());
} else {
checkRootNode(ls, "test", frame);
}
depth++;
}
assertEquals("LexicalScope depth", 2, depth);
depth = 0;
for (Scope ls : dynamicScopes) {
if (depth == 0) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "b", 20L, "c", 1L);
assertNull(ls.getArguments());
} else {
checkRootNode(ls, "test", frame);
}
depth++;
}
assertEquals("DynamicScope depth", 2, depth);
break;
case 12:
case 13:
case 14:
case 15:
for (Scope ls : lexicalScopes) {
if (depth == 0) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables);
assertNull(ls.getArguments());
} else if (depth == 1) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "b", null, "c", null);
assertNull(ls.getArguments());
} else {
checkRootNode(ls, "test", frame);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "n", null, "a", null);
}
depth++;
}
assertEquals("LexicalScope depth", 3, depth);
depth = 0;
for (Scope ls : dynamicScopes) {
if (depth == 0) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables);
assertNull(ls.getArguments());
} else if (depth == 1) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
long bVal = (line < 14) ? 20L : 5L;
long cVal = (line < 15) ? 1L : 6L;
checkVars(variables, "b", bVal, "c", cVal);
assertNull(ls.getArguments());
} else {
checkRootNode(ls, "test", frame);
TruffleObject variables = (TruffleObject) ls.getVariables();
long aVal = (line == 12) ? 0L : 4L;
checkVars(variables, "n", "n_n", "a", aVal);
}
depth++;
}
assertEquals("DynamicScope depth", 3, depth);
break;
case 16:
for (Scope ls : lexicalScopes) {
if (depth == 0) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "d", null);
assertNull(ls.getArguments());
} else if (depth == 1) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "b", null, "c", null);
assertNull(ls.getArguments());
} else {
checkRootNode(ls, "test", frame);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "n", null, "a", null);
}
depth++;
}
assertEquals("LexicalScope depth", 3, depth);
depth = 0;
for (Scope ls : dynamicScopes) {
if (depth == 0) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "d", 7L);
assertNull(ls.getArguments());
} else if (depth == 1) {
checkBlock(ls);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "b", 5L, "c", 6L);
assertNull(ls.getArguments());
} else {
checkRootNode(ls, "test", frame);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables, "n", "n_n", "a", 4L);
}
depth++;
}
assertEquals("DynamicScope depth", 3, depth);
break;
case 22:
case 23:
for (Scope ls : lexicalScopes) {
checkRootNode(ls, "main", frame);
TruffleObject arguments = (TruffleObject) ls.getArguments();
checkVars(arguments);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables);
depth++;
}
assertEquals("LexicalScope depth", 1, depth);
depth = 0;
for (Scope ls : dynamicScopes) {
checkRootNode(ls, "main", frame);
TruffleObject arguments = (TruffleObject) ls.getArguments();
checkVars(arguments);
TruffleObject variables = (TruffleObject) ls.getVariables();
checkVars(variables);
depth++;
}
assertEquals("DynamicScope depth", 1, depth);
break;
default:
fail("Untested line: " + line);
break;
}
}
use of com.oracle.truffle.api.Scope in project graal by oracle.
the class PolyglotLanguageContext method lookupGuest.
Object lookupGuest(String symbolName) {
ensureInitialized(null);
Iterable<?> topScopes = VMAccessor.instrumentAccess().findTopScopes(env);
for (Object topScope : topScopes) {
Scope scope = (Scope) topScope;
TruffleObject variables = (TruffleObject) scope.getVariables();
int symbolInfo = ForeignAccess.sendKeyInfo(keyInfoNode, variables, symbolName);
if (KeyInfo.isExisting(symbolInfo) && KeyInfo.isReadable(symbolInfo)) {
try {
return ForeignAccess.sendRead(readNode, variables, symbolName);
} catch (InteropException ex) {
throw new AssertionError(symbolName, ex);
}
}
}
return null;
}
use of com.oracle.truffle.api.Scope in project graal by oracle.
the class SLLanguage method findLocalScopes.
@Override
public Iterable<Scope> findLocalScopes(SLContext context, Node node, Frame frame) {
final SLLexicalScope scope = SLLexicalScope.createScope(node);
return new Iterable<Scope>() {
@Override
public Iterator<Scope> iterator() {
return new Iterator<Scope>() {
private SLLexicalScope previousScope;
private SLLexicalScope nextScope = scope;
@Override
public boolean hasNext() {
if (nextScope == null) {
nextScope = previousScope.findParent();
}
return nextScope != null;
}
@Override
public Scope next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Scope vscope = Scope.newBuilder(nextScope.getName(), nextScope.getVariables(frame)).node(nextScope.getNode()).arguments(nextScope.getArguments(frame)).build();
previousScope = nextScope;
nextScope = null;
return vscope;
}
};
}
};
}
use of com.oracle.truffle.api.Scope in project graal by oracle.
the class DebuggerSessionSnippets method getTopScope.
/**
* Returns a language top scope. The top scopes have global validity and unlike
* {@link DebugStackFrame#getScope()} have no relation to the suspended location.
*
* @since 0.30
*/
public DebugScope getTopScope(String languageId) {
LanguageInfo info = debugger.getEnv().getLanguages().get(languageId);
if (info == null) {
return null;
}
Iterable<Scope> scopes = debugger.getEnv().findTopScopes(languageId);
Iterator<Scope> it = scopes.iterator();
if (!it.hasNext()) {
return null;
}
return new DebugScope(it.next(), it, debugger, info);
}
Aggregations