2013-12-21 18:17:45 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
2014-01-21 21:40:13 +00:00
|
|
|
#include "qstr.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "obj.h"
|
2014-03-26 21:47:19 +00:00
|
|
|
#include "map.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
#include "runtime.h"
|
|
|
|
#include "bc.h"
|
2014-03-23 19:48:29 +00:00
|
|
|
#include "objgenerator.h"
|
2013-12-21 18:17:45 +00:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* generator wrapper */
|
|
|
|
|
|
|
|
typedef struct _mp_obj_gen_wrap_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t *fun;
|
|
|
|
} mp_obj_gen_wrap_t;
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_gen_wrap_t *self = self_in;
|
|
|
|
mp_obj_t self_fun = self->fun;
|
|
|
|
assert(MP_OBJ_IS_TYPE(self_fun, &fun_bc_type));
|
|
|
|
int bc_n_args;
|
|
|
|
uint bc_n_state;
|
|
|
|
const byte *bc_code;
|
|
|
|
mp_obj_fun_bc_get(self_fun, &bc_n_args, &bc_n_state, &bc_code);
|
|
|
|
if (n_args != bc_n_args) {
|
2014-02-15 16:10:44 +00:00
|
|
|
nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "function takes %d positional arguments but %d were given", bc_n_args, n_args));
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
2014-01-18 14:10:48 +00:00
|
|
|
if (n_kw != 0) {
|
2014-02-15 16:10:44 +00:00
|
|
|
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "function does not take keyword arguments"));
|
2014-01-18 14:10:48 +00:00
|
|
|
}
|
2013-12-30 22:32:17 +00:00
|
|
|
|
2014-01-29 20:30:52 +00:00
|
|
|
return mp_obj_new_gen_instance(bc_code, bc_n_state, n_args, args);
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const mp_obj_type_t gen_wrap_type = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_generator,
|
2014-01-18 14:10:48 +00:00
|
|
|
.call = gen_wrap_call,
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|
|
|
|
|
2014-01-29 20:30:52 +00:00
|
|
|
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
|
|
|
|
o->base.type = &gen_wrap_type;
|
|
|
|
o->fun = fun;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* generator instance */
|
|
|
|
|
|
|
|
typedef struct _mp_obj_gen_instance_t {
|
|
|
|
mp_obj_base_t base;
|
2014-01-18 23:24:36 +00:00
|
|
|
const byte *code_info;
|
2013-12-21 18:17:45 +00:00
|
|
|
const byte *ip;
|
|
|
|
mp_obj_t *sp;
|
2014-03-22 20:53:53 +00:00
|
|
|
// bit 0 is saved currently_in_except_block value
|
2014-03-22 11:49:31 +00:00
|
|
|
mp_exc_stack *exc_sp;
|
2014-01-18 14:10:48 +00:00
|
|
|
uint n_state;
|
2014-03-22 20:53:53 +00:00
|
|
|
// Variable-length
|
|
|
|
mp_obj_t state[0];
|
|
|
|
// Variable-length, never accessed by name, only as (void*)(state + n_state)
|
|
|
|
mp_exc_stack exc_state[0];
|
2013-12-21 18:17:45 +00:00
|
|
|
} mp_obj_gen_instance_t;
|
|
|
|
|
2014-01-13 17:19:16 +00:00
|
|
|
void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
2013-12-21 18:17:45 +00:00
|
|
|
print(env, "<generator object 'fun-name' at %p>", self_in);
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t gen_instance_getiter(mp_obj_t self_in) {
|
|
|
|
return self_in;
|
|
|
|
}
|
|
|
|
|
2014-03-26 19:33:23 +00:00
|
|
|
mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
|
2013-12-21 18:17:45 +00:00
|
|
|
mp_obj_gen_instance_t *self = self_in;
|
2014-01-26 23:01:37 +00:00
|
|
|
if (self->ip == 0) {
|
2014-03-26 19:33:23 +00:00
|
|
|
*ret_val = MP_OBJ_NULL;
|
|
|
|
return MP_VM_RETURN_NORMAL;
|
2014-01-26 23:01:37 +00:00
|
|
|
}
|
2014-01-26 18:50:11 +00:00
|
|
|
if (self->sp == self->state - 1) {
|
|
|
|
if (send_value != mp_const_none) {
|
2014-02-15 16:10:44 +00:00
|
|
|
nlr_jump(mp_obj_new_exception_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"));
|
2014-01-26 18:50:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*self->sp = send_value;
|
|
|
|
}
|
2014-03-26 19:33:23 +00:00
|
|
|
mp_vm_return_kind_t ret_kind = mp_execute_byte_code_2(self->code_info, &self->ip,
|
2014-03-22 15:50:12 +00:00
|
|
|
&self->state[self->n_state - 1], &self->sp, (mp_exc_stack*)(self->state + self->n_state),
|
|
|
|
&self->exc_sp, throw_value);
|
2014-03-23 19:48:29 +00:00
|
|
|
|
2014-03-26 19:33:23 +00:00
|
|
|
switch (ret_kind) {
|
2014-02-15 22:55:00 +00:00
|
|
|
case MP_VM_RETURN_NORMAL:
|
|
|
|
// Explicitly mark generator as completed. If we don't do this,
|
|
|
|
// subsequent next() may re-execute statements after last yield
|
|
|
|
// again and again, leading to side effects.
|
|
|
|
// TODO: check how return with value behaves under such conditions
|
|
|
|
// in CPython.
|
|
|
|
self->ip = 0;
|
2014-03-26 19:33:23 +00:00
|
|
|
*ret_val = *self->sp;
|
|
|
|
break;
|
2014-03-23 19:48:29 +00:00
|
|
|
|
|
|
|
case MP_VM_RETURN_YIELD:
|
2014-03-26 19:33:23 +00:00
|
|
|
*ret_val = *self->sp;
|
|
|
|
break;
|
2014-03-23 19:48:29 +00:00
|
|
|
|
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
|
|
|
self->ip = 0;
|
2014-03-26 19:33:23 +00:00
|
|
|
*ret_val = self->state[self->n_state - 1];
|
|
|
|
break;
|
2014-03-23 19:48:29 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
2014-03-26 19:33:23 +00:00
|
|
|
*ret_val = mp_const_none;
|
|
|
|
break;
|
2014-03-23 19:48:29 +00:00
|
|
|
}
|
2014-03-26 19:33:23 +00:00
|
|
|
|
|
|
|
return ret_kind;
|
2014-03-23 19:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) {
|
2014-03-26 19:33:23 +00:00
|
|
|
mp_obj_t ret;
|
|
|
|
switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
|
2014-03-23 19:48:29 +00:00
|
|
|
case MP_VM_RETURN_NORMAL:
|
|
|
|
// Optimize return w/o value in case generator is used in for loop
|
|
|
|
if (ret == mp_const_none) {
|
2014-03-26 19:27:58 +00:00
|
|
|
return MP_OBJ_NULL;
|
2014-02-15 22:55:00 +00:00
|
|
|
} else {
|
2014-03-23 19:48:29 +00:00
|
|
|
nlr_jump(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
|
2014-02-15 22:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case MP_VM_RETURN_YIELD:
|
2014-03-23 19:48:29 +00:00
|
|
|
return ret;
|
2014-02-15 22:55:00 +00:00
|
|
|
|
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
2014-03-23 19:48:29 +00:00
|
|
|
nlr_jump(ret);
|
2014-03-22 14:44:58 +00:00
|
|
|
|
2014-02-15 22:55:00 +00:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return mp_const_none;
|
2013-12-21 18:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
2014-01-26 18:50:11 +00:00
|
|
|
|
|
|
|
mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
|
2014-03-23 19:48:29 +00:00
|
|
|
return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
|
2014-01-26 23:01:37 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
|
2014-03-23 19:48:29 +00:00
|
|
|
mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
|
2014-03-26 19:27:58 +00:00
|
|
|
if (ret == MP_OBJ_NULL) {
|
2014-02-15 16:10:44 +00:00
|
|
|
nlr_jump(mp_obj_new_exception(&mp_type_StopIteration));
|
|
|
|
} else {
|
|
|
|
return ret;
|
2014-01-26 23:01:37 +00:00
|
|
|
}
|
2014-01-26 18:50:11 +00:00
|
|
|
}
|
2014-02-15 16:10:44 +00:00
|
|
|
|
2014-02-12 16:15:40 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
|
2014-01-26 18:50:11 +00:00
|
|
|
|
2014-03-22 15:50:12 +00:00
|
|
|
STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) {
|
2014-03-23 19:48:29 +00:00
|
|
|
mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, n_args == 2 ? args[1] : args[2]);
|
2014-03-26 19:27:58 +00:00
|
|
|
if (ret == MP_OBJ_NULL) {
|
2014-03-22 15:50:12 +00:00
|
|
|
nlr_jump(mp_obj_new_exception(&mp_type_StopIteration));
|
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
|
|
|
|
|
2014-03-23 19:48:29 +00:00
|
|
|
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
|
2014-03-26 19:33:23 +00:00
|
|
|
mp_obj_t ret;
|
|
|
|
switch (mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_type_GeneratorExit, &ret)) {
|
|
|
|
case MP_VM_RETURN_YIELD:
|
|
|
|
nlr_jump(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
|
2014-03-23 19:48:29 +00:00
|
|
|
|
2014-03-26 19:33:23 +00:00
|
|
|
// Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
|
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
|
|
|
// ret should always be an instance of an exception class
|
|
|
|
if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_GeneratorExit) ||
|
|
|
|
mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
nlr_jump(ret);
|
|
|
|
|
|
|
|
default:
|
|
|
|
// The only choice left is MP_VM_RETURN_NORMAL which is successful close
|
2014-03-23 19:48:29 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
|
2014-03-22 15:50:12 +00:00
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC const mp_map_elem_t gen_instance_locals_dict_table[] = {
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&gen_instance_close_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&gen_instance_send_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_throw), (mp_obj_t)&gen_instance_throw_obj },
|
2014-01-26 18:50:11 +00:00
|
|
|
};
|
2013-12-21 18:17:45 +00:00
|
|
|
|
2014-03-26 21:47:19 +00:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
const mp_obj_type_t gen_instance_type = {
|
2014-02-15 16:10:44 +00:00
|
|
|
{ &mp_type_type },
|
2014-02-15 11:34:50 +00:00
|
|
|
.name = MP_QSTR_generator,
|
2014-01-07 15:58:30 +00:00
|
|
|
.print = gen_instance_print,
|
|
|
|
.getiter = gen_instance_getiter,
|
|
|
|
.iternext = gen_instance_iternext,
|
2014-03-26 21:47:19 +00:00
|
|
|
.locals_dict = (mp_obj_t)&gen_instance_locals_dict,
|
2013-12-21 18:17:45 +00:00
|
|
|
};
|
|
|
|
|
2014-01-02 20:57:05 +00:00
|
|
|
mp_obj_t mp_obj_new_gen_instance(const byte *bytecode, uint n_state, int n_args, const mp_obj_t *args) {
|
2014-03-22 11:49:31 +00:00
|
|
|
// TODO: 4 is hardcoded number from vm.c, calc exc stack size instead.
|
|
|
|
mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte, n_state * sizeof(mp_obj_t) + 4 * sizeof(mp_exc_stack));
|
2013-12-21 18:17:45 +00:00
|
|
|
o->base.type = &gen_instance_type;
|
2014-01-18 23:24:36 +00:00
|
|
|
o->code_info = bytecode;
|
2014-01-02 20:57:05 +00:00
|
|
|
o->ip = bytecode;
|
2014-01-18 14:10:48 +00:00
|
|
|
o->sp = &o->state[0] - 1; // sp points to top of stack, which starts off 1 below the state
|
2014-03-22 11:49:31 +00:00
|
|
|
o->exc_sp = (mp_exc_stack*)(o->state + n_state) - 1;
|
2014-01-18 14:10:48 +00:00
|
|
|
o->n_state = n_state;
|
2014-01-02 20:57:05 +00:00
|
|
|
|
2014-01-18 14:10:48 +00:00
|
|
|
// copy args to end of state array, in reverse (that's how mp_execute_byte_code_2 needs it)
|
2014-01-02 20:57:05 +00:00
|
|
|
for (int i = 0; i < n_args; i++) {
|
2014-01-18 14:10:48 +00:00
|
|
|
o->state[n_state - 1 - i] = args[i];
|
2014-01-02 20:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
// prelude for making cells (closed over variables)
|
|
|
|
// for now we just make sure there are no cells variables
|
|
|
|
// need to work out how to implement closed over variables in generators
|
2014-01-18 23:24:36 +00:00
|
|
|
|
|
|
|
// get code info size
|
|
|
|
machine_uint_t code_info_size = bytecode[0] | (bytecode[1] << 8) | (bytecode[2] << 16) | (bytecode[3] << 24);
|
|
|
|
o->ip += code_info_size;
|
2014-01-02 20:57:05 +00:00
|
|
|
assert(o->ip[0] == 0);
|
|
|
|
o->ip += 1;
|
|
|
|
|
2013-12-21 18:17:45 +00:00
|
|
|
return o;
|
|
|
|
}
|