blob: ba78ec40f3dfb7a64daef69d05951fffe2dcfd46 [file] [log] [blame]
Damien429d7192013-10-04 19:53:11 +01001#include <stdio.h>
2#include <string.h>
3#include <assert.h>
4
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +03005#include "mpconfig.h"
Damience89a212013-10-15 22:25:17 +01006#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +01007#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +00008#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +00009#include "obj.h"
Damien George230fec72014-03-30 21:21:24 +010010#include "objtuple.h"
Damien Georgecaac5422014-03-25 14:18:18 +000011#include "objmodule.h"
Damien George20773972014-02-22 18:12:43 +000012#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000013#include "runtime0.h"
14#include "runtime.h"
Damien George2326d522014-03-27 23:26:35 +000015#include "emitglue.h"
Damien660365e2013-12-17 18:27:24 +000016#include "builtin.h"
Damien Georgecaac5422014-03-25 14:18:18 +000017#include "builtintables.h"
Damien George5fa93b62014-01-22 14:35:10 +000018#include "bc.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000019#include "smallint.h"
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +030020#include "objgenerator.h"
Damien660365e2013-12-17 18:27:24 +000021
Damien7f5dacf2013-10-10 11:24:39 +010022#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010023#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020024#define DEBUG_printf DEBUG_printf
Damien George41eb6082014-02-26 22:40:35 +000025#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
Damien7f5dacf2013-10-10 11:24:39 +010026#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000027#define DEBUG_printf(...) (void)0
28#define DEBUG_OP_printf(...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010029#endif
Damien429d7192013-10-04 19:53:11 +010030
Damieneb19efb2013-10-10 22:06:54 +010031// locals and globals need to be pointers because they can be the same in outer module scope
Damien George7efc5b32014-04-05 22:36:42 +010032STATIC mp_obj_dict_t *dict_locals;
33STATIC mp_obj_dict_t *dict_globals;
Damienbd254452013-10-16 20:39:12 +010034
Damien George7efc5b32014-04-05 22:36:42 +010035// dictionary for the __main__ module
Damien George8b0535e2014-04-05 21:53:54 +010036STATIC mp_obj_dict_t dict_main;
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030037
38const mp_obj_module_t mp_module___main__ = {
39 .base = { &mp_type_module },
40 .name = MP_QSTR___main__,
Damien George8b0535e2014-04-05 21:53:54 +010041 .globals = (mp_obj_dict_t*)&dict_main,
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030042};
43
Damien Georged17926d2014-03-30 13:35:08 +010044void mp_init(void) {
stijn72521a12014-05-03 11:28:29 +020045 // call port specific initialization if any
46#ifdef MICROPY_PORT_INIT_FUNC
47 MICROPY_PORT_INIT_FUNC;
48#endif
49
Damien George2326d522014-03-27 23:26:35 +000050 mp_emit_glue_init();
51
Damien Georgecaac5422014-03-25 14:18:18 +000052 // init global module stuff
53 mp_module_init();
Damien George0d028742014-01-22 23:59:20 +000054
Damien George7efc5b32014-04-05 22:36:42 +010055 // initialise the __main__ module
Damien George8b0535e2014-04-05 21:53:54 +010056 mp_obj_dict_init(&dict_main, 1);
Damien George8b0535e2014-04-05 21:53:54 +010057 mp_obj_dict_store(&dict_main, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030058
59 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George7efc5b32014-04-05 22:36:42 +010060 dict_locals = dict_globals = &dict_main;
Damien429d7192013-10-04 19:53:11 +010061}
62
Damien Georged17926d2014-03-30 13:35:08 +010063void mp_deinit(void) {
Damien George7efc5b32014-04-05 22:36:42 +010064 //mp_obj_dict_free(&dict_main);
Damien George2326d522014-03-27 23:26:35 +000065 mp_module_deinit();
66 mp_emit_glue_deinit();
Damien429d7192013-10-04 19:53:11 +010067}
68
Damien Georged17926d2014-03-30 13:35:08 +010069mp_obj_t mp_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +000070 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +000071 uint len;
72 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +000073 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +000074}
75
Damien Georged17926d2014-03-30 13:35:08 +010076mp_obj_t mp_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +010077 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +000078 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +010079}
80
Damien Georged17926d2014-03-30 13:35:08 +010081mp_obj_t mp_load_const_bytes(qstr qstr) {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +020082 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
83 uint len;
84 const byte *data = qstr_data(qstr, &len);
85 return mp_obj_new_bytes(data, len);
86}
87
Damien Georged17926d2014-03-30 13:35:08 +010088mp_obj_t mp_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +010089 // logic: search locals, globals, builtins
Damien George7efc5b32014-04-05 22:36:42 +010090 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Paul Sokolovskya0d32992014-04-05 04:51:26 +030091 // If we're at the outer scope (locals == globals), dispatch to load_global right away
Damien George7efc5b32014-04-05 22:36:42 +010092 if (dict_locals != dict_globals) {
93 mp_map_elem_t *elem = mp_map_lookup(&dict_locals->map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Paul Sokolovskya0d32992014-04-05 04:51:26 +030094 if (elem != NULL) {
95 return elem->value;
96 }
Damiena3977762013-10-09 23:10:10 +010097 }
Paul Sokolovskya0d32992014-04-05 04:51:26 +030098 return mp_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +010099}
100
Damien Georged17926d2014-03-30 13:35:08 +0100101mp_obj_t mp_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100102 // logic: search globals, builtins
103 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George7efc5b32014-04-05 22:36:42 +0100104 mp_map_elem_t *elem = mp_map_lookup(&dict_globals->map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100105 if (elem == NULL) {
Damien George7efc5b32014-04-05 22:36:42 +0100106 // TODO lookup in dynamic table of builtins first
107 elem = mp_map_lookup((mp_map_t*)&mp_builtin_object_dict_obj.map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
Damien429d7192013-10-04 19:53:11 +0100108 if (elem == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100109 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NameError, "name '%s' is not defined", qstr_str(qstr)));
Damien429d7192013-10-04 19:53:11 +0100110 }
111 }
112 return elem->value;
113}
114
Damien Georged17926d2014-03-30 13:35:08 +0100115mp_obj_t mp_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100116 DEBUG_OP_printf("load_build_class\n");
Damien George7efc5b32014-04-05 22:36:42 +0100117 // TODO lookup __build_class__ in dynamic table of builtins first
118 // ... else no user-defined __build_class__, return builtin one
119 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100120}
121
Damien Georged17926d2014-03-30 13:35:08 +0100122void mp_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100123 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George7efc5b32014-04-05 22:36:42 +0100124 mp_obj_dict_store(dict_locals, MP_OBJ_NEW_QSTR(qstr), obj);
Damiena3977762013-10-09 23:10:10 +0100125}
126
Damien Georged17926d2014-03-30 13:35:08 +0100127void mp_delete_name(qstr qstr) {
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200128 DEBUG_OP_printf("delete name %s\n", qstr_str(qstr));
Damien George1d24ea52014-04-08 21:11:49 +0100129 // TODO convert KeyError to NameError if qstr not found
130 mp_obj_dict_delete(dict_locals, MP_OBJ_NEW_QSTR(qstr));
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200131}
132
Damien Georged17926d2014-03-30 13:35:08 +0100133void mp_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100134 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George7efc5b32014-04-05 22:36:42 +0100135 mp_obj_dict_store(dict_globals, MP_OBJ_NEW_QSTR(qstr), obj);
Damien429d7192013-10-04 19:53:11 +0100136}
137
Damien George1d24ea52014-04-08 21:11:49 +0100138void mp_delete_global(qstr qstr) {
139 DEBUG_OP_printf("delete global %s\n", qstr_str(qstr));
140 // TODO convert KeyError to NameError if qstr not found
141 mp_obj_dict_delete(dict_globals, MP_OBJ_NEW_QSTR(qstr));
142}
143
Damien Georged17926d2014-03-30 13:35:08 +0100144mp_obj_t mp_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000145 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000146
Damiend99b0522013-12-21 18:17:45 +0000147 if (MP_OBJ_IS_SMALL_INT(arg)) {
148 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000149 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100150 case MP_UNARY_OP_BOOL:
Damien George9d68e9c2014-03-12 15:38:15 +0000151 return MP_BOOL(val != 0);
Damien Georged17926d2014-03-30 13:35:08 +0100152 case MP_UNARY_OP_POSITIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000153 return arg;
Damien Georged17926d2014-03-30 13:35:08 +0100154 case MP_UNARY_OP_NEGATIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000155 // check for overflow
156 if (val == MP_SMALL_INT_MIN) {
157 return mp_obj_new_int(-val);
158 } else {
159 return MP_OBJ_NEW_SMALL_INT(-val);
160 }
Damien Georged17926d2014-03-30 13:35:08 +0100161 case MP_UNARY_OP_INVERT:
Damien George9d68e9c2014-03-12 15:38:15 +0000162 return MP_OBJ_NEW_SMALL_INT(~val);
163 default:
164 assert(0);
165 return arg;
Damien7410e442013-11-02 19:47:57 +0000166 }
Damien George1e708fe2014-01-23 18:27:51 +0000167 } else {
168 mp_obj_type_t *type = mp_obj_get_type(arg);
169 if (type->unary_op != NULL) {
170 mp_obj_t result = type->unary_op(op, arg);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100171 if (result != MP_OBJ_NOT_SUPPORTED) {
Damiend99b0522013-12-21 18:17:45 +0000172 return result;
173 }
Damien7410e442013-11-02 19:47:57 +0000174 }
Damiend99b0522013-12-21 18:17:45 +0000175 // TODO specify in error message what the operator is
Damien Georgeea13f402014-04-05 18:32:08 +0100176 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bad operand type for unary operator: '%s'", mp_obj_get_type_str(arg)));
Damien7410e442013-11-02 19:47:57 +0000177 }
Damien429d7192013-10-04 19:53:11 +0100178}
179
Damien Georged17926d2014-03-30 13:35:08 +0100180mp_obj_t mp_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100181 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000182
183 // TODO correctly distinguish inplace operators for mutable objects
184 // lookup logic that CPython uses for +=:
185 // check for implemented +=
186 // then check for implemented +
187 // then check for implemented seq.inplace_concat
188 // then check for implemented seq.concat
189 // then fail
190 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
191
Damien George9aa2a522014-02-01 23:04:09 +0000192 // deal with is
Damien Georged17926d2014-03-30 13:35:08 +0100193 if (op == MP_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200194 return MP_BOOL(lhs == rhs);
195 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200196
Damien Georgebcbeea02014-01-11 10:47:22 +0000197 // deal with == and != for all types
Damien Georged17926d2014-03-30 13:35:08 +0100198 if (op == MP_BINARY_OP_EQUAL || op == MP_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000199 if (mp_obj_equal(lhs, rhs)) {
Damien Georged17926d2014-03-30 13:35:08 +0100200 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000201 return mp_const_true;
202 } else {
203 return mp_const_false;
204 }
205 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100206 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000207 return mp_const_false;
208 } else {
209 return mp_const_true;
210 }
211 }
212 }
213
214 // deal with exception_match for all types
Damien Georged17926d2014-03-30 13:35:08 +0100215 if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000216 // rhs must be issubclass(rhs, BaseException)
217 if (mp_obj_is_exception_type(rhs)) {
218 // if lhs is an instance of an exception, then extract and use its type
219 if (mp_obj_is_exception_instance(lhs)) {
220 lhs = mp_obj_get_type(lhs);
221 }
Damien George71510152014-03-03 22:38:13 +0000222 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000223 return mp_const_true;
224 } else {
225 return mp_const_false;
226 }
227 }
Paul Sokolovsky4b2b7ce2014-03-23 20:49:39 +0200228 assert(0);
229 return mp_const_false;
Damien Georgebcbeea02014-01-11 10:47:22 +0000230 }
231
Damien George1a9951d2014-01-06 22:13:00 +0000232 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000233 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000234 if (MP_OBJ_IS_SMALL_INT(rhs)) {
235 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000236 // This is a binary operation: lhs_val op rhs_val
237 // We need to be careful to handle overflow; see CERT INT32-C
238 // Operations that can overflow:
239 // + result always fits in machine_int_t, then handled by SMALL_INT check
240 // - result always fits in machine_int_t, then handled by SMALL_INT check
241 // * checked explicitly
242 // / if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
243 // % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
244 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000245 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100246 case MP_BINARY_OP_OR:
247 case MP_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
248 case MP_BINARY_OP_XOR:
249 case MP_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
250 case MP_BINARY_OP_AND:
251 case MP_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
252 case MP_BINARY_OP_LSHIFT:
253 case MP_BINARY_OP_INPLACE_LSHIFT: {
Damien George9d68e9c2014-03-12 15:38:15 +0000254 if (rhs_val < 0) {
255 // negative shift not allowed
Damien Georgeea13f402014-04-05 18:32:08 +0100256 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
Damien George9d68e9c2014-03-12 15:38:15 +0000257 } else if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
258 // left-shift will overflow, so use higher precision integer
259 lhs = mp_obj_new_int_from_ll(lhs_val);
260 goto generic_binary_op;
261 } else {
262 // use standard precision
263 lhs_val <<= rhs_val;
264 }
265 break;
266 }
Damien Georged17926d2014-03-30 13:35:08 +0100267 case MP_BINARY_OP_RSHIFT:
268 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000269 if (rhs_val < 0) {
270 // negative shift not allowed
Damien Georgeea13f402014-04-05 18:32:08 +0100271 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
Damien George9d68e9c2014-03-12 15:38:15 +0000272 } else {
273 // standard precision is enough for right-shift
274 lhs_val >>= rhs_val;
275 }
276 break;
Damien Georged17926d2014-03-30 13:35:08 +0100277 case MP_BINARY_OP_ADD:
278 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
279 case MP_BINARY_OP_SUBTRACT:
280 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
281 case MP_BINARY_OP_MULTIPLY:
282 case MP_BINARY_OP_INPLACE_MULTIPLY: {
Damien George9d68e9c2014-03-12 15:38:15 +0000283
284 // If long long type exists and is larger than machine_int_t, then
285 // we can use the following code to perform overflow-checked multiplication.
Damien Georgeecf5b772014-04-04 11:13:51 +0000286 // Otherwise (eg in x64 case) we must use mp_small_int_mul_overflow.
Damien George9d68e9c2014-03-12 15:38:15 +0000287 #if 0
288 // compute result using long long precision
289 long long res = (long long)lhs_val * (long long)rhs_val;
290 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
291 // result overflowed SMALL_INT, so return higher precision integer
292 return mp_obj_new_int_from_ll(res);
293 } else {
294 // use standard precision
295 lhs_val = (mp_small_int_t)res;
296 }
297 #endif
298
Damien Georgeecf5b772014-04-04 11:13:51 +0000299 if (mp_small_int_mul_overflow(lhs_val, rhs_val)) {
300 // use higher precision
301 lhs = mp_obj_new_int_from_ll(lhs_val);
302 goto generic_binary_op;
303 } else {
304 // use standard precision
305 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
306 }
Damien George9d68e9c2014-03-12 15:38:15 +0000307 break;
308 }
Damien Georged17926d2014-03-30 13:35:08 +0100309 case MP_BINARY_OP_FLOOR_DIVIDE:
310 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300311 if (rhs_val == 0) {
312 goto zero_division;
313 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000314 lhs_val = mp_small_int_floor_divide(lhs_val, rhs_val);
Rachel Dowdall56402792014-03-22 20:19:24 +0000315 break;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300316
Damien George9d68e9c2014-03-12 15:38:15 +0000317 #if MICROPY_ENABLE_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100318 case MP_BINARY_OP_TRUE_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300319 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
320 if (rhs_val == 0) {
Damien George70f33cd2014-04-02 17:06:05 +0100321 goto zero_division;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300322 }
323 return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000324 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000325
Damien Georged17926d2014-03-30 13:35:08 +0100326 case MP_BINARY_OP_MODULO:
Damien Georgeecf5b772014-04-04 11:13:51 +0000327 case MP_BINARY_OP_INPLACE_MODULO: {
328 lhs_val = mp_small_int_modulo(lhs_val, rhs_val);
Rachel Dowdallcde86312014-03-22 17:29:27 +0000329 break;
330 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000331
Damien Georged17926d2014-03-30 13:35:08 +0100332 case MP_BINARY_OP_POWER:
333 case MP_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000334 if (rhs_val < 0) {
335 #if MICROPY_ENABLE_FLOAT
336 lhs = mp_obj_new_float(lhs_val);
337 goto generic_binary_op;
338 #else
Damien Georgeea13f402014-04-05 18:32:08 +0100339 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
Damien George9d68e9c2014-03-12 15:38:15 +0000340 #endif
341 } else {
Damien George9d68e9c2014-03-12 15:38:15 +0000342 machine_int_t ans = 1;
343 while (rhs_val > 0) {
344 if (rhs_val & 1) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000345 if (mp_small_int_mul_overflow(ans, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100346 goto power_overflow;
347 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000348 ans *= lhs_val;
Damien George9d68e9c2014-03-12 15:38:15 +0000349 }
Damien George5bf565e2014-04-04 00:16:32 +0100350 if (rhs_val == 1) {
351 break;
352 }
Damien George9d68e9c2014-03-12 15:38:15 +0000353 rhs_val /= 2;
Damien Georgeecf5b772014-04-04 11:13:51 +0000354 if (mp_small_int_mul_overflow(lhs_val, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100355 goto power_overflow;
356 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000357 lhs_val *= lhs_val;
Damien George1a9951d2014-01-06 22:13:00 +0000358 }
Damien George9d68e9c2014-03-12 15:38:15 +0000359 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000360 }
Damien George1a9951d2014-01-06 22:13:00 +0000361 break;
Damien George5bf565e2014-04-04 00:16:32 +0100362
363 power_overflow:
364 // use higher precision
365 lhs = mp_obj_new_int_from_ll(MP_OBJ_SMALL_INT_VALUE(lhs));
366 goto generic_binary_op;
367
Damien Georged17926d2014-03-30 13:35:08 +0100368 case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
369 case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
370 case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
371 case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000372
Damien George8bcb9862014-04-17 16:26:50 +0100373 default:
374 goto unsupported_op;
Damien George1a9951d2014-01-06 22:13:00 +0000375 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200376 // TODO: We just should make mp_obj_new_int() inline and use that
377 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000378 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000379 } else {
380 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000381 }
Damien George3f759b72014-01-31 00:42:12 +0000382#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000383 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien Georgeae491052014-04-10 20:08:11 +0100384 mp_obj_t res = mp_obj_float_binary_op(op, lhs_val, rhs);
385 if (res == MP_OBJ_NULL) {
386 goto unsupported_op;
387 } else {
388 return res;
389 }
Damien George0c36da02014-03-08 15:24:39 +0000390 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien Georgeae491052014-04-10 20:08:11 +0100391 mp_obj_t res = mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
392 if (res == MP_OBJ_NULL) {
393 goto unsupported_op;
394 } else {
395 return res;
396 }
Damien George3f759b72014-01-31 00:42:12 +0000397#endif
Damien429d7192013-10-04 19:53:11 +0100398 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000399 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000400
Damien George9aa2a522014-02-01 23:04:09 +0000401 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000402 *
403 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000404 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000405 */
Damien Georged17926d2014-03-30 13:35:08 +0100406 if (op == MP_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000407 mp_obj_type_t *type = mp_obj_get_type(rhs);
408 if (type->binary_op != NULL) {
409 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100410 if (res != MP_OBJ_NOT_SUPPORTED) {
Damien George5fa93b62014-01-22 14:35:10 +0000411 return res;
412 }
413 }
414 if (type->getiter != NULL) {
415 /* second attempt, walk the iterator */
416 mp_obj_t next = NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100417 mp_obj_t iter = mp_getiter(rhs);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100418 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien George5fa93b62014-01-22 14:35:10 +0000419 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000420 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000421 }
Damien7410e442013-11-02 19:47:57 +0000422 }
Damien George9aa2a522014-02-01 23:04:09 +0000423 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000424 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000425
Damien Georgeea13f402014-04-05 18:32:08 +0100426 nlr_raise(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000427 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000428 mp_obj_get_type_str(rhs)));
429 return mp_const_none;
430 }
431
Damien George5fa93b62014-01-22 14:35:10 +0000432 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000433 mp_obj_type_t *type;
434generic_binary_op:
435 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000436 if (type->binary_op != NULL) {
437 mp_obj_t result = type->binary_op(op, lhs, rhs);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100438 if (result != MP_OBJ_NOT_SUPPORTED) {
Damien George5fa93b62014-01-22 14:35:10 +0000439 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000440 }
Damien429d7192013-10-04 19:53:11 +0100441 }
Damiend99b0522013-12-21 18:17:45 +0000442
Damien George5fa93b62014-01-22 14:35:10 +0000443 // TODO implement dispatch for reverse binary ops
444
Damiend99b0522013-12-21 18:17:45 +0000445 // TODO specify in error message what the operator is
Damien Georgeae491052014-04-10 20:08:11 +0100446unsupported_op:
Damien Georgeea13f402014-04-05 18:32:08 +0100447 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200448 "unsupported operand types for binary operator: '%s', '%s'",
449 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000450 return mp_const_none;
Damien George70f33cd2014-04-02 17:06:05 +0100451
452zero_division:
Damien Georgeea13f402014-04-05 18:32:08 +0100453 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
Damien429d7192013-10-04 19:53:11 +0100454}
455
Damien Georged17926d2014-03-30 13:35:08 +0100456mp_obj_t mp_call_function_0(mp_obj_t fun) {
457 return mp_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100458}
459
Damien Georged17926d2014-03-30 13:35:08 +0100460mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
461 return mp_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100462}
463
Damien Georged17926d2014-03-30 13:35:08 +0100464mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
Damiend99b0522013-12-21 18:17:45 +0000465 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000466 args[0] = arg1;
467 args[1] = arg2;
Damien Georged17926d2014-03-30 13:35:08 +0100468 return mp_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100469}
470
Damien Georgecd82e022014-02-02 13:11:48 +0000471// wrapper that accepts n_args and n_kw in one argument
472// native emitter can only pass at most 3 arguments to a function
Damien Georged17926d2014-03-30 13:35:08 +0100473mp_obj_t mp_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
474 return mp_call_function_n_kw(fun_in, n_args_kw & 0xff, (n_args_kw >> 8) & 0xff, args);
Damien Georgecd82e022014-02-02 13:11:48 +0000475}
476
Damien George20006db2014-01-18 14:10:48 +0000477// args contains, eg: arg0 arg1 key0 value0 key1 value1
Damien Georged17926d2014-03-30 13:35:08 +0100478mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damiend99b0522013-12-21 18:17:45 +0000479 // TODO improve this: fun object can specify its type and we parse here the arguments,
480 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100481
John R. Lenton9c83ec02014-01-07 23:06:46 +0000482 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
483
Damien George8b56beb2014-01-31 23:49:49 +0000484 // get the type
485 mp_obj_type_t *type = mp_obj_get_type(fun_in);
486
487 // do the call
488 if (type->call != NULL) {
Paul Sokolovsky755565d2014-04-25 21:15:16 +0300489 mp_obj_t res = type->call(fun_in, n_args, n_kw, args);
490 if (res != NULL) {
491 return res;
492 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000493 }
Paul Sokolovsky755565d2014-04-25 21:15:16 +0300494
495 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not callable", mp_obj_get_type_str(fun_in)));
Damien86c7fc72013-11-26 15:16:41 +0000496}
497
Damien George20006db2014-01-18 14:10:48 +0000498// args contains: fun self/NULL arg(0) ... arg(n_args-2) arg(n_args-1) kw_key(0) kw_val(0) ... kw_key(n_kw-1) kw_val(n_kw-1)
499// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damien Georged17926d2014-03-30 13:35:08 +0100500mp_obj_t mp_call_method_n_kw(uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +0000501 DEBUG_OP_printf("call method (fun=%p, self=%p, n_args=%u, n_kw=%u, args=%p)\n", args[0], args[1], n_args, n_kw, args);
502 int adjust = (args[1] == NULL) ? 0 : 1;
Damien Georged17926d2014-03-30 13:35:08 +0100503 return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000504}
505
Damien George523b5752014-03-31 11:59:23 +0100506mp_obj_t mp_call_method_n_kw_var(bool have_self, uint n_args_n_kw, const mp_obj_t *args) {
Damien George230fec72014-03-30 21:21:24 +0100507 mp_obj_t fun = *args++;
508 mp_obj_t self = MP_OBJ_NULL;
509 if (have_self) {
510 self = *args++; // may be MP_OBJ_NULL
511 }
512 uint n_args = n_args_n_kw & 0xff;
513 uint n_kw = (n_args_n_kw >> 8) & 0xff;
Damien George523b5752014-03-31 11:59:23 +0100514 mp_obj_t pos_seq = args[n_args + 2 * n_kw]; // map be MP_OBJ_NULL
515 mp_obj_t kw_dict = args[n_args + 2 * n_kw + 1]; // map be MP_OBJ_NULL
Damien George230fec72014-03-30 21:21:24 +0100516
517 DEBUG_OP_printf("call method var (fun=%p, self=%p, n_args=%u, n_kw=%u, args=%p, seq=%p, dict=%p)\n", fun, self, n_args, n_kw, args, pos_seq, kw_dict);
518
519 // We need to create the following array of objects:
520 // args[0 .. n_args] unpacked(pos_seq) args[n_args .. n_args + 2 * n_kw] unpacked(kw_dict)
521 // TODO: optimize one day to avoid constructing new arg array? Will be hard.
522
523 // The new args array
524 mp_obj_t *args2;
525 uint args2_alloc;
526 uint args2_len = 0;
527
528 // Try to get a hint for the size of the kw_dict
529 uint kw_dict_len = 0;
530 if (kw_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
531 kw_dict_len = mp_obj_dict_len(kw_dict);
532 }
533
534 // Extract the pos_seq sequence to the new args array.
535 // Note that it can be arbitrary iterator.
536 if (pos_seq == MP_OBJ_NULL) {
537 // no sequence
538
539 // allocate memory for the new array of args
540 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len);
541 args2 = m_new(mp_obj_t, args2_alloc);
542
543 // copy the self
544 if (self != MP_OBJ_NULL) {
545 args2[args2_len++] = self;
546 }
547
548 // copy the fixed pos args
549 m_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
550 args2_len += n_args;
551
552 } else if (MP_OBJ_IS_TYPE(pos_seq, &mp_type_tuple) || MP_OBJ_IS_TYPE(pos_seq, &mp_type_list)) {
553 // optimise the case of a tuple and list
554
555 // get the items
556 uint len;
557 mp_obj_t *items;
558 mp_obj_get_array(pos_seq, &len, &items);
559
560 // allocate memory for the new array of args
561 args2_alloc = 1 + n_args + len + 2 * (n_kw + kw_dict_len);
562 args2 = m_new(mp_obj_t, args2_alloc);
563
564 // copy the self
565 if (self != MP_OBJ_NULL) {
566 args2[args2_len++] = self;
567 }
568
569 // copy the fixed and variable position args
570 m_seq_cat(args2 + args2_len, args, n_args, items, len, mp_obj_t);
571 args2_len += n_args + len;
572
573 } else {
574 // generic iterator
575
576 // allocate memory for the new array of args
577 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len) + 3;
578 args2 = m_new(mp_obj_t, args2_alloc);
579
580 // copy the self
581 if (self != MP_OBJ_NULL) {
582 args2[args2_len++] = self;
583 }
584
585 // copy the fixed position args
586 m_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
587
588 // extract the variable position args from the iterator
589 mp_obj_t iterable = mp_getiter(pos_seq);
590 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100591 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George230fec72014-03-30 21:21:24 +0100592 if (args2_len >= args2_alloc) {
593 args2 = m_renew(mp_obj_t, args2, args2_alloc, args2_alloc * 2);
594 args2_alloc *= 2;
595 }
596 args2[args2_len++] = item;
597 }
598 }
599
600 // The size of the args2 array now is the number of positional args.
601 uint pos_args_len = args2_len;
602
603 // Copy the fixed kw args.
604 m_seq_copy(args2 + args2_len, args + n_args, 2 * n_kw, mp_obj_t);
605 args2_len += 2 * n_kw;
606
607 // Extract (key,value) pairs from kw_dict dictionary and append to args2.
608 // Note that it can be arbitrary iterator.
609 if (kw_dict == MP_OBJ_NULL) {
610 // pass
611 } else if (MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
612 // dictionary
613 mp_map_t *map = mp_obj_dict_get_map(kw_dict);
614 assert(args2_len + 2 * map->used <= args2_alloc); // should have enough, since kw_dict_len is in this case hinted correctly above
615 for (uint i = 0; i < map->alloc; i++) {
616 if (map->table[i].key != MP_OBJ_NULL) {
617 args2[args2_len++] = map->table[i].key;
618 args2[args2_len++] = map->table[i].value;
619 }
620 }
621 } else {
622 // generic mapping
623 // TODO is calling 'items' on the mapping the correct thing to do here?
624 mp_obj_t dest[2];
625 mp_load_method(kw_dict, MP_QSTR_items, dest);
626 mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest));
627 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100628 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George230fec72014-03-30 21:21:24 +0100629 if (args2_len + 1 >= args2_alloc) {
630 uint new_alloc = args2_alloc * 2;
631 if (new_alloc < 4) {
632 new_alloc = 4;
633 }
634 args2 = m_renew(mp_obj_t, args2, args2_alloc, new_alloc);
635 args2_alloc = new_alloc;
636 }
637 mp_obj_t *items;
638 mp_obj_get_array_fixed_n(item, 2, &items);
639 args2[args2_len++] = items[0];
640 args2[args2_len++] = items[1];
641 }
642 }
643
644 mp_obj_t res = mp_call_function_n_kw(fun, pos_args_len, (args2_len - pos_args_len) / 2, args2);
645 m_del(mp_obj_t, args2, args2_alloc);
646
647 return res;
648}
649
Damien George932bf1c2014-01-18 23:42:49 +0000650// unpacked items are stored in reverse order into the array pointed to by items
Damien Georged17926d2014-03-30 13:35:08 +0100651void mp_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200652 uint seq_len;
Damien George3e1a5c12014-03-29 13:43:38 +0000653 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
Damiend99b0522013-12-21 18:17:45 +0000654 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000655 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000656 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
657 } else {
658 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000659 }
Damiend99b0522013-12-21 18:17:45 +0000660 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200661 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000662 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200663 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000664 }
Damien George932bf1c2014-01-18 23:42:49 +0000665 for (uint i = 0; i < num; i++) {
666 items[i] = seq_items[num - 1 - i];
667 }
Damien86c7fc72013-11-26 15:16:41 +0000668 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100669 mp_obj_t iterable = mp_getiter(seq_in);
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200670
671 for (seq_len = 0; seq_len < num; seq_len++) {
Damien Georged17926d2014-03-30 13:35:08 +0100672 mp_obj_t el = mp_iternext(iterable);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100673 if (el == MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200674 goto too_short;
675 }
676 items[num - 1 - seq_len] = el;
677 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100678 if (mp_iternext(iterable) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200679 goto too_long;
680 }
Damien86c7fc72013-11-26 15:16:41 +0000681 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200682 return;
683
684too_short:
Damien Georgeea13f402014-04-05 18:32:08 +0100685 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", seq_len));
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200686too_long:
Damien Georgeea13f402014-04-05 18:32:08 +0100687 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "too many values to unpack (expected %d)", num));
Damien86c7fc72013-11-26 15:16:41 +0000688}
689
Damien George495d7812014-04-08 17:51:47 +0100690// unpacked items are stored in reverse order into the array pointed to by items
691void mp_unpack_ex(mp_obj_t seq_in, uint num_in, mp_obj_t *items) {
692 uint num_left = num_in & 0xff;
693 uint num_right = (num_in >> 8) & 0xff;
694 DEBUG_OP_printf("unpack ex %d %d\n", num_left, num_right);
695 uint seq_len;
696 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
697 mp_obj_t *seq_items;
698 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
699 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
700 } else {
701 if (num_left == 0 && num_right == 0) {
702 // *a, = b # sets a to b if b is a list
703 items[0] = seq_in;
704 return;
705 }
706 mp_obj_list_get(seq_in, &seq_len, &seq_items);
707 }
708 if (seq_len < num_left + num_right) {
709 goto too_short;
710 }
711 for (uint i = 0; i < num_right; i++) {
712 items[i] = seq_items[seq_len - 1 - i];
713 }
714 items[num_right] = mp_obj_new_list(seq_len - num_left - num_right, seq_items + num_left);
715 for (uint i = 0; i < num_left; i++) {
716 items[num_right + 1 + i] = seq_items[num_left - 1 - i];
717 }
718 } else {
719 // Generic iterable; this gets a bit messy: we unpack known left length to the
720 // items destination array, then the rest to a dynamically created list. Once the
721 // iterable is exhausted, we take from this list for the right part of the items.
722 // TODO Improve to waste less memory in the dynamically created list.
723 mp_obj_t iterable = mp_getiter(seq_in);
724 mp_obj_t item;
725 for (seq_len = 0; seq_len < num_left; seq_len++) {
726 item = mp_iternext(iterable);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100727 if (item == MP_OBJ_STOP_ITERATION) {
Damien George495d7812014-04-08 17:51:47 +0100728 goto too_short;
729 }
730 items[num_left + num_right + 1 - 1 - seq_len] = item;
731 }
732 mp_obj_t rest = mp_obj_new_list(0, NULL);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100733 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George495d7812014-04-08 17:51:47 +0100734 mp_obj_list_append(rest, item);
735 }
736 uint rest_len;
737 mp_obj_t *rest_items;
738 mp_obj_list_get(rest, &rest_len, &rest_items);
739 if (rest_len < num_right) {
740 goto too_short;
741 }
742 items[num_right] = rest;
743 for (uint i = 0; i < num_right; i++) {
744 items[num_right - 1 - i] = rest_items[rest_len - num_right + i];
745 }
746 mp_obj_list_set_len(rest, rest_len - num_right);
747 }
748 return;
749
750too_short:
751 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", seq_len));
752}
753
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300754mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000755 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300756 // use load_method
Damien George062478e2014-01-09 20:57:50 +0000757 mp_obj_t dest[2];
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300758 mp_load_method(base, attr, dest);
759 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000760 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000761 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000762 } else {
763 // load_method returned a method, so build a bound method object
764 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000765 }
Damiend99b0522013-12-21 18:17:45 +0000766}
767
Damien George7c9c6672014-01-25 00:17:36 +0000768// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
769// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
770// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Damien Georgee44d26a2014-03-31 22:57:56 +0100771void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000772 // clear output to indicate no attribute/method found yet
773 dest[0] = MP_OBJ_NULL;
774 dest[1] = MP_OBJ_NULL;
775
776 // get the type
777 mp_obj_type_t *type = mp_obj_get_type(base);
778
Damien Georgee44d26a2014-03-31 22:57:56 +0100779 // look for built-in names
780 if (0) {
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +0300781#if MICROPY_CPYTHON_COMPAT
Damien Georgee44d26a2014-03-31 22:57:56 +0100782 } else if (attr == MP_QSTR___class__) {
783 // a.__class__ is equivalent to type(a)
784 dest[0] = type;
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +0300785#endif
Damien Georgee44d26a2014-03-31 22:57:56 +0100786
787 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
788 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
789 dest[1] = base;
790
791 } else if (type->load_attr != NULL) {
792 // this type can do its own load, so call it
793 type->load_attr(base, attr, dest);
794
795 } else if (type->locals_dict != NULL) {
796 // generic method lookup
797 // this is a lookup in the object (ie not class or type)
798 assert(MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)); // Micro Python restriction, for now
799 mp_map_t *locals_map = mp_obj_dict_get_map(type->locals_dict);
800 mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
801 if (elem != NULL) {
802 // check if the methods are functions, static or class methods
803 // see http://docs.python.org/3.3/howto/descriptor.html
804 if (MP_OBJ_IS_TYPE(elem->value, &mp_type_staticmethod)) {
805 // return just the function
806 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
807 } else if (MP_OBJ_IS_TYPE(elem->value, &mp_type_classmethod)) {
808 // return a bound method, with self being the type of this object
809 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
810 dest[1] = mp_obj_get_type(base);
811 } else if (mp_obj_is_callable(elem->value)) {
812 // return a bound method, with self being this object
813 dest[0] = elem->value;
814 dest[1] = base;
815 } else {
816 // class member is a value, so just return that value
817 dest[0] = elem->value;
Damiend57eba52013-11-02 23:58:14 +0000818 }
819 }
Damiena3977762013-10-09 23:10:10 +0100820 }
Damien George7c9c6672014-01-25 00:17:36 +0000821}
Damiena3977762013-10-09 23:10:10 +0100822
Damien Georged17926d2014-03-30 13:35:08 +0100823void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George7c9c6672014-01-25 00:17:36 +0000824 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
825
Damien Georged17926d2014-03-30 13:35:08 +0100826 mp_load_method_maybe(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000827
828 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000829 // no attribute/method called attr
830 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000831 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
Damien Georgeea13f402014-04-05 18:32:08 +0100832 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
Paul Sokolovsky7f8b3132014-03-25 00:55:39 +0200833 "type object '%s' has no attribute '%s'", qstr_str(((mp_obj_type_t*)base)->name), qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +0000834 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100835 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damien George062478e2014-01-09 20:57:50 +0000836 }
837 }
Damiena3977762013-10-09 23:10:10 +0100838}
839
Damien Georged17926d2014-03-30 13:35:08 +0100840void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100841 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000842 mp_obj_type_t *type = mp_obj_get_type(base);
843 if (type->store_attr != NULL) {
844 if (type->store_attr(base, attr, value)) {
845 return;
846 }
Damiena3977762013-10-09 23:10:10 +0100847 }
Damien Georgeea13f402014-04-05 18:32:08 +0100848 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
Damiena3977762013-10-09 23:10:10 +0100849}
850
Damien Georged17926d2014-03-30 13:35:08 +0100851mp_obj_t mp_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000852 mp_obj_type_t *type = mp_obj_get_type(o_in);
853 if (type->getiter != NULL) {
854 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +0100855 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000856 // check for __iter__ method
Damien George7c9c6672014-01-25 00:17:36 +0000857 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100858 mp_load_method_maybe(o_in, MP_QSTR___iter__, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000859 if (dest[0] != MP_OBJ_NULL) {
Damien George9e6e9352014-03-26 18:37:06 +0000860 // __iter__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100861 return mp_call_method_n_kw(0, 0, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000862 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100863 mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000864 if (dest[0] != MP_OBJ_NULL) {
865 // __getitem__ exists, create an iterator
866 return mp_obj_new_getitem_iter(dest);
867 } else {
868 // object not iterable
Damien Georgeea13f402014-04-05 18:32:08 +0100869 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
Damien George9e6e9352014-03-26 18:37:06 +0000870 }
Damien George7c9c6672014-01-25 00:17:36 +0000871 }
Damience89a212013-10-15 22:25:17 +0100872 }
873}
874
Damien Georgeea8d06c2014-04-17 23:19:36 +0100875// may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration()
Damien George66eaf842014-03-26 19:27:58 +0000876// may also raise StopIteration()
Damien Georged17926d2014-03-30 13:35:08 +0100877mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000878 mp_obj_type_t *type = mp_obj_get_type(o_in);
879 if (type->iternext != NULL) {
880 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100881 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000882 // check for __next__ method
883 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100884 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000885 if (dest[0] != MP_OBJ_NULL) {
886 // __next__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100887 return mp_call_method_n_kw(0, 0, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000888 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100889 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
Damien George9e6e9352014-03-26 18:37:06 +0000890 }
Damien Georgec5966122014-02-15 16:10:44 +0000891 }
892}
893
Damien Georgeea8d06c2014-04-17 23:19:36 +0100894// will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration() (or any subclass thereof)
Damien George66eaf842014-03-26 19:27:58 +0000895// may raise other exceptions
Damien Georged17926d2014-03-30 13:35:08 +0100896mp_obj_t mp_iternext(mp_obj_t o_in) {
Damien George66eaf842014-03-26 19:27:58 +0000897 mp_obj_type_t *type = mp_obj_get_type(o_in);
898 if (type->iternext != NULL) {
899 return type->iternext(o_in);
900 } else {
901 // check for __next__ method
902 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100903 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George66eaf842014-03-26 19:27:58 +0000904 if (dest[0] != MP_OBJ_NULL) {
905 // __next__ exists, call it and return its result
906 nlr_buf_t nlr;
907 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100908 mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
Damien George66eaf842014-03-26 19:27:58 +0000909 nlr_pop();
910 return ret;
911 } else {
912 if (mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100913 return MP_OBJ_STOP_ITERATION;
Damien George66eaf842014-03-26 19:27:58 +0000914 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100915 nlr_raise(nlr.ret_val);
Damien George66eaf842014-03-26 19:27:58 +0000916 }
917 }
918 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100919 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
Damien George66eaf842014-03-26 19:27:58 +0000920 }
921 }
922}
923
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300924// TODO: Unclear what to do with StopIterarion exception here.
925mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val) {
Paul Sokolovsky7da06602014-03-31 04:19:12 +0300926 assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL));
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300927 mp_obj_type_t *type = mp_obj_get_type(self_in);
928
929 if (type == &mp_type_gen_instance) {
930 return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
931 }
932
933 if (type->iternext != NULL && send_value == mp_const_none) {
934 mp_obj_t ret = type->iternext(self_in);
935 if (ret != MP_OBJ_NULL) {
936 *ret_val = ret;
937 return MP_VM_RETURN_YIELD;
938 } else {
939 // Emulate raise StopIteration()
940 // Special case, handled in vm.c
941 *ret_val = MP_OBJ_NULL;
942 return MP_VM_RETURN_NORMAL;
943 }
944 }
945
946 mp_obj_t dest[3]; // Reserve slot for send() arg
947
948 if (send_value == mp_const_none) {
949 mp_load_method_maybe(self_in, MP_QSTR___next__, dest);
950 if (dest[0] != MP_OBJ_NULL) {
951 *ret_val = mp_call_method_n_kw(0, 0, dest);
952 return MP_VM_RETURN_YIELD;
953 }
954 }
955
956 if (send_value != MP_OBJ_NULL) {
957 mp_load_method(self_in, MP_QSTR_send, dest);
958 dest[2] = send_value;
959 *ret_val = mp_call_method_n_kw(1, 0, dest);
960 return MP_VM_RETURN_YIELD;
961 }
962
963 if (throw_value != MP_OBJ_NULL) {
964 if (mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) {
965 mp_load_method_maybe(self_in, MP_QSTR_close, dest);
966 if (dest[0] != MP_OBJ_NULL) {
967 *ret_val = mp_call_method_n_kw(0, 0, dest);
968 // We assume one can't "yield" from close()
969 return MP_VM_RETURN_NORMAL;
970 }
971 }
Paul Sokolovskya2109d92014-03-31 04:14:30 +0300972 mp_load_method_maybe(self_in, MP_QSTR_throw, dest);
973 if (dest[0] != MP_OBJ_NULL) {
974 *ret_val = mp_call_method_n_kw(1, 0, &throw_value);
975 // If .throw() method returned, we assume it's value to yield
Damien Georgeea13f402014-04-05 18:32:08 +0100976 // - any exception would be thrown with nlr_raise().
Paul Sokolovskya2109d92014-03-31 04:14:30 +0300977 return MP_VM_RETURN_YIELD;
978 }
979 // If there's nowhere to throw exception into, then we assume that object
980 // is just incapable to handle it, so any exception thrown into it
981 // will be propagated up. This behavior is approved by test_pep380.py
982 // test_delegation_of_close_to_non_generator(),
983 // test_delegating_throw_to_non_generator()
984 *ret_val = throw_value;
985 return MP_VM_RETURN_EXCEPTION;
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300986 }
987
988 assert(0);
989 return MP_VM_RETURN_NORMAL; // Should be unreachable
990}
991
Damien Georged17926d2014-03-30 13:35:08 +0100992mp_obj_t mp_make_raise_obj(mp_obj_t o) {
Damien Georgec5966122014-02-15 16:10:44 +0000993 DEBUG_printf("raise %p\n", o);
994 if (mp_obj_is_exception_type(o)) {
995 // o is an exception type (it is derived from BaseException (or is BaseException))
996 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +0000997 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
998 // could have const instances in ROM which we return here instead
Damien Georged17926d2014-03-30 13:35:08 +0100999 return mp_call_function_n_kw(o, 0, 0, NULL);
Damien Georgec5966122014-02-15 16:10:44 +00001000 } else if (mp_obj_is_exception_instance(o)) {
1001 // o is an instance of an exception, so use it as the exception
1002 return o;
1003 } else {
1004 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
1005 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +01001006 }
1007}
1008
Damien Georged17926d2014-03-30 13:35:08 +01001009mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +00001010 DEBUG_printf("import name %s\n", qstr_str(name));
1011
Damiendb4c3612013-12-10 17:27:24 +00001012 // build args array
Damiend99b0522013-12-21 18:17:45 +00001013 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +00001014 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +00001015 args[1] = mp_const_none; // TODO should be globals
1016 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +00001017 args[3] = fromlist;
1018 args[4] = level; // must be 0; we don't yet support other values
1019
1020 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +00001021 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001022}
1023
Damien Georged17926d2014-03-30 13:35:08 +01001024mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001025 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1026
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001027 mp_obj_t dest[2];
1028
1029 mp_load_method_maybe(module, name, dest);
1030
1031 if (dest[1] != MP_OBJ_NULL) {
1032 // Hopefully we can't import bound method from an object
1033import_error:
Paul Sokolovsky42453dc2014-04-12 03:00:40 +03001034 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "cannot import name %s", qstr_str(name)));
Damiendb4c3612013-12-10 17:27:24 +00001035 }
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001036
1037 if (dest[0] != MP_OBJ_NULL) {
1038 return dest[0];
1039 }
1040
1041 // See if it's a package, then can try FS import
1042 mp_load_method_maybe(module, MP_QSTR___path__, dest);
1043 if (dest[0] == MP_OBJ_NULL) {
1044 goto import_error;
1045 }
1046
1047 mp_load_method_maybe(module, MP_QSTR___name__, dest);
1048 uint pkg_name_len;
1049 const char *pkg_name = mp_obj_str_get_data(dest[0], &pkg_name_len);
1050
1051 char dot_name[pkg_name_len + 1 + qstr_len(name)];
1052 memcpy(dot_name, pkg_name, pkg_name_len);
1053 dot_name[pkg_name_len] = '.';
1054 memcpy(dot_name + pkg_name_len + 1, qstr_str(name), qstr_len(name));
1055 qstr dot_name_q = qstr_from_strn(dot_name, sizeof(dot_name));
1056
1057 mp_obj_t args[5];
1058 args[0] = MP_OBJ_NEW_QSTR(dot_name_q);
1059 args[1] = mp_const_none; // TODO should be globals
1060 args[2] = mp_const_none; // TODO should be locals
1061 args[3] = mp_const_true; // Pass sentinel "non empty" value to force returning of leaf module
Paul Sokolovsky69f18672014-04-12 02:47:46 +03001062 args[4] = MP_OBJ_NEW_SMALL_INT(0);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001063
1064 // TODO lookup __import__ and call that instead of going straight to builtin implementation
1065 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001066}
1067
Damien Georged17926d2014-03-30 13:35:08 +01001068void mp_import_all(mp_obj_t module) {
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001069 DEBUG_printf("import all %p\n", module);
1070
Paul Sokolovsky599bbc12014-04-18 04:11:19 +03001071 // TODO: Support __all__
Damien George8b0535e2014-04-05 21:53:54 +01001072 mp_map_t *map = mp_obj_dict_get_map(mp_obj_module_get_globals(module));
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001073 for (uint i = 0; i < map->alloc; i++) {
Damien George8b0535e2014-04-05 21:53:54 +01001074 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
Paul Sokolovsky599bbc12014-04-18 04:11:19 +03001075 qstr name = MP_OBJ_QSTR_VALUE(map->table[i].key);
1076 if (*qstr_str(name) != '_') {
1077 mp_store_name(name, map->table[i].value);
1078 }
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001079 }
1080 }
1081}
1082
Damien George7efc5b32014-04-05 22:36:42 +01001083mp_obj_dict_t *mp_locals_get(void) {
1084 return dict_locals;
Damien George66028ab2014-01-03 14:03:48 +00001085}
1086
Damien George7efc5b32014-04-05 22:36:42 +01001087void mp_locals_set(mp_obj_dict_t *d) {
1088 DEBUG_OP_printf("mp_locals_set(%p)\n", d);
1089 dict_locals = d;
Damien George66028ab2014-01-03 14:03:48 +00001090}
1091
Damien George7efc5b32014-04-05 22:36:42 +01001092mp_obj_dict_t *mp_globals_get(void) {
1093 return dict_globals;
Damien George66028ab2014-01-03 14:03:48 +00001094}
1095
Damien George7efc5b32014-04-05 22:36:42 +01001096void mp_globals_set(mp_obj_dict_t *d) {
1097 DEBUG_OP_printf("mp_globals_set(%p)\n", d);
1098 dict_globals = d;
Damien George66028ab2014-01-03 14:03:48 +00001099}
1100
Damien George6902eed2014-04-04 10:52:59 +00001101void *m_malloc_fail(int num_bytes) {
1102 DEBUG_printf("memory allocation failed, allocating %d bytes\n", num_bytes);
Damien Georgeea13f402014-04-05 18:32:08 +01001103 nlr_raise((mp_obj_t)&mp_const_MemoryError_obj);
Damien George6902eed2014-04-04 10:52:59 +00001104}
1105
Damien6ba13142013-11-02 20:34:54 +00001106// these must correspond to the respective enum
Damien Georged17926d2014-03-30 13:35:08 +01001107void *const mp_fun_table[MP_F_NUMBER_OF] = {
1108 mp_load_const_dec,
Damien Georgecdd96df2014-04-06 12:58:40 +01001109 mp_obj_new_int_from_long_str,
Damien Georged17926d2014-03-30 13:35:08 +01001110 mp_load_const_str,
1111 mp_load_name,
1112 mp_load_global,
1113 mp_load_build_class,
1114 mp_load_attr,
1115 mp_load_method,
1116 mp_store_name,
1117 mp_store_attr,
Damien George729f7b42014-04-17 22:10:53 +01001118 mp_obj_subscr,
Damien Georged17926d2014-03-30 13:35:08 +01001119 mp_obj_is_true,
1120 mp_unary_op,
1121 mp_binary_op,
Damien George15d18062014-03-31 16:28:13 +01001122 mp_obj_new_tuple,
1123 mp_obj_new_list,
1124 mp_obj_list_append,
1125 mp_obj_new_dict,
1126 mp_obj_dict_store,
1127 mp_obj_new_set,
1128 mp_obj_set_store,
Damien Georgedf8127a2014-04-13 11:04:33 +01001129 mp_make_function_from_raw_code,
Damien Georged17926d2014-03-30 13:35:08 +01001130 mp_call_function_n_kw_for_native,
1131 mp_call_method_n_kw,
1132 mp_getiter,
Damien Georgecdd96df2014-04-06 12:58:40 +01001133 mp_import_name,
1134 mp_import_from,
1135 mp_import_all,
1136 mp_obj_new_slice,
1137 mp_unpack_sequence,
Damien Georged17926d2014-03-30 13:35:08 +01001138 mp_iternext,
Damien429d7192013-10-04 19:53:11 +01001139};
1140
1141/*
Damien Georged17926d2014-03-30 13:35:08 +01001142void mp_f_vector(mp_fun_kind_t fun_kind) {
1143 (mp_f_table[fun_kind])();
Damien429d7192013-10-04 19:53:11 +01001144}
1145*/