|
| 1 | +#include <assert.h> |
| 2 | +#include <string.h> |
| 3 | +#include <errno.h> |
| 4 | +#include <dlfcn.h> |
| 5 | +#include <ffi.h> |
| 6 | + |
| 7 | +#include "nlr.h" |
| 8 | +#include "misc.h" |
| 9 | +#include "mpconfig.h" |
| 10 | +#include "qstr.h" |
| 11 | +#include "obj.h" |
| 12 | +#include "runtime.h" |
| 13 | + |
| 14 | +typedef struct _mp_obj_opaque_t { |
| 15 | + mp_obj_base_t base; |
| 16 | + void *val; |
| 17 | +} mp_obj_opaque_t; |
| 18 | + |
| 19 | +typedef struct _mp_obj_ffimod_t { |
| 20 | + mp_obj_base_t base; |
| 21 | + void *handle; |
| 22 | +} mp_obj_ffimod_t; |
| 23 | + |
| 24 | +typedef struct _mp_obj_ffivar_t { |
| 25 | + mp_obj_base_t base; |
| 26 | + void *var; |
| 27 | + char type; |
| 28 | +// ffi_type *type; |
| 29 | +} mp_obj_ffivar_t; |
| 30 | + |
| 31 | +typedef struct _mp_obj_ffifunc_t { |
| 32 | + mp_obj_base_t base; |
| 33 | + void *func; |
| 34 | + char rettype; |
| 35 | + ffi_cif cif; |
| 36 | + ffi_type *params[]; |
| 37 | +} mp_obj_ffifunc_t; |
| 38 | + |
| 39 | +typedef struct _mp_obj_fficallback_t { |
| 40 | + mp_obj_base_t base; |
| 41 | + void *func; |
| 42 | + ffi_closure *clo; |
| 43 | + char rettype; |
| 44 | + ffi_cif cif; |
| 45 | + ffi_type *params[]; |
| 46 | +} mp_obj_fficallback_t; |
| 47 | + |
| 48 | +static const mp_obj_type_t opaque_type; |
| 49 | +static const mp_obj_type_t ffimod_type; |
| 50 | +static const mp_obj_type_t ffifunc_type; |
| 51 | +static const mp_obj_type_t fficallback_type; |
| 52 | +static const mp_obj_type_t ffivar_type; |
| 53 | + |
| 54 | +static ffi_type *char2ffi_type(char c) |
| 55 | +{ |
| 56 | + switch (c) { |
| 57 | + case 'b': return &ffi_type_schar; |
| 58 | + case 'B': return &ffi_type_uchar; |
| 59 | + case 'i': return &ffi_type_sint; |
| 60 | + case 'I': return &ffi_type_uint; |
| 61 | + case 'l': return &ffi_type_slong; |
| 62 | + case 'L': return &ffi_type_ulong; |
| 63 | + case 'p': |
| 64 | + case 's': return &ffi_type_pointer; |
| 65 | + case 'v': return &ffi_type_void; |
| 66 | + default: return NULL; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +static ffi_type *get_ffi_type(mp_obj_t o_in) |
| 71 | +{ |
| 72 | + if (MP_OBJ_IS_STR(o_in)) { |
| 73 | + uint len; |
| 74 | + const byte *s = mp_obj_str_get_data(o_in, &len); |
| 75 | + ffi_type *t = char2ffi_type(*s); |
| 76 | + if (t != NULL) { |
| 77 | + return t; |
| 78 | + } |
| 79 | + } |
| 80 | + // TODO: Support actual libffi type objects |
| 81 | + |
| 82 | + nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "Unknown type")); |
| 83 | +} |
| 84 | + |
| 85 | +static mp_obj_t return_ffi_value(int val, char type) |
| 86 | +{ |
| 87 | + switch (type) { |
| 88 | + case 's': { |
| 89 | + const char *s = (const char *)val; |
| 90 | + return mp_obj_new_str((const byte *)s, strlen(s), false); |
| 91 | + } |
| 92 | + case 'v': |
| 93 | + return mp_const_none; |
| 94 | + default: |
| 95 | + return mp_obj_new_int(val); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// FFI module |
| 100 | + |
| 101 | +static void ffimod_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
| 102 | + mp_obj_ffimod_t *self = self_in; |
| 103 | + print(env, "<ffimod %p>", self->handle); |
| 104 | +} |
| 105 | + |
| 106 | +static mp_obj_t ffimod_close(mp_obj_t self_in) { |
| 107 | + mp_obj_ffimod_t *self = self_in; |
| 108 | + dlclose(self->handle); |
| 109 | + return mp_const_none; |
| 110 | +} |
| 111 | +static MP_DEFINE_CONST_FUN_OBJ_1(ffimod_close_obj, ffimod_close); |
| 112 | + |
| 113 | +static mp_obj_t ffimod_func(uint n_args, const mp_obj_t *args) { |
| 114 | + mp_obj_ffimod_t *self = args[0]; |
| 115 | + const char *rettype = mp_obj_str_get_str(args[1]); |
| 116 | + const char *symname = mp_obj_str_get_str(args[2]); |
| 117 | + |
| 118 | + void *sym = dlsym(self->handle, symname); |
| 119 | + if (sym == NULL) { |
| 120 | + nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", errno)); |
| 121 | + } |
| 122 | + int nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(args[3])); |
| 123 | + mp_obj_ffifunc_t *o = m_new_obj_var(mp_obj_ffifunc_t, ffi_type*, nparams); |
| 124 | + o->base.type = &ffifunc_type; |
| 125 | + |
| 126 | + o->func = sym; |
| 127 | + o->rettype = *rettype; |
| 128 | + |
| 129 | + mp_obj_t iterable = rt_getiter(args[3]); |
| 130 | + mp_obj_t item; |
| 131 | + int i = 0; |
| 132 | + while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { |
| 133 | + o->params[i++] = get_ffi_type(item); |
| 134 | + } |
| 135 | + |
| 136 | + int res = ffi_prep_cif(&o->cif, FFI_DEFAULT_ABI, nparams, char2ffi_type(*rettype), o->params); |
| 137 | + if (res != FFI_OK) { |
| 138 | + nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "Error in ffi_prep_cif")); |
| 139 | + } |
| 140 | + |
| 141 | + return o; |
| 142 | +} |
| 143 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ffimod_func_obj, 4, 4, ffimod_func); |
| 144 | + |
| 145 | +static void call_py_func(ffi_cif *cif, void *ret, void** args, mp_obj_t func) { |
| 146 | + mp_obj_t pyargs[cif->nargs]; |
| 147 | + for (int i = 0; i < cif->nargs; i++) { |
| 148 | + pyargs[i] = mp_obj_new_int(*(int*)args[i]); |
| 149 | + } |
| 150 | + mp_obj_t res = rt_call_function_n_kw(func, cif->nargs, 0, pyargs); |
| 151 | + |
| 152 | + *(ffi_arg*)ret = mp_obj_int_get(res); |
| 153 | +} |
| 154 | + |
| 155 | +static mp_obj_t mod_ffi_callback(mp_obj_t rettype_in, mp_obj_t func_in, mp_obj_t paramtypes_in) { |
| 156 | + const char *rettype = mp_obj_str_get_str(rettype_in); |
| 157 | + |
| 158 | + int nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(paramtypes_in)); |
| 159 | + mp_obj_fficallback_t *o = m_new_obj_var(mp_obj_fficallback_t, ffi_type*, nparams); |
| 160 | + o->base.type = &fficallback_type; |
| 161 | + |
| 162 | + o->clo = ffi_closure_alloc(sizeof(ffi_closure), &o->func); |
| 163 | + |
| 164 | + o->rettype = *rettype; |
| 165 | + |
| 166 | + mp_obj_t iterable = rt_getiter(paramtypes_in); |
| 167 | + mp_obj_t item; |
| 168 | + int i = 0; |
| 169 | + while ((item = rt_iternext(iterable)) != mp_const_stop_iteration) { |
| 170 | + o->params[i++] = get_ffi_type(item); |
| 171 | + } |
| 172 | + |
| 173 | + int res = ffi_prep_cif(&o->cif, FFI_DEFAULT_ABI, nparams, char2ffi_type(*rettype), o->params); |
| 174 | + if (res != FFI_OK) { |
| 175 | + nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "Error in ffi_prep_cif")); |
| 176 | + } |
| 177 | + |
| 178 | + res = ffi_prep_closure_loc(o->clo, &o->cif, call_py_func, func_in, o->func); |
| 179 | + if (res != FFI_OK) { |
| 180 | + nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "ffi_prep_closure_loc")); |
| 181 | + } |
| 182 | + |
| 183 | + return o; |
| 184 | +} |
| 185 | +MP_DEFINE_CONST_FUN_OBJ_3(mod_ffi_callback_obj, mod_ffi_callback); |
| 186 | + |
| 187 | +static mp_obj_t ffimod_var(mp_obj_t self_in, mp_obj_t vartype_in, mp_obj_t symname_in) { |
| 188 | + mp_obj_ffimod_t *self = self_in; |
| 189 | + const char *rettype = mp_obj_str_get_str(vartype_in); |
| 190 | + const char *symname = mp_obj_str_get_str(symname_in); |
| 191 | + |
| 192 | + void *sym = dlsym(self->handle, symname); |
| 193 | + if (sym == NULL) { |
| 194 | + nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", errno)); |
| 195 | + } |
| 196 | + mp_obj_ffivar_t *o = m_new_obj(mp_obj_ffivar_t); |
| 197 | + o->base.type = &ffivar_type; |
| 198 | + |
| 199 | + o->var = sym; |
| 200 | + o->type = *rettype; |
| 201 | + return o; |
| 202 | +} |
| 203 | +MP_DEFINE_CONST_FUN_OBJ_3(ffimod_var_obj, ffimod_var); |
| 204 | + |
| 205 | +static mp_obj_t ffimod_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
| 206 | + const char *fname = mp_obj_str_get_str(args[0]); |
| 207 | + void *mod = dlopen(fname, RTLD_NOW | RTLD_LOCAL); |
| 208 | + |
| 209 | + if (mod == NULL) { |
| 210 | + nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_OSError, "[Errno %d]", errno)); |
| 211 | + } |
| 212 | + mp_obj_ffimod_t *o = m_new_obj(mp_obj_ffimod_t); |
| 213 | + o->base.type = type_in; |
| 214 | + o->handle = mod; |
| 215 | + return o; |
| 216 | +} |
| 217 | + |
| 218 | +static const mp_method_t ffimod_type_methods[] = { |
| 219 | + { "func", &ffimod_func_obj }, |
| 220 | + { "var", &ffimod_var_obj }, |
| 221 | + { "close", &ffimod_close_obj }, |
| 222 | + { NULL, NULL }, |
| 223 | +}; |
| 224 | + |
| 225 | +static const mp_obj_type_t ffimod_type = { |
| 226 | + { &mp_const_type }, |
| 227 | + "ffimod", |
| 228 | + .print = ffimod_print, |
| 229 | + .make_new = ffimod_make_new, |
| 230 | + .methods = ffimod_type_methods, |
| 231 | +}; |
| 232 | + |
| 233 | +// FFI function |
| 234 | + |
| 235 | +static void ffifunc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
| 236 | + mp_obj_ffifunc_t *self = self_in; |
| 237 | + print(env, "<ffifunc %p>", self->func); |
| 238 | +} |
| 239 | + |
| 240 | +mp_obj_t ffifunc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
| 241 | + mp_obj_ffifunc_t *self = self_in; |
| 242 | + assert(n_kw == 0); |
| 243 | + assert(n_args == self->cif.nargs); |
| 244 | + |
| 245 | + int values[n_args]; |
| 246 | + void *valueptrs[n_args]; |
| 247 | + int i; |
| 248 | + for (i = 0; i < n_args; i++) { |
| 249 | + mp_obj_t a = args[i]; |
| 250 | + if (a == mp_const_none) { |
| 251 | + values[i] = 0; |
| 252 | + } else if (MP_OBJ_IS_INT(a)) { |
| 253 | + values[i] = mp_obj_int_get(a); |
| 254 | + } else if (MP_OBJ_IS_STR(a) || MP_OBJ_IS_TYPE(a, &bytes_type)) { |
| 255 | + const char *s = mp_obj_str_get_str(a); |
| 256 | + values[i] = (int)s; |
| 257 | + } else if (MP_OBJ_IS_TYPE(a, &fficallback_type)) { |
| 258 | + mp_obj_fficallback_t *p = a; |
| 259 | + values[i] = (int)p->func; |
| 260 | + } else { |
| 261 | + assert(0); |
| 262 | + } |
| 263 | + valueptrs[i] = &values[i]; |
| 264 | + } |
| 265 | + |
| 266 | + int retval; |
| 267 | + ffi_call(&self->cif, self->func, &retval, valueptrs); |
| 268 | + return return_ffi_value(retval, self->rettype); |
| 269 | +} |
| 270 | + |
| 271 | +static const mp_obj_type_t ffifunc_type = { |
| 272 | + { &mp_const_type }, |
| 273 | + "ffifunc", |
| 274 | + .print = ffifunc_print, |
| 275 | + .call = ffifunc_call, |
| 276 | +}; |
| 277 | + |
| 278 | +// FFI callback for Python function |
| 279 | + |
| 280 | +static void fficallback_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
| 281 | + mp_obj_fficallback_t *self = self_in; |
| 282 | + print(env, "<fficallback %p>", self->func); |
| 283 | +} |
| 284 | + |
| 285 | +static const mp_obj_type_t fficallback_type = { |
| 286 | + { &mp_const_type }, |
| 287 | + "fficallback", |
| 288 | + .print = fficallback_print, |
| 289 | +}; |
| 290 | + |
| 291 | +// FFI variable |
| 292 | + |
| 293 | +static void ffivar_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { |
| 294 | + mp_obj_ffivar_t *self = self_in; |
| 295 | + print(env, "<ffivar @%p: 0x%x>", self->var, *(int*)self->var); |
| 296 | +} |
| 297 | + |
| 298 | +static const mp_obj_type_t ffivar_type = { |
| 299 | + { &mp_const_type }, |
| 300 | + "ffivar", |
| 301 | + .print = ffivar_print, |
| 302 | +}; |
| 303 | + |
| 304 | +// Generic opaque storage object |
| 305 | + |
| 306 | +static const mp_obj_type_t opaque_type = { |
| 307 | + { &mp_const_type }, |
| 308 | + "opaqueval", |
| 309 | +// .print = opaque_print, |
| 310 | +}; |
| 311 | + |
| 312 | + |
| 313 | +mp_obj_t mod_ffi_open(uint n_args, const mp_obj_t *args) { |
| 314 | + return ffimod_make_new((mp_obj_t)&ffimod_type, n_args, 0, args); |
| 315 | +} |
| 316 | +MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_ffi_open_obj, 1, 2, mod_ffi_open); |
| 317 | + |
| 318 | +mp_obj_t mod_ffi_as_bytearray(mp_obj_t ptr, mp_obj_t size) { |
| 319 | + return mp_obj_new_bytearray_by_ref(mp_obj_int_get(size), (void*)mp_obj_int_get(ptr)); |
| 320 | +} |
| 321 | +MP_DEFINE_CONST_FUN_OBJ_2(mod_ffi_as_bytearray_obj, mod_ffi_as_bytearray); |
| 322 | + |
| 323 | + |
| 324 | +void ffi_init() { |
| 325 | + mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("ffi")); |
| 326 | + rt_store_attr(m, MP_QSTR_open, (mp_obj_t)&mod_ffi_open_obj); |
| 327 | + rt_store_attr(m, QSTR_FROM_STR_STATIC("callback"), (mp_obj_t)&mod_ffi_callback_obj); |
| 328 | + // there would be as_bytes, but bytes currently is value, not reference type! |
| 329 | + rt_store_attr(m, QSTR_FROM_STR_STATIC("as_bytearray"), (mp_obj_t)&mod_ffi_as_bytearray_obj); |
| 330 | +} |
0 commit comments