38
38
*/
39
39
#include "capi.h"
40
40
41
- #define FORCE_TO_NATIVE (__obj__ ) (polyglot_invoke(PY_TRUFFLE_CEXT, "PyTruffle_Set_Ptr", (__obj__), truffle_is_handle_to_managed((__obj__)) ? (__obj__) : truffle_deref_handle_for_managed(__obj__)))
41
+
42
+ MUST_INLINE static void force_to_native (void * obj ) {
43
+ if (polyglot_is_value (obj )) {
44
+ void * handle = truffle_deref_handle_for_managed (obj );
45
+ if (!polyglot_invoke (PY_TRUFFLE_CEXT , "PyTruffle_Set_Ptr" , obj , handle )) {
46
+ truffle_release_handle (handle );
47
+ }
48
+ }
49
+ }
42
50
43
51
static void initialize_type_structure (PyTypeObject * structure , const char * typname ) {
44
52
// explicit type cast is required because the type flags are not yet initialized !
45
53
PyTypeObject * ptype = polyglot_as__typeobject (UPCALL_CEXT_O ("PyTruffle_Type" , polyglot_from_string (typname , SRC_CS )));
46
54
47
55
// We eagerly create a native pointer for all builtin types. This is necessary for pointer comparisons to work correctly.
48
56
// TODO Remove this as soon as this is properly supported.
49
- FORCE_TO_NATIVE (ptype );
57
+ force_to_native (ptype );
50
58
51
59
unsigned long original_flags = structure -> tp_flags ;
52
60
Py_ssize_t basicsize = structure -> tp_basicsize ;
@@ -59,30 +67,30 @@ static void initialize_type_structure(PyTypeObject* structure, const char* typna
59
67
static void initialize_globals () {
60
68
// None
61
69
PyObject * jnone = UPCALL_CEXT_O ("Py_None" );
62
- FORCE_TO_NATIVE (jnone );
70
+ force_to_native (jnone );
63
71
truffle_assign_managed (& _Py_NoneStruct , jnone );
64
72
65
73
// NotImplemented
66
74
void * jnotimpl = UPCALL_CEXT_O ("Py_NotImplemented" );
67
- FORCE_TO_NATIVE (jnotimpl );
75
+ force_to_native (jnotimpl );
68
76
truffle_assign_managed (& _Py_NotImplementedStruct , jnotimpl );
69
77
70
78
// Ellipsis
71
79
void * jellipsis = UPCALL_CEXT_O ("Py_Ellipsis" );
72
- FORCE_TO_NATIVE (jellipsis );
80
+ force_to_native (jellipsis );
73
81
truffle_assign_managed (& _Py_EllipsisObject , jellipsis );
74
82
75
83
// True, False
76
84
void * jtrue = UPCALL_CEXT_O ("Py_True" );
77
- FORCE_TO_NATIVE (jtrue );
85
+ force_to_native (jtrue );
78
86
truffle_assign_managed (& _Py_TrueStruct , polyglot_as__longobject (jtrue ));
79
87
void * jfalse = UPCALL_CEXT_O ("Py_False" );
80
- FORCE_TO_NATIVE (jfalse );
88
+ force_to_native (jfalse );
81
89
truffle_assign_managed (& _Py_FalseStruct , polyglot_as__longobject (jfalse ));
82
90
83
91
// error marker
84
92
void * jerrormarker = UPCALL_CEXT_O ("Py_ErrorHandler" );
85
- FORCE_TO_NATIVE (jerrormarker );
93
+ force_to_native (jerrormarker );
86
94
truffle_assign_managed (& marker_struct , jerrormarker );
87
95
}
88
96
@@ -205,7 +213,7 @@ PyObject* to_sulong(void *o) {
205
213
206
214
/** to be used from Java code only; reads native 'ob_type' field */
207
215
void * get_ob_type (PyObject * obj ) {
208
- return native_to_java (obj -> ob_type );
216
+ return native_to_java (( PyObject * ) obj -> ob_type );
209
217
}
210
218
211
219
typedef struct PyObjectHandle {
0 commit comments