@@ -222,46 +222,41 @@ protected boolean isStandardEncoding(RubyEncoding encoding) {
222
222
/** Use {@link NegotiateCompatibleStringEncodingNode} instead if both arguments are always Strings, for footprint */
223
223
public abstract static class NegotiateCompatibleEncodingNode extends RubyBaseNode {
224
224
225
- @ Child private TruffleString .GetByteCodeRangeNode codeRangeNode ;
226
-
227
225
@ NeverDefault
228
226
public static NegotiateCompatibleEncodingNode create () {
229
227
return NegotiateCompatibleEncodingNodeGen .create ();
230
228
}
231
229
232
- public abstract RubyEncoding executeNegotiate (Object first , Object second );
230
+ public abstract RubyEncoding executeNegotiate (Object first , RubyEncoding firstEncoding , Object second ,
231
+ RubyEncoding secondEncoding );
233
232
234
233
@ Specialization (
235
234
guards = {
236
- "encodingNode.execute(this, first) == cachedEncoding" ,
237
- "encodingNode.execute(this, second) == cachedEncoding" ,
235
+ "firstEncoding == cachedEncoding" ,
236
+ "secondEncoding == cachedEncoding" ,
238
237
"cachedEncoding != null" },
239
238
limit = "getCacheLimit()" )
240
- protected RubyEncoding negotiateSameEncodingCached (Object first , Object second ,
241
- @ Cached @ Shared ToRubyEncodingNode encodingNode ,
242
- @ Cached ("encodingNode.execute(this, first) " ) RubyEncoding cachedEncoding ) {
239
+ protected RubyEncoding negotiateSameEncodingCached (
240
+ Object first , RubyEncoding firstEncoding , Object second , RubyEncoding secondEncoding ,
241
+ @ Cached ("firstEncoding " ) RubyEncoding cachedEncoding ) {
243
242
return cachedEncoding ;
244
243
}
245
244
246
245
@ Specialization (
247
246
guards = { "firstEncoding == secondEncoding" , "firstEncoding != null" },
248
- replaces = "negotiateSameEncodingCached" ,
249
- limit = "1" )
250
- protected RubyEncoding negotiateSameEncodingUncached (Object first , Object second ,
251
- @ Cached @ Shared ToRubyEncodingNode encodingNode ,
252
- @ Bind ("encodingNode.execute(this, first)" ) RubyEncoding firstEncoding ,
253
- @ Bind ("encodingNode.execute(this, second)" ) RubyEncoding secondEncoding ) {
254
- return encodingNode .execute (this , first );
247
+ replaces = "negotiateSameEncodingCached" )
248
+ protected RubyEncoding negotiateSameEncodingUncached (
249
+ Object first , RubyEncoding firstEncoding , Object second , RubyEncoding secondEncoding ) {
250
+ return firstEncoding ;
255
251
}
256
252
257
253
@ Specialization (guards = { "libFirst.isRubyString(first)" , "libSecond.isRubyString(second)" }, limit = "1" )
258
- protected static RubyEncoding negotiateStringStringEncoding (Object first , Object second ,
254
+ protected static RubyEncoding negotiateStringStringEncoding (
255
+ Object first , RubyEncoding firstEncoding , Object second , RubyEncoding secondEncoding ,
259
256
@ Cached @ Shared RubyStringLibrary libFirst ,
260
257
@ Cached @ Shared RubyStringLibrary libSecond ,
261
258
@ Cached NegotiateCompatibleStringEncodingNode negotiateNode ,
262
259
@ Bind ("this" ) Node node ) {
263
- final RubyEncoding firstEncoding = libFirst .getEncoding (first );
264
- final RubyEncoding secondEncoding = libSecond .getEncoding (second );
265
260
return negotiateNode .execute (
266
261
node ,
267
262
libFirst .getTString (first ),
@@ -274,20 +269,20 @@ protected static RubyEncoding negotiateStringStringEncoding(Object first, Object
274
269
guards = {
275
270
"libFirst.isRubyString(first)" ,
276
271
"isNotRubyString(second)" ,
277
- "getCodeRange(first, libFirst) == codeRange " ,
272
+ "codeRange == codeRangeCached " ,
278
273
"firstEncoding == firstEncodingCached" ,
279
274
"secondEncoding == secondEncodingCached" ,
280
275
"firstEncodingCached != secondEncodingCached" },
281
276
limit = "getCacheLimit()" )
282
- protected RubyEncoding negotiateStringObjectCached (Object first , Object second ,
283
- @ Cached @ Shared ToRubyEncodingNode encodingNode ,
277
+ protected RubyEncoding negotiateStringObjectCached (
278
+ Object first , RubyEncoding firstEncoding , Object second , RubyEncoding secondEncoding ,
284
279
@ Cached @ Shared RubyStringLibrary libFirst ,
285
- @ Bind ( "encodingNode.execute(this, first) " ) RubyEncoding firstEncoding ,
286
- @ Bind ( "encodingNode.execute(this, second) " ) RubyEncoding secondEncoding ,
287
- @ Cached ( "encodingNode.execute(this, first)" ) RubyEncoding firstEncodingCached ,
288
- @ Cached ( "encodingNode.execute(this, second )" ) RubyEncoding secondEncodingCached ,
289
- @ Cached ("getCodeRange(first, libFirst) " ) TruffleString .CodeRange codeRange ,
290
- @ Cached ("negotiateStringObjectUncached(first, second, encodingNode, firstEncoding, secondEncoding , libFirst)" ) RubyEncoding negotiatedEncoding ) {
280
+ @ Cached ( "firstEncoding " ) RubyEncoding firstEncodingCached ,
281
+ @ Cached ( "secondEncoding " ) RubyEncoding secondEncodingCached ,
282
+ @ Cached @ Shared TruffleString . GetByteCodeRangeNode codeRangeNode ,
283
+ @ Bind ( "getCodeRange(codeRangeNode, first, libFirst )" ) TruffleString . CodeRange codeRange ,
284
+ @ Cached ("codeRange " ) TruffleString .CodeRange codeRangeCached ,
285
+ @ Cached ("negotiateStringObjectUncached(first, firstEncoding, second, secondEncoding, codeRangeNode , libFirst)" ) RubyEncoding negotiatedEncoding ) {
291
286
return negotiatedEncoding ;
292
287
}
293
288
@@ -297,10 +292,9 @@ protected RubyEncoding negotiateStringObjectCached(Object first, Object second,
297
292
"firstEncoding != secondEncoding" ,
298
293
"isNotRubyString(second)" },
299
294
replaces = "negotiateStringObjectCached" , limit = "1" )
300
- protected RubyEncoding negotiateStringObjectUncached (Object first , Object second ,
301
- @ Cached @ Shared ToRubyEncodingNode encodingNode ,
302
- @ Bind ("encodingNode.execute(this, first)" ) RubyEncoding firstEncoding ,
303
- @ Bind ("encodingNode.execute(this, second)" ) RubyEncoding secondEncoding ,
295
+ protected RubyEncoding negotiateStringObjectUncached (
296
+ Object first , RubyEncoding firstEncoding , Object second , RubyEncoding secondEncoding ,
297
+ @ Cached @ Shared TruffleString .GetByteCodeRangeNode codeRangeNode ,
304
298
@ Cached @ Shared RubyStringLibrary libFirst ) {
305
299
306
300
if (secondEncoding == null ) {
@@ -315,7 +309,7 @@ protected RubyEncoding negotiateStringObjectUncached(Object first, Object second
315
309
return firstEncoding ;
316
310
}
317
311
318
- if (getCodeRange (first , libFirst ) == ASCII ) {
312
+ if (getCodeRange (codeRangeNode , first , libFirst ) == ASCII ) {
319
313
return secondEncoding ;
320
314
}
321
315
@@ -328,12 +322,12 @@ protected RubyEncoding negotiateStringObjectUncached(Object first, Object second
328
322
"firstEncoding != secondEncoding" ,
329
323
"isNotRubyString(first)" },
330
324
limit = "1" )
331
- protected RubyEncoding negotiateObjectString (Object first , Object second ,
332
- @ Cached @ Shared ToRubyEncodingNode encodingNode ,
333
- @ Bind ( "encodingNode.execute(this, first)" ) RubyEncoding firstEncoding ,
334
- @ Bind ( "encodingNode.execute(this, second)" ) RubyEncoding secondEncoding ,
335
- @ Cached @ Shared RubyStringLibrary libSecond ) {
336
- return negotiateStringObjectUncached ( second , first , encodingNode , secondEncoding , firstEncoding , libSecond );
325
+ protected RubyEncoding negotiateObjectString (
326
+ Object first , RubyEncoding firstEncoding , Object second , RubyEncoding secondEncoding ,
327
+ @ Cached @ Shared RubyStringLibrary libSecond ,
328
+ @ Cached @ Shared TruffleString . GetByteCodeRangeNode codeRangeNode ) {
329
+ return negotiateStringObjectUncached ( second , secondEncoding , first , firstEncoding , codeRangeNode ,
330
+ libSecond );
337
331
}
338
332
339
333
@ Specialization (
@@ -343,14 +337,14 @@ protected RubyEncoding negotiateObjectString(Object first, Object second,
343
337
"isNotRubyString(second)" ,
344
338
"firstEncoding != null" ,
345
339
"secondEncoding != null" ,
346
- "encodingNode.execute(this, first) == firstEncoding " ,
347
- "encodingNode.execute(this, second) == secondEncoding " , },
340
+ "firstEncoding == firstEncodingCached " ,
341
+ "secondEncoding == secondEncodingCached " , },
348
342
limit = "getCacheLimit()" )
349
- protected RubyEncoding negotiateObjectObjectCached (Object first , Object second ,
350
- @ Cached @ Shared ToRubyEncodingNode encodingNode ,
351
- @ Cached ("encodingNode.execute(this, first) " ) RubyEncoding firstEncoding ,
352
- @ Cached ("encodingNode.execute(this, second) " ) RubyEncoding secondEncoding ,
353
- @ Cached ("areCompatible(firstEncoding, secondEncoding )" ) RubyEncoding negotiatedEncoding ) {
343
+ protected RubyEncoding negotiateObjectObjectCached (
344
+ Object first , RubyEncoding firstEncoding , Object second , RubyEncoding secondEncoding ,
345
+ @ Cached ("firstEncoding " ) RubyEncoding firstEncodingCached ,
346
+ @ Cached ("secondEncoding " ) RubyEncoding secondEncodingCached ,
347
+ @ Cached ("areCompatible(firstEncodingCached, secondEncodingCached )" ) RubyEncoding negotiatedEncoding ) {
354
348
return negotiatedEncoding ;
355
349
}
356
350
@@ -359,12 +353,9 @@ protected RubyEncoding negotiateObjectObjectCached(Object first, Object second,
359
353
"firstEncoding != secondEncoding" ,
360
354
"isNotRubyString(first)" ,
361
355
"isNotRubyString(second)" },
362
- replaces = "negotiateObjectObjectCached" ,
363
- limit = "1" )
364
- protected RubyEncoding negotiateObjectObjectUncached (Object first , Object second ,
365
- @ Cached @ Shared ToRubyEncodingNode encodingNode ,
366
- @ Bind ("encodingNode.execute(this, first)" ) RubyEncoding firstEncoding ,
367
- @ Bind ("encodingNode.execute(this, second)" ) RubyEncoding secondEncoding ) {
356
+ replaces = "negotiateObjectObjectCached" )
357
+ protected RubyEncoding negotiateObjectObjectUncached (
358
+ Object first , RubyEncoding firstEncoding , Object second , RubyEncoding secondEncoding ) {
368
359
return areCompatible (firstEncoding , secondEncoding );
369
360
}
370
361
@@ -390,12 +381,8 @@ protected static RubyEncoding areCompatible(RubyEncoding enc1, RubyEncoding enc2
390
381
return null ;
391
382
}
392
383
393
- protected TruffleString .CodeRange getCodeRange (Object string , RubyStringLibrary libString ) {
394
- if (codeRangeNode == null ) {
395
- CompilerDirectives .transferToInterpreterAndInvalidate ();
396
- codeRangeNode = insert (TruffleString .GetByteCodeRangeNode .create ());
397
- }
398
-
384
+ protected TruffleString .CodeRange getCodeRange (TruffleString .GetByteCodeRangeNode codeRangeNode , Object string ,
385
+ RubyStringLibrary libString ) {
399
386
return codeRangeNode .execute (libString .getTString (string ), libString .getTEncoding (string ));
400
387
}
401
388
@@ -419,8 +406,12 @@ public static CompatibleQueryNode create() {
419
406
420
407
@ Specialization
421
408
protected Object isCompatible (Object first , Object second ,
409
+ @ Cached ToRubyEncodingNode toRubyEncodingNode ,
422
410
@ Cached InlinedConditionProfile noNegotiatedEncodingProfile ) {
423
- final RubyEncoding negotiatedEncoding = negotiateCompatibleEncodingNode .executeNegotiate (first , second );
411
+ final var firstEncoding = toRubyEncodingNode .execute (this , first );
412
+ final var secondEncoding = toRubyEncodingNode .execute (this , second );
413
+ final RubyEncoding negotiatedEncoding = negotiateCompatibleEncodingNode .executeNegotiate (first ,
414
+ firstEncoding , second , secondEncoding );
424
415
425
416
if (noNegotiatedEncodingProfile .profile (this , negotiatedEncoding == null )) {
426
417
return nil ;
@@ -841,13 +832,16 @@ protected static RubyEncoding checkEncoding(Node node, Object first, Object seco
841
832
@ Cached ToRubyEncodingNode toRubyEncodingNode ,
842
833
@ Cached (inline = false ) NegotiateCompatibleEncodingNode negotiateCompatibleEncodingNode ,
843
834
@ Cached InlinedBranchProfile errorProfile ) {
844
- final RubyEncoding negotiatedEncoding = negotiateCompatibleEncodingNode .executeNegotiate (first , second );
835
+ final var firstEncoding = toRubyEncodingNode .execute (node , first );
836
+ final var secondEncoding = toRubyEncodingNode .execute (node , second );
837
+ final RubyEncoding negotiatedEncoding = negotiateCompatibleEncodingNode .executeNegotiate (first ,
838
+ firstEncoding , second , secondEncoding );
845
839
846
840
if (negotiatedEncoding == null ) {
847
841
errorProfile .enter (node );
848
842
throw new RaiseException (getContext (node ), coreExceptions (node ).encodingCompatibilityErrorIncompatible (
849
- toRubyEncodingNode . execute ( node , first ) ,
850
- toRubyEncodingNode . execute ( node , second ) ,
843
+ firstEncoding ,
844
+ secondEncoding ,
851
845
node ));
852
846
}
853
847
0 commit comments