@@ -179,24 +179,17 @@ STATIC mp_obj_t ffimod_close(mp_obj_t self_in) {
179179}
180180STATIC MP_DEFINE_CONST_FUN_OBJ_1 (ffimod_close_obj , ffimod_close );
181181
182- STATIC mp_obj_t ffimod_func (mp_uint_t n_args , const mp_obj_t * args ) {
183- (void )n_args ; // always 4
184- mp_obj_ffimod_t * self = args [0 ];
185- const char * rettype = mp_obj_str_get_str (args [1 ]);
186- const char * symname = mp_obj_str_get_str (args [2 ]);
182+ STATIC mp_obj_t make_func (mp_obj_t rettype_in , void * func , mp_obj_t argtypes_in ) {
183+ const char * rettype = mp_obj_str_get_str (rettype_in );
187184
188- void * sym = dlsym (self -> handle , symname );
189- if (sym == NULL ) {
190- nlr_raise (mp_obj_new_exception_arg1 (& mp_type_OSError , MP_OBJ_NEW_SMALL_INT (errno )));
191- }
192- mp_int_t nparams = MP_OBJ_SMALL_INT_VALUE (mp_obj_len_maybe (args [3 ]));
185+ mp_int_t nparams = MP_OBJ_SMALL_INT_VALUE (mp_obj_len_maybe (argtypes_in ));
193186 mp_obj_ffifunc_t * o = m_new_obj_var (mp_obj_ffifunc_t , ffi_type * , nparams );
194187 o -> base .type = & ffifunc_type ;
195188
196- o -> func = sym ;
189+ o -> func = func ;
197190 o -> rettype = * rettype ;
198191
199- mp_obj_t iterable = mp_getiter (args [ 3 ] );
192+ mp_obj_t iterable = mp_getiter (argtypes_in );
200193 mp_obj_t item ;
201194 int i = 0 ;
202195 while ((item = mp_iternext (iterable )) != MP_OBJ_STOP_ITERATION ) {
@@ -210,8 +203,26 @@ STATIC mp_obj_t ffimod_func(mp_uint_t n_args, const mp_obj_t *args) {
210203
211204 return o ;
212205}
206+
207+ STATIC mp_obj_t ffimod_func (mp_uint_t n_args , const mp_obj_t * args ) {
208+ (void )n_args ; // always 4
209+ mp_obj_ffimod_t * self = args [0 ];
210+ const char * symname = mp_obj_str_get_str (args [2 ]);
211+
212+ void * sym = dlsym (self -> handle , symname );
213+ if (sym == NULL ) {
214+ nlr_raise (mp_obj_new_exception_arg1 (& mp_type_OSError , MP_OBJ_NEW_SMALL_INT (errno )));
215+ }
216+ return make_func (args [1 ], sym , args [3 ]);
217+ }
213218MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (ffimod_func_obj , 4 , 4 , ffimod_func );
214219
220+ STATIC mp_obj_t mod_ffi_func (mp_obj_t rettype , mp_obj_t addr_in , mp_obj_t argtypes ) {
221+ void * addr = (void * )mp_obj_int_get_truncated (addr_in );
222+ return make_func (rettype , addr , argtypes );
223+ }
224+ MP_DEFINE_CONST_FUN_OBJ_3 (mod_ffi_func_obj , mod_ffi_func );
225+
215226STATIC void call_py_func (ffi_cif * cif , void * ret , void * * args , mp_obj_t func ) {
216227 mp_obj_t pyargs [cif -> nargs ];
217228 for (uint i = 0 ; i < cif -> nargs ; i ++ ) {
@@ -465,6 +476,7 @@ STATIC const mp_map_elem_t mp_module_ffi_globals_table[] = {
465476 { MP_OBJ_NEW_QSTR (MP_QSTR___name__ ), MP_OBJ_NEW_QSTR (MP_QSTR_ffi ) },
466477 { MP_OBJ_NEW_QSTR (MP_QSTR_open ), (mp_obj_t )& mod_ffi_open_obj },
467478 { MP_OBJ_NEW_QSTR (MP_QSTR_callback ), (mp_obj_t )& mod_ffi_callback_obj },
479+ { MP_OBJ_NEW_QSTR (MP_QSTR_func ), (mp_obj_t )& mod_ffi_func_obj },
468480 { MP_OBJ_NEW_QSTR (MP_QSTR_as_bytearray ), (mp_obj_t )& mod_ffi_as_bytearray_obj },
469481};
470482
0 commit comments