Skip to content

Commit 5912314

Browse files
committed
[GR-71628] Use cached TruffleString nodes.
PullRequest: js/3637
2 parents 27fb524 + 4cac101 commit 5912314

20 files changed

+280
-240
lines changed

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

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
*/
4141
package com.oracle.truffle.js.builtins;
4242

43-
import com.oracle.truffle.api.CompilerDirectives;
4443
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4544
import com.oracle.truffle.api.dsl.Cached;
4645
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -113,58 +112,43 @@ protected Object createNode(JSContext context, JSBuiltin builtin, boolean constr
113112
return null;
114113
}
115114

116-
public abstract static class JSBigIntOperation extends JSBuiltinNode {
117-
118-
@Child private JSToIntegerAsIntNode toIntegerNode;
119-
120-
public JSBigIntOperation(JSContext context, JSBuiltin builtin) {
121-
super(context, builtin);
122-
}
123-
124-
@TruffleBoundary
125-
protected JSException noBigIntFailure(Object value) {
126-
throw Errors.createTypeError(JSRuntime.safeToString(value) + " is not a BigInt");
127-
}
128-
129-
protected int toIntegerAsInt(Object target) {
130-
if (toIntegerNode == null) {
131-
CompilerDirectives.transferToInterpreterAndInvalidate();
132-
toIntegerNode = insert(JSToIntegerAsIntNode.create());
133-
}
134-
return toIntegerNode.executeInt(target);
135-
}
115+
@TruffleBoundary
116+
static JSException noBigIntFailure(Object value) {
117+
throw Errors.createTypeError(JSRuntime.safeToString(value) + " is not a BigInt");
136118
}
137119

138-
public abstract static class JSBigIntToStringNode extends JSBigIntOperation {
120+
public abstract static class JSBigIntToStringNode extends JSBuiltinNode {
139121

140122
public JSBigIntToStringNode(JSContext context, JSBuiltin builtin) {
141123
super(context, builtin);
142124
}
143125

144-
@SuppressWarnings("unused")
145126
@Specialization(guards = {"isUndefined(radix)"})
146-
protected TruffleString toStringBigIntRadix10(BigInt thisObj, Object radix,
147-
@Cached @Shared InlinedBranchProfile radixErrorBranch) {
148-
return toStringImpl(thisObj, 10, radixErrorBranch);
127+
protected TruffleString toStringBigIntRadix10(BigInt thisObj, @SuppressWarnings("unused") Object radix,
128+
@Cached @Shared TruffleString.FromJavaStringNode fromJavaString) {
129+
return Strings.fromBigInt(fromJavaString, thisObj, 10);
149130
}
150131

151132
@Specialization(guards = {"!isUndefined(radix)"})
152133
protected TruffleString toStringBigInt(BigInt thisObj, Object radix,
134+
@Cached @Shared TruffleString.FromJavaStringNode fromJavaString,
135+
@Cached @Shared JSToIntegerAsIntNode toInteger,
153136
@Cached @Shared InlinedBranchProfile radixErrorBranch) {
154-
return toStringImpl(thisObj, radix, radixErrorBranch);
137+
return toStringImpl(thisObj, radix, fromJavaString, toInteger, radixErrorBranch);
155138
}
156139

157-
@SuppressWarnings("unused")
158140
@Specialization(guards = {"isUndefined(radix)"})
159-
protected TruffleString toStringRadix10(JSBigIntObject thisObj, Object radix,
160-
@Cached @Shared InlinedBranchProfile radixErrorBranch) {
161-
return toStringImpl(JSBigInt.valueOf(thisObj), 10, radixErrorBranch);
141+
protected TruffleString toStringRadix10(JSBigIntObject thisObj, @SuppressWarnings("unused") Object radix,
142+
@Cached @Shared TruffleString.FromJavaStringNode fromJavaString) {
143+
return Strings.fromBigInt(fromJavaString, JSBigInt.valueOf(thisObj), 10);
162144
}
163145

164146
@Specialization(guards = {"!isUndefined(radix)"})
165147
protected TruffleString toString(JSBigIntObject thisObj, Object radix,
148+
@Cached @Shared TruffleString.FromJavaStringNode fromJavaString,
149+
@Cached @Shared JSToIntegerAsIntNode toInteger,
166150
@Cached @Shared InlinedBranchProfile radixErrorBranch) {
167-
return toStringImpl(JSBigInt.valueOf(thisObj), radix, radixErrorBranch);
151+
return toStringImpl(JSBigInt.valueOf(thisObj), radix, fromJavaString, toInteger, radixErrorBranch);
168152
}
169153

170154
@SuppressWarnings("unused")
@@ -173,17 +157,20 @@ protected void toStringNoBigInt(Object thisObj, Object radix) {
173157
throw Errors.createTypeError("BigInt.prototype.toString requires that 'this' be a BigInt");
174158
}
175159

176-
private TruffleString toStringImpl(BigInt numberVal, Object radix, InlinedBranchProfile radixErrorBranch) {
177-
int radixVal = toIntegerAsInt(radix);
160+
private TruffleString toStringImpl(BigInt numberVal, Object radix,
161+
TruffleString.FromJavaStringNode fromJavaString,
162+
JSToIntegerAsIntNode toIntegerAsInt,
163+
InlinedBranchProfile radixErrorBranch) {
164+
int radixVal = toIntegerAsInt.executeInt(radix);
178165
if (radixVal < 2 || radixVal > 36) {
179166
radixErrorBranch.enter(this);
180167
throw Errors.createRangeError("toString() expects radix in range 2-36");
181168
}
182-
return Strings.fromBigInt(numberVal, radixVal);
169+
return Strings.fromBigInt(fromJavaString, numberVal, radixVal);
183170
}
184171
}
185172

186-
public abstract static class JSBigIntToLocaleStringIntlNode extends JSBigIntOperation {
173+
public abstract static class JSBigIntToLocaleStringIntlNode extends JSBuiltinNode {
187174

188175
@Child InitializeNumberFormatNode initNumberFormatNode;
189176

@@ -219,24 +206,22 @@ protected Object failForNonBigInts(Object notANumber, Object locales, Object opt
219206

220207
}
221208

222-
public abstract static class JSBigIntToLocaleStringNode extends JSBigIntOperation {
209+
public abstract static class JSBigIntToLocaleStringNode extends JSBuiltinNode {
223210

224211
public JSBigIntToLocaleStringNode(JSContext context, JSBuiltin builtin) {
225212
super(context, builtin);
226213
}
227214

228215
@Specialization
229-
protected TruffleString toLocaleStringBigInt(BigInt thisObj) {
230-
return toLocaleStringImpl(thisObj);
216+
protected TruffleString toLocaleStringBigInt(BigInt thisObj,
217+
@Cached @Shared TruffleString.FromJavaStringNode fromJavaString) {
218+
return Strings.fromBigInt(fromJavaString, thisObj);
231219
}
232220

233221
@Specialization
234-
protected TruffleString toLocaleStringJSBigInt(JSBigIntObject thisObj) {
235-
return toLocaleStringImpl(JSBigInt.valueOf(thisObj));
236-
}
237-
238-
private static TruffleString toLocaleStringImpl(BigInt bi) {
239-
return Strings.fromBigInt(bi);
222+
protected TruffleString toLocaleStringJSBigInt(JSBigIntObject thisObj,
223+
@Cached @Shared TruffleString.FromJavaStringNode fromJavaString) {
224+
return Strings.fromBigInt(fromJavaString, JSBigInt.valueOf(thisObj));
240225
}
241226

242227
@Fallback
@@ -245,7 +230,7 @@ protected Object failForNonBigInts(Object thisObject) {
245230
}
246231
}
247232

248-
public abstract static class JSBigIntValueOfNode extends JSBigIntOperation {
233+
public abstract static class JSBigIntValueOfNode extends JSBuiltinNode {
249234

250235
public JSBigIntValueOfNode(JSContext context, JSBuiltin builtin) {
251236
super(context, builtin);

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ public DebugClassNode(JSContext context, JSBuiltin builtin, boolean getName) {
247247
this.getName = getName;
248248
}
249249

250+
@TruffleBoundary
250251
@Specialization
251252
protected Object clazz(Object obj) {
252253
if (obj == null) {
@@ -348,19 +349,22 @@ protected TruffleString debugPrint(JSDynamicObject object, int level, int levelS
348349
Strings.builderAppend(sb, key);
349350
if (desc.isDataDescriptor()) {
350351
Object value = JSObject.get(object, key);
351-
if (JSDynamicObject.isJSDynamicObject(value)) {
352-
if ((JSGuards.isJSOrdinaryObject(value) || JSGlobal.isJSGlobalObject(value)) && !key.equals(JSObject.CONSTRUCTOR)) {
352+
TruffleString valueStr;
353+
if (value instanceof JSDynamicObject valueObj) {
354+
if ((JSGuards.isJSOrdinaryObject(valueObj) || JSGlobal.isJSGlobalObject(valueObj)) && !key.equals(JSObject.CONSTRUCTOR)) {
353355
if (level < levelStop && !key.equals(JSObject.CONSTRUCTOR)) {
354-
value = debugPrint((JSDynamicObject) value, level + 1, levelStop);
356+
valueStr = debugPrint(valueObj, level + 1, levelStop);
355357
} else {
356-
value = Strings.EMPTY_OBJECT_DOTS;
358+
valueStr = Strings.EMPTY_OBJECT_DOTS;
357359
}
358360
} else {
359-
value = JSObject.getJSClass((JSDynamicObject) value);
361+
valueStr = Strings.fromJavaString(JSObject.getJSClass(valueObj).toString());
360362
}
363+
} else {
364+
valueStr = Strings.fromObject(value);
361365
}
362366
Strings.builderAppend(sb, Strings.COLON_SPACE);
363-
Strings.builderAppend(sb, Strings.fromObject(value));
367+
Strings.builderAppend(sb, valueStr);
364368
}
365369
if (!key.equals(properties.get(properties.size() - 1))) {
366370
Strings.builderAppend(sb, ',');

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,8 @@ private static final class ScriptEngineGlobalScopeBindingsPropertyProxy extends
17921792
@Override
17931793
@TruffleBoundary
17941794
public Object get(JSDynamicObject store) {
1795-
return JSInteropUtil.readMemberOrDefault(globalContextBindings, key, Undefined.instance, bindingsInterop, ImportValueNode.getUncached());
1795+
return JSInteropUtil.readMemberOrDefault(globalContextBindings, key, Undefined.instance, bindingsInterop,
1796+
ImportValueNode.getUncached(), TruffleString.ToJavaStringNode.getUncached());
17961797
}
17971798

17981799
@TruffleBoundary

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -514,8 +514,9 @@ abstract static class JavaSuperNode extends JSBuiltinNode {
514514
@Specialization
515515
protected Object superAdapter(Object adapter,
516516
@CachedLibrary(limit = "InteropLibraryLimit") InteropLibrary interop,
517-
@Cached ImportValueNode toJSType) {
518-
return JSInteropUtil.readMemberOrDefault(adapter, Strings.SUPER, Undefined.instance, interop, toJSType);
517+
@Cached ImportValueNode toJSType,
518+
@Cached TruffleString.ToJavaStringNode toJavaString) {
519+
return JSInteropUtil.readMemberOrDefault(adapter, Strings.SUPER, Undefined.instance, interop, toJSType, toJavaString);
519520
}
520521
}
521522

0 commit comments

Comments
 (0)