blob: a4d22aa8def9957d5d56fbb9d6f616e1e1d19dda [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) {
Damiend99b0522013-12-21 18:17:45 +000053 mp_obj_gen_wrap_t *self = 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 George3e1a5c12014-03-29 13:43:38 +000055 assert(MP_OBJ_IS_TYPE(self_fun, &mp_type_fun_bc));
Damien George6baf76e2013-12-30 22:32:17 +000056
Damien Georgeb534e1b2014-09-04 14:44:01 +010057 // skip code-info block
58 const byte *code_info = self_fun->bytecode;
59 mp_uint_t code_info_size = mp_decode_uint(&code_info);
60 const byte *ip = self_fun->bytecode + code_info_size;
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030061
Damien George1084b0f2014-10-25 15:07:02 +010062 // bytecode prelude: skip arg names
63 ip += (self_fun->n_pos_args + self_fun->n_kwonly_args) * sizeof(mp_obj_t);
64
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030065 // bytecode prelude: get state size and exception stack size
Damien Georgeb534e1b2014-09-04 14:44:01 +010066 mp_uint_t n_state = mp_decode_uint(&ip);
67 mp_uint_t n_exc_stack = mp_decode_uint(&ip);
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030068
69 // allocate the generator object, with room for local stack and exception stack
70 mp_obj_gen_instance_t *o = m_new_obj_var(mp_obj_gen_instance_t, byte,
71 n_state * sizeof(mp_obj_t) + n_exc_stack * sizeof(mp_exc_stack_t));
72 o->base.type = &mp_type_gen_instance;
73
74 o->globals = self_fun->globals;
75 o->code_state.n_state = n_state;
Damien Georgeb534e1b2014-09-04 14:44:01 +010076 o->code_state.ip = ip;
Paul Sokolovsky5f4a6672014-06-11 20:17:38 +030077 mp_setup_code_state(&o->code_state, self_fun, n_args, n_kw, args);
78 return o;
Damiend99b0522013-12-21 18:17:45 +000079}
80
Damien George3e1a5c12014-03-29 13:43:38 +000081const mp_obj_type_t mp_type_gen_wrap = {
Damien Georgec5966122014-02-15 16:10:44 +000082 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +000083 .name = MP_QSTR_generator,
Damien George20006db2014-01-18 14:10:48 +000084 .call = gen_wrap_call,
Damiend99b0522013-12-21 18:17:45 +000085};
86
Damien Georged0691cc2014-01-29 20:30:52 +000087mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun) {
Damiend99b0522013-12-21 18:17:45 +000088 mp_obj_gen_wrap_t *o = m_new_obj(mp_obj_gen_wrap_t);
Damien George3e1a5c12014-03-29 13:43:38 +000089 o->base.type = &mp_type_gen_wrap;
Damiend99b0522013-12-21 18:17:45 +000090 o->fun = fun;
91 return o;
92}
93
94/******************************************************************************/
95/* generator instance */
96
Damien George969a6b32014-12-10 22:07:04 +000097STATIC void 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 +030098 mp_obj_gen_instance_t *self = self_in;
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +030099 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 +0000100}
101
Damien George69b3ba02014-03-26 19:33:23 +0000102mp_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 +0200103 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_gen_instance));
Damiend99b0522013-12-21 18:17:45 +0000104 mp_obj_gen_instance_t *self = self_in;
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300105 if (self->code_state.ip == 0) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100106 *ret_val = MP_OBJ_STOP_ITERATION;
Damien George69b3ba02014-03-26 19:33:23 +0000107 return MP_VM_RETURN_NORMAL;
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200108 }
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300109 if (self->code_state.sp == self->code_state.state - 1) {
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200110 if (send_value != mp_const_none) {
Damien Georgeea13f402014-04-05 18:32:08 +0100111 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 +0200112 }
113 } else {
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300114 *self->code_state.sp = send_value;
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200115 }
Paul Sokolovskyb7e90ea2014-04-17 05:49:47 +0300116 mp_obj_dict_t *old_globals = mp_globals_get();
117 mp_globals_set(self->globals);
Damien Georgeaabd83e2014-06-07 14:16:08 +0100118 mp_vm_return_kind_t ret_kind = mp_execute_bytecode(&self->code_state, throw_value);
Paul Sokolovskyb7e90ea2014-04-17 05:49:47 +0300119 mp_globals_set(old_globals);
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200120
Damien George69b3ba02014-03-26 19:33:23 +0000121 switch (ret_kind) {
Damien Georgec8f78bc2014-02-15 22:55:00 +0000122 case MP_VM_RETURN_NORMAL:
Damien Georged2d64f02015-01-14 21:32:42 +0000123 default:
Damien Georgec8f78bc2014-02-15 22:55:00 +0000124 // Explicitly mark generator as completed. If we don't do this,
125 // subsequent next() may re-execute statements after last yield
126 // again and again, leading to side effects.
127 // TODO: check how return with value behaves under such conditions
128 // in CPython.
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300129 self->code_state.ip = 0;
130 *ret_val = *self->code_state.sp;
Damien George69b3ba02014-03-26 19:33:23 +0000131 break;
Damien Georgec8f78bc2014-02-15 22:55:00 +0000132
133 case MP_VM_RETURN_YIELD:
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300134 *ret_val = *self->code_state.sp;
Damien George69b3ba02014-03-26 19:33:23 +0000135 break;
Damien Georgec8f78bc2014-02-15 22:55:00 +0000136
137 case MP_VM_RETURN_EXCEPTION:
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300138 self->code_state.ip = 0;
139 *ret_val = self->code_state.state[self->code_state.n_state - 1];
Damien George69b3ba02014-03-26 19:33:23 +0000140 break;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200141 }
Damien George69b3ba02014-03-26 19:33:23 +0000142
143 return ret_kind;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200144}
145
146STATIC 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 +0000147 mp_obj_t ret;
148 switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200149 case MP_VM_RETURN_NORMAL:
Damien Georged2d64f02015-01-14 21:32:42 +0000150 default:
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200151 // Optimize return w/o value in case generator is used in for loop
Damien Georgeea8d06c2014-04-17 23:19:36 +0100152 if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
153 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200154 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100155 nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200156 }
157
158 case MP_VM_RETURN_YIELD:
Paul Sokolovsky817e76a2014-03-31 04:09:53 +0300159 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 +0100160 nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
Paul Sokolovsky817e76a2014-03-31 04:09:53 +0300161 }
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200162 return ret;
163
164 case MP_VM_RETURN_EXCEPTION:
Damien Georgeea8d06c2014-04-17 23:19:36 +0100165 // TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300166 // of mp_iternext() protocol, but this function is called by other methods
Damien Georgeea8d06c2014-04-17 23:19:36 +0100167 // too, which may not handled MP_OBJ_STOP_ITERATION.
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300168 if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100169 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300170 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100171 nlr_raise(ret);
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300172 }
Damiend99b0522013-12-21 18:17:45 +0000173 }
174}
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200175
Damien George969a6b32014-12-10 22:07:04 +0000176STATIC mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200177 return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200178}
179
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200180STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200181 mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100182 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100183 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damien Georgec5966122014-02-15 16:10:44 +0000184 } else {
185 return ret;
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200186 }
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200187}
Damien Georgec5966122014-02-15 16:10:44 +0000188
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200189STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200190
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300191STATIC mp_obj_t gen_instance_close(mp_obj_t self_in);
Damien Georgeecc88e92014-08-30 00:35:11 +0100192STATIC mp_obj_t gen_instance_throw(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300193 mp_obj_t exc = (n_args == 2) ? args[1] : args[2];
Damien Georged17926d2014-03-30 13:35:08 +0100194 exc = mp_make_raise_obj(exc);
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300195
196 mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100197 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100198 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200199 } else {
200 return ret;
201 }
202}
203
204STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
205
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200206STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
Damien George69b3ba02014-03-26 19:33:23 +0000207 mp_obj_t ret;
Damien George07ddab52014-03-29 13:15:08 +0000208 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 +0000209 case MP_VM_RETURN_YIELD:
Damien Georgeea13f402014-04-05 18:32:08 +0100210 nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200211
Damien George69b3ba02014-03-26 19:33:23 +0000212 // Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
213 case MP_VM_RETURN_EXCEPTION:
214 // ret should always be an instance of an exception class
215 if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_GeneratorExit) ||
216 mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
217 return mp_const_none;
218 }
Damien Georgeea13f402014-04-05 18:32:08 +0100219 nlr_raise(ret);
Damien George69b3ba02014-03-26 19:33:23 +0000220
221 default:
222 // The only choice left is MP_VM_RETURN_NORMAL which is successful close
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200223 return mp_const_none;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200224 }
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200225}
226
227STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200228
Damien George9b196cd2014-03-26 21:47:19 +0000229STATIC const mp_map_elem_t gen_instance_locals_dict_table[] = {
230 { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&gen_instance_close_obj },
231 { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&gen_instance_send_obj },
232 { MP_OBJ_NEW_QSTR(MP_QSTR_throw), (mp_obj_t)&gen_instance_throw_obj },
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200233};
Damiend99b0522013-12-21 18:17:45 +0000234
Damien George9b196cd2014-03-26 21:47:19 +0000235STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
236
Damien George3e1a5c12014-03-29 13:43:38 +0000237const mp_obj_type_t mp_type_gen_instance = {
Damien Georgec5966122014-02-15 16:10:44 +0000238 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000239 .name = MP_QSTR_generator,
Damien George97209d32014-01-07 15:58:30 +0000240 .print = gen_instance_print,
Paul Sokolovskybf271402014-05-17 11:18:23 +0300241 .getiter = mp_identity,
Damien George97209d32014-01-07 15:58:30 +0000242 .iternext = gen_instance_iternext,
Damien George9b196cd2014-03-26 21:47:19 +0000243 .locals_dict = (mp_obj_t)&gen_instance_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000244};