Skip to content
Snippets Groups Projects
Commit e0eeff8e authored by Florian Angerer's avatar Florian Angerer
Browse files

[GR-4133] Fix: NPE and UnsupportedSpecializationException when retrieving arguments from RScope.

parents e0f9b45f 647280dc
Branches
No related tags found
No related merge requests found
...@@ -36,6 +36,7 @@ import com.oracle.truffle.api.nodes.Node; ...@@ -36,6 +36,7 @@ import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.r.runtime.ArgumentsSignature; import com.oracle.truffle.r.runtime.ArgumentsSignature;
import com.oracle.truffle.r.runtime.RArguments; import com.oracle.truffle.r.runtime.RArguments;
import com.oracle.truffle.r.runtime.RInternalError; import com.oracle.truffle.r.runtime.RInternalError;
import com.oracle.truffle.r.runtime.context.RContext;
import com.oracle.truffle.r.runtime.data.RFunction; import com.oracle.truffle.r.runtime.data.RFunction;
import com.oracle.truffle.r.runtime.data.RStringVector; import com.oracle.truffle.r.runtime.data.RStringVector;
import com.oracle.truffle.r.runtime.env.REnvironment.PutException; import com.oracle.truffle.r.runtime.env.REnvironment.PutException;
...@@ -64,8 +65,20 @@ public final class RScope extends AbstractScope { ...@@ -64,8 +65,20 @@ public final class RScope extends AbstractScope {
@Override @Override
protected String getName() { protected String getName() {
// TODO promises (= closure) // just to be sure
return "function"; if (env == REnvironment.emptyEnv()) {
return "empty environment";
}
assert env.getFrame() != null;
RFunction function = RArguments.getFunction(env.getFrame());
if (function != null) {
String name = function.getName();
return "function environment" + (name != null ? " for function " + name : "");
} else {
String name = env.getName();
return "explicit environment" + (name != null ? ": " + name : "");
}
} }
@Override @Override
...@@ -103,8 +116,20 @@ public final class RScope extends AbstractScope { ...@@ -103,8 +116,20 @@ public final class RScope extends AbstractScope {
} }
private static String[] collectArgs(REnvironment env) { private static String[] collectArgs(REnvironment env) {
ArgumentsSignature signature = RArguments.getSignature(env.getFrame());
return signature.getNames(); if (env != REnvironment.emptyEnv()) {
assert RArguments.isRFrame(env.getFrame());
RFunction f = RArguments.getFunction(env.getFrame());
if (f != null) {
return RContext.getRRuntimeASTAccess().getArgumentsSignature(f).getNames();
} else {
ArgumentsSignature suppliedSignature = RArguments.getSuppliedSignature(env.getFrame());
if (suppliedSignature != null) {
return suppliedSignature.getNames();
}
}
}
return new String[0];
} }
public static RScope createScope(Node node, Frame frame) { public static RScope createScope(Node node, Frame frame) {
...@@ -274,7 +299,7 @@ public final class RScope extends AbstractScope { ...@@ -274,7 +299,7 @@ public final class RScope extends AbstractScope {
@Override @Override
public ForeignAccess getForeignAccess() { public ForeignAccess getForeignAccess() {
return VariableNamesMessageResolutionForeign.ACCESS; return ArgumentNamesMessageResolutionForeign.ACCESS;
} }
public static boolean isInstance(TruffleObject obj) { public static boolean isInstance(TruffleObject obj) {
......
...@@ -29,6 +29,7 @@ import static org.junit.Assert.assertTrue; ...@@ -29,6 +29,7 @@ import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Set; import java.util.Set;
...@@ -320,6 +321,39 @@ public class FastRDebugTest { ...@@ -320,6 +321,39 @@ public class FastRDebugTest {
assertExecutedOK(); assertExecutedOK();
} }
@Test
public void testScopeArguments() throws Throwable {
final Source source = RSource.fromTextInternal("main <- function(a, b, c, d) {\n" +
" x <- 10L\n" +
"}\n" +
"closure <- function() {\n" +
" x <<- 123L\n" +
" x\n" +
"}\n",
RSource.Internal.DEBUGTEST_DEBUG);
engine.eval(source);
// @formatter:on
run.addLast(() -> {
assertNull(suspendedEvent);
assertNotNull(debuggerSession);
debuggerSession.suspendNextExecution();
});
assertArguments(1, "main(1, 2, 3, 4)");
stepInto(1);
assertArguments(2, "x <- 10L", "a", "b", "c", "d");
continueExecution();
performWork();
final Source evalSource = RSource.fromTextInternal("main(1, 2, 3, 4)\n", RSource.Internal.DEBUGTEST_EVAL);
engine.eval(evalSource);
assertExecutedOK();
}
@Test @Test
public void testChangedScopeChain() throws Throwable { public void testChangedScopeChain() throws Throwable {
final Source source = RSource.fromTextInternal("main <- function(e) {\n" + final Source source = RSource.fromTextInternal("main <- function(e) {\n" +
...@@ -461,6 +495,35 @@ public class FastRDebugTest { ...@@ -461,6 +495,35 @@ public class FastRDebugTest {
}); });
} }
private void assertArguments(final int line, final String code, final String... expectedArgs) {
run.addLast(() -> {
try {
final DebugStackFrame frame = suspendedEvent.getTopStackFrame();
DebugScope scope = frame.getScope();
Set<String> actualIdentifiers = new HashSet<>();
scope.getArguments().forEach((x) -> actualIdentifiers.add(x.getName()));
assertEquals(line + ": " + code, expectedArgs.length, actualIdentifiers.size());
Set<String> expectedIds = new HashSet<>(Arrays.asList(expectedArgs));
Assert.assertEquals(expectedIds, actualIdentifiers);
if (!run.isEmpty()) {
run.removeFirst().run();
}
} catch (RuntimeException | Error e) {
final DebugStackFrame frame = suspendedEvent.getTopStackFrame();
frame.forEach(var -> {
System.out.println(var);
});
throw e;
}
});
}
private void compareScope(final int line, final String code, boolean includeAncestors, boolean completeMatch, final Object[] expectedFrame) { private void compareScope(final int line, final String code, boolean includeAncestors, boolean completeMatch, final Object[] expectedFrame) {
final DebugStackFrame frame = suspendedEvent.getTopStackFrame(); final DebugStackFrame frame = suspendedEvent.getTopStackFrame();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment