|
74 | 74 | import static com.oracle.graal.python.nodes.SpecialMethodNames.J___CALL__;
|
75 | 75 | import static com.oracle.graal.python.nodes.SpecialMethodNames.J___NEW__;
|
76 | 76 | import static com.oracle.graal.python.nodes.SpecialMethodNames.J___REPR__;
|
| 77 | +import static com.oracle.graal.python.nodes.StringLiterals.J_NFI_LANGUAGE; |
77 | 78 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.AttributeError;
|
78 | 79 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
|
79 | 80 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError;
|
|
122 | 123 | import com.oracle.graal.python.nodes.object.GetClassNode;
|
123 | 124 | import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
|
124 | 125 | import com.oracle.graal.python.nodes.util.CastToTruffleStringNode;
|
| 126 | +import com.oracle.graal.python.runtime.PythonContext; |
125 | 127 | import com.oracle.graal.python.runtime.exception.PException;
|
| 128 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
126 | 129 | import com.oracle.truffle.api.dsl.Bind;
|
127 | 130 | import com.oracle.truffle.api.dsl.Cached;
|
128 | 131 | import com.oracle.truffle.api.dsl.Cached.Shared;
|
|
133 | 136 | import com.oracle.truffle.api.dsl.Specialization;
|
134 | 137 | import com.oracle.truffle.api.frame.VirtualFrame;
|
135 | 138 | import com.oracle.truffle.api.nodes.Node;
|
| 139 | +import com.oracle.truffle.api.source.Source; |
136 | 140 | import com.oracle.truffle.api.strings.TruffleString;
|
| 141 | +import com.oracle.truffle.nfi.api.SignatureLibrary; |
137 | 142 |
|
138 | 143 | @CoreFunctions(extendClasses = PythonBuiltinClassType.PyCFuncPtr)
|
139 | 144 | public class PyCFuncPtrBuiltins extends PythonBuiltins {
|
@@ -262,46 +267,39 @@ FFIType _ctypes_get_ffi_type(Object obj,
|
262 | 267 | return dict.ffi_type_pointer;
|
263 | 268 | }
|
264 | 269 |
|
| 270 | + @TruffleBoundary |
265 | 271 | CThunkObject _ctypes_alloc_callback(Object callable, Object[] converters, Object restype, int flags,
|
266 | 272 | PyTypeStgDictNode pyTypeStgDictNode) {
|
267 | 273 | int nArgs = converters.length;
|
268 |
| - CThunkObject p = CThunkObjectNew(nArgs); |
| 274 | + CThunkObject thunk = CThunkObjectNew(nArgs); |
269 | 275 |
|
270 |
| - p.flags = flags; |
| 276 | + thunk.flags = flags; |
271 | 277 | int i;
|
272 | 278 | for (i = 0; i < nArgs; ++i) {
|
273 | 279 | Object cnv = converters[i];
|
274 |
| - p.atypes[i] = _ctypes_get_ffi_type(cnv, pyTypeStgDictNode); |
| 280 | + thunk.atypes[i] = _ctypes_get_ffi_type(cnv, pyTypeStgDictNode); |
275 | 281 | }
|
276 | 282 |
|
277 |
| - p.restype = restype; |
| 283 | + thunk.restype = restype; |
278 | 284 | if (restype == null || restype == PNone.NONE) {
|
279 |
| - p.setfunc = FieldSet.nil; |
280 |
| - p.ffi_restype = new FFIType(); |
| 285 | + thunk.setfunc = FieldSet.nil; |
| 286 | + thunk.ffi_restype = new FFIType(); |
281 | 287 | } else {
|
282 | 288 | StgDictObject dict = pyTypeStgDictNode.execute(restype);
|
283 | 289 | if (dict == null || dict.setfunc == FieldSet.nil) {
|
284 | 290 | throw raise(TypeError, INVALID_RESULT_TYPE_FOR_CALLBACK_FUNCTION);
|
285 | 291 | }
|
286 |
| - p.setfunc = dict.setfunc; |
287 |
| - p.ffi_restype = dict.ffi_type_pointer; |
288 |
| - } |
289 |
| - /*- |
290 |
| - p.pcl_write = ffi_closure_alloc(sizeof(ffi_closure), p.pcl_exec); |
291 |
| - ffi_abi cc = FFI_DEFAULT_ABI; |
292 |
| - int result = ffi_prep_cif(p.cif, cc, nArgs, _ctypes_get_ffi_type(restype, pyTypeStgDictNode), p.atypes); |
293 |
| - if (result != FFI_OK) { |
294 |
| - throw raise(RuntimeError, FFI_PREP_CIF_FAILED_WITH_D, result); |
295 |
| - } |
296 |
| - result = ffi_prep_closure_loc(p.pcl_write, p.cif, closure_fcn, p, p.pcl_exec); |
297 |
| - if (result != FFI_OK) { |
298 |
| - throw raise(RuntimeError, FFI_PREP_CLOSURE_FAILED_WITH_D, result); |
299 |
| - } |
300 |
| - */ |
301 |
| - p.createCallback(); |
302 |
| - p.converters = converters; |
303 |
| - p.callable = callable; |
304 |
| - return p; |
| 292 | + thunk.setfunc = dict.setfunc; |
| 293 | + thunk.ffi_restype = dict.ffi_type_pointer; |
| 294 | + } |
| 295 | + TruffleString signatureStr = FFIType.buildNFISignature(thunk.atypes, thunk.ffi_restype, true); |
| 296 | + Source source = Source.newBuilder(J_NFI_LANGUAGE, signatureStr.toJavaStringUncached(), "<ctypes callback>").build(); |
| 297 | + Object nfiSignature = PythonContext.get(this).getEnv().parseInternal(source).call(); |
| 298 | + thunk.pcl_exec = SignatureLibrary.getUncached().createClosure(nfiSignature, new CThunkObject.CtypeCallback(thunk)); |
| 299 | + thunk.pcl_write = thunk.pcl_exec; |
| 300 | + thunk.converters = converters; |
| 301 | + thunk.callable = callable; |
| 302 | + return thunk; |
305 | 303 | }
|
306 | 304 | }
|
307 | 305 |
|
|
0 commit comments