136
136
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
137
137
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
138
138
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryClinicBuiltinNode ;
139
+ import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
139
140
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
140
141
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryClinicBuiltinNode ;
141
142
import com .oracle .graal .python .nodes .function .builtins .clinic .ArgumentClinicProvider ;
148
149
import com .oracle .graal .python .util .PythonUtils ;
149
150
import com .oracle .truffle .api .CompilerAsserts ;
150
151
import com .oracle .truffle .api .CompilerDirectives ;
152
+ import com .oracle .truffle .api .CompilerDirectives .CompilationFinal ;
151
153
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
152
154
import com .oracle .truffle .api .TruffleLogger ;
153
155
import com .oracle .truffle .api .dsl .Bind ;
@@ -182,6 +184,9 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
182
184
return CtypesModuleBuiltinsFactory .getFactories ();
183
185
}
184
186
187
+ @ CompilationFinal private Object strlenFunction ;
188
+ @ CompilationFinal private Object memcpyFunction ;
189
+
185
190
private static final String NFI_LANGUAGE = "nfi" ;
186
191
187
192
protected static final int FUNCFLAG_STDCALL = 0x0 ;
@@ -224,17 +229,24 @@ public void postInitialize(Python3Core core) {
224
229
ctypesModule .setAttribute ("RTLD_GLOBAL" , RTLD_GLOBAL .getValueIfDefined ());
225
230
226
231
DLHandler handle = DlOpenNode .loadNFILibrary (core .getContext (), NFIBackend .NATIVE , "" , rtldLocal );
227
- setCtypeNFIHelpers (core .getContext (), handle );
232
+ setCtypeNFIHelpers (this , core .getContext (), handle );
228
233
NativeFunction memmove = MemMoveFunction .create (handle , core .getContext ());
229
234
ctypesModule .setAttribute ("_memmove_addr" , factory .createNativeVoidPtr (memmove , memmove .adr ));
230
235
NativeFunction memset = MemSetFunction .create (handle , core .getContext ());
231
236
ctypesModule .setAttribute ("_memset_addr" , factory .createNativeVoidPtr (memset , memset .adr ));
232
237
}
233
238
234
- private static void setCtypeNFIHelpers (PythonContext context , DLHandler h ) {
235
- Object strlen = createNFIHelperFunction (context , h , "strlen" , "(POINTER):UINT32" );
236
- Object memcpy = createNFIHelperFunction (context , h , "memcpy" , "([UINT8], POINTER, UINT32):POINTER" );
237
- context .setCtypesNFIHelpers (strlen , memcpy );
239
+ Object getStrlenFunction () {
240
+ return strlenFunction ;
241
+ }
242
+
243
+ Object getMemcpyFunction () {
244
+ return memcpyFunction ;
245
+ }
246
+
247
+ private static void setCtypeNFIHelpers (CtypesModuleBuiltins ctypesModuleBuiltins , PythonContext context , DLHandler h ) {
248
+ ctypesModuleBuiltins .strlenFunction = createNFIHelperFunction (context , h , "strlen" , "(POINTER):UINT32" );
249
+ ctypesModuleBuiltins .memcpyFunction = createNFIHelperFunction (context , h , "memcpy" , "([UINT8], POINTER, UINT32):POINTER" );
238
250
}
239
251
240
252
private static Object createNFIHelperFunction (PythonContext context , DLHandler h , String name , String signature ) {
@@ -879,12 +891,12 @@ protected static Object getObjectAt(PythonContext context, Object ptr) {
879
891
return ptr ;
880
892
}
881
893
882
- @ Builtin (name = "call_function" , minNumOfPositionalArgs = 1 )
894
+ @ Builtin (name = "call_function" , minNumOfPositionalArgs = 1 , declaresExplicitSelf = true )
883
895
@ GenerateNodeFactory
884
- protected abstract static class CallFunctionNode extends PythonBinaryBuiltinNode {
896
+ protected abstract static class CallFunctionNode extends PythonTernaryBuiltinNode {
885
897
886
898
@ Specialization
887
- Object call_function (VirtualFrame frame , Object f , PTuple arguments ,
899
+ Object call_function (VirtualFrame frame , PythonModule ctypesModule , Object f , PTuple arguments ,
888
900
@ Cached AuditNode auditNode ,
889
901
@ Cached CallProcNode callProcNode ,
890
902
@ Cached GetInternalObjectArrayNode getArray ,
@@ -902,7 +914,8 @@ Object call_function(VirtualFrame frame, Object f, PTuple arguments,
902
914
null ,
903
915
ctypes ,
904
916
factory (),
905
- getContext ());
917
+ getContext (),
918
+ (CtypesModuleBuiltins ) ctypesModule .getBuiltins ());
906
919
}
907
920
}
908
921
@@ -928,12 +941,12 @@ Object error(VirtualFrame frame, Object o) {
928
941
}
929
942
}
930
943
931
- @ Builtin (name = "call_cdeclfunction" , minNumOfPositionalArgs = 1 )
944
+ @ Builtin (name = "call_cdeclfunction" , minNumOfPositionalArgs = 1 , declaresExplicitSelf = true )
932
945
@ GenerateNodeFactory
933
- protected abstract static class CallCdeclfunctionNode extends PythonBinaryBuiltinNode {
946
+ protected abstract static class CallCdeclfunctionNode extends PythonTernaryBuiltinNode {
934
947
935
948
@ Specialization
936
- Object doit (VirtualFrame frame , Object f , PTuple arguments ,
949
+ Object doit (VirtualFrame frame , PythonModule ctypesModule , Object f , PTuple arguments ,
937
950
@ Cached AuditNode auditNode ,
938
951
@ Cached GetInternalObjectArrayNode getArray ,
939
952
@ Cached CallProcNode callProcNode ,
@@ -951,7 +964,8 @@ Object doit(VirtualFrame frame, Object f, PTuple arguments,
951
964
null ,
952
965
ctypes ,
953
966
factory (),
954
- getContext ());
967
+ getContext (),
968
+ (CtypesModuleBuiltins ) ctypesModule .getBuiltins ());
955
969
}
956
970
}
957
971
@@ -976,7 +990,8 @@ protected abstract static class CallProcNode extends PNodeWithRaise {
976
990
abstract Object execute (VirtualFrame frame , NativeFunction pProc , Object [] argtuple , int flags , Object [] argtypes , Object [] converters , Object restype , Object checker ,
977
991
CtypesThreadState state ,
978
992
PythonObjectFactory factory ,
979
- PythonContext context );
993
+ PythonContext context ,
994
+ CtypesModuleBuiltins ctypesModuleBuiltins );
980
995
981
996
/*
982
997
* bpo-13097: Max number of arguments _ctypes_callproc will accept.
@@ -997,6 +1012,7 @@ Object _ctypes_callproc(VirtualFrame frame,
997
1012
@ SuppressWarnings ("unused" ) CtypesThreadState state ,
998
1013
PythonObjectFactory factory ,
999
1014
PythonContext context ,
1015
+ CtypesModuleBuiltins ctypesModuleBuiltins ,
1000
1016
@ Cached ConvParamNode convParamNode ,
1001
1017
@ Cached PyTypeStgDictNode pyTypeStgDictNode ,
1002
1018
@ Cached CallNode callNode ,
@@ -1078,7 +1094,7 @@ Object _ctypes_callproc(VirtualFrame frame,
1078
1094
throw CompilerDirectives .shouldNotReachHere (e );
1079
1095
}
1080
1096
} else if (!isLLVM && ilib .isPointer (result )) {
1081
- result = getNativeBytes (getContext (), result , getRaiseNode ());
1097
+ result = getNativeBytes (ctypesModuleBuiltins , getContext (), result , getRaiseNode ());
1082
1098
} else if (ilib .isNumber (result )) {
1083
1099
byte [] bytes = new byte [rtype .size ];
1084
1100
CtypesNodes .setValue (rtype .type , bytes , 0 , result );
@@ -1139,6 +1155,7 @@ Object doManaged(NativeFunction pProc,
1139
1155
@ SuppressWarnings ("unused" ) CtypesThreadState state ,
1140
1156
@ SuppressWarnings ("unused" ) PythonObjectFactory factory ,
1141
1157
@ SuppressWarnings ("unused" ) PythonContext context ,
1158
+ @ SuppressWarnings ("unused" ) CtypesModuleBuiltins ctypesModuleBuiltins ,
1142
1159
@ CachedLibrary (limit = "1" ) InteropLibrary ilib ) {
1143
1160
return callManagedFunction (pProc , argarray , ilib );
1144
1161
}
@@ -1161,11 +1178,11 @@ protected static Object getFunction(NativeFunction pProc, String signature, Pyth
1161
1178
}
1162
1179
1163
1180
@ TruffleBoundary
1164
- private static byte [] getNativeBytes (PythonContext context , Object pointer , PRaiseNode raiseNode ) {
1181
+ private static byte [] getNativeBytes (CtypesModuleBuiltins ctypesModuleBuiltins , PythonContext context , Object pointer , PRaiseNode raiseNode ) {
1165
1182
try {
1166
- long size = (Long ) InteropLibrary .getUncached ().execute (context .getStrlenFunction (), pointer );
1183
+ long size = (Long ) InteropLibrary .getUncached ().execute (ctypesModuleBuiltins .getStrlenFunction (), pointer );
1167
1184
byte [] bytes = new byte [(int ) size ];
1168
- InteropLibrary .getUncached ().execute (context .getMemcpyFunction (), context .getEnv ().asGuestValue (bytes ), pointer , size );
1185
+ InteropLibrary .getUncached ().execute (ctypesModuleBuiltins .getMemcpyFunction (), context .getEnv ().asGuestValue (bytes ), pointer , size );
1169
1186
return bytes ;
1170
1187
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e ) {
1171
1188
throw raiseNode .raise (SystemError , e );
0 commit comments