51
51
import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .PySequenceArrayWrapper ;
52
52
import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .PythonNativeWrapper ;
53
53
import com .oracle .graal .python .builtins .objects .cext .NativeWrappers .PythonObjectNativeWrapper ;
54
+ import com .oracle .graal .python .builtins .objects .cext .PythonObjectNativeWrapperMRFactory .PAsPointerNodeGen ;
54
55
import com .oracle .graal .python .builtins .objects .cext .PythonObjectNativeWrapperMRFactory .ReadNativeMemberNodeGen ;
55
56
import com .oracle .graal .python .builtins .objects .cext .PythonObjectNativeWrapperMRFactory .ToPyObjectNodeGen ;
56
57
import com .oracle .graal .python .builtins .objects .cext .PythonObjectNativeWrapperMRFactory .WriteNativeMemberNodeGen ;
76
77
import com .oracle .graal .python .nodes .truffle .PythonArithmeticTypes ;
77
78
import com .oracle .graal .python .runtime .interop .PythonMessageResolution ;
78
79
import com .oracle .graal .python .runtime .sequence .PSequence ;
80
+ import com .oracle .truffle .api .Assumption ;
79
81
import com .oracle .truffle .api .CompilerDirectives ;
80
82
import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
81
83
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
@@ -529,7 +531,7 @@ public Object access(Object object) {
529
531
530
532
@ Resolve (message = "TO_NATIVE" )
531
533
abstract static class ToNativeNode extends Node {
532
- @ Child private ToPyObjectNode toPyObjectNode = ToPyObjectNodeGen .create ();
534
+ @ Child private ToPyObjectNode toPyObjectNode = ToPyObjectNode .create ();
533
535
534
536
Object access (PythonNativeWrapper obj ) {
535
537
if (!obj .isNative ()) {
@@ -543,7 +545,7 @@ Object access(PythonNativeWrapper obj) {
543
545
abstract static class IsPointerNode extends Node {
544
546
@ Child private Node isPointerNode ;
545
547
546
- Object access (PythonNativeWrapper obj ) {
548
+ boolean access (PythonNativeWrapper obj ) {
547
549
return obj .isNative () && (!(obj .getNativePointer () instanceof TruffleObject ) || ForeignAccess .sendIsPointer (getIsPointerNode (), (TruffleObject ) obj .getNativePointer ()));
548
550
}
549
551
@@ -558,11 +560,26 @@ private Node getIsPointerNode() {
558
560
559
561
@ Resolve (message = "AS_POINTER" )
560
562
abstract static class AsPointerNode extends Node {
561
- @ Child private Node asPointerNode ;
563
+ @ Child private PAsPointerNode pAsPointerNode = PAsPointerNode . create () ;
562
564
563
565
long access (PythonNativeWrapper obj ) {
566
+ return pAsPointerNode .execute (obj );
567
+ }
568
+ }
569
+
570
+ abstract static class PAsPointerNode extends PBaseNode {
571
+ @ Child private Node asPointerNode ;
572
+
573
+ public abstract long execute (PythonNativeWrapper o );
574
+
575
+ @ Specialization (assumptions = "getSingleNativeContextAssumption()" )
576
+ long doFast (PythonNativeWrapper obj ) {
564
577
// the native pointer object must either be a TruffleObject or a primitive
565
578
Object nativePointer = obj .getNativePointer ();
579
+ return ensureLong (nativePointer );
580
+ }
581
+
582
+ private long ensureLong (Object nativePointer ) {
566
583
if (nativePointer instanceof TruffleObject ) {
567
584
if (asPointerNode == null ) {
568
585
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -575,8 +592,22 @@ long access(PythonNativeWrapper obj) {
575
592
}
576
593
}
577
594
return (long ) nativePointer ;
595
+ }
578
596
597
+ @ Specialization (replaces = "doFast" )
598
+ long doSlow (PythonNativeWrapper obj ,
599
+ @ Cached ("create()" ) ToPyObjectNode toPyObjectNode ) {
600
+ return ensureLong (toPyObjectNode .execute (obj ));
579
601
}
602
+
603
+ protected Assumption getSingleNativeContextAssumption () {
604
+ return getContext ().getSingleNativeContextAssumption ();
605
+ }
606
+
607
+ public static PAsPointerNode create () {
608
+ return PAsPointerNodeGen .create ();
609
+ }
610
+
580
611
}
581
612
582
613
abstract static class ToPyObjectNode extends TransformToNativeNode {
@@ -662,5 +693,9 @@ private CExtNodes.ToSulongNode getToSulongNode() {
662
693
}
663
694
return toSulongNode ;
664
695
}
696
+
697
+ public static ToPyObjectNode create () {
698
+ return ToPyObjectNodeGen .create ();
699
+ }
665
700
}
666
701
}
0 commit comments