diff options
author | Damien George <damien.p.george@gmail.com> | 2014-02-08 18:53:41 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-02-08 18:53:41 +0000 |
commit | 0a587b85fb19e57644ac420c642d0d75d2f9346f (patch) | |
tree | 4ed8d63bde33817b0eed0700a35f80c9f13ef014 | |
parent | 9a58d760c3594aa5c75ad4b6be8b70f719e8a867 (diff) | |
download | micropython-0a587b85fb19e57644ac420c642d0d75d2f9346f.tar.gz micropython-0a587b85fb19e57644ac420c642d0d75d2f9346f.zip |
py: Pass keyword args to native functions by using the stack.
Passing keyword arguments to a native function now no longer requires
heap memory. The kw_args map is created on the stack using the args
array as the table.
-rw-r--r-- | py/objfun.c | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/py/objfun.c b/py/objfun.c index fbc0cab118..9ce517f806 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -54,15 +54,11 @@ mp_obj_t fun_native_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_ if (self->is_kw) { // function allows keywords - // TODO if n_kw==0 then don't allocate any memory for map (either pass NULL or allocate it on the heap) - mp_map_t *kw_args = mp_map_new(n_kw); - for (int i = 0; i < 2 * n_kw; i += 2) { - mp_map_lookup(kw_args, args[n_args + i], MP_MAP_LOOKUP_ADD_IF_NOT_FOUND)->value = args[n_args + i + 1]; - } - mp_obj_t res = ((mp_fun_kw_t)self->fun)(n_args, args, kw_args); - // TODO clean up kw_args + // we create a map directly from the given args array + mp_map_t kw_args; + mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); - return res; + return ((mp_fun_kw_t)self->fun)(n_args, args, &kw_args); } else if (self->n_args_min <= 3 && self->n_args_min == self->n_args_max) { // function requires a fixed number of arguments |