18
18
import com .oracle .graal .python .builtins .modules .ctypes .memory .Pointer .Storage ;
19
19
import com .oracle .graal .python .builtins .modules .ctypes .memory .Pointer .ZeroStorage ;
20
20
import com .oracle .graal .python .builtins .objects .buffer .PythonBufferAccessLibrary ;
21
+ import com .oracle .graal .python .builtins .objects .bytes .PBytesLike ;
21
22
import com .oracle .graal .python .builtins .objects .cext .PythonNativeVoidPtr ;
23
+ import com .oracle .graal .python .builtins .objects .common .SequenceStorageNodes ;
24
+ import com .oracle .graal .python .builtins .objects .memoryview .PMemoryView ;
22
25
import com .oracle .graal .python .nodes .ErrorMessages ;
23
26
import com .oracle .graal .python .nodes .PRaiseNode ;
24
27
import com .oracle .graal .python .nodes .util .CastToJavaUnsignedLongNode ;
28
+ import com .oracle .graal .python .runtime .sequence .storage .ByteSequenceStorage ;
29
+ import com .oracle .graal .python .runtime .sequence .storage .NativeSequenceStorage ;
25
30
import com .oracle .graal .python .util .PythonUtils ;
26
31
import com .oracle .truffle .api .CompilerDirectives ;
27
32
import com .oracle .truffle .api .dsl .Cached ;
32
37
import com .oracle .truffle .api .dsl .GenerateUncached ;
33
38
import com .oracle .truffle .api .dsl .ImportStatic ;
34
39
import com .oracle .truffle .api .dsl .Specialization ;
40
+ import com .oracle .truffle .api .interop .InteropLibrary ;
41
+ import com .oracle .truffle .api .interop .UnsupportedMessageException ;
35
42
import com .oracle .truffle .api .library .CachedLibrary ;
36
43
import com .oracle .truffle .api .nodes .Node ;
37
44
@@ -602,9 +609,33 @@ static long doPointerArray(Node inliningTarget, MemoryBlock memory, PointerArray
602
609
}
603
610
604
611
@ Specialization
605
- static long doMemoryView (MemoryBlock memory , MemoryViewStorage storage , int offset ) {
606
- // TODO
607
- throw CompilerDirectives .shouldNotReachHere ("Memoryview not implemented" );
612
+ static long doMemoryView (Node inliningTarget , MemoryBlock memory , MemoryViewStorage storage , int offset ,
613
+ @ CachedLibrary (limit = "1" ) InteropLibrary ilib ,
614
+ @ Cached (inline = false ) SequenceStorageNodes .StorageToNativeNode storageToNativeNode ) {
615
+ PMemoryView mv = storage .memoryView ;
616
+ Object ptr = null ;
617
+ if (mv .getBufferPointer () != null ) {
618
+ ptr = mv .getBufferPointer ();
619
+ } else if (mv .getBuffer () instanceof PBytesLike bytes ) {
620
+ if (bytes .getSequenceStorage () instanceof NativeSequenceStorage nativeSequenceStorage ) {
621
+ ptr = nativeSequenceStorage .getPtr ();
622
+ }
623
+ if (bytes .getSequenceStorage () instanceof ByteSequenceStorage byteSequenceStorage ) {
624
+ NativeSequenceStorage nativeStorage = storageToNativeNode .execute (byteSequenceStorage .getInternalByteArray (), byteSequenceStorage .length ());
625
+ bytes .setSequenceStorage (nativeStorage );
626
+ ptr = nativeStorage .getPtr ();
627
+ }
628
+ }
629
+ if (ptr != null && ilib .isPointer (ptr )) {
630
+ try {
631
+ long nativePointer = ilib .asPointer (ptr );
632
+ memory .storage = new LongPointerStorage (nativePointer );
633
+ return nativePointer + offset ;
634
+ } catch (UnsupportedMessageException e ) {
635
+ throw CompilerDirectives .shouldNotReachHere (e );
636
+ }
637
+ }
638
+ throw PRaiseNode .raiseUncached (inliningTarget , NotImplementedError , ErrorMessages .MEMORYVIEW_CANNOT_BE_CONVERTED_TO_NATIVE_MEMORY );
608
639
}
609
640
}
610
641
@@ -619,9 +650,9 @@ public final Object execute(Node inliningTarget, Pointer ptr) {
619
650
protected abstract Object execute (Node inliningTarget , MemoryBlock memory , Storage storage , int offset );
620
651
621
652
@ Specialization
622
- static Object doNFIPointer (@ SuppressWarnings ("unused" ) MemoryBlock memory , NFIPointerStorage storage , int offset ) {
653
+ static Object doNFIPointer (Node inliningTarget , @ SuppressWarnings ("unused" ) MemoryBlock memory , NFIPointerStorage storage , int offset ) {
623
654
if (offset != 0 ) {
624
- throw CompilerDirectives . shouldNotReachHere ( "Invalid offset for a pointer" );
655
+ throw PRaiseNode . raiseUncached ( inliningTarget , NotImplementedError , ErrorMessages . CANNOT_APPLY_OFFSET_TO_AN_OBJECT_POINTER );
625
656
}
626
657
return storage .pointer ;
627
658
}
@@ -638,8 +669,8 @@ public final long execute(Node inliningTarget, Pointer ptr) {
638
669
639
670
@ Specialization
640
671
@ SuppressWarnings ("unused" )
641
- static long doNFIPointer (MemoryBlock memory , NFIPointerStorage storage , int offset ) {
642
- throw CompilerDirectives . shouldNotReachHere ( "Cannot convert Object pointer to native" );
672
+ static long doNFIPointer (Node inliningTarget , MemoryBlock memory , NFIPointerStorage storage , int offset ) {
673
+ throw PRaiseNode . raiseUncached ( inliningTarget , NotImplementedError , ErrorMessages . CANNOT_CONVERT_OBJECT_POINTER_TO_NATIVE );
643
674
}
644
675
}
645
676
0 commit comments