blob: ce26cbce26523f1dd3f5c69e5eaa398d50134880 [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
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "mpconfig.h"
Damiend99b0522013-12-21 18:17:45 +000032#include "nlr.h"
33#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000034#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +000035#include "obj.h"
36#include "runtime.h"
37#include "bc.h"
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +020038#include "objgenerator.h"
Paul Sokolovskyb7e90ea2014-04-17 05:49:47 +030039#include "objfun.h"
Damiend99b0522013-12-21 18:17:45 +000040
41/******************************************************************************/
42/* generator wrapper */
43
44typedef struct _mp_obj_gen_wrap_t {
45 mp_obj_base_t base;
Damiend99b0522013-12-21 18:17:45 +000046 mp_obj_t *fun;
47} mp_obj_gen_wrap_t;
48
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030049typedef struct _mp_obj_gen_instance_t {
50 mp_obj_base_t base;
51 mp_obj_dict_t *globals;
52 mp_code_state code_state;
53} mp_obj_gen_instance_t;
Paul Sokolovsky7fafb282014-03-30 20:21:28 +030054
Damien Georgeecc88e92014-08-30 00:35:11 +010055STATIC 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) {
Damiend99b0522013-12-21 18:17:45 +000056 mp_obj_gen_wrap_t *self = self_in;
Paul Sokolovskyb7e90ea2014-04-17 05:49:47 +030057 mp_obj_fun_bc_t *self_fun = (mp_obj_fun_bc_t*)self->fun;
Damien George3e1a5c12014-03-29 13:43:38 +000058 assert(MP_OBJ_IS_TYPE(self_fun, &mp_type_fun_bc));
Damien George6baf76e2013-12-30 22:32:17 +000059
Damien Georgeb534e1b2014-09-04 14:44:01 +010060 // skip code-info block
61 const byte *code_info = self_fun->bytecode;
62 mp_uint_t code_info_size = mp_decode_uint(&code_info);
63 const byte *ip = self_fun->bytecode + code_info_size;
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030064
Damien George1084b0f2014-10-25 15:07:02 +010065 // bytecode prelude: skip arg names
66 ip += (self_fun->n_pos_args + self_fun->n_kwonly_args) * sizeof(mp_obj_t);
67
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030068 // bytecode prelude: get state size and exception stack size
Damien Georgeb534e1b2014-09-04 14:44:01 +010069 mp_uint_t n_state = mp_decode_uint(&ip);
70 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030071
72 // allocate the generator object, with room for local stack and exception stack
73 mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte,
74 n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
75 o->base.type = &mp_type_gen_instance;
76
77 o->globals = self_fun->globals;
78 o->code_state.n_state = n_state;
Damien Georgeb534e1b2014-09-04 14:44:01 +010079 o->code_state.ip = ip;
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030080 mp_setup_code_state(&o->code_state, self_fun, n_args, n_kw, args);
81 return o;
Damiend99b0522013-12-21 18:17:45 +000082}
83
Damien George3e1a5c12014-03-29 13:43:38 +000084const mp_obj_type_t mp_type_gen_wrap = {
Damien Georgec5966122014-02-15 16:10:44 +000085 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000086 .name = MP_QSTR_generator,
Damien George20006db2014-01-18 14:10:48 +000087 .call = gen_wrap_call,
Damiend99b0522013-12-21 18:17:45 +000088};
89
Damien Georged0691cc2014-01-29 20:30:52 +000090mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
Damiend99b0522013-12-21 18:17:45 +000091 mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
Damien George3e1a5c12014-03-29 13:43:38 +000092 o->base.type = &mp_type_gen_wrap;
Damiend99b0522013-12-21 18:17:45 +000093 o->fun = fun;
94 return o;
95}
96
97/******************************************************************************/
98/* generator instance */
99
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200100void gen_instance_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) {
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300101 mp_obj_gen_instance_t *self = self_in;
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300102 print(env, "<generator object '%s' at %p>", mp_obj_code_get_name(self->code_state.code_info), self_in);
Damiend99b0522013-12-21 18:17:45 +0000103}
104
Damien George69b3ba02014-03-26 19:33:23 +0000105mp_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 +0200106 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
Damiend99b0522013-12-21 18:17:45 +0000107 mp_obj_gen_instance_t *self = self_in;
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300108 if (self->code_state.ip == 0) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100109 *ret_val = MP_OBJ_STOP_ITERATION;
Damien George69b3ba02014-03-26 19:33:23 +0000110 return MP_VM_RETURN_NORMAL;
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200111 }
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300112 if (self->code_state.sp == self->code_state.state - 1) {
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200113 if (send_value != mp_const_none) {
Damien Georgeea13f402014-04-05 18:32:08 +0100114 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 +0200115 }
116 } else {
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300117 *self->code_state.sp = send_value;
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200118 }
Paul Sokolovskyb7e90ea2014-04-17 05:49:47 +0300119 mp_obj_dict_t *old_globals = mp_globals_get();
120 mp_globals_set(self->globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100121 mp_vm_return_kind_t ret_kind = mp_execute_bytecode(&self->code_state, throw_value);
Paul Sokolovskyb7e90ea2014-04-17 05:49:47 +0300122 mp_globals_set(old_globals);
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200123
Damien George69b3ba02014-03-26 19:33:23 +0000124 switch (ret_kind) {
Damien Georgec8f78bc2014-02-15 22:55:00 +0000125 case MP_VM_RETURN_NORMAL:
126 // Explicitly mark generator as completed. If we don't do this,
127 // subsequent next() may re-execute statements after last yield
128 // again and again, leading to side effects.
129 // TODO: check how return with value behaves under such conditions
130 // in CPython.
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300131 self->code_state.ip = 0;
132 *ret_val = *self->code_state.sp;
Damien George69b3ba02014-03-26 19:33:23 +0000133 break;
Damien Georgec8f78bc2014-02-15 22:55:00 +0000134
135 case MP_VM_RETURN_YIELD:
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300136 *ret_val = *self->code_state.sp;
Damien George69b3ba02014-03-26 19:33:23 +0000137 break;
Damien Georgec8f78bc2014-02-15 22:55:00 +0000138
139 case MP_VM_RETURN_EXCEPTION:
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300140 self->code_state.ip = 0;
141 *ret_val = self->code_state.state[self->code_state.n_state - 1];
Damien George69b3ba02014-03-26 19:33:23 +0000142 break;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200143
144 default:
145 assert(0);
Damien George69b3ba02014-03-26 19:33:23 +0000146 *ret_val = mp_const_none;
147 break;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200148 }
Damien George69b3ba02014-03-26 19:33:23 +0000149
150 return ret_kind;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200151}
152
153STATIC 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 +0000154 mp_obj_t ret;
155 switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200156 case MP_VM_RETURN_NORMAL:
157 // Optimize return w/o value in case generator is used in for loop
Damien Georgeea8d06c2014-04-17 23:19:36 +0100158 if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
159 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200160 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100161 nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200162 }
163
164 case MP_VM_RETURN_YIELD:
Paul Sokolovsky817e76a2014-03-31 04:09:53 +0300165 if (throw_value != MP_OBJ_NULL && mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) {
Damien Georgeea13f402014-04-05 18:32:08 +0100166 nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
Paul Sokolovsky817e76a2014-03-31 04:09:53 +0300167 }
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200168 return ret;
169
170 case MP_VM_RETURN_EXCEPTION:
Damien Georgeea8d06c2014-04-17 23:19:36 +0100171 // TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300172 // of mp_iternext() protocol, but this function is called by other methods
Damien Georgeea8d06c2014-04-17 23:19:36 +0100173 // too, which may not handled MP_OBJ_STOP_ITERATION.
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300174 if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100175 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300176 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100177 nlr_raise(ret);
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300178 }
Paul Sokolovsky61fd20f2014-03-22 16:44:58 +0200179
Damien Georgec8f78bc2014-02-15 22:55:00 +0000180 default:
Damien Georgec8f78bc2014-02-15 22:55:00 +0000181 assert(0);
182 return mp_const_none;
Damiend99b0522013-12-21 18:17:45 +0000183 }
184}
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200185
186mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200187 return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200188}
189
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200190STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200191 mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100192 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100193 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damien Georgec5966122014-02-15 16:10:44 +0000194 } else {
195 return ret;
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200196 }
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200197}
Damien Georgec5966122014-02-15 16:10:44 +0000198
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200199STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200200
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300201STATIC mp_obj_t gen_instance_close(mp_obj_t self_in);
Damien Georgeecc88e92014-08-30 00:35:11 +0100202STATIC mp_obj_t gen_instance_throw(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300203 mp_obj_t exc = (n_args == 2) ? args[1] : args[2];
Damien Georged17926d2014-03-30 13:35:08 +0100204 exc = mp_make_raise_obj(exc);
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300205
206 mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100207 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100208 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200209 } else {
210 return ret;
211 }
212}
213
214STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
215
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200216STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
Damien George69b3ba02014-03-26 19:33:23 +0000217 mp_obj_t ret;
Damien George07ddab52014-03-29 13:15:08 +0000218 switch (mp_obj_gen_resume(self_in, mp_const_none, (mp_obj_t)&mp_const_GeneratorExit_obj, &ret)) {
Damien George69b3ba02014-03-26 19:33:23 +0000219 case MP_VM_RETURN_YIELD:
Damien Georgeea13f402014-04-05 18:32:08 +0100220 nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200221
Damien George69b3ba02014-03-26 19:33:23 +0000222 // Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
223 case MP_VM_RETURN_EXCEPTION:
224 // ret should always be an instance of an exception class
225 if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_GeneratorExit) ||
226 mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
227 return mp_const_none;
228 }
Damien Georgeea13f402014-04-05 18:32:08 +0100229 nlr_raise(ret);
Damien George69b3ba02014-03-26 19:33:23 +0000230
231 default:
232 // The only choice left is MP_VM_RETURN_NORMAL which is successful close
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200233 return mp_const_none;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200234 }
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200235}
236
237STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200238
Damien George9b196cd2014-03-26 21:47:19 +0000239STATIC const mp_map_elem_t gen_instance_locals_dict_table[] = {
240 { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&gen_instance_close_obj },
241 { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&gen_instance_send_obj },
242 { MP_OBJ_NEW_QSTR(MP_QSTR_throw), (mp_obj_t)&gen_instance_throw_obj },
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200243};
Damiend99b0522013-12-21 18:17:45 +0000244
Damien George9b196cd2014-03-26 21:47:19 +0000245STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
246
Damien George3e1a5c12014-03-29 13:43:38 +0000247const mp_obj_type_t mp_type_gen_instance = {
Damien Georgec5966122014-02-15 16:10:44 +0000248 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000249 .name = MP_QSTR_generator,
Damien George97209d32014-01-07 15:58:30 +0000250 .print = gen_instance_print,
Paul Sokolovskybf271402014-05-17 11:18:23 +0300251 .getiter = mp_identity,
Damien George97209d32014-01-07 15:58:30 +0000252 .iternext = gen_instance_iternext,
Damien George9b196cd2014-03-26 21:47:19 +0000253 .locals_dict = (mp_obj_t)&gen_instance_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000254};