2014-01-28 22:24:00 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <ffi.h>
|
|
|
|
|
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
|
|
|
#include "runtime.h"
|
2014-02-14 15:16:35 +00:00
|
|
|
#include "binary.h"
|
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;
|
|
|
|
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;
|
|
|
|
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 'p':
|
|
|
|
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)) {
|
|
|
|
uint 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-05 17:32:08 +00:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "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': {
|
|
|
|
const char *s = (const char *)val;
|
|
|
|
return mp_obj_new_str((const byte *)s, strlen(s), false);
|
|
|
|
}
|
|
|
|
case 'v':
|
|
|
|
return mp_const_none;
|
|
|
|
default:
|
|
|
|
return mp_obj_new_int(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FFI module
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC void ffimod_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffimod_t *self = self_in;
|
|
|
|
print(env, "<ffimod %p>", self->handle);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC mp_obj_t ffimod_func(uint n_args, const mp_obj_t *args) {
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffimod_t *self = args[0];
|
|
|
|
const char *rettype = mp_obj_str_get_str(args[1]);
|
|
|
|
const char *symname = mp_obj_str_get_str(args[2]);
|
|
|
|
|
|
|
|
void *sym = dlsym(self->handle, symname);
|
|
|
|
if (sym == NULL) {
|
2014-04-05 17:32:08 +00:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", errno));
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
int nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(args[3]));
|
|
|
|
mp_obj_ffifunc_t *o = m_new_obj_var(mp_obj_ffifunc_t, ffi_type*, nparams);
|
|
|
|
o->base.type = &ffifunc_type;
|
|
|
|
|
|
|
|
o->func = sym;
|
|
|
|
o->rettype = *rettype;
|
|
|
|
|
2014-03-30 12:35:08 +00:00
|
|
|
mp_obj_t iterable = mp_getiter(args[3]);
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_t item;
|
|
|
|
int i = 0;
|
2014-03-30 12:35:08 +00:00
|
|
|
while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
|
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-05 17:32:08 +00:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Error in ffi_prep_cif"));
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ffimod_func_obj, 4, 4, ffimod_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];
|
|
|
|
for (int i = 0; i < cif->nargs; i++) {
|
|
|
|
pyargs[i] = mp_obj_new_int(*(int*)args[i]);
|
|
|
|
}
|
2014-03-30 12:35:08 +00:00
|
|
|
mp_obj_t res = mp_call_function_n_kw(func, cif->nargs, 0, pyargs);
|
2014-01-28 22:24:00 +00:00
|
|
|
|
|
|
|
*(ffi_arg*)ret = mp_obj_int_get(res);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
int nparams = MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(paramtypes_in));
|
|
|
|
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 12:35:08 +00: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-03-30 12:35:08 +00:00
|
|
|
while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
|
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-05 17:32:08 +00:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "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-05 17:32:08 +00:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "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) {
|
2014-04-05 17:32:08 +00:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", errno));
|
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);
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC mp_obj_t ffimod_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
2014-01-28 22:24:00 +00:00
|
|
|
const char *fname = mp_obj_str_get_str(args[0]);
|
|
|
|
void *mod = dlopen(fname, RTLD_NOW | RTLD_LOCAL);
|
|
|
|
|
|
|
|
if (mod == NULL) {
|
2014-04-05 17:32:08 +00:00
|
|
|
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "[Errno %d]", 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 },
|
|
|
|
{ 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
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC void ffifunc_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffifunc_t *self = self_in;
|
|
|
|
print(env, "<ffifunc %p>", self->func);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t ffifunc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
|
|
|
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];
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < n_args; i++) {
|
|
|
|
mp_obj_t a = args[i];
|
|
|
|
if (a == mp_const_none) {
|
|
|
|
values[i] = 0;
|
|
|
|
} else if (MP_OBJ_IS_INT(a)) {
|
|
|
|
values[i] = mp_obj_int_get(a);
|
2014-04-08 14:32:07 +00: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);
|
2014-01-28 23:08:11 +00:00
|
|
|
values[i] = (ffi_arg)s;
|
2014-04-08 14:32:07 +00:00
|
|
|
} else if (((mp_obj_base_t*)a)->type->buffer_p.get_buffer != NULL) {
|
|
|
|
mp_obj_base_t *o = (mp_obj_base_t*)a;
|
|
|
|
buffer_info_t bufinfo;
|
|
|
|
o->type->buffer_p.get_buffer(o, &bufinfo, BUFFER_READ); // TODO: BUFFER_READ?
|
|
|
|
if (bufinfo.buf == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
values[i] = (ffi_arg)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;
|
2014-01-28 23:08:11 +00:00
|
|
|
values[i] = (ffi_arg)p->func;
|
2014-01-28 22:24:00 +00:00
|
|
|
} else {
|
2014-04-08 14:32:07 +00:00
|
|
|
goto error;
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|
|
|
|
valueptrs[i] = &values[i];
|
|
|
|
}
|
|
|
|
|
2014-01-28 23:08:11 +00:00
|
|
|
ffi_arg retval;
|
2014-01-28 22:24:00 +00:00
|
|
|
ffi_call(&self->cif, self->func, &retval, valueptrs);
|
|
|
|
return return_ffi_value(retval, self->rettype);
|
2014-04-08 14:32:07 +00: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
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC void fficallback_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_fficallback_t *self = self_in;
|
|
|
|
print(env, "<fficallback %p>", self->func);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
2014-03-29 13:48:32 +00:00
|
|
|
STATIC void ffivar_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
2014-01-28 22:24:00 +00:00
|
|
|
mp_obj_ffivar_t *self = self_in;
|
|
|
|
print(env, "<ffivar @%p: 0x%x>", self->var, *(int*)self->var);
|
|
|
|
}
|
|
|
|
|
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 02:17:41 +00: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 02:17:41 +00: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
|
|
|
|
|
|
|
mp_obj_t mod_ffi_open(uint n_args, const mp_obj_t *args) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
mp_obj_t mod_ffi_as_bytearray(mp_obj_t ptr, mp_obj_t size) {
|
|
|
|
return mp_obj_new_bytearray_by_ref(mp_obj_int_get(size), (void*)mp_obj_int_get(ptr));
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(mod_ffi_as_bytearray_obj, mod_ffi_as_bytearray);
|
|
|
|
|
|
|
|
|
|
|
|
void ffi_init() {
|
|
|
|
mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("ffi"));
|
2014-03-30 12:35:08 +00:00
|
|
|
mp_store_attr(m, MP_QSTR_open, (mp_obj_t)&mod_ffi_open_obj);
|
|
|
|
mp_store_attr(m, QSTR_FROM_STR_STATIC("callback"), (mp_obj_t)&mod_ffi_callback_obj);
|
2014-01-28 22:24:00 +00:00
|
|
|
// there would be as_bytes, but bytes currently is value, not reference type!
|
2014-03-30 12:35:08 +00:00
|
|
|
mp_store_attr(m, QSTR_FROM_STR_STATIC("as_bytearray"), (mp_obj_t)&mod_ffi_as_bytearray_obj);
|
2014-01-28 22:24:00 +00:00
|
|
|
}
|