65
65
import com .oracle .graal .python .builtins .objects .bytes .PBytesLike ;
66
66
import com .oracle .graal .python .builtins .objects .common .HashingStorage ;
67
67
import com .oracle .graal .python .builtins .objects .common .HashingStorageLibrary ;
68
- import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes ;
69
68
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes .GetInternalByteArrayNode ;
70
69
import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodesFactory .GetInternalByteArrayNodeGen ;
71
70
import com .oracle .graal .python .builtins .objects .dict .PDict ;
@@ -344,47 +343,34 @@ protected static CodingErrorAction convertCodingErrorAction(String errors) {
344
343
@ Builtin (name = "__truffle_encode" , minNumOfPositionalArgs = 1 , parameterNames = {"obj" , "encoding" , "errors" })
345
344
@ GenerateNodeFactory
346
345
public abstract static class CodecsEncodeNode extends EncodeBaseNode {
347
- @ Child private SequenceStorageNodes .LenNode lenNode ;
348
346
@ Child private HandleEncodingErrorNode handleEncodingErrorNode ;
349
347
350
348
@ Specialization (guards = "isString(str)" )
351
349
Object encode (Object str , @ SuppressWarnings ("unused" ) PNone encoding , @ SuppressWarnings ("unused" ) PNone errors ,
352
350
@ Shared ("castStr" ) @ Cached CastToJavaStringNode castStr ) {
353
- String profiledStr = cast (castStr , str );
354
- PBytes bytes = encodeString (str , profiledStr , "utf-8" , STRICT );
355
- return factory ().createTuple (new Object []{bytes , getLength (bytes )});
351
+ return encodeString (str , cast (castStr , str ), "utf-8" , STRICT );
356
352
}
357
353
358
354
@ Specialization (guards = {"isString(str)" , "isString(encoding)" })
359
355
Object encode (Object str , Object encoding , @ SuppressWarnings ("unused" ) PNone errors ,
360
356
@ Shared ("castStr" ) @ Cached CastToJavaStringNode castStr ,
361
357
@ Shared ("castEncoding" ) @ Cached CastToJavaStringNode castEncoding ) {
362
- String profiledStr = cast (castStr , str );
363
- String profiledEncoding = cast (castEncoding , encoding );
364
- PBytes bytes = encodeString (str , profiledStr , profiledEncoding , STRICT );
365
- return factory ().createTuple (new Object []{bytes , getLength (bytes )});
358
+ return encodeString (str , cast (castStr , str ), cast (castEncoding , encoding ), STRICT );
366
359
}
367
360
368
361
@ Specialization (guards = {"isString(str)" , "isString(errors)" })
369
362
Object encode (Object str , @ SuppressWarnings ("unused" ) PNone encoding , Object errors ,
370
363
@ Shared ("castStr" ) @ Cached CastToJavaStringNode castStr ,
371
364
@ Shared ("castErrors" ) @ Cached CastToJavaStringNode castErrors ) {
372
- String profiledStr = cast (castStr , str );
373
- String profiledErrors = cast (castErrors , errors );
374
- PBytes bytes = encodeString (str , profiledStr , "utf-8" , profiledErrors );
375
- return factory ().createTuple (new Object []{bytes , getLength (bytes )});
365
+ return encodeString (str , cast (castStr , str ), "utf-8" , cast (castErrors , errors ));
376
366
}
377
367
378
368
@ Specialization (guards = {"isString(str)" , "isString(encoding)" , "isString(errors)" })
379
369
Object encode (Object str , Object encoding , Object errors ,
380
370
@ Shared ("castStr" ) @ Cached CastToJavaStringNode castStr ,
381
371
@ Shared ("castEncoding" ) @ Cached CastToJavaStringNode castEncoding ,
382
372
@ Shared ("castErrors" ) @ Cached CastToJavaStringNode castErrors ) {
383
- String profiledStr = cast (castStr , str );
384
- String profiledEncoding = cast (castEncoding , encoding );
385
- String profiledErrors = cast (castErrors , errors );
386
- PBytes bytes = encodeString (str , profiledStr , profiledEncoding , profiledErrors );
387
- return factory ().createTuple (new Object []{bytes , getLength (bytes )});
373
+ return encodeString (str , cast (castStr , str ), cast (castEncoding , encoding ), cast (castErrors , errors ));
388
374
}
389
375
390
376
private static String cast (CastToJavaStringNode cast , Object obj ) {
@@ -401,7 +387,7 @@ Object encode(Object str, @SuppressWarnings("unused") Object encoding, @Suppress
401
387
throw raise (TypeError , ErrorMessages .CANT_CONVERT_TO_STR_EXPLICITELY , str );
402
388
}
403
389
404
- private PBytes encodeString (Object self , String input , String encoding , String errors ) {
390
+ private Object encodeString (Object self , String input , String encoding , String errors ) {
405
391
CodingErrorAction errorAction = convertCodingErrorAction (errors );
406
392
Charset charset = CharsetMapping .getCharset (encoding );
407
393
if (charset == null ) {
@@ -417,15 +403,8 @@ private PBytes encodeString(Object self, String input, String encoding, String e
417
403
CompilerDirectives .transferToInterpreterAndInvalidate ();
418
404
throw raise (MemoryError );
419
405
}
420
- return factory ().createBytes (encoder .getBytes ());
421
- }
422
-
423
- private int getLength (PBytes b ) {
424
- if (lenNode == null ) {
425
- CompilerDirectives .transferToInterpreterAndInvalidate ();
426
- lenNode = insert (SequenceStorageNodes .LenNode .create ());
427
- }
428
- return lenNode .execute (b .getSequenceStorage ());
406
+ PBytes bytes = factory ().createBytes (encoder .getBytes ());
407
+ return factory ().createTuple (new Object []{bytes , input .length ()});
429
408
}
430
409
431
410
private void handleEncodingError (TruffleEncoder encoder , String errorAction , Object input ) {
0 commit comments