diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-04-08 17:32:07 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-04-08 19:08:34 +0300 |
commit | f898a95cbaa9046da4e50b500d13b1765d67a94d (patch) | |
tree | d5d95e657997ba57c77d9bb07097de96e5533b80 /unix/modffi.c | |
parent | 7a70a3ae9a1052d7669a9c90ca23e820fd4f5d5e (diff) | |
download | micropython-f898a95cbaa9046da4e50b500d13b1765d67a94d.tar.gz micropython-f898a95cbaa9046da4e50b500d13b1765d67a94d.zip |
unix modffi: Support any object implementing buffer protocol as a native arg.
Diffstat (limited to 'unix/modffi.c')
-rw-r--r-- | unix/modffi.c | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/unix/modffi.c b/unix/modffi.c index dd4c7012f7..8764cb7c9e 100644 --- a/unix/modffi.c +++ b/unix/modffi.c @@ -253,14 +253,22 @@ mp_obj_t ffifunc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t * values[i] = 0; } else if (MP_OBJ_IS_INT(a)) { values[i] = mp_obj_int_get(a); - } else if (MP_OBJ_IS_STR(a) || MP_OBJ_IS_TYPE(a, &mp_type_bytes)) { + } else if (MP_OBJ_IS_STR(a)) { const char *s = mp_obj_str_get_str(a); values[i] = (ffi_arg)s; + } 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; } else if (MP_OBJ_IS_TYPE(a, &fficallback_type)) { mp_obj_fficallback_t *p = a; values[i] = (ffi_arg)p->func; } else { - assert(0); + goto error; } valueptrs[i] = &values[i]; } @@ -268,6 +276,9 @@ mp_obj_t ffifunc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t * ffi_arg retval; ffi_call(&self->cif, self->func, &retval, valueptrs); return return_ffi_value(retval, self->rettype); + +error: + nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Don't know how to pass object to native function")); } STATIC const mp_obj_type_t ffifunc_type = { |