blob: 1b06975c96e8b0cd6bac85ea4a8ec9dffaacdd50 [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:
123 // Explicitly mark generator as completed. If we don't do this,
124 // subsequent next() may re-execute statements after last yield
125 // again and again, leading to side effects.
126 // TODO: check how return with value behaves under such conditions
127 // in CPython.
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300128 self->code_state.ip = 0;
129 *ret_val = *self->code_state.sp;
Damien George69b3ba02014-03-26 19:33:23 +0000130 break;
Damien Georgec8f78bc2014-02-15 22:55:00 +0000131
132 case MP_VM_RETURN_YIELD:
Paul Sokolovskyb4ebad32014-05-31 16:50:46 +0300133 *ret_val = *self->code_state.sp;
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
141 default:
142 assert(0);
Damien George69b3ba02014-03-26 19:33:23 +0000143 *ret_val = mp_const_none;
144 break;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200145 }
Damien George69b3ba02014-03-26 19:33:23 +0000146
147 return ret_kind;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200148}
149
150STATIC 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 +0000151 mp_obj_t ret;
152 switch (mp_obj_gen_resume(self_in, send_value, throw_value, &ret)) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200153 case MP_VM_RETURN_NORMAL:
154 // Optimize return w/o value in case generator is used in for loop
Damien Georgeea8d06c2014-04-17 23:19:36 +0100155 if (ret == mp_const_none || ret == MP_OBJ_STOP_ITERATION) {
156 return MP_OBJ_STOP_ITERATION;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200157 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100158 nlr_raise(mp_obj_new_exception_args(&mp_type_StopIteration, 1, &ret));
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200159 }
160
161 case MP_VM_RETURN_YIELD:
Paul Sokolovsky817e76a2014-03-31 04:09:53 +0300162 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 +0100163 nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
Paul Sokolovsky817e76a2014-03-31 04:09:53 +0300164 }
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200165 return ret;
166
167 case MP_VM_RETURN_EXCEPTION:
Damien Georgeea8d06c2014-04-17 23:19:36 +0100168 // TODO: Optimization of returning MP_OBJ_STOP_ITERATION is really part
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300169 // of mp_iternext() protocol, but this function is called by other methods
Damien Georgeea8d06c2014-04-17 23:19:36 +0100170 // too, which may not handled MP_OBJ_STOP_ITERATION.
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300171 if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100172 return MP_OBJ_STOP_ITERATION;
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300173 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100174 nlr_raise(ret);
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300175 }
Paul Sokolovsky61fd20f2014-03-22 16:44:58 +0200176
Damien Georgec8f78bc2014-02-15 22:55:00 +0000177 default:
Damien Georgec8f78bc2014-02-15 22:55:00 +0000178 assert(0);
179 return mp_const_none;
Damiend99b0522013-12-21 18:17:45 +0000180 }
181}
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200182
Damien George969a6b32014-12-10 22:07:04 +0000183STATIC mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200184 return gen_resume_and_raise(self_in, mp_const_none, MP_OBJ_NULL);
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200185}
186
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200187STATIC mp_obj_t gen_instance_send(mp_obj_t self_in, mp_obj_t send_value) {
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200188 mp_obj_t ret = gen_resume_and_raise(self_in, send_value, MP_OBJ_NULL);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100189 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100190 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Damien Georgec5966122014-02-15 16:10:44 +0000191 } else {
192 return ret;
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200193 }
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200194}
Damien Georgec5966122014-02-15 16:10:44 +0000195
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200196STATIC MP_DEFINE_CONST_FUN_OBJ_2(gen_instance_send_obj, gen_instance_send);
Paul Sokolovsky14d28be2014-01-27 01:01:37 +0200197
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300198STATIC mp_obj_t gen_instance_close(mp_obj_t self_in);
Damien Georgeecc88e92014-08-30 00:35:11 +0100199STATIC mp_obj_t gen_instance_throw(mp_uint_t n_args, const mp_obj_t *args) {
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300200 mp_obj_t exc = (n_args == 2) ? args[1] : args[2];
Damien Georged17926d2014-03-30 13:35:08 +0100201 exc = mp_make_raise_obj(exc);
Paul Sokolovsky9a54a222014-03-30 13:13:12 +0300202
203 mp_obj_t ret = gen_resume_and_raise(args[0], mp_const_none, exc);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100204 if (ret == MP_OBJ_STOP_ITERATION) {
Damien Georgeea13f402014-04-05 18:32:08 +0100205 nlr_raise(mp_obj_new_exception(&mp_type_StopIteration));
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200206 } else {
207 return ret;
208 }
209}
210
211STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gen_instance_throw_obj, 2, 4, gen_instance_throw);
212
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200213STATIC mp_obj_t gen_instance_close(mp_obj_t self_in) {
Damien George69b3ba02014-03-26 19:33:23 +0000214 mp_obj_t ret;
Damien George07ddab52014-03-29 13:15:08 +0000215 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 +0000216 case MP_VM_RETURN_YIELD:
Damien Georgeea13f402014-04-05 18:32:08 +0100217 nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "generator ignored GeneratorExit"));
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200218
Damien George69b3ba02014-03-26 19:33:23 +0000219 // Swallow StopIteration & GeneratorExit (== successful close), and re-raise any other
220 case MP_VM_RETURN_EXCEPTION:
221 // ret should always be an instance of an exception class
222 if (mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_GeneratorExit) ||
223 mp_obj_is_subclass_fast(mp_obj_get_type(ret), &mp_type_StopIteration)) {
224 return mp_const_none;
225 }
Damien Georgeea13f402014-04-05 18:32:08 +0100226 nlr_raise(ret);
Damien George69b3ba02014-03-26 19:33:23 +0000227
228 default:
229 // The only choice left is MP_VM_RETURN_NORMAL which is successful close
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200230 return mp_const_none;
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200231 }
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200232}
233
234STATIC MP_DEFINE_CONST_FUN_OBJ_1(gen_instance_close_obj, gen_instance_close);
Paul Sokolovsky48caa092014-03-22 17:50:12 +0200235
Damien George9b196cd2014-03-26 21:47:19 +0000236STATIC const mp_map_elem_t gen_instance_locals_dict_table[] = {
237 { MP_OBJ_NEW_QSTR(MP_QSTR_close), (mp_obj_t)&gen_instance_close_obj },
238 { MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&gen_instance_send_obj },
239 { MP_OBJ_NEW_QSTR(MP_QSTR_throw), (mp_obj_t)&gen_instance_throw_obj },
Paul Sokolovskybf38e2a2014-01-26 20:50:11 +0200240};
Damiend99b0522013-12-21 18:17:45 +0000241
Damien George9b196cd2014-03-26 21:47:19 +0000242STATIC MP_DEFINE_CONST_DICT(gen_instance_locals_dict, gen_instance_locals_dict_table);
243
Damien George3e1a5c12014-03-29 13:43:38 +0000244const mp_obj_type_t mp_type_gen_instance = {
Damien Georgec5966122014-02-15 16:10:44 +0000245 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000246 .name = MP_QSTR_generator,
Damien George97209d32014-01-07 15:58:30 +0000247 .print = gen_instance_print,
Paul Sokolovskybf271402014-05-17 11:18:23 +0300248 .getiter = mp_identity,
Damien George97209d32014-01-07 15:58:30 +0000249 .iternext = gen_instance_iternext,
Damien George9b196cd2014-03-26 21:47:19 +0000250 .locals_dict = (mp_obj_t)&gen_instance_locals_dict,
Damiend99b0522013-12-21 18:17:45 +0000251};