diff options
Diffstat (limited to 'py/runtime.c')
-rw-r--r-- | py/runtime.c | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/py/runtime.c b/py/runtime.c index ed01a5be08..4898864962 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -7,6 +7,7 @@ #include "mpconfig.h" #include "qstr.h" #include "obj.h" +#include "objtuple.h" #include "objmodule.h" #include "parsenum.h" #include "runtime0.h" @@ -16,6 +17,7 @@ #include "builtintables.h" #include "bc.h" #include "intdivmod.h" +#include "objgenerator.h" #if 0 // print debugging info #define DEBUG_PRINT (1) @@ -503,6 +505,148 @@ mp_obj_t mp_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) { return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust); } +mp_obj_t mp_call_method_n_kw_var(bool have_self, uint n_args_n_kw, const mp_obj_t *args, mp_obj_t pos_seq, mp_obj_t kw_dict) { + mp_obj_t fun = *args++; + mp_obj_t self = MP_OBJ_NULL; + if (have_self) { + self = *args++; // may be MP_OBJ_NULL + } + uint n_args = n_args_n_kw & 0xff; + uint n_kw = (n_args_n_kw >> 8) & 0xff; + + DEBUG_OP_printf("call method var (fun=%p, self=%p, n_args=%u, n_kw=%u, args=%p, seq=%p, dict=%p)\n", fun, self, n_args, n_kw, args, pos_seq, kw_dict); + + // We need to create the following array of objects: + // args[0 .. n_args] unpacked(pos_seq) args[n_args .. n_args + 2 * n_kw] unpacked(kw_dict) + // TODO: optimize one day to avoid constructing new arg array? Will be hard. + + // The new args array + mp_obj_t *args2; + uint args2_alloc; + uint args2_len = 0; + + // Try to get a hint for the size of the kw_dict + uint kw_dict_len = 0; + if (kw_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) { + kw_dict_len = mp_obj_dict_len(kw_dict); + } + + // Extract the pos_seq sequence to the new args array. + // Note that it can be arbitrary iterator. + if (pos_seq == MP_OBJ_NULL) { + // no sequence + + // allocate memory for the new array of args + args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len); + args2 = m_new(mp_obj_t, args2_alloc); + + // copy the self + if (self != MP_OBJ_NULL) { + args2[args2_len++] = self; + } + + // copy the fixed pos args + m_seq_copy(args2 + args2_len, args, n_args, mp_obj_t); + args2_len += n_args; + + } else if (MP_OBJ_IS_TYPE(pos_seq, &mp_type_tuple) || MP_OBJ_IS_TYPE(pos_seq, &mp_type_list)) { + // optimise the case of a tuple and list + + // get the items + uint len; + mp_obj_t *items; + mp_obj_get_array(pos_seq, &len, &items); + + // allocate memory for the new array of args + args2_alloc = 1 + n_args + len + 2 * (n_kw + kw_dict_len); + args2 = m_new(mp_obj_t, args2_alloc); + + // copy the self + if (self != MP_OBJ_NULL) { + args2[args2_len++] = self; + } + + // copy the fixed and variable position args + m_seq_cat(args2 + args2_len, args, n_args, items, len, mp_obj_t); + args2_len += n_args + len; + + } else { + // generic iterator + + // allocate memory for the new array of args + args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len) + 3; + args2 = m_new(mp_obj_t, args2_alloc); + + // copy the self + if (self != MP_OBJ_NULL) { + args2[args2_len++] = self; + } + + // copy the fixed position args + m_seq_copy(args2 + args2_len, args, n_args, mp_obj_t); + + // extract the variable position args from the iterator + mp_obj_t iterable = mp_getiter(pos_seq); + mp_obj_t item; + while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) { + if (args2_len >= args2_alloc) { + args2 = m_renew(mp_obj_t, args2, args2_alloc, args2_alloc * 2); + args2_alloc *= 2; + } + args2[args2_len++] = item; + } + } + + // The size of the args2 array now is the number of positional args. + uint pos_args_len = args2_len; + + // Copy the fixed kw args. + m_seq_copy(args2 + args2_len, args + n_args, 2 * n_kw, mp_obj_t); + args2_len += 2 * n_kw; + + // Extract (key,value) pairs from kw_dict dictionary and append to args2. + // Note that it can be arbitrary iterator. + if (kw_dict == MP_OBJ_NULL) { + // pass + } else if (MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) { + // dictionary + mp_map_t *map = mp_obj_dict_get_map(kw_dict); + assert(args2_len + 2 * map->used <= args2_alloc); // should have enough, since kw_dict_len is in this case hinted correctly above + for (uint i = 0; i < map->alloc; i++) { + if (map->table[i].key != MP_OBJ_NULL) { + args2[args2_len++] = map->table[i].key; + args2[args2_len++] = map->table[i].value; + } + } + } else { + // generic mapping + // TODO is calling 'items' on the mapping the correct thing to do here? + mp_obj_t dest[2]; + mp_load_method(kw_dict, MP_QSTR_items, dest); + mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest)); + mp_obj_t item; + while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) { + if (args2_len + 1 >= args2_alloc) { + uint new_alloc = args2_alloc * 2; + if (new_alloc < 4) { + new_alloc = 4; + } + args2 = m_renew(mp_obj_t, args2, args2_alloc, new_alloc); + args2_alloc = new_alloc; + } + mp_obj_t *items; + mp_obj_get_array_fixed_n(item, 2, &items); + args2[args2_len++] = items[0]; + args2[args2_len++] = items[1]; + } + } + + mp_obj_t res = mp_call_function_n_kw(fun, pos_args_len, (args2_len - pos_args_len) / 2, args2); + m_del(mp_obj_t, args2, args2_alloc); + + return res; +} + mp_obj_t mp_build_tuple(int n_args, mp_obj_t *items) { return mp_obj_new_tuple(n_args, items); } @@ -760,6 +904,62 @@ mp_obj_t mp_iternext(mp_obj_t o_in) { } } +// TODO: Unclear what to do with StopIterarion exception here. +mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) { + mp_obj_type_t *type = mp_obj_get_type(self_in); + + if (type == &mp_type_gen_instance) { + return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val); + } + + if (type->iternext != NULL && send_value == mp_const_none) { + mp_obj_t ret = type->iternext(self_in); + if (ret != MP_OBJ_NULL) { + *ret_val = ret; + return MP_VM_RETURN_YIELD; + } else { + // Emulate raise StopIteration() + // Special case, handled in vm.c + *ret_val = MP_OBJ_NULL; + return MP_VM_RETURN_NORMAL; + } + } + + mp_obj_t dest[3]; // Reserve slot for send() arg + + if (send_value == mp_const_none) { + mp_load_method_maybe(self_in, MP_QSTR___next__, dest); + if (dest[0] != MP_OBJ_NULL) { + *ret_val = mp_call_method_n_kw(0, 0, dest); + return MP_VM_RETURN_YIELD; + } + } + + if (send_value != MP_OBJ_NULL) { + mp_load_method(self_in, MP_QSTR_send, dest); + dest[2] = send_value; + *ret_val = mp_call_method_n_kw(1, 0, dest); + return MP_VM_RETURN_YIELD; + } + + if (throw_value != MP_OBJ_NULL) { + if (mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) { + mp_load_method_maybe(self_in, MP_QSTR_close, dest); + if (dest[0] != MP_OBJ_NULL) { + *ret_val = mp_call_method_n_kw(0, 0, dest); + // We assume one can't "yield" from close() + return MP_VM_RETURN_NORMAL; + } + } + mp_load_method(self_in, MP_QSTR_throw, dest); + *ret_val = mp_call_method_n_kw(1, 0, &throw_value); + return MP_VM_RETURN_YIELD; + } + + assert(0); + return MP_VM_RETURN_NORMAL; // Should be unreachable +} + mp_obj_t mp_make_raise_obj(mp_obj_t o) { DEBUG_printf("raise %p\n", o); if (mp_obj_is_exception_type(o)) { |