@@ -84,6 +84,27 @@ extern void *Py_NoValue;
8484extern init_upcall upcalls [];
8585extern unsigned init_upcall_n ;
8686
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+
87108/* upcall functions for calling into Python */
88109extern PyObject * (* PY_TRUFFLE_LANDING )(void * rcv , void * name , ...);
89110extern uint64_t (* PY_TRUFFLE_LANDING_L )(void * rcv , void * name , ...);
@@ -101,16 +122,16 @@ extern void* (*PY_TRUFFLE_CEXT_LANDING_PTR)(void* name, ...);
101122#define UPCALL_P (__recv__ , __name__ , ...) (PY_TRUFFLE_LANDING_L((__recv__), __name__, ##__VA_ARGS__))
102123
103124/* 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__)) )
105126
106127/* 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__)) )
108129
109130/* 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__)) )
111132
112133/* 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__) ))
114135
115136/* Call function of 'python_cext' module with return type 'PyObject *'; does polyglot cast and error handling */
116137#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, ...);
122143#define UPCALL_CEXT_NOCAST (__name__ , ...) PY_TRUFFLE_CEXT_LANDING(__name__, ##__VA_ARGS__)
123144
124145/* 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__) ))
126147
127148/* Call function of 'python_cext' module with a primitive return; no polyglot cast but error handling */
128149#define UPCALL_CEXT_P (__name__ , ...) (PY_TRUFFLE_CEXT_LANDING_L(__name__, ##__VA_ARGS__))
129150
130151/* 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__)) )
132153
133154/* 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__)) )
135156
136157/* 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__) ))
138159
139160#define UPCALL_ID (name ) \
140161 static void* _jls_ ## name; \
0 commit comments