2013-12-21 13:17:45 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2014-05-02 10:47:01 -04:00
|
|
|
#include "mpconfig.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "nlr.h"
|
|
|
|
#include "misc.h"
|
2014-01-21 16:40:13 -05:00
|
|
|
#include "qstr.h"
|
2013-12-21 13:17:45 -05:00
|
|
|
#include "obj.h"
|
|
|
|
#include "runtime.h"
|
|
|
|
#include "bc.h"
|
2014-03-23 15:48:29 -04:00
|
|
|
#include "objgenerator.h"
|
2014-04-16 22:49:47 -04:00
|
|
|
#include "objfun.h"
|
2013-12-21 13:17:45 -05: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-04-16 22:49:47 -04:00
|
|
|
mp_obj_t mp_obj_new_gen_instance(mp_obj_dict_t *globals, const byte *bytecode, uint n_args, const mp_obj_t *args,
|
|
|
|
uint n_args2, const mp_obj_t *args2);
|
2014-03-30 13:21:28 -04:00
|
|
|
|
2014-02-12 11:15:40 -05: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 13:17:45 -05:00
|
|
|
mp_obj_gen_wrap_t *self = self_in;
|
2014-04-16 22:49:47 -04:00
|
|
|
mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t*)self->fun;
|
2014-03-29 09:43:38 -04:00
|
|
|
assert(MP_OBJ_IS_TYPE(self_fun, &mp_type_fun_bc));
|
2013-12-30 17:32:17 -05:00
|
|
|
|
2014-03-30 13:21:28 -04:00
|
|
|
const mp_obj_t *args1, *args2;
|
|
|
|
uint len1, len2;
|
2014-03-30 16:55:28 -04:00
|
|
|
if (!mp_obj_fun_prepare_simple_args(self_fun, n_args, n_kw, args, &len1, &args1, &len2, &args2)) {
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-03-30 13:21:28 -04:00
|
|
|
|
2014-04-17 14:16:11 -04:00
|
|
|
return mp_obj_new_gen_instance(self_fun->globals, self_fun->bytecode, len1, args1, len2, args2);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
2014-03-29 09:43:38 -04:00
|
|
|
const mp_obj_type_t mp_type_gen_wrap = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_generator,
|
2014-01-18 09:10:48 -05:00
|
|
|
.call = gen_wrap_call,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2014-01-29 15:30:52 -05:00
|
|
|
mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
|
2014-03-29 09:43:38 -04:00
|
|
|
o->base.type = &mp_type_gen_wrap;
|
2013-12-21 13:17:45 -05:00
|
|
|
o->fun = fun;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/* generator instance */
|
|
|
|
|
|
|
|
typedef struct _mp_obj_gen_instance_t {
|
|
|
|
mp_obj_base_t base;
|
2014-04-16 22:49:47 -04:00
|
|
|
mp_obj_dict_t *globals;
|
2014-01-18 18:24:36 -05:00
|
|
|
const byte *code_info;
|
2013-12-21 13:17:45 -05:00
|
|
|
const byte *ip;
|
|
|
|
mp_obj_t *sp;
|
2014-03-22 16:53:53 -04:00
|
|
|
// bit 0 is saved currently_in_except_block value
|
2014-03-29 20:57:09 -04:00
|
|
|
mp_exc_stack_t *exc_sp;
|
2014-01-18 09:10:48 -05:00
|
|
|
uint n_state;
|
2014-03-22 16:53:53 -04:00
|
|
|
// Variable-length
|
|
|
|
mp_obj_t state[0];
|
|
|
|
// Variable-length, never accessed by name, only as (void*)(state + n_state)
|
2014-03-29 20:57:09 -04:00
|
|
|
mp_exc_stack_t exc_state[0];
|
2013-12-21 13:17:45 -05:00
|
|
|
} mp_obj_gen_instance_t;
|
|
|
|
|
2014-01-13 12:19:16 -05:00
|
|
|
void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
|
2014-05-01 15:20:07 -04:00
|
|
|
mp_obj_gen_instance_t *self = self_in;
|
|
|
|
print(env, "<generator object '%s' at %p>", mp_obj_code_get_name(self->code_info), self_in);
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t gen_instance_getiter(mp_obj_t self_in) {
|
|
|
|
return self_in;
|
|
|
|
}
|
|
|
|
|
2014-03-26 15:33:23 -04: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) {
|
2014-03-29 20:32:30 -04:00
|
|
|
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
|
2013-12-21 13:17:45 -05:00
|
|
|
mp_obj_gen_instance_t *self = self_in;
|
2014-01-26 18:01:37 -05:00
|
|
|
if (self->ip == 0) {
|
2014-04-17 18:19:36 -04:00
|
|
|
*ret_val = MP_OBJ_STOP_ITERATION;
|
2014-03-26 15:33:23 -04:00
|
|
|
return MP_VM_RETURN_NORMAL;
|
2014-01-26 18:01:37 -05:00
|
|
|
}
|
2014-01-26 13:50:11 -05:00
|
|
|
if (self->sp == self->state - 1) {
|
|
|
|
if (send_value != mp_const_none) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"));
|
2014-01-26 13:50:11 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*self->sp = send_value;
|
|
|
|
}
|
2014-04-16 22:49:47 -04:00
|
|
|
mp_obj_dict_t *old_globals = mp_globals_get();
|
|
|
|
mp_globals_set(self->globals);
|
2014-03-26 15:33:23 -04:00
|
|
|
mp_vm_return_kind_t ret_kind = mp_execute_byte_code_2(self->code_info, &self->ip,
|
2014-03-29 20:57:09 -04:00
|
|
|
&self->state[self->n_state - 1], &self->sp, (mp_exc_stack_t*)(self->state + self->n_state),
|
2014-03-22 11:50:12 -04:00
|
|
|
&self->exc_sp, throw_value);
|
2014-04-16 22:49:47 -04:00
|
|
|
mp_globals_set(old_globals);
|
2014-03-23 15:48:29 -04:00
|
|
|
|
2014-03-26 15:33:23 -04:00
|
|
|
switch (ret_kind) {
|
2014-02-15 17:55:00 -05: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 15:33:23 -04:00
|
|
|
*ret_val = *self->sp;
|
|
|
|
break;
|
2014-03-23 15:48:29 -04:00
|
|
|
|
|
|
|
case MP_VM_RETURN_YIELD:
|
2014-03-26 15:33:23 -04:00
|
|
|
*ret_val = *self->sp;
|
|
|
|
break;
|
2014-03-23 15:48:29 -04:00
|
|
|
|
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
|
|
|
self->ip = 0;
|
2014-03-26 15:33:23 -04:00
|
|
|
*ret_val = self->state[self->n_state - 1];
|
|
|
|
break;
|
2014-03-23 15:48:29 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
2014-03-26 15:33:23 -04:00
|
|
|
*ret_val = mp_const_none;
|
|
|
|
break;
|
2014-03-23 15:48:29 -04:00
|
|
|
}
|
2014-03-26 15:33:23 -04:00
|
|
|
|
|
|
|
return ret_kind;
|
2014-03-23 15:48:29 -04: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 15:33:23 -04:00
|
|
|
mp_obj_t ret;
|
|
|
|
switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
|
2014-03-23 15:48:29 -04:00
|
|
|
case MP_VM_RETURN_NORMAL:
|
|
|
|
// Optimize return w/o value in case generator is used in for loop
|
2014-04-17 18:19:36 -04:00
|
|
|
if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
|
|
|
|
return MP_OBJ_STOP_ITERATION;
|
2014-02-15 17:55:00 -05:00
|
|
|
} else {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
|
2014-02-15 17:55:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case MP_VM_RETURN_YIELD:
|
2014-03-30 21:09:53 -04:00
|
|
|
if (throw_value != MP_OBJ_NULL && mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
|
2014-03-30 21:09:53 -04:00
|
|
|
}
|
2014-03-23 15:48:29 -04:00
|
|
|
return ret;
|
2014-02-15 17:55:00 -05:00
|
|
|
|
|
|
|
case MP_VM_RETURN_EXCEPTION:
|
2014-04-17 18:19:36 -04:00
|
|
|
// TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
|
2014-03-30 16:14:55 -04:00
|
|
|
// of mp_iternext() protocol, but this function is called by other methods
|
2014-04-17 18:19:36 -04:00
|
|
|
// too, which may not handled MP_OBJ_STOP_ITERATION.
|
2014-03-30 16:14:55 -04:00
|
|
|
if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
|
2014-04-17 18:19:36 -04:00
|
|
|
return MP_OBJ_STOP_ITERATION;
|
2014-03-30 16:14:55 -04:00
|
|
|
} else {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(ret);
|
2014-03-30 16:14:55 -04:00
|
|
|
}
|
2014-03-22 10:44:58 -04:00
|
|
|
|
2014-02-15 17:55:00 -05:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return mp_const_none;
|
2013-12-21 13:17:45 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-26 13:50:11 -05:00
|
|
|
|
|
|
|
mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
|
2014-03-23 15:48:29 -04:00
|
|
|
return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
|
2014-01-26 18:01:37 -05:00
|
|
|
}
|
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
|
2014-03-23 15:48:29 -04:00
|
|
|
mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
|
2014-04-17 18:19:36 -04:00
|
|
|
if (ret == MP_OBJ_STOP_ITERATION) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
|
2014-02-15 11:10:44 -05:00
|
|
|
} else {
|
|
|
|
return ret;
|
2014-01-26 18:01:37 -05:00
|
|
|
}
|
2014-01-26 13:50:11 -05:00
|
|
|
}
|
2014-02-15 11:10:44 -05:00
|
|
|
|
2014-02-12 11:15:40 -05:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
|
2014-01-26 13:50:11 -05:00
|
|
|
|
2014-03-30 06:13:12 -04:00
|
|
|
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in);
|
2014-03-22 11:50:12 -04:00
|
|
|
STATIC mp_obj_t gen_instance_throw(uint n_args, const mp_obj_t *args) {
|
2014-03-30 06:13:12 -04:00
|
|
|
mp_obj_t exc = (n_args == 2) ? args[1] : args[2];
|
2014-03-30 08:35:08 -04:00
|
|
|
exc = mp_make_raise_obj(exc);
|
2014-03-30 06:13:12 -04:00
|
|
|
|
|
|
|
mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
|
2014-04-17 18:19:36 -04:00
|
|
|
if (ret == MP_OBJ_STOP_ITERATION) {
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
|
2014-03-22 11:50:12 -04:00
|
|
|
} else {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
|
|
|
|
|
2014-03-23 15:48:29 -04:00
|
|
|
STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
|
2014-03-26 15:33:23 -04:00
|
|
|
mp_obj_t ret;
|
2014-03-29 09:15:08 -04:00
|
|
|
switch (mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_const_GeneratorExit_obj, &ret)) {
|
2014-03-26 15:33:23 -04:00
|
|
|
case MP_VM_RETURN_YIELD:
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
|
2014-03-23 15:48:29 -04:00
|
|
|
|
2014-03-26 15:33:23 -04: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;
|
|
|
|
}
|
2014-04-05 13:32:08 -04:00
|
|
|
nlr_raise(ret);
|
2014-03-26 15:33:23 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
// The only choice left is MP_VM_RETURN_NORMAL which is successful close
|
2014-03-23 15:48:29 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
|
2014-03-22 11:50:12 -04:00
|
|
|
|
2014-03-26 17:47:19 -04: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 13:50:11 -05:00
|
|
|
};
|
2013-12-21 13:17:45 -05:00
|
|
|
|
2014-03-26 17:47:19 -04:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
|
|
|
|
|
2014-03-29 09:43:38 -04:00
|
|
|
const mp_obj_type_t mp_type_gen_instance = {
|
2014-02-15 11:10:44 -05:00
|
|
|
{ &mp_type_type },
|
2014-02-15 06:34:50 -05:00
|
|
|
.name = MP_QSTR_generator,
|
2014-01-07 10:58:30 -05:00
|
|
|
.print = gen_instance_print,
|
|
|
|
.getiter = gen_instance_getiter,
|
|
|
|
.iternext = gen_instance_iternext,
|
2014-03-26 17:47:19 -04:00
|
|
|
.locals_dict = (mp_obj_t)&gen_instance_locals_dict,
|
2013-12-21 13:17:45 -05:00
|
|
|
};
|
|
|
|
|
2014-04-16 22:49:47 -04:00
|
|
|
mp_obj_t mp_obj_new_gen_instance(mp_obj_dict_t *globals, const byte *bytecode, uint n_args, const mp_obj_t *args,
|
|
|
|
uint n_args2, const mp_obj_t *args2) {
|
2014-03-29 20:33:08 -04:00
|
|
|
const byte *code_info = bytecode;
|
2014-03-27 06:55:21 -04:00
|
|
|
// get code info size, and skip the line number table
|
|
|
|
machine_uint_t code_info_size = bytecode[0] | (bytecode[1] << 8) | (bytecode[2] << 16) | (bytecode[3] << 24);
|
|
|
|
bytecode += code_info_size;
|
|
|
|
|
2014-03-27 07:07:04 -04:00
|
|
|
// bytecode prelude: get state size and exception stack size
|
|
|
|
machine_uint_t n_state = bytecode[0] | (bytecode[1] << 8);
|
|
|
|
machine_uint_t n_exc_stack = bytecode[2] | (bytecode[3] << 8);
|
|
|
|
bytecode += 4;
|
2014-03-27 06:55:21 -04:00
|
|
|
|
2014-04-09 14:01:45 -04:00
|
|
|
// allocate the generator object, with room for local stack and exception stack
|
2014-03-29 20:57:09 -04:00
|
|
|
mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte, n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
|
2014-03-29 09:43:38 -04:00
|
|
|
o->base.type = &mp_type_gen_instance;
|
2014-04-16 22:49:47 -04:00
|
|
|
o->globals = globals;
|
2014-03-29 20:33:08 -04:00
|
|
|
o->code_info = code_info;
|
2014-01-18 09:10:48 -05:00
|
|
|
o->sp = &o->state[0] - 1; // sp points to top of stack, which starts off 1 below the state
|
2014-03-29 20:57:09 -04:00
|
|
|
o->exc_sp = (mp_exc_stack_t*)(o->state + n_state) - 1;
|
2014-01-18 09:10:48 -05:00
|
|
|
o->n_state = n_state;
|
2014-01-02 15:57:05 -05:00
|
|
|
|
2014-01-18 09:10:48 -05:00
|
|
|
// copy args to end of state array, in reverse (that's how mp_execute_byte_code_2 needs it)
|
2014-03-30 13:21:28 -04:00
|
|
|
for (uint i = 0; i < n_args; i++) {
|
2014-01-18 09:10:48 -05:00
|
|
|
o->state[n_state - 1 - i] = args[i];
|
2014-01-02 15:57:05 -05:00
|
|
|
}
|
2014-03-30 13:21:28 -04:00
|
|
|
for (uint i = 0; i < n_args2; i++) {
|
|
|
|
o->state[n_state - 1 - n_args - i] = args2[i];
|
|
|
|
}
|
2014-01-02 15:57:05 -05:00
|
|
|
|
2014-04-09 14:53:31 -04:00
|
|
|
// set rest of state to MP_OBJ_NULL
|
|
|
|
for (uint i = 0; i < n_state - n_args - n_args2; i++) {
|
|
|
|
o->state[i] = MP_OBJ_NULL;
|
|
|
|
}
|
|
|
|
|
2014-04-09 14:01:45 -04:00
|
|
|
// bytecode prelude: initialise closed over variables
|
|
|
|
for (uint n_local = *bytecode++; n_local > 0; n_local--) {
|
|
|
|
uint local_num = *bytecode++;
|
2014-04-09 14:53:31 -04:00
|
|
|
o->state[n_state - 1 - local_num] = mp_obj_new_cell(o->state[n_state - 1 - local_num]);
|
2014-04-09 14:01:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// set ip to start of actual byte code
|
|
|
|
o->ip = bytecode;
|
|
|
|
|
2013-12-21 13:17:45 -05:00
|
|
|
return o;
|
|
|
|
}
|