@@ -84,6 +84,27 @@ extern void *Py_NoValue;
84
84
extern init_upcall upcalls [];
85
85
extern unsigned init_upcall_n ;
86
86
87
+ /* upcall helpers */
88
+ MUST_INLINE
89
+ PyObject * polyglot_ensure_ptr (void * obj ) {
90
+ return polyglot_fits_in_i64 (obj ) ? (PyObject * ) polyglot_as_i64 (obj ) : (PyObject * ) obj ;
91
+ }
92
+
93
+ MUST_INLINE
94
+ int32_t polyglot_ensure_i32 (void * obj ) {
95
+ return polyglot_fits_in_i32 (obj ) ? polyglot_as_i32 (obj ) : (int32_t ) obj ;
96
+ }
97
+
98
+ MUST_INLINE
99
+ int64_t polyglot_ensure_i64 (void * obj ) {
100
+ return polyglot_fits_in_i64 (obj ) ? polyglot_as_i64 (obj ) : (int64_t ) obj ;
101
+ }
102
+
103
+ MUST_INLINE
104
+ double polyglot_ensure_double (void * obj ) {
105
+ return polyglot_fits_in_double (obj ) ? polyglot_as_double (obj ) : (double ) ((int64_t )obj );
106
+ }
107
+
87
108
/* upcall functions for calling into Python */
88
109
extern PyObject * (* PY_TRUFFLE_LANDING )(void * rcv , void * name , ...);
89
110
extern uint64_t (* PY_TRUFFLE_LANDING_L )(void * rcv , void * name , ...);
@@ -101,16 +122,16 @@ extern void* (*PY_TRUFFLE_CEXT_LANDING_PTR)(void* name, ...);
101
122
#define UPCALL_P (__recv__ , __name__ , ...) (PY_TRUFFLE_LANDING_L((__recv__), __name__, ##__VA_ARGS__))
102
123
103
124
/* Call function with return type 'int'; no polyglot cast but error handling */
104
- #define UPCALL_I (__recv__ , __name__ , ...) UPCALL_P(__recv__, __name__, ##__VA_ARGS__)
125
+ #define UPCALL_I (__recv__ , __name__ , ...) (polyglot_ensure_i32( UPCALL_P(__recv__, __name__, ##__VA_ARGS__)) )
105
126
106
127
/* Call function with return type 'long'; no polyglot cast but error handling */
107
- #define UPCALL_L (__recv__ , __name__ , ...) UPCALL_P(__recv__, __name__, ##__VA_ARGS__)
128
+ #define UPCALL_L (__recv__ , __name__ , ...) (polyglot_ensure_i64( UPCALL_P(__recv__, __name__, ##__VA_ARGS__)) )
108
129
109
130
/* Call function with return type 'double'; no polyglot cast but error handling */
110
- #define UPCALL_D (__recv__ , __name__ , ...) PY_TRUFFLE_LANDING_D((__recv__), __name__, ##__VA_ARGS__)
131
+ #define UPCALL_D (__recv__ , __name__ , ...) (polyglot_ensure_double( PY_TRUFFLE_LANDING_D((__recv__), __name__, ##__VA_ARGS__)) )
111
132
112
133
/* Call function with return type 'void*'; no polyglot cast and no error handling */
113
- #define UPCALL_PTR (__name__ , ...) (PY_TRUFFLE_LANDING_PTR(__name__, ##__VA_ARGS__))
134
+ #define UPCALL_PTR (__name__ , ...) (polyglot_ensure_ptr( PY_TRUFFLE_LANDING_PTR(__name__, ##__VA_ARGS__) ))
114
135
115
136
/* Call function of 'python_cext' module with return type 'PyObject *'; does polyglot cast and error handling */
116
137
#define UPCALL_CEXT_O (__name__ , ...) PY_TRUFFLE_CEXT_LANDING(__name__, ##__VA_ARGS__)
@@ -122,19 +143,19 @@ extern void* (*PY_TRUFFLE_CEXT_LANDING_PTR)(void* name, ...);
122
143
#define UPCALL_CEXT_NOCAST (__name__ , ...) PY_TRUFFLE_CEXT_LANDING(__name__, ##__VA_ARGS__)
123
144
124
145
/* Call function of 'python_cext' module with return type 'void*'; no polyglot cast and no error handling */
125
- #define UPCALL_CEXT_PTR (__name__ , ...) (PY_TRUFFLE_CEXT_LANDING_PTR(__name__, ##__VA_ARGS__))
146
+ #define UPCALL_CEXT_PTR (__name__ , ...) (polyglot_ensure_ptr( PY_TRUFFLE_CEXT_LANDING_PTR(__name__, ##__VA_ARGS__) ))
126
147
127
148
/* Call function of 'python_cext' module with a primitive return; no polyglot cast but error handling */
128
149
#define UPCALL_CEXT_P (__name__ , ...) (PY_TRUFFLE_CEXT_LANDING_L(__name__, ##__VA_ARGS__))
129
150
130
151
/* Call function of 'python_cext' module with return type 'int'; no polyglot cast but error handling */
131
- #define UPCALL_CEXT_I (__name__ , ...) UPCALL_CEXT_P(__name__, ##__VA_ARGS__)
152
+ #define UPCALL_CEXT_I (__name__ , ...) (polyglot_ensure_i32( UPCALL_CEXT_P(__name__, ##__VA_ARGS__)) )
132
153
133
154
/* Call function of 'python_cext' module with return type 'long'; no polyglot cast but error handling */
134
- #define UPCALL_CEXT_L (__name__ , ...) UPCALL_CEXT_P(__name__, ##__VA_ARGS__)
155
+ #define UPCALL_CEXT_L (__name__ , ...) (polyglot_ensure_i64( UPCALL_CEXT_P(__name__, ##__VA_ARGS__)) )
135
156
136
157
/* Call function of 'python_cext' module with return type 'double'; no polyglot cast but error handling */
137
- #define UPCALL_CEXT_D (__name__ , ...) (PY_TRUFFLE_CEXT_LANDING_D(__name__, ##__VA_ARGS__))
158
+ #define UPCALL_CEXT_D (__name__ , ...) (polyglot_ensure_double( PY_TRUFFLE_CEXT_LANDING_D(__name__, ##__VA_ARGS__) ))
138
159
139
160
#define UPCALL_ID (name ) \
140
161
static void* _jls_ ## name; \
0 commit comments