Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke Warning.getValue().to_text and use it from ReplDebuggerInstrument #11591

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -222,26 +219,15 @@ public Either<Exception, String> 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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading