4343import static com .oracle .graal .python .builtins .modules .cext .PythonCextBuiltins .checkThrowableBeforeNative ;
4444import static com .oracle .graal .python .util .PythonUtils .EMPTY_OBJECT_ARRAY ;
4545
46+ import com .oracle .graal .python .PythonLanguage ;
4647import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
4748import com .oracle .graal .python .builtins .objects .PNone ;
4849import com .oracle .graal .python .builtins .objects .cext .capi .PythonNativeWrapper .PythonStructNativeWrapper ;
@@ -136,13 +137,30 @@ protected Object execute(Object[] arguments) throws UnsupportedTypeException, Ar
136137 }
137138
138139 @ ExportMessage
139- protected boolean isPointer () {
140- return isNative ();
140+ protected boolean isPointer (
141+ @ Bind Node inliningTarget ) {
142+ if (PythonLanguage .get (inliningTarget ).isSingleContext ()) {
143+ return isNative ();
144+ }
145+ return getClosurePointerMultiContext () != -1 ;
141146 }
142147
143148 @ ExportMessage
144- protected long asPointer () {
145- return getNativePointer ();
149+ protected long asPointer (
150+ @ Bind Node inliningTarget ) throws UnsupportedMessageException {
151+ if (PythonLanguage .get (inliningTarget ).isSingleContext ()) {
152+ return getNativePointer ();
153+ }
154+ long pointer = getClosurePointerMultiContext ();
155+ if (pointer == -1 ) {
156+ throw UnsupportedMessageException .create ();
157+ }
158+ return pointer ;
159+ }
160+
161+ @ TruffleBoundary
162+ private long getClosurePointerMultiContext () {
163+ return PythonContext .get (null ).getCApiContext ().getClosurePointer (this );
146164 }
147165
148166 protected abstract String getSignature ();
@@ -151,9 +169,12 @@ protected long asPointer() {
151169 @ TruffleBoundary
152170 protected void toNative (
153171 @ CachedLibrary (limit = "1" ) SignatureLibrary signatureLibrary ) {
154- if (!isPointer ()) {
172+ if (!isPointer (null )) {
155173 CApiContext cApiContext = PythonContext .get (null ).getCApiContext ();
156- setNativePointer (cApiContext .registerClosure (getSignature (), this , getDelegate (), signatureLibrary ));
174+ long pointer = cApiContext .registerClosure (getSignature (), this , getDelegate (), signatureLibrary );
175+ if (PythonLanguage .get (null ).isSingleContext ()) {
176+ setNativePointer (pointer );
177+ }
157178 }
158179 }
159180
0 commit comments