2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
2019-05-14 08:51:57 -04:00
|
|
|
* Copyright (c) 2014-2018 Paul Sokolovsky
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2014-01-28 17:24:00 -05:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <ffi.h>
|
2015-09-20 04:00:50 -04:00
|
|
|
#include <stdint.h>
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2015-01-01 15:40:19 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/binary.h"
|
2016-10-06 23:05:15 -04:00
|
|
|
#include "py/mperrno.h"
|
2021-05-15 23:30:25 -04:00
|
|
|
#include "py/objint.h"
|
2021-06-15 02:02:50 -04:00
|
|
|
#include "py/gc.h"
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2014-04-21 16:17:12 -04:00
|
|
|
/*
|
|
|
|
* modffi uses character codes to encode a value type, based on "struct"
|
|
|
|
* module type codes, with some extensions and overridings.
|
|
|
|
*
|
|
|
|
* Extra/overridden typecodes:
|
|
|
|
* v - void, can be used only as return type
|
|
|
|
* P - const void*, pointer to read-only memory
|
|
|
|
* p - void*, meaning pointer to a writable memory (note that this
|
|
|
|
* clashes with struct's "p" as "Pascal string").
|
|
|
|
* s - as argument, the same as "p", as return value, causes string
|
|
|
|
* to be allocated and returned, instead of pointer value.
|
2015-11-20 10:51:08 -05:00
|
|
|
* O - mp_obj_t, passed as is (mostly useful as a callback param)
|
2014-04-21 16:17:12 -04:00
|
|
|
*
|
2014-06-30 18:57:08 -04:00
|
|
|
* TODO:
|
|
|
|
* C - callback function
|
|
|
|
*
|
2014-04-21 16:17:12 -04:00
|
|
|
* Note: all constraint specified by typecode can be not enforced at this time,
|
|
|
|
* but may be later.
|
|
|
|
*/
|
|
|
|
|
2021-04-14 01:37:33 -04:00
|
|
|
// This union is large enough to hold any supported argument/return value.
|
|
|
|
typedef union _ffi_union_t {
|
|
|
|
ffi_arg ffi;
|
2021-05-15 23:30:25 -04:00
|
|
|
unsigned char B;
|
|
|
|
unsigned short int H;
|
|
|
|
unsigned int I;
|
|
|
|
unsigned long int L;
|
|
|
|
unsigned long long int Q;
|
2021-04-14 01:37:33 -04:00
|
|
|
float flt;
|
|
|
|
double dbl;
|
|
|
|
} ffi_union_t;
|
|
|
|
|
2014-01-28 17:24:00 -05:00
|
|
|
typedef struct _mp_obj_opaque_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
void *val;
|
|
|
|
} mp_obj_opaque_t;
|
|
|
|
|
|
|
|
typedef struct _mp_obj_ffimod_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
void *handle;
|
|
|
|
} mp_obj_ffimod_t;
|
|
|
|
|
|
|
|
typedef struct _mp_obj_ffivar_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
void *var;
|
|
|
|
char type;
|
|
|
|
// ffi_type *type;
|
|
|
|
} mp_obj_ffivar_t;
|
|
|
|
|
|
|
|
typedef struct _mp_obj_ffifunc_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
void *func;
|
|
|
|
char rettype;
|
2015-02-25 16:37:22 -05:00
|
|
|
const char *argtypes;
|
2014-01-28 17:24:00 -05:00
|
|
|
ffi_cif cif;
|
|
|
|
ffi_type *params[];
|
|
|
|
} mp_obj_ffifunc_t;
|
|
|
|
|
|
|
|
typedef struct _mp_obj_fficallback_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
void *func;
|
|
|
|
ffi_closure *clo;
|
|
|
|
char rettype;
|
2021-06-15 02:02:50 -04:00
|
|
|
mp_obj_t pyfunc;
|
2014-01-28 17:24:00 -05:00
|
|
|
ffi_cif cif;
|
|
|
|
ffi_type *params[];
|
|
|
|
} mp_obj_fficallback_t;
|
|
|
|
|
2020-04-16 03:13:57 -04:00
|
|
|
// STATIC const mp_obj_type_t opaque_type;
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC const mp_obj_type_t ffimod_type;
|
|
|
|
STATIC const mp_obj_type_t ffifunc_type;
|
|
|
|
STATIC const mp_obj_type_t fficallback_type;
|
|
|
|
STATIC const mp_obj_type_t ffivar_type;
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
STATIC ffi_type *char2ffi_type(char c) {
|
2014-01-28 17:24:00 -05:00
|
|
|
switch (c) {
|
2020-02-26 23:36:53 -05:00
|
|
|
case 'b':
|
|
|
|
return &ffi_type_schar;
|
|
|
|
case 'B':
|
|
|
|
return &ffi_type_uchar;
|
|
|
|
case 'h':
|
|
|
|
return &ffi_type_sshort;
|
|
|
|
case 'H':
|
|
|
|
return &ffi_type_ushort;
|
|
|
|
case 'i':
|
|
|
|
return &ffi_type_sint;
|
|
|
|
case 'I':
|
|
|
|
return &ffi_type_uint;
|
|
|
|
case 'l':
|
|
|
|
return &ffi_type_slong;
|
|
|
|
case 'L':
|
|
|
|
return &ffi_type_ulong;
|
|
|
|
case 'q':
|
|
|
|
return &ffi_type_sint64;
|
|
|
|
case 'Q':
|
|
|
|
return &ffi_type_uint64;
|
2015-01-08 10:41:11 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2020-02-26 23:36:53 -05:00
|
|
|
case 'f':
|
|
|
|
return &ffi_type_float;
|
|
|
|
case 'd':
|
|
|
|
return &ffi_type_double;
|
2015-01-08 10:41:11 -05:00
|
|
|
#endif
|
2015-01-21 18:08:58 -05:00
|
|
|
case 'O': // mp_obj_t
|
2014-06-30 18:57:08 -04:00
|
|
|
case 'C': // (*)()
|
2014-04-21 16:17:12 -04:00
|
|
|
case 'P': // const void*
|
|
|
|
case 'p': // void*
|
2020-02-26 23:36:53 -05:00
|
|
|
case 's':
|
|
|
|
return &ffi_type_pointer;
|
|
|
|
case 'v':
|
|
|
|
return &ffi_type_void;
|
|
|
|
default:
|
|
|
|
return NULL;
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
STATIC ffi_type *get_ffi_type(mp_obj_t o_in) {
|
2019-01-30 06:05:48 -05:00
|
|
|
if (mp_obj_is_str(o_in)) {
|
2017-03-25 04:54:07 -04:00
|
|
|
const char *s = mp_obj_str_get_str(o_in);
|
2014-01-28 17:24:00 -05:00
|
|
|
ffi_type *t = char2ffi_type(*s);
|
|
|
|
if (t != NULL) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Support actual libffi type objects
|
|
|
|
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("unknown type"));
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2021-04-14 01:37:33 -04:00
|
|
|
STATIC mp_obj_t return_ffi_value(ffi_union_t *val, char type) {
|
2014-01-28 17:24:00 -05:00
|
|
|
switch (type) {
|
|
|
|
case 's': {
|
2021-04-14 01:37:33 -04:00
|
|
|
const char *s = (const char *)(intptr_t)val->ffi;
|
2015-01-24 17:42:39 -05:00
|
|
|
if (!s) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2017-11-15 21:17:51 -05:00
|
|
|
return mp_obj_new_str(s, strlen(s));
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
case 'v':
|
|
|
|
return mp_const_none;
|
2015-01-08 10:41:11 -05:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-04-16 20:27:43 -04:00
|
|
|
case 'f': {
|
2021-04-14 01:37:33 -04:00
|
|
|
return mp_obj_new_float_from_f(val->flt);
|
2014-04-16 20:27:43 -04:00
|
|
|
}
|
|
|
|
case 'd': {
|
2021-04-14 01:37:33 -04:00
|
|
|
return mp_obj_new_float_from_d(val->dbl);
|
2014-04-16 20:27:43 -04:00
|
|
|
}
|
2015-01-08 10:41:11 -05:00
|
|
|
#endif
|
2021-05-15 23:30:25 -04:00
|
|
|
case 'b':
|
|
|
|
case 'h':
|
|
|
|
case 'i':
|
|
|
|
case 'l':
|
|
|
|
return mp_obj_new_int((signed)val->ffi);
|
|
|
|
case 'B':
|
|
|
|
case 'H':
|
|
|
|
case 'I':
|
|
|
|
case 'L':
|
|
|
|
return mp_obj_new_int_from_uint(val->ffi);
|
|
|
|
case 'q':
|
|
|
|
return mp_obj_new_int_from_ll(val->Q);
|
|
|
|
case 'Q':
|
|
|
|
return mp_obj_new_int_from_ull(val->Q);
|
2015-01-21 18:08:58 -05:00
|
|
|
case 'O':
|
2021-04-14 01:37:33 -04:00
|
|
|
return (mp_obj_t)(intptr_t)val->ffi;
|
2014-01-28 17:24:00 -05:00
|
|
|
default:
|
2021-04-14 01:37:33 -04:00
|
|
|
return mp_obj_new_int(val->ffi);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FFI module
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void ffimod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_ffimod_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, "<ffimod %p>", self->handle);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC mp_obj_t ffimod_close(mp_obj_t self_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_ffimod_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-28 17:24:00 -05:00
|
|
|
dlclose(self->handle);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ffimod_close_obj, ffimod_close);
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2015-02-10 18:20:35 -05:00
|
|
|
STATIC mp_obj_t make_func(mp_obj_t rettype_in, void *func, mp_obj_t argtypes_in) {
|
|
|
|
const char *rettype = mp_obj_str_get_str(rettype_in);
|
2015-02-25 16:37:22 -05:00
|
|
|
const char *argtypes = mp_obj_str_get_str(argtypes_in);
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2015-02-10 18:20:35 -05:00
|
|
|
mp_int_t nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(argtypes_in));
|
2020-02-26 23:36:53 -05:00
|
|
|
mp_obj_ffifunc_t *o = m_new_obj_var(mp_obj_ffifunc_t, ffi_type *, nparams);
|
2014-01-28 17:24:00 -05:00
|
|
|
o->base.type = &ffifunc_type;
|
|
|
|
|
2015-02-10 18:20:35 -05:00
|
|
|
o->func = func;
|
2014-01-28 17:24:00 -05:00
|
|
|
o->rettype = *rettype;
|
2015-02-25 16:37:22 -05:00
|
|
|
o->argtypes = argtypes;
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2016-01-09 18:14:54 -05:00
|
|
|
mp_obj_iter_buf_t iter_buf;
|
|
|
|
mp_obj_t iterable = mp_getiter(argtypes_in, &iter_buf);
|
2014-01-28 17:24:00 -05:00
|
|
|
mp_obj_t item;
|
|
|
|
int i = 0;
|
2014-04-19 13:05:47 -04:00
|
|
|
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-28 17:24:00 -05:00
|
|
|
o->params[i++] = get_ffi_type(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = ffi_prep_cif(&o->cif, FFI_DEFAULT_ABI, nparams, char2ffi_type(*rettype), o->params);
|
|
|
|
if (res != FFI_OK) {
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("error in ffi_prep_cif"));
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
2015-02-10 18:20:35 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t ffimod_func(size_t n_args, const mp_obj_t *args) {
|
2015-02-10 18:20:35 -05:00
|
|
|
(void)n_args; // always 4
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_ffimod_t *self = MP_OBJ_TO_PTR(args[0]);
|
2015-02-10 18:20:35 -05:00
|
|
|
const char *symname = mp_obj_str_get_str(args[2]);
|
|
|
|
|
|
|
|
void *sym = dlsym(self->handle, symname);
|
|
|
|
if (sym == NULL) {
|
2016-10-06 23:05:15 -04:00
|
|
|
mp_raise_OSError(MP_ENOENT);
|
2015-02-10 18:20:35 -05:00
|
|
|
}
|
|
|
|
return make_func(args[1], sym, args[3]);
|
|
|
|
}
|
2014-01-28 17:24:00 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ffimod_func_obj, 4, 4, ffimod_func);
|
|
|
|
|
2015-02-10 18:20:35 -05:00
|
|
|
STATIC mp_obj_t mod_ffi_func(mp_obj_t rettype, mp_obj_t addr_in, mp_obj_t argtypes) {
|
2020-02-26 23:36:53 -05:00
|
|
|
void *addr = (void *)MP_OBJ_TO_PTR(mp_obj_int_get_truncated(addr_in));
|
2015-02-10 18:20:35 -05:00
|
|
|
return make_func(rettype, addr, argtypes);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_3(mod_ffi_func_obj, mod_ffi_func);
|
|
|
|
|
2021-06-15 02:02:50 -04:00
|
|
|
STATIC void call_py_func(ffi_cif *cif, void *ret, void **args, void *user_data) {
|
2014-01-28 17:24:00 -05:00
|
|
|
mp_obj_t pyargs[cif->nargs];
|
2021-06-15 02:02:50 -04:00
|
|
|
mp_obj_fficallback_t *o = user_data;
|
|
|
|
mp_obj_t pyfunc = o->pyfunc;
|
|
|
|
|
2015-01-16 12:47:07 -05:00
|
|
|
for (uint i = 0; i < cif->nargs; i++) {
|
2020-02-26 23:36:53 -05:00
|
|
|
pyargs[i] = mp_obj_new_int(*(mp_int_t *)args[i]);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
2021-06-15 02:02:50 -04:00
|
|
|
mp_obj_t res = mp_call_function_n_kw(pyfunc, cif->nargs, 0, pyargs);
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2014-12-15 17:28:02 -05:00
|
|
|
if (res != mp_const_none) {
|
2020-02-26 23:36:53 -05:00
|
|
|
*(ffi_arg *)ret = mp_obj_int_get_truncated(res);
|
2014-12-15 17:28:02 -05:00
|
|
|
}
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2021-06-15 02:02:50 -04:00
|
|
|
STATIC void call_py_func_with_lock(ffi_cif *cif, void *ret, void **args, void *user_data) {
|
|
|
|
mp_obj_t pyargs[cif->nargs];
|
|
|
|
mp_obj_fficallback_t *o = user_data;
|
|
|
|
mp_obj_t pyfunc = o->pyfunc;
|
|
|
|
nlr_buf_t nlr;
|
|
|
|
|
|
|
|
#if MICROPY_ENABLE_SCHEDULER
|
|
|
|
mp_sched_lock();
|
|
|
|
#endif
|
|
|
|
gc_lock();
|
|
|
|
|
|
|
|
if (nlr_push(&nlr) == 0) {
|
|
|
|
for (uint i = 0; i < cif->nargs; i++) {
|
|
|
|
pyargs[i] = mp_obj_new_int(*(mp_int_t *)args[i]);
|
|
|
|
}
|
|
|
|
mp_obj_t res = mp_call_function_n_kw(pyfunc, cif->nargs, 0, pyargs);
|
|
|
|
|
|
|
|
if (res != mp_const_none) {
|
|
|
|
*(ffi_arg *)ret = mp_obj_int_get_truncated(res);
|
|
|
|
}
|
|
|
|
nlr_pop();
|
|
|
|
} else {
|
|
|
|
// Uncaught exception
|
|
|
|
mp_printf(MICROPY_ERROR_PRINTER, "Uncaught exception in FFI callback\n");
|
|
|
|
mp_obj_print_exception(MICROPY_ERROR_PRINTER, MP_OBJ_FROM_PTR(nlr.ret_val));
|
|
|
|
}
|
|
|
|
|
|
|
|
gc_unlock();
|
|
|
|
#if MICROPY_ENABLE_SCHEDULER
|
|
|
|
mp_sched_unlock();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t mod_ffi_callback(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
// first 3 args are positional: retttype, func, paramtypes.
|
|
|
|
mp_obj_t rettype_in = pos_args[0];
|
|
|
|
mp_obj_t func_in = pos_args[1];
|
|
|
|
mp_obj_t paramtypes_in = pos_args[2];
|
|
|
|
|
|
|
|
// arg parsing is used only for additional kwargs
|
|
|
|
enum { ARG_lock };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_lock, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 3, pos_args + 3, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
bool lock_in = args[ARG_lock].u_bool;
|
|
|
|
|
2014-01-28 17:24:00 -05:00
|
|
|
const char *rettype = mp_obj_str_get_str(rettype_in);
|
|
|
|
|
2014-12-14 19:18:12 -05:00
|
|
|
mp_int_t nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(paramtypes_in));
|
2020-02-26 23:36:53 -05:00
|
|
|
mp_obj_fficallback_t *o = m_new_obj_var(mp_obj_fficallback_t, ffi_type *, nparams);
|
2014-01-28 17:24:00 -05:00
|
|
|
o->base.type = &fficallback_type;
|
|
|
|
|
|
|
|
o->clo = ffi_closure_alloc(sizeof(ffi_closure), &o->func);
|
|
|
|
|
|
|
|
o->rettype = *rettype;
|
2021-06-15 02:02:50 -04:00
|
|
|
o->pyfunc = func_in;
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2016-01-09 18:14:54 -05:00
|
|
|
mp_obj_iter_buf_t iter_buf;
|
|
|
|
mp_obj_t iterable = mp_getiter(paramtypes_in, &iter_buf);
|
2014-01-28 17:24:00 -05:00
|
|
|
mp_obj_t item;
|
|
|
|
int i = 0;
|
2014-04-19 13:05:47 -04:00
|
|
|
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-28 17:24:00 -05:00
|
|
|
o->params[i++] = get_ffi_type(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
int res = ffi_prep_cif(&o->cif, FFI_DEFAULT_ABI, nparams, char2ffi_type(*rettype), o->params);
|
|
|
|
if (res != FFI_OK) {
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("error in ffi_prep_cif"));
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2021-06-15 02:02:50 -04:00
|
|
|
res = ffi_prep_closure_loc(o->clo, &o->cif,
|
|
|
|
lock_in? call_py_func_with_lock: call_py_func, o, o->func);
|
2014-01-28 17:24:00 -05:00
|
|
|
if (res != FFI_OK) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("ffi_prep_closure_loc"));
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
2021-06-15 02:02:50 -04:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(mod_ffi_callback_obj, 3, mod_ffi_callback);
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC mp_obj_t ffimod_var(mp_obj_t self_in, mp_obj_t vartype_in, mp_obj_t symname_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_ffimod_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-28 17:24:00 -05:00
|
|
|
const char *rettype = mp_obj_str_get_str(vartype_in);
|
|
|
|
const char *symname = mp_obj_str_get_str(symname_in);
|
|
|
|
|
|
|
|
void *sym = dlsym(self->handle, symname);
|
|
|
|
if (sym == NULL) {
|
2016-10-06 23:05:15 -04:00
|
|
|
mp_raise_OSError(MP_ENOENT);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
mp_obj_ffivar_t *o = m_new_obj(mp_obj_ffivar_t);
|
|
|
|
o->base.type = &ffivar_type;
|
|
|
|
|
|
|
|
o->var = sym;
|
|
|
|
o->type = *rettype;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_3(ffimod_var_obj, ffimod_var);
|
|
|
|
|
2015-02-05 17:18:11 -05:00
|
|
|
STATIC mp_obj_t ffimod_addr(mp_obj_t self_in, mp_obj_t symname_in) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_ffimod_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-02-05 17:18:11 -05:00
|
|
|
const char *symname = mp_obj_str_get_str(symname_in);
|
|
|
|
|
|
|
|
void *sym = dlsym(self->handle, symname);
|
|
|
|
if (sym == NULL) {
|
2016-10-06 23:05:15 -04:00
|
|
|
mp_raise_OSError(MP_ENOENT);
|
2015-02-05 17:18:11 -05:00
|
|
|
}
|
2016-01-15 09:26:24 -05:00
|
|
|
return mp_obj_new_int((uintptr_t)sym);
|
2015-02-05 17:18:11 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(ffimod_addr_obj, ffimod_addr);
|
|
|
|
|
2016-01-03 10:55:55 -05:00
|
|
|
STATIC mp_obj_t ffimod_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)n_args;
|
|
|
|
(void)n_kw;
|
|
|
|
|
2015-01-20 17:36:32 -05:00
|
|
|
const char *fname = NULL;
|
|
|
|
if (args[0] != mp_const_none) {
|
|
|
|
fname = mp_obj_str_get_str(args[0]);
|
|
|
|
}
|
2014-01-28 17:24:00 -05:00
|
|
|
void *mod = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
|
|
|
|
|
|
|
|
if (mod == NULL) {
|
2016-10-06 23:05:15 -04:00
|
|
|
mp_raise_OSError(errno);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
mp_obj_ffimod_t *o = m_new_obj(mp_obj_ffimod_t);
|
2016-01-03 10:55:55 -05:00
|
|
|
o->base.type = type;
|
2014-01-28 17:24:00 -05:00
|
|
|
o->handle = mod;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t ffimod_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_func), MP_ROM_PTR(&ffimod_func_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_var), MP_ROM_PTR(&ffimod_var_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_addr), MP_ROM_PTR(&ffimod_addr_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&ffimod_close_obj) },
|
2014-01-28 17:24:00 -05:00
|
|
|
};
|
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(ffimod_locals_dict, ffimod_locals_dict_table);
|
|
|
|
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC const mp_obj_type_t ffimod_type = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_ffimod,
|
2014-01-28 17:24:00 -05:00
|
|
|
.print = ffimod_print,
|
|
|
|
.make_new = ffimod_make_new,
|
2020-02-26 23:36:53 -05:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&ffimod_locals_dict,
|
2014-01-28 17:24:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// FFI function
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void ffifunc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_ffifunc_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, "<ffifunc %p>", self->func);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2021-05-15 23:30:25 -04:00
|
|
|
STATIC unsigned long long ffi_get_int_value(mp_obj_t o) {
|
|
|
|
if (mp_obj_is_small_int(o)) {
|
|
|
|
return MP_OBJ_SMALL_INT_VALUE(o);
|
|
|
|
} else {
|
|
|
|
unsigned long long res;
|
|
|
|
mp_obj_int_to_bytes_impl(o, MP_ENDIANNESS_BIG, sizeof(res), (byte *)&res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC ffi_union_t ffi_int_obj_to_ffi_union(mp_obj_t o, const char argtype) {
|
|
|
|
ffi_union_t ret;
|
|
|
|
if ((argtype | 0x20) == 'q') {
|
|
|
|
ret.Q = ffi_get_int_value(o);
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
mp_uint_t val = mp_obj_int_get_truncated(o);
|
|
|
|
switch (argtype) {
|
|
|
|
case 'b':
|
|
|
|
case 'B':
|
|
|
|
ret.B = val;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case 'H':
|
|
|
|
ret.H = val;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
ret.I = val;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
ret.L = val;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret.ffi = val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-01-03 04:59:18 -05:00
|
|
|
STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
|
2019-02-24 22:53:17 -05:00
|
|
|
(void)n_kw;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_ffifunc_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-01-28 17:24:00 -05:00
|
|
|
assert(n_kw == 0);
|
|
|
|
assert(n_args == self->cif.nargs);
|
|
|
|
|
2021-04-14 01:37:33 -04:00
|
|
|
ffi_union_t values[n_args];
|
2014-01-28 17:24:00 -05:00
|
|
|
void *valueptrs[n_args];
|
2015-02-25 16:37:22 -05:00
|
|
|
const char *argtype = self->argtypes;
|
|
|
|
for (uint i = 0; i < n_args; i++, argtype++) {
|
2014-01-28 17:24:00 -05:00
|
|
|
mp_obj_t a = args[i];
|
2015-02-25 16:37:22 -05:00
|
|
|
if (*argtype == 'O') {
|
2021-04-14 01:37:33 -04:00
|
|
|
values[i].ffi = (ffi_arg)(intptr_t)a;
|
2015-04-28 14:40:34 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
} else if (*argtype == 'f') {
|
2021-04-14 01:37:33 -04:00
|
|
|
values[i].flt = mp_obj_get_float_to_f(a);
|
2015-04-28 14:40:34 -04:00
|
|
|
} else if (*argtype == 'd') {
|
2021-04-14 01:37:33 -04:00
|
|
|
values[i].dbl = mp_obj_get_float_to_d(a);
|
2015-04-28 14:40:34 -04:00
|
|
|
#endif
|
2015-02-25 16:37:22 -05:00
|
|
|
} else if (a == mp_const_none) {
|
2021-04-14 01:37:33 -04:00
|
|
|
values[i].ffi = 0;
|
2019-01-30 06:05:48 -05:00
|
|
|
} else if (mp_obj_is_int(a)) {
|
2021-05-15 23:30:25 -04:00
|
|
|
values[i] = ffi_int_obj_to_ffi_union(a, *argtype);
|
2019-01-30 06:05:48 -05:00
|
|
|
} else if (mp_obj_is_str(a)) {
|
2014-01-28 17:24:00 -05:00
|
|
|
const char *s = mp_obj_str_get_str(a);
|
2021-04-14 01:37:33 -04:00
|
|
|
values[i].ffi = (ffi_arg)(intptr_t)s;
|
2020-02-26 23:36:53 -05:00
|
|
|
} else if (((mp_obj_base_t *)MP_OBJ_TO_PTR(a))->type->buffer_p.get_buffer != NULL) {
|
|
|
|
mp_obj_base_t *o = (mp_obj_base_t *)MP_OBJ_TO_PTR(a);
|
2014-04-18 17:31:24 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
2016-01-15 09:26:24 -05:00
|
|
|
int ret = o->type->buffer_p.get_buffer(MP_OBJ_FROM_PTR(o), &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ?
|
2015-05-01 16:34:08 -04:00
|
|
|
if (ret != 0) {
|
2014-04-08 10:32:07 -04:00
|
|
|
goto error;
|
|
|
|
}
|
2021-04-14 01:37:33 -04:00
|
|
|
values[i].ffi = (ffi_arg)(intptr_t)bufinfo.buf;
|
2019-01-30 06:05:48 -05:00
|
|
|
} else if (mp_obj_is_type(a, &fficallback_type)) {
|
2016-01-15 09:26:24 -05:00
|
|
|
mp_obj_fficallback_t *p = MP_OBJ_TO_PTR(a);
|
2021-04-14 01:37:33 -04:00
|
|
|
values[i].ffi = (ffi_arg)(intptr_t)p->func;
|
2014-01-28 17:24:00 -05:00
|
|
|
} else {
|
2014-04-08 10:32:07 -04:00
|
|
|
goto error;
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
valueptrs[i] = &values[i];
|
|
|
|
}
|
|
|
|
|
2021-04-14 01:37:33 -04:00
|
|
|
ffi_union_t retval;
|
|
|
|
ffi_call(&self->cif, self->func, &retval, valueptrs);
|
|
|
|
return return_ffi_value(&retval, self->rettype);
|
2014-04-08 10:32:07 -04:00
|
|
|
|
|
|
|
error:
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("don't know how to pass object to native function"));
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC const mp_obj_type_t ffifunc_type = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_ffifunc,
|
2014-01-28 17:24:00 -05:00
|
|
|
.print = ffifunc_print,
|
|
|
|
.call = ffifunc_call,
|
|
|
|
};
|
|
|
|
|
|
|
|
// FFI callback for Python function
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void fficallback_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2016-01-15 09:26:24 -05:00
|
|
|
mp_obj_fficallback_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, "<fficallback %p>", self->func);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2021-06-15 02:02:50 -04:00
|
|
|
STATIC mp_obj_t fficallback_cfun(mp_obj_t self_in) {
|
|
|
|
mp_obj_fficallback_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mp_obj_new_int_from_ull((uintptr_t)self->func);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(fficallback_cfun_obj, fficallback_cfun);
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t fficallback_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_cfun), MP_ROM_PTR(&fficallback_cfun_obj) }
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(fficallback_locals_dict, fficallback_locals_dict_table);
|
|
|
|
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC const mp_obj_type_t fficallback_type = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_fficallback,
|
2014-01-28 17:24:00 -05:00
|
|
|
.print = fficallback_print,
|
2021-06-15 02:02:50 -04:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&fficallback_locals_dict
|
2014-01-28 17:24:00 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// FFI variable
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void ffivar_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2016-01-15 09:26:24 -05:00
|
|
|
mp_obj_ffivar_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-12-14 19:18:12 -05:00
|
|
|
// Variable value printed as cast to int
|
2020-02-26 23:36:53 -05:00
|
|
|
mp_printf(print, "<ffivar @%p: 0x%x>", self->var, *(int *)self->var);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC mp_obj_t ffivar_get(mp_obj_t self_in) {
|
2016-01-15 09:26:24 -05:00
|
|
|
mp_obj_ffivar_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-10 22:17:41 -04:00
|
|
|
return mp_binary_get_val_array(self->type, self->var, 0);
|
2014-02-14 10:16:35 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(ffivar_get_obj, ffivar_get);
|
|
|
|
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC mp_obj_t ffivar_set(mp_obj_t self_in, mp_obj_t val_in) {
|
2016-01-15 09:26:24 -05:00
|
|
|
mp_obj_ffivar_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-04-10 22:17:41 -04:00
|
|
|
mp_binary_set_val_array(self->type, self->var, 0, val_in);
|
2014-02-14 10:16:35 -05:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(ffivar_set_obj, ffivar_set);
|
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t ffivar_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_get), MP_ROM_PTR(&ffivar_get_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_set), MP_ROM_PTR(&ffivar_set_obj) },
|
2014-02-14 10:16:35 -05:00
|
|
|
};
|
|
|
|
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(ffivar_locals_dict, ffivar_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t ffivar_type = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_ffivar,
|
2014-01-28 17:24:00 -05:00
|
|
|
.print = ffivar_print,
|
2020-02-26 23:36:53 -05:00
|
|
|
.locals_dict = (mp_obj_dict_t *)&ffivar_locals_dict,
|
2014-01-28 17:24:00 -05:00
|
|
|
};
|
|
|
|
|
2014-02-15 11:10:44 -05:00
|
|
|
// Generic opaque storage object (unused)
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2014-02-15 11:10:44 -05:00
|
|
|
/*
|
2014-03-29 09:48:32 -04:00
|
|
|
STATIC const mp_obj_type_t opaque_type = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_opaqueval,
|
2014-01-28 17:24:00 -05:00
|
|
|
// .print = opaque_print,
|
|
|
|
};
|
2014-02-15 11:10:44 -05:00
|
|
|
*/
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2016-01-03 09:21:40 -05:00
|
|
|
STATIC mp_obj_t mod_ffi_open(size_t n_args, const mp_obj_t *args) {
|
2016-01-03 10:55:55 -05:00
|
|
|
return ffimod_make_new(&ffimod_type, n_args, 0, args);
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_ffi_open_obj, 1, 2, mod_ffi_open);
|
|
|
|
|
2014-12-10 17:07:04 -05:00
|
|
|
STATIC mp_obj_t mod_ffi_as_bytearray(mp_obj_t ptr, mp_obj_t size) {
|
2020-02-26 23:36:53 -05:00
|
|
|
return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void *)(uintptr_t)mp_obj_int_get_truncated(ptr));
|
2014-01-28 17:24:00 -05:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(mod_ffi_as_bytearray_obj, mod_ffi_as_bytearray);
|
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_ffi_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ffi) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mod_ffi_open_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_callback), MP_ROM_PTR(&mod_ffi_callback_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_func), MP_ROM_PTR(&mod_ffi_func_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_as_bytearray), MP_ROM_PTR(&mod_ffi_as_bytearray_obj) },
|
2014-04-17 17:00:15 -04:00
|
|
|
};
|
2014-01-28 17:24:00 -05:00
|
|
|
|
2014-11-29 09:39:27 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_ffi_globals, mp_module_ffi_globals_table);
|
2014-04-17 17:00:15 -04:00
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_ffi = {
|
|
|
|
.base = { &mp_type_module },
|
2020-02-26 23:36:53 -05:00
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_ffi_globals,
|
2014-04-17 17:00:15 -04:00
|
|
|
};
|