Print out Error values as well as warnings

This commit is contained in:
Jaroslav Tulach 2024-08-18 10:09:22 +02:00
parent aa7c9a7621
commit 13125460ae
3 changed files with 64 additions and 10 deletions

View File

@ -97,9 +97,6 @@ public final class ReplDebuggerInstrument extends TruffleInstrument {
MessageEndpoint client = env.startServer(URI.create(DebugServerInfo.URI), handler);
if (client != null) {
handler.setClient(client);
} else {
env.getLogger(ReplDebuggerInstrument.class)
.warning("ReplDebuggerInstrument was initialized, " + "but no client connected");
}
} catch (MessageTransport.VetoException e) {
env.getLogger(ReplDebuggerInstrument.class)
@ -145,7 +142,8 @@ public final class ReplDebuggerInstrument extends TruffleInstrument {
this.atExit = atExit;
}
private Object getValue(MaterializedFrame frame, FramePointer ptr, boolean onlyWarnings) {
private Object readValue(
MaterializedFrame frame, FramePointer ptr, boolean onlyWarningsOrErrors) {
var raw = getProperFrame(frame, ptr).getValue(ptr.frameSlotIdx());
if (WarningsLibrary.getUncached().hasWarnings(raw)) {
try {
@ -168,7 +166,12 @@ public final class ReplDebuggerInstrument extends TruffleInstrument {
// go on
}
}
return onlyWarnings ? null : raw;
if (onlyWarningsOrErrors) {
if (!InteropLibrary.getUncached().isException(raw)) {
return null;
}
}
return raw;
}
private MaterializedFrame getProperFrame(MaterializedFrame frame, FramePointer ptr) {
@ -184,13 +187,13 @@ public final class ReplDebuggerInstrument extends TruffleInstrument {
return listBindings(false);
}
public Map<String, Object> listBindings(boolean onlyWarnings) {
public Map<String, Object> listBindings(boolean onlyWarningsOrErrors) {
Map<String, FramePointer> flatScope =
nodeState.getLastScope().getLocalScope().flattenBindings();
Map<String, Object> result = new HashMap<>();
for (Map.Entry<String, FramePointer> entry : flatScope.entrySet()) {
var valueOrNull =
getValue(nodeState.getLastScope().getFrame(), entry.getValue(), onlyWarnings);
readValue(nodeState.getLastScope().getFrame(), entry.getValue(), onlyWarningsOrErrors);
if (valueOrNull != null) {
result.put(entry.getKey(), valueOrNull);
}

View File

@ -67,7 +67,8 @@ public class PolyglotErrorTest {
3 -> panic3
4 -> panic4
5 -> panic5
_ -> panic6
6 -> panic6
_ -> panic7
panic1 = PolyglotErrorTest.bar (TypeCa.Ca 'x')
@ -83,6 +84,12 @@ public class PolyglotErrorTest {
panic5 = PolyglotErrorTest.bar (TypeCe.Ce "Foo")
panic6 = PolyglotErrorTest.bar (TypeCe.Ce 44)
panic7 =
j = Error.throw 1
d = Error.throw 2
t = j + d
v = [j, d, t]
v
""";
var src = Source.newBuilder("enso", code, "test.enso").build();
var module = ctx.eval(src);
@ -144,4 +151,18 @@ public class PolyglotErrorTest {
assertEquals(
"[[Error in method `to_text` of [Ce 44]: Expected Text but got 44]]", v.asString());
}
@Test
public void panic7() {
var r = panic.execute(7);
assertTrue("Got array back: " + r, r.hasArrayElements());
assertEquals("Got three elements", 3, r.getArraySize());
assertTrue("Error 1 at 0th" + r, r.getArrayElement(0).isException());
assertTrue("Error 2 at 1st" + r, r.getArrayElement(1).isException());
assertTrue("Error 1 at 2nd " + r, r.getArrayElement(2).isException());
assertEquals("(Error: 1)", r.getArrayElement(0).toString());
assertEquals("(Error: 2)", r.getArrayElement(1).toString());
assertEquals("(Error: 1)", r.getArrayElement(2).toString());
}
}

View File

@ -4,6 +4,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
@ -43,7 +44,7 @@ public class DebugServerWithScriptTest {
}
@Test
public void propertyListingVariables() throws Exception {
public void listingVariablesWithWarnings() throws Exception {
var code =
"""
from Standard.Base import all
@ -63,7 +64,7 @@ public class DebugServerWithScriptTest {
assertEquals("Three", 3, r.getArrayElement(2).asInt());
assertEquals("No output printed", "", out.toString());
assertThat(
"Error contains some warnings",
"Stderr contains some warnings",
err.toString(),
AllOf.allOf(
containsString("d = 2"),
@ -71,4 +72,33 @@ public class DebugServerWithScriptTest {
containsString("doubled value"),
not(containsString("j = 1"))));
}
@Test
public void panicOnError() throws Exception {
var code =
"""
from Standard.Base import all
inspect =
j = 1
d = Error.throw 2
t = j + d
v = [j, d, t]
v
""";
var r = ContextUtils.evalModule(ctx, code, "ScriptTest.enso", "inspect");
assertTrue("Got array back: " + r, r.hasArrayElements());
assertEquals("Got three elements", 3, r.getArraySize());
assertFalse("No error at 0th" + r, r.getArrayElement(0).isException());
assertTrue("Error 2 at 1st" + r, r.getArrayElement(1).isException());
assertTrue("Error 2 at 2nd " + r, r.getArrayElement(2).isException());
assertEquals("No output printed", "", out.toString());
assertThat(
"Stderr contains some errors",
err.toString(),
AllOf.allOf(
containsString("d = Error:2"),
containsString("t = Error:2"),
not(containsString("j = 1"))));
}
}