4040 */
4141package com .oracle .truffle .js .builtins ;
4242
43- import com .oracle .truffle .api .CompilerDirectives ;
4443import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
4544import com .oracle .truffle .api .dsl .Cached ;
4645import 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 );
0 commit comments