Skip to content

Commit 06e400d

Browse files
committed
Avoid using toDisplayString for host exception ToPrimitive/ToString conversion.
1 parent 3cd9f31 commit 06e400d

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/builtins/ErrorPrototypeBuiltins.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.oracle.truffle.js.builtins.ErrorPrototypeBuiltinsFactory.ErrorPrototypeToStringNodeGen;
5555
import com.oracle.truffle.js.builtins.ErrorPrototypeBuiltinsFactory.ForeignErrorPrototypeCauseNodeGen;
5656
import com.oracle.truffle.js.builtins.ErrorPrototypeBuiltinsFactory.ForeignErrorPrototypeMessageNodeGen;
57+
import com.oracle.truffle.js.builtins.ErrorPrototypeBuiltinsFactory.ForeignErrorPrototypeNameNodeGen;
5758
import com.oracle.truffle.js.builtins.ErrorPrototypeBuiltinsFactory.ForeignErrorPrototypeStackNodeGen;
5859
import com.oracle.truffle.js.nodes.access.IsObjectNode;
5960
import com.oracle.truffle.js.nodes.access.PropertyGetNode;
@@ -157,11 +158,11 @@ protected Object toStringObject(Object errorObj,
157158
if (Strings.length(strMessage) == 0) {
158159
return strName;
159160
}
160-
return toStringIntl(strName, strMessage);
161+
return concatNameAndMessage(strName, strMessage);
161162
}
162163

163164
@TruffleBoundary
164-
private static Object toStringIntl(TruffleString strName, TruffleString strMessage) {
165+
private static Object concatNameAndMessage(TruffleString strName, TruffleString strMessage) {
165166
return Strings.concatAll(strName, Strings.COLON_SPACE, strMessage);
166167
}
167168

@@ -207,6 +208,7 @@ protected ForeignErrorPrototypeBuiltins() {
207208
public enum ForeignError implements BuiltinEnum<ForeignError> {
208209
cause(0),
209210
message(0),
211+
name(0),
210212
stack(0);
211213

212214
private final int length;
@@ -233,6 +235,8 @@ protected Object createNode(JSContext context, JSBuiltin builtin, boolean constr
233235
return ForeignErrorPrototypeCauseNodeGen.create(context, builtin, args().withThis().createArgumentNodes(context));
234236
case message:
235237
return ForeignErrorPrototypeMessageNodeGen.create(context, builtin, args().withThis().createArgumentNodes(context));
238+
case name:
239+
return ForeignErrorPrototypeNameNodeGen.create(context, builtin, args().withThis().createArgumentNodes(context));
236240
case stack:
237241
return ForeignErrorPrototypeStackNodeGen.create(context, builtin, args().withThis().createArgumentNodes(context));
238242
}
@@ -266,6 +270,31 @@ protected Object getMessage(Object error,
266270

267271
}
268272

273+
@ImportStatic(JSConfig.class)
274+
public abstract static class ForeignErrorPrototypeNameNode extends JSBuiltinNode {
275+
276+
public ForeignErrorPrototypeNameNode(JSContext context, JSBuiltin builtin) {
277+
super(context, builtin);
278+
}
279+
280+
@Specialization(limit = "InteropLibraryLimit")
281+
protected Object getName(Object error,
282+
@CachedLibrary("error") InteropLibrary interop,
283+
@CachedLibrary(limit = "InteropLibraryLimit") InteropLibrary interopMeta,
284+
@Cached ImportValueNode importNode) {
285+
try {
286+
if (interop.isException(error) && interop.hasMetaObject(error)) {
287+
return importNode.executeWithTarget(interopMeta.getMetaQualifiedName(interop.getMetaObject(error)));
288+
}
289+
} catch (UnsupportedMessageException e) {
290+
// Interop contract violation
291+
assert false : e;
292+
}
293+
return Strings.UC_ERROR;
294+
}
295+
296+
}
297+
269298
@ImportStatic(JSConfig.class)
270299
public abstract static class ForeignErrorPrototypeStackNode extends JSBuiltinNode {
271300

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/nodes/cast/JSToPrimitiveNode.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,6 @@ public static Object tryHostObjectToPrimitive(Object object, Hint hint, InteropL
258258
}
259259
} else if (interop.isMetaObject(object)) {
260260
return javaClassToString(object, interop);
261-
} else if (interop.isException(object)) {
262-
return javaExceptionToString(object, interop);
263261
}
264262
return null;
265263
}
@@ -278,15 +276,6 @@ private static TruffleString javaClassToString(Object object, InteropLibrary int
278276
}
279277
}
280278

281-
@TruffleBoundary
282-
private static TruffleString javaExceptionToString(Object object, InteropLibrary interop) {
283-
try {
284-
return InteropLibrary.getUncached().asTruffleString(interop.toDisplayString(object, true));
285-
} catch (UnsupportedMessageException e) {
286-
throw Errors.createTypeErrorInteropException(object, e, "toString", interop);
287-
}
288-
}
289-
290279
@Fallback
291280
protected TruffleString doFallback(Object value) {
292281
assert value != null;

0 commit comments

Comments
 (0)