blob: 0a684856289e9f5b2b07d67643bd9bfa9a536ea5 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
Paul Sokolovskyda9f0922014-05-13 08:44:45 +03007 * Copyright (c) 2014 Paul Sokolovsky
Damien George04b91472014-05-03 23:27:38 +01008 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
Damiend99b0522013-12-21 18:17:45 +000028#include <stdlib.h>
Damiend99b0522013-12-21 18:17:45 +000029#include <assert.h>
30
Damien George51dfcb42015-01-01 20:27:54 +000031#include "py/nlr.h"
32#include "py/obj.h"
33#include "py/runtime.h"
34#include "py/bc.h"
35#include "py/objgenerator.h"
36#include "py/objfun.h"
Damiend99b0522013-12-21 18:17:45 +000037
38/******************************************************************************/
39/* generator wrapper */
40
41typedef struct _mp_obj_gen_wrap_t {
42 mp_obj_base_t base;
Damiend99b0522013-12-21 18:17:45 +000043 mp_obj_t *fun;
44} mp_obj_gen_wrap_t;
45
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030046typedef struct _mp_obj_gen_instance_t {
47 mp_obj_base_t base;
48 mp_obj_dict_t *globals;
49 mp_code_state code_state;
50} mp_obj_gen_instance_t;
Paul Sokolovsky7fafb282014-03-30 20:21:28 +030051
Damien Georgeecc88e92014-08-30 00:35:11 +010052STATIC mp_obj_t gen_wrap_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
Damien George999cedb2015-11-27 17:01:44 +000053 mp_obj_gen_wrap_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovskyb7e90ea2014-04-17 05:49:47 +030054 mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t*)self->fun;
Damien George999cedb2015-11-27 17:01:44 +000055 assert(self_fun->base.type == &mp_type_fun_bc);
Damien George6baf76e2013-12-30 22:32:17 +000056
Damien George9b7f5832015-03-18 17:47:47 +000057 // get start of bytecode
58 const byte *ip = self_fun->bytecode;
Damien George1084b0f2014-10-25 15:07:02 +010059
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030060 // bytecode prelude: get state size and exception stack size
Damien Georgeb534e1b2014-09-04 14:44:01 +010061 mp_uint_t n_state = mp_decode_uint(&ip);
62 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030063
64 // allocate the generator object, with room for local stack and exception stack
65 mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte,
66 n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
67 o->base.type = &mp_type_gen_instance;
68
69 o->globals = self_fun->globals;
70 o->code_state.n_state = n_state;
Damien George99886182015-04-06 22:38:53 +010071 o->code_state.ip = (byte*)(ip - self_fun->bytecode); // offset to prelude
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030072 mp_setup_code_state(&o->code_state, self_fun, n_args, n_kw, args);
Damien George999cedb2015-11-27 17:01:44 +000073 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +000074}
75
Damien George3e1a5c12014-03-29 13:43:38 +000076const mp_obj_type_t mp_type_gen_wrap = {
Damien Georgec5966122014-02-15 16:10:44 +000077 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000078 .name = MP_QSTR_generator,
Damien George20006db2014-01-18 14:10:48 +000079 .call = gen_wrap_call,
Damiend99b0522013-12-21 18:17:45 +000080};
81
Damien Georged0691cc2014-01-29 20:30:52 +000082mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
Damiend99b0522013-12-21 18:17:45 +000083 mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
Damien George3e1a5c12014-03-29 13:43:38 +000084 o->base.type = &mp_type_gen_wrap;
Damien George999cedb2015-11-27 17:01:44 +000085 o->fun = MP_OBJ_TO_PTR(fun);
86 return MP_OBJ_FROM_PTR(o);
Damiend99b0522013-12-21 18:17:45 +000087}
88
89/******************************************************************************/
90/* generator instance */
91
Damien George7f9d1d62015-04-09 23:56:15 +010092STATIC void gen_instance_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
Damien Georgeff8dd3f2015-01-20 12:47:20 +000093 (void)kind;
Damien George999cedb2015-11-27 17:01:44 +000094 mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
95 mp_printf(print, "<generator object '%q' at %p>", mp_obj_code_get_name(self->code_state.code_info), self);
Damiend99b0522013-12-21 18:17:45 +000096}
97
Damien George69b3ba02014-03-26 19:33:23 +000098mp_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) {
Paul Sokolovskyaaff7162014-03-30 02:32:30 +020099 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
Damien George999cedb2015-11-27 17:01:44 +0000100 mp_obj_gen_instance_t *self = MP_OBJ_TO_PTR(self_in);
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300101 if (self->code_state.ip == 0) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100102 *ret_val = MP_OBJ_STOP_ITERATION;
Damien George69b3ba02014-03-26 19:33:23 +0000103 return MP_VM_RETURN_NORMAL;
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200104 }
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300105 if (self->code_state.sp == self->code_state.state - 1) {
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200106 if (send_value != mp_const_none) {
Damien Georgeea13f402014-04-05 18:32:08 +0100107 nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "can't send non-None value to a just-started generator"));
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200108 }
109 } else {
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300110 *self->code_state.sp = send_value;
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200111 }
Paul Sokolovskyb7e90ea2014-04-17 05:49:47 +0300112 mp_obj_dict_t *old_globals = mp_globals_get();
113 mp_globals_set(self->globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100114 mp_vm_return_kind_t ret_kind = mp_execute_bytecode(&self->code_state, throw_value);
Paul Sokolovskyb7e90ea2014-04-17 05:49:47 +0300115 mp_globals_set(old_globals);
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200116
Damien George69b3ba02014-03-26 19:33:23 +0000117 switch (ret_kind) {
Damien Georgec8f78bc2014-02-15 22:55:00 +0000118 case MP_VM_RETURN_NORMAL:
Damien Georged2d64f02015-01-14 21:32:42 +0000119 default:
Damien Georgec8f78bc2014-02-15 22:55:00 +0000120 // Explicitly mark generator as completed. If we don't do this,
121 // subsequent next() may re-execute statements after last yield
122 // again and again, leading to side effects.
123 // TODO: check how return with value behaves under such conditions
124 // in CPython.
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300125 self->code_state.ip = 0;
126 *ret_val = *self->code_state.sp;
Damien George69b3ba02014-03-26 19:33:23 +0000127 break;
Damien Georgec8f78bc2014-02-15 22:55:00 +0000128
129 case MP_VM_RETURN_YIELD:
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300130 *ret_val = *self->code_state.sp;
Paul Sokolovskyaa9dbb12015-05-11 02:59:25 +0300131 if (*ret_val == MP_OBJ_STOP_ITERATION) {
132 self->code_state.ip = 0;
133 }
Damien George69b3ba02014-03-26 19:33:23 +0000134 break;
Damien Georgec8f78bc2014-02-15 22:55:00 +0000135
136 case MP_VM_RETURN_EXCEPTION:
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300137 self->code_state.ip = 0;
138 *ret_val = self->code_state.state[self->code_state.n_state - 1];
Damien George69b3ba02014-03-26 19:33:23 +0000139 break;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200140 }
Damien George69b3ba02014-03-26 19:33:23 +0000141
142 return ret_kind;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200143}
144
145STATIC mp_obj_t gen_resume_and_raise(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value) {
Damien George69b3ba02014-03-26 19:33:23 +0000146 mp_obj_t ret;
147 switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200148 case MP_VM_RETURN_NORMAL:
Damien Georged2d64f02015-01-14 21:32:42 +0000149 default:
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200150 // Optimize return w/o value in case generator is used in for loop
Damien Georgeea8d06c2014-04-17 23:19:36 +0100151 if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
152 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200153 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100154 nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200155 }
156
157 case MP_VM_RETURN_YIELD:
Damien George999cedb2015-11-27 17:01:44 +0000158 if (throw_value != MP_OBJ_NULL && mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(throw_value)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit))) {
Damien Georgeea13f402014-04-05 18:32:08 +0100159 nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
Paul Sokolovsky817e76a2014-03-31 04:09:53 +0300160 }
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200161 return ret;
162
163 case MP_VM_RETURN_EXCEPTION:
Damien Georgeea8d06c2014-04-17 23:19:36 +0100164 // TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300165 // of mp_iternext() protocol, but this function is called by other methods
Damien Georgeea8d06c2014-04-17 23:19:36 +0100166 // too, which may not handled MP_OBJ_STOP_ITERATION.
Damien George999cedb2015-11-27 17:01:44 +0000167 if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
Paul Sokolovskyd5e629a2015-05-11 02:59:25 +0300168 mp_obj_t val = mp_obj_exception_get_value(ret);
169 if (val == mp_const_none) {
170 return MP_OBJ_STOP_ITERATION;
171 }
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300172 }
Paul Sokolovskyd5e629a2015-05-11 02:59:25 +0300173 nlr_raise(ret);
Damiend99b0522013-12-21 18:17:45 +0000174 }
175}
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200176
Damien George969a6b32014-12-10 22:07:04 +0000177STATIC mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200178 return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200179}
180
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200181STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200182 mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100183 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100184 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damien Georgec5966122014-02-15 16:10:44 +0000185 } else {
186 return ret;
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200187 }
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200188}
Damien Georgec5966122014-02-15 16:10:44 +0000189
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200190STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200191
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300192STATIC mp_obj_t gen_instance_close(mp_obj_t self_in);
Damien Georgeecc88e92014-08-30 00:35:11 +0100193STATIC mp_obj_t gen_instance_throw(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300194 mp_obj_t exc = (n_args == 2) ? args[1] : args[2];
Damien Georged17926d2014-03-30 13:35:08 +0100195 exc = mp_make_raise_obj(exc);
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300196
197 mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100198 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100199 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200200 } else {
201 return ret;
202 }
203}
204
205STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
206
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200207STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
Damien George69b3ba02014-03-26 19:33:23 +0000208 mp_obj_t ret;
Damien George999cedb2015-11-27 17:01:44 +0000209 switch (mp_obj_gen_resume(self_in, mp_const_none, MP_OBJ_FROM_PTR(&mp_const_GeneratorExit_obj), &ret)) {
Damien George69b3ba02014-03-26 19:33:23 +0000210 case MP_VM_RETURN_YIELD:
Damien Georgeea13f402014-04-05 18:32:08 +0100211 nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200212
Damien George69b3ba02014-03-26 19:33:23 +0000213 // Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
214 case MP_VM_RETURN_EXCEPTION:
215 // ret should always be an instance of an exception class
Damien George999cedb2015-11-27 17:01:44 +0000216 if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_GeneratorExit)) ||
217 mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type(ret)), MP_OBJ_FROM_PTR(&mp_type_StopIteration))) {
Damien George69b3ba02014-03-26 19:33:23 +0000218 return mp_const_none;
219 }
Damien Georgeea13f402014-04-05 18:32:08 +0100220 nlr_raise(ret);
Damien George69b3ba02014-03-26 19:33:23 +0000221
222 default:
223 // The only choice left is MP_VM_RETURN_NORMAL which is successful close
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200224 return mp_const_none;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200225 }
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200226}
227
228STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200229
Damien Georgecbf76742015-11-27 13:38:15 +0000230STATIC const mp_rom_map_elem_t gen_instance_locals_dict_table[] = {
231 { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&gen_instance_close_obj) },
232 { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&gen_instance_send_obj) },
233 { MP_ROM_QSTR(MP_QSTR_throw), MP_ROM_PTR(&gen_instance_throw_obj) },
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200234};
Damiend99b0522013-12-21 18:17:45 +0000235
Damien George9b196cd2014-03-26 21:47:19 +0000236STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
237
Damien George3e1a5c12014-03-29 13:43:38 +0000238const mp_obj_type_t mp_type_gen_instance = {
Damien Georgec5966122014-02-15 16:10:44 +0000239 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000240 .name = MP_QSTR_generator,
Damien George97209d32014-01-07 15:58:30 +0000241 .print = gen_instance_print,
Paul Sokolovskybf271402014-05-17 11:18:23 +0300242 .getiter = mp_identity,
Damien George97209d32014-01-07 15:58:30 +0000243 .iternext = gen_instance_iternext,
Damien George999cedb2015-11-27 17:01:44 +0000244 .locals_dict = (mp_obj_dict_t*)&gen_instance_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000245};