From ef19f863b4e2229b857eafa25657bf8a38bd4d2b Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Tue, 19 Nov 2024 14:16:01 +0100 Subject: [PATCH] Invoke Warning.getValue().to_text and use it also from ReplDebuggerInstrument --- .../repl/debugger/ReplDebuggerInstrument.java | 26 +++++------------- .../enso/interpreter/test/WarningsTest.java | 27 +++++++++++++++++++ .../runtime/warning/WithWarnings.java | 15 ++++++++--- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/engine/runtime-instrument-repl-debugger/src/main/java/org/enso/interpreter/instrument/repl/debugger/ReplDebuggerInstrument.java b/engine/runtime-instrument-repl-debugger/src/main/java/org/enso/interpreter/instrument/repl/debugger/ReplDebuggerInstrument.java index 87012f057480..2bc198d510ba 100644 --- a/engine/runtime-instrument-repl-debugger/src/main/java/org/enso/interpreter/instrument/repl/debugger/ReplDebuggerInstrument.java +++ b/engine/runtime-instrument-repl-debugger/src/main/java/org/enso/interpreter/instrument/repl/debugger/ReplDebuggerInstrument.java @@ -15,7 +15,6 @@ import com.oracle.truffle.api.instrumentation.TruffleInstrument; import com.oracle.truffle.api.interop.ExceptionType; import com.oracle.truffle.api.interop.InteropLibrary; -import com.oracle.truffle.api.interop.InvalidArrayIndexException; import com.oracle.truffle.api.interop.TruffleObject; import com.oracle.truffle.api.interop.UnsupportedMessageException; import com.oracle.truffle.api.library.ExportLibrary; @@ -41,12 +40,10 @@ import org.enso.interpreter.runtime.EnsoContext; import org.enso.interpreter.runtime.callable.CallerInfo; import org.enso.interpreter.runtime.callable.function.Function; -import org.enso.interpreter.runtime.data.hash.HashMapToVectorNode; import org.enso.interpreter.runtime.data.text.Text; -import org.enso.interpreter.runtime.data.vector.ArrayLikeAtNode; -import org.enso.interpreter.runtime.data.vector.ArrayLikeLengthNode; import org.enso.interpreter.runtime.state.State; import org.enso.interpreter.runtime.warning.WarningsLibrary; +import org.enso.interpreter.runtime.warning.WithWarnings; import org.graalvm.options.OptionDescriptor; import org.graalvm.options.OptionDescriptors; import org.graalvm.options.OptionKey; @@ -222,26 +219,15 @@ public Either showObject(Object object) { } private Object formatObject(Object raw) { - if (raw instanceof Text) { - return toJavaStringNode.execute((Text) raw); + if (raw instanceof Text txt) { + return toJavaStringNode.execute(txt); } if (WarningsLibrary.getUncached().hasWarnings(raw)) { try { - var sb = new StringBuilder(); - sb.append(WarningsLibrary.getUncached().removeWarnings(raw)); + var value = Text.create(WarningsLibrary.getUncached().removeWarnings(raw).toString()); var mappedWarnings = WarningsLibrary.getUncached().getWarnings(raw, true); - var pairs = HashMapToVectorNode.getUncached().execute(mappedWarnings); - var size = ArrayLikeLengthNode.getUncached().executeLength(pairs); - for (var i = 0L; i < size; i++) { - try { - var pair = ArrayLikeAtNode.getUncached().executeAt(pairs, i); - var value = ArrayLikeAtNode.getUncached().executeAt(pair, 1); - sb.append("\n ! ").append(value); - } catch (InvalidArrayIndexException ex) { - // go on - } - } - return sb.toString(); + var txt = WithWarnings.warningsToText(mappedWarnings, this, Text.create("\n ! ")); + return value.add(txt); } catch (UnsupportedMessageException e) { // go on } diff --git a/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/WarningsTest.java b/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/WarningsTest.java index 61aead7772ee..2aee6c704dda 100644 --- a/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/WarningsTest.java +++ b/engine/runtime-integration-tests/src/test/java/org/enso/interpreter/test/WarningsTest.java @@ -138,6 +138,33 @@ public void allWarningsAreExceptions() throws Exception { } } + @Test + public void toDisplayText() throws Exception { + var code = + """ + from Standard.Base import Integer, Warning, Error, Text + + type My_Warning + private Value msg + + to_display_text self -> Text = Error.throw "Don't call me!" + to_text self -> Text = "My_Warning to_text: "+self.msg + + fn = + Warning.attach (My_Warning.Value "ONE") 1 + """; + + var module = ctx.eval(LanguageInfo.ID, code); + var ownWarning = module.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "fn"); + + assertTrue("Warning is seen as exception", ownWarning.isException()); + try { + throw ownWarning.throwException(); + } catch (PolyglotException ex) { + assertEquals("My_Warning to_text: ONE", ex.getMessage()); + } + } + private void assertWarningsForAType(Value v) { var type = v.getMetaObject(); diff --git a/engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/WithWarnings.java b/engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/WithWarnings.java index 5bd99c29db4a..7fa989270c24 100644 --- a/engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/WithWarnings.java +++ b/engine/runtime/src/main/java/org/enso/interpreter/runtime/warning/WithWarnings.java @@ -183,25 +183,34 @@ private PanicException asException(Node where) { WarningsLibrary.getUncached(), ArrayLikeAtNodeGen.getUncached(), ArrayLikeLengthNodeGen.getUncached()); + var text = warningsToText(warnsMap, where, null); + return new PanicException(text, where); + } + + @CompilerDirectives.TruffleBoundary + public static Text warningsToText(EnsoHashMap warnsMap, Node where, Text prefix) + throws PanicException { var warns = Warning.fromMapToArray(warnsMap); var ctx = EnsoContext.get(where); var scopeOfAny = ctx.getBuiltins().any().getDefinitionScope(); var toText = UnresolvedSymbol.build("to_text", scopeOfAny); var node = InteropMethodCallNode.getUncached(); var state = State.create(ctx); - var text = Text.empty(); for (var w : warns) { try { - var wText = node.execute(toText, state, new Object[] {w}); + var wText = node.execute(toText, state, new Object[] {w.getValue()}); if (wText instanceof Text t) { + if (prefix != null) { + text = text.add(prefix); + } text = text.add(t); } } catch (ArityException e) { throw ctx.raiseAssertionPanic(where, null, e); } } - return new PanicException(text, where); + return text; } @ExportMessage