2014-05-03 23:27:38 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
2014-05-13 06:44:45 +01:00
|
|
|
* Copyright (c) 2014 Paul Sokolovsky
|
2014-05-03 23:27:38 +01: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 22:24:00 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <ffi.h>
|
2015-09-20 09:00:50 +01:00
|
|
|
#include <stdint.h>
|
2014-01-28 22:24:00 +00:00
|
|
|
|
2015-01-01 20:40:19 +00:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/binary.h"
|
2014-01-28 22:24:00 +00:00
|
|
|
|
2014-04-21 21:17:12 +01: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.
|
|
|
|
*
|
2014-06-30 23:57:08 +01:00
|
|
|
* TODO:
|
|
|
|
* O - mp_obj_t, passed as is (mostly useful as callback param)
|
|
|
|
* C - callback function
|
|
|
|
*
|
2014-04-21 21:17:12 +01:00
|
|
|
* Note: all constraint specified by typecode can be not enforced at this time,
|
|
|
|
* but may be later.
|
|
|
|
*/
|
|
|
|
|
2014-01-28 22:24:00 +00: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 21:37:22 +00:00
|
|
|
const char *argtypes;
|
2014-01-28 22:24:00 +00: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;
|
|
|
|
ffi_cif cif;
|
|
|
|
ffi_type *params[];
|
|
|
|
} mp_obj_fficallback_t;
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
//STATIC const mp_obj_type_t opaque_type;
|
|
|
|
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 22:24:00 +00:00
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC ffi_type *char2ffi_type(char c)
|
2014-01-28 22:24:00 +00:00
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case 'b': return &ffi_type_schar;
|
|
|
|
case 'B': return &ffi_type_uchar;
|
2014-06-29 13:47:48 +01:00
|
|
|
case 'h': return &ffi_type_sshort;
|
|
|
|
case 'H': return &ffi_type_ushort;
|
2014-01-28 22:24:00 +00:00
|
|
|
case 'i': return &ffi_type_sint;
|
|
|
|
case 'I': return &ffi_type_uint;
|
|
|
|
case 'l': return &ffi_type_slong;
|
|
|
|
case 'L': return &ffi_type_ulong;
|
2015-01-08 15:41:11 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-04-17 01:27:43 +01:00
|
|
|
case 'f': return &ffi_type_float;
|
|
|
|
case 'd': return &ffi_type_double;
|
2015-01-08 15:41:11 +00:00
|
|
|
#endif
|
2015-01-21 23:08:58 +00:00
|
|
|
case 'O': // mp_obj_t
|
2014-06-30 23:57:08 +01:00
|
|
|
case 'C': // (*)()
|
2014-04-21 21:17:12 +01:00
|
|
|
case 'P': // const void*
|
|
|
|
case 'p': // void*
|
2014-01-28 22:24:00 +00:00
|
|
|
case 's': return &ffi_type_pointer;
|
|
|
|
case 'v': return &ffi_type_void;
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC ffi_type *get_ffi_type(mp_obj_t o_in)
|
2014-01-28 22:24:00 +00:00
|
|
|
{
|
|
|
|
if (MP_OBJ_IS_STR(o_in)) {
|
2014-08-30 14:19:41 +01:00
|
|
|
mp_uint_t len;
|
2014-02-08 19:55:11 +00:00
|
|
|
const char *s = mp_obj_str_get_data(o_in, &len);
|
2014-01-28 22:24:00 +00:00
|
|
|
ffi_type *t = char2ffi_type(*s);
|
|
|
|
if (t != NULL) {
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: Support actual libffi type objects
|
|
|
|
|
2014-04-21 21:51:58 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "Unknown type"));
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC mp_obj_t return_ffi_value(ffi_arg val, char type)
|
2014-01-28 22:24:00 +00:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case 's': {
|
2015-09-20 09:00:50 +01:00
|
|
|
const char *s = (const char *)(intptr_t)val;
|
2015-01-24 22:42:39 +00:00
|
|
|
if (!s) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-05-25 22:27:57 +01:00
|
|
|
return mp_obj_new_str(s, strlen(s), false);
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
case 'v':
|
|
|
|
return mp_const_none;
|
2015-01-08 15:41:11 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-04-17 01:27:43 +01:00
|
|
|
case 'f': {
|
2014-09-06 17:46:52 +01:00
|
|
|
union { ffi_arg ffi; float flt; } val_union = { .ffi = val };
|
|
|
|
return mp_obj_new_float(val_union.flt);
|
2014-04-17 01:27:43 +01:00
|
|
|
}
|
|
|
|
case 'd': {
|
|
|
|
double *p = (double*)&val;
|
|
|
|
return mp_obj_new_float(*p);
|
|
|
|
}
|
2015-01-08 15:41:11 +00:00
|
|
|
#endif
|
2015-01-21 23:08:58 +00:00
|
|
|
case 'O':
|
2015-09-20 09:00:50 +01:00
|
|
|
return (mp_obj_t)(intptr_t)val;
|
2014-01-28 22:24:00 +00:00
|
|
|
default:
|
|
|
|
return mp_obj_new_int(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FFI module
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void ffimod_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)kind;
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffimod_t *self = self_in;
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_printf(print, "<ffimod %p>", self->handle);
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC mp_obj_t ffimod_close(mp_obj_t self_in) {
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffimod_t *self = self_in;
|
|
|
|
dlclose(self->handle);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ffimod_close_obj, ffimod_close);
|
2014-01-28 22:24:00 +00:00
|
|
|
|
2015-02-10 23:20:35 +00: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 21:37:22 +00:00
|
|
|
const char *argtypes = mp_obj_str_get_str(argtypes_in);
|
2014-01-28 22:24:00 +00:00
|
|
|
|
2015-02-10 23:20:35 +00:00
|
|
|
mp_int_t nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(argtypes_in));
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffifunc_t *o = m_new_obj_var(mp_obj_ffifunc_t, ffi_type*, nparams);
|
|
|
|
o->base.type = &ffifunc_type;
|
|
|
|
|
2015-02-10 23:20:35 +00:00
|
|
|
o->func = func;
|
2014-01-28 22:24:00 +00:00
|
|
|
o->rettype = *rettype;
|
2015-02-25 21:37:22 +00:00
|
|
|
o->argtypes = argtypes;
|
2014-01-28 22:24:00 +00:00
|
|
|
|
2015-02-10 23:20:35 +00:00
|
|
|
mp_obj_t iterable = mp_getiter(argtypes_in);
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_t item;
|
|
|
|
int i = 0;
|
2014-04-19 18:05:47 +01:00
|
|
|
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-28 22:24:00 +00: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) {
|
2014-04-21 21:51:58 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Error in ffi_prep_cif"));
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
2015-02-10 23:20:35 +00:00
|
|
|
|
|
|
|
STATIC mp_obj_t ffimod_func(mp_uint_t n_args, const mp_obj_t *args) {
|
|
|
|
(void)n_args; // always 4
|
|
|
|
mp_obj_ffimod_t *self = args[0];
|
|
|
|
const char *symname = mp_obj_str_get_str(args[2]);
|
|
|
|
|
|
|
|
void *sym = dlsym(self->handle, symname);
|
|
|
|
if (sym == NULL) {
|
2015-08-30 22:17:35 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOENT)));
|
2015-02-10 23:20:35 +00:00
|
|
|
}
|
|
|
|
return make_func(args[1], sym, args[3]);
|
|
|
|
}
|
2014-01-28 22:24:00 +00:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ffimod_func_obj, 4, 4, ffimod_func);
|
|
|
|
|
2015-02-10 23:20:35 +00:00
|
|
|
STATIC mp_obj_t mod_ffi_func(mp_obj_t rettype, mp_obj_t addr_in, mp_obj_t argtypes) {
|
|
|
|
void *addr = (void*)mp_obj_int_get_truncated(addr_in);
|
|
|
|
return make_func(rettype, addr, argtypes);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_3(mod_ffi_func_obj, mod_ffi_func);
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC void call_py_func(ffi_cif *cif, void *ret, void** args, mp_obj_t func) {
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_t pyargs[cif->nargs];
|
2015-01-16 17:47:07 +00:00
|
|
|
for (uint i = 0; i < cif->nargs; i++) {
|
2014-12-15 00:18:12 +00:00
|
|
|
pyargs[i] = mp_obj_new_int(*(mp_int_t*)args[i]);
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_obj_t res = mp_call_function_n_kw(func, cif->nargs, 0, pyargs);
|
2014-01-28 22:24:00 +00:00
|
|
|
|
2014-12-15 22:28:02 +00:00
|
|
|
if (res != mp_const_none) {
|
|
|
|
*(ffi_arg*)ret = mp_obj_int_get_truncated(res);
|
|
|
|
}
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC mp_obj_t mod_ffi_callback(mp_obj_t rettype_in, mp_obj_t func_in, mp_obj_t paramtypes_in) {
|
2014-01-28 22:24:00 +00:00
|
|
|
const char *rettype = mp_obj_str_get_str(rettype_in);
|
|
|
|
|
2014-12-15 00:18:12 +00:00
|
|
|
mp_int_t nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(paramtypes_in));
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_fficallback_t *o = m_new_obj_var(mp_obj_fficallback_t, ffi_type*, nparams);
|
|
|
|
o->base.type = &fficallback_type;
|
|
|
|
|
|
|
|
o->clo = ffi_closure_alloc(sizeof(ffi_closure), &o->func);
|
|
|
|
|
|
|
|
o->rettype = *rettype;
|
|
|
|
|
2014-03-30 13:35:08 +01:00
|
|
|
mp_obj_t iterable = mp_getiter(paramtypes_in);
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_t item;
|
|
|
|
int i = 0;
|
2014-04-19 18:05:47 +01:00
|
|
|
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
|
2014-01-28 22:24:00 +00: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) {
|
2014-04-21 21:51:58 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Error in ffi_prep_cif"));
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res = ffi_prep_closure_loc(o->clo, &o->cif, call_py_func, func_in, o->func);
|
|
|
|
if (res != FFI_OK) {
|
2014-04-21 21:51:58 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "ffi_prep_closure_loc"));
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_3(mod_ffi_callback_obj, mod_ffi_callback);
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC mp_obj_t ffimod_var(mp_obj_t self_in, mp_obj_t vartype_in, mp_obj_t symname_in) {
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffimod_t *self = self_in;
|
|
|
|
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) {
|
2015-08-30 22:17:35 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOENT)));
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
mp_obj_ffivar_t *o = m_new_obj(mp_obj_ffivar_t);
|
|
|
|
o->base.type = &ffivar_type;
|
|
|
|
|
|
|
|
o->var = sym;
|
|
|
|
o->type = *rettype;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_3(ffimod_var_obj, ffimod_var);
|
|
|
|
|
2015-02-05 22:18:11 +00:00
|
|
|
STATIC mp_obj_t ffimod_addr(mp_obj_t self_in, mp_obj_t symname_in) {
|
|
|
|
mp_obj_ffimod_t *self = self_in;
|
|
|
|
const char *symname = mp_obj_str_get_str(symname_in);
|
|
|
|
|
|
|
|
void *sym = dlsym(self->handle, symname);
|
|
|
|
if (sym == NULL) {
|
2015-08-30 22:17:35 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ENOENT)));
|
2015-02-05 22:18:11 +00:00
|
|
|
}
|
|
|
|
return mp_obj_new_int((mp_int_t)sym);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(ffimod_addr_obj, ffimod_addr);
|
|
|
|
|
2014-08-30 00:35:11 +01:00
|
|
|
STATIC mp_obj_t ffimod_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)n_args;
|
|
|
|
(void)n_kw;
|
|
|
|
|
2015-01-20 22:36:32 +00:00
|
|
|
const char *fname = NULL;
|
|
|
|
if (args[0] != mp_const_none) {
|
|
|
|
fname = mp_obj_str_get_str(args[0]);
|
|
|
|
}
|
2014-01-28 22:24:00 +00:00
|
|
|
void *mod = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
|
|
|
|
|
|
|
|
if (mod == NULL) {
|
2014-07-31 10:49:14 +01:00
|
|
|
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno)));
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
mp_obj_ffimod_t *o = m_new_obj(mp_obj_ffimod_t);
|
|
|
|
o->base.type = type_in;
|
|
|
|
o->handle = mod;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC const mp_map_elem_t ffimod_locals_dict_table[] = {
|
2014-03-26 21:47:19 +00:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_func), (mp_obj_t) &ffimod_func_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_var), (mp_obj_t) &ffimod_var_obj },
|
2015-02-05 22:18:11 +00:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_addr), (mp_obj_t) &ffimod_addr_obj },
|
2014-03-26 21:47:19 +00:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t) &ffimod_close_obj },
|
2014-01-28 22:24:00 +00:00
|
|
|
};
|
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(ffimod_locals_dict, ffimod_locals_dict_table);
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC const mp_obj_type_t ffimod_type = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_ffimod,
|
2014-01-28 22:24:00 +00:00
|
|
|
.print = ffimod_print,
|
|
|
|
.make_new = ffimod_make_new,
|
2014-03-26 21:47:19 +00:00
|
|
|
.locals_dict = (mp_obj_t)&ffimod_locals_dict,
|
2014-01-28 22:24:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// FFI function
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void ffifunc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)kind;
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffifunc_t *self = self_in;
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_printf(print, "<ffifunc %p>", self->func);
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t ffifunc_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffifunc_t *self = self_in;
|
|
|
|
assert(n_kw == 0);
|
|
|
|
assert(n_args == self->cif.nargs);
|
|
|
|
|
2014-01-28 23:08:11 +00:00
|
|
|
ffi_arg values[n_args];
|
2014-01-28 22:24:00 +00:00
|
|
|
void *valueptrs[n_args];
|
2015-02-25 21:37:22 +00:00
|
|
|
const char *argtype = self->argtypes;
|
|
|
|
for (uint i = 0; i < n_args; i++, argtype++) {
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_t a = args[i];
|
2015-02-25 21:37:22 +00:00
|
|
|
if (*argtype == 'O') {
|
2015-09-20 09:00:50 +01:00
|
|
|
values[i] = (ffi_arg)(intptr_t)a;
|
2015-04-28 19:40:34 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
} else if (*argtype == 'f') {
|
|
|
|
float *p = (float*)&values[i];
|
|
|
|
*p = mp_obj_get_float(a);
|
|
|
|
} else if (*argtype == 'd') {
|
|
|
|
double *p = (double*)&values[i];
|
|
|
|
*p = mp_obj_get_float(a);
|
|
|
|
#endif
|
2015-02-25 21:37:22 +00:00
|
|
|
} else if (a == mp_const_none) {
|
2014-01-28 22:24:00 +00:00
|
|
|
values[i] = 0;
|
|
|
|
} else if (MP_OBJ_IS_INT(a)) {
|
2014-12-05 23:13:52 +00:00
|
|
|
values[i] = mp_obj_int_get_truncated(a);
|
2014-04-08 15:32:07 +01:00
|
|
|
} else if (MP_OBJ_IS_STR(a)) {
|
2014-01-28 22:24:00 +00:00
|
|
|
const char *s = mp_obj_str_get_str(a);
|
2015-09-20 09:00:50 +01:00
|
|
|
values[i] = (ffi_arg)(intptr_t)s;
|
2014-04-08 15:32:07 +01:00
|
|
|
} else if (((mp_obj_base_t*)a)->type->buffer_p.get_buffer != NULL) {
|
|
|
|
mp_obj_base_t *o = (mp_obj_base_t*)a;
|
2014-04-18 22:31:24 +01:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
int ret = o->type->buffer_p.get_buffer(o, &bufinfo, MP_BUFFER_READ); // TODO: MP_BUFFER_READ?
|
2015-05-01 21:34:08 +01:00
|
|
|
if (ret != 0) {
|
2014-04-08 15:32:07 +01:00
|
|
|
goto error;
|
|
|
|
}
|
2015-09-20 09:00:50 +01:00
|
|
|
values[i] = (ffi_arg)(intptr_t)bufinfo.buf;
|
2014-01-28 22:24:00 +00:00
|
|
|
} else if (MP_OBJ_IS_TYPE(a, &fficallback_type)) {
|
|
|
|
mp_obj_fficallback_t *p = a;
|
2015-09-20 09:00:50 +01:00
|
|
|
values[i] = (ffi_arg)(intptr_t)p->func;
|
2014-01-28 22:24:00 +00:00
|
|
|
} else {
|
2014-04-08 15:32:07 +01:00
|
|
|
goto error;
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
valueptrs[i] = &values[i];
|
|
|
|
}
|
|
|
|
|
2014-09-07 00:04:42 +01:00
|
|
|
// If ffi_arg is not big enough to hold a double, then we must pass along a
|
|
|
|
// pointer to a memory location of the correct size.
|
|
|
|
// TODO check if this needs to be done for other types which don't fit into
|
|
|
|
// ffi_arg.
|
2015-01-08 15:41:11 +00:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-09-07 00:04:42 +01:00
|
|
|
if (sizeof(ffi_arg) == 4 && self->rettype == 'd') {
|
|
|
|
double retval;
|
|
|
|
ffi_call(&self->cif, self->func, &retval, valueptrs);
|
|
|
|
return mp_obj_new_float(retval);
|
2015-01-08 15:41:11 +00:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
2014-09-07 00:04:42 +01:00
|
|
|
ffi_arg retval;
|
|
|
|
ffi_call(&self->cif, self->func, &retval, valueptrs);
|
|
|
|
return return_ffi_value(retval, self->rettype);
|
|
|
|
}
|
2014-04-08 15:32:07 +01:00
|
|
|
|
|
|
|
error:
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Don't know how to pass object to native function"));
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC const mp_obj_type_t ffifunc_type = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_ffifunc,
|
2014-01-28 22:24:00 +00:00
|
|
|
.print = ffifunc_print,
|
|
|
|
.call = ffifunc_call,
|
|
|
|
};
|
|
|
|
|
|
|
|
// FFI callback for Python function
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void fficallback_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)kind;
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_fficallback_t *self = self_in;
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_printf(print, "<fficallback %p>", self->func);
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC const mp_obj_type_t fficallback_type = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_fficallback,
|
2014-01-28 22:24:00 +00:00
|
|
|
.print = fficallback_print,
|
|
|
|
};
|
|
|
|
|
|
|
|
// FFI variable
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void ffivar_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 12:47:20 +00:00
|
|
|
(void)kind;
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffivar_t *self = self_in;
|
2014-12-15 00:18:12 +00:00
|
|
|
// Variable value printed as cast to int
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_printf(print, "<ffivar @%p: 0x%x>", self->var, *(int*)self->var);
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC mp_obj_t ffivar_get(mp_obj_t self_in) {
|
2014-02-14 15:16:35 +00:00
|
|
|
mp_obj_ffivar_t *self = self_in;
|
2014-04-11 03:17:41 +01:00
|
|
|
return mp_binary_get_val_array(self->type, self->var, 0);
|
2014-02-14 15:16:35 +00:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(ffivar_get_obj, ffivar_get);
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC mp_obj_t ffivar_set(mp_obj_t self_in, mp_obj_t val_in) {
|
2014-02-14 15:16:35 +00:00
|
|
|
mp_obj_ffivar_t *self = self_in;
|
2014-04-11 03:17:41 +01:00
|
|
|
mp_binary_set_val_array(self->type, self->var, 0, val_in);
|
2014-02-14 15:16:35 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(ffivar_set_obj, ffivar_set);
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC const mp_map_elem_t ffivar_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_get), (mp_obj_t)&ffivar_get_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_set), (mp_obj_t)&ffivar_set_obj },
|
2014-02-14 15:16:35 +00:00
|
|
|
};
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(ffivar_locals_dict, ffivar_locals_dict_table);
|
|
|
|
|
|
|
|
STATIC const mp_obj_type_t ffivar_type = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_ffivar,
|
2014-01-28 22:24:00 +00:00
|
|
|
.print = ffivar_print,
|
2014-03-29 13:48:32 +00:00
|
|
|
.locals_dict = (mp_obj_t)&ffivar_locals_dict,
|
2014-01-28 22:24:00 +00:00
|
|
|
};
|
|
|
|
|
2014-02-15 16:10:44 +00:00
|
|
|
// Generic opaque storage object (unused)
|
2014-01-28 22:24:00 +00:00
|
|
|
|
2014-02-15 16:10:44 +00:00
|
|
|
/*
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC const mp_obj_type_t opaque_type = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_opaqueval,
|
2014-01-28 22:24:00 +00:00
|
|
|
// .print = opaque_print,
|
|
|
|
};
|
2014-02-15 16:10:44 +00:00
|
|
|
*/
|
2014-01-28 22:24:00 +00:00
|
|
|
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mod_ffi_open(mp_uint_t n_args, const mp_obj_t *args) {
|
2014-01-28 22:24:00 +00:00
|
|
|
return ffimod_make_new((mp_obj_t)&ffimod_type, n_args, 0, args);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_ffi_open_obj, 1, 2, mod_ffi_open);
|
|
|
|
|
2014-12-10 22:07:04 +00:00
|
|
|
STATIC mp_obj_t mod_ffi_as_bytearray(mp_obj_t ptr, mp_obj_t size) {
|
2014-12-05 23:13:52 +00:00
|
|
|
return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void*)mp_obj_int_get_truncated(ptr));
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(mod_ffi_as_bytearray_obj, mod_ffi_as_bytearray);
|
|
|
|
|
2014-04-17 22:00:15 +01:00
|
|
|
STATIC const mp_map_elem_t mp_module_ffi_globals_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ffi) },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mod_ffi_open_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_callback), (mp_obj_t)&mod_ffi_callback_obj },
|
2015-02-10 23:20:35 +00:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_func), (mp_obj_t)&mod_ffi_func_obj },
|
2014-04-17 22:00:15 +01:00
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_as_bytearray), (mp_obj_t)&mod_ffi_as_bytearray_obj },
|
|
|
|
};
|
2014-01-28 22:24:00 +00:00
|
|
|
|
2014-11-29 14:39:27 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_ffi_globals, mp_module_ffi_globals_table);
|
2014-04-17 22:00:15 +01:00
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_ffi = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.name = MP_QSTR_ffi,
|
|
|
|
.globals = (mp_obj_dict_t*)&mp_module_ffi_globals,
|
|
|
|
};
|