75
75
import com .oracle .graal .python .util .PythonUtils ;
76
76
import com .oracle .truffle .api .CompilerAsserts ;
77
77
import com .oracle .truffle .api .CompilerDirectives ;
78
+ import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
78
79
import com .oracle .truffle .api .dsl .Cached ;
79
80
import com .oracle .truffle .api .dsl .Cached .Exclusive ;
80
81
import com .oracle .truffle .api .dsl .Cached .Shared ;
@@ -397,7 +398,7 @@ static Object doPSequence(PySequenceArrayWrapper object,
397
398
@ CachedLibrary (limit = "3" ) PythonNativeWrapperLibrary lib ,
398
399
@ Exclusive @ Cached ToNativeStorageNode toNativeStorageNode ) {
399
400
PSequence sequence = (PSequence ) lib .getDelegate (object );
400
- NativeSequenceStorage nativeStorage = toNativeStorageNode .execute (getStorage .execute (sequence ));
401
+ NativeSequenceStorage nativeStorage = toNativeStorageNode .execute (getStorage .execute (sequence ), sequence instanceof PBytesLike );
401
402
if (nativeStorage == null ) {
402
403
CompilerDirectives .transferToInterpreter ();
403
404
throw new IllegalStateException ("could not allocate native storage" );
@@ -423,22 +424,38 @@ protected static boolean isPSequence(Object obj) {
423
424
@ GenerateUncached
424
425
abstract static class ToNativeStorageNode extends Node {
425
426
426
- public abstract NativeSequenceStorage execute (SequenceStorage object );
427
+ public abstract NativeSequenceStorage execute (SequenceStorage object , boolean isBytesLike );
427
428
428
- @ Specialization (guards = "!isNative(s)" )
429
- static NativeSequenceStorage doManaged (SequenceStorage s ,
429
+ public static boolean isEmptySequenceStorage (SequenceStorage s ) {
430
+ return s instanceof EmptySequenceStorage ;
431
+ }
432
+
433
+ @ Specialization (guards = {"!isNative(s)" , "!isEmptySequenceStorage(s)" })
434
+ static NativeSequenceStorage doManaged (SequenceStorage s , @ SuppressWarnings ("unused" ) boolean isBytesLike ,
435
+ @ Cached ConditionProfile isObjectArrayProfile ,
430
436
@ Shared ("storageToNativeNode" ) @ Cached SequenceStorageNodes .StorageToNativeNode storageToNativeNode ,
431
437
@ Cached SequenceStorageNodes .GetInternalArrayNode getInternalArrayNode ) {
432
- return storageToNativeNode .execute (getInternalArrayNode .execute (s ));
438
+ Object array = getInternalArrayNode .execute (s );
439
+ if (isBytesLike ) {
440
+ assert array instanceof byte [];
441
+ } else if (!isObjectArrayProfile .profile (array instanceof Object [])) {
442
+ array = generalize (s );
443
+ }
444
+ return storageToNativeNode .execute (array );
445
+ }
446
+
447
+ @ TruffleBoundary
448
+ private static Object generalize (SequenceStorage s ) {
449
+ return s .getInternalArray ();
433
450
}
434
451
435
452
@ Specialization
436
- static NativeSequenceStorage doNative (NativeSequenceStorage s ) {
453
+ static NativeSequenceStorage doNative (NativeSequenceStorage s , @ SuppressWarnings ( "unused" ) boolean isBytesLike ) {
437
454
return s ;
438
455
}
439
456
440
457
@ Specialization
441
- static NativeSequenceStorage doEmptyStorage (@ SuppressWarnings ("unused" ) EmptySequenceStorage s ,
458
+ static NativeSequenceStorage doEmptyStorage (@ SuppressWarnings ("unused" ) EmptySequenceStorage s , @ SuppressWarnings ( "unused" ) boolean isBytesLike ,
442
459
@ Shared ("storageToNativeNode" ) @ Cached SequenceStorageNodes .StorageToNativeNode storageToNativeNode ) {
443
460
// TODO(fa): not sure if that completely reflects semantics
444
461
return storageToNativeNode .execute (PythonUtils .EMPTY_BYTE_ARRAY );
0 commit comments