blob: a2de2d75b1eab257a9e1e0f7622848528be72545 [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
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien429d7192013-10-04 19:53:11 +010027#include <stdio.h>
28#include <string.h>
29#include <assert.h>
stijn01d6be42014-05-05 12:18:27 +020030#include <alloca.h>
Damien429d7192013-10-04 19:53:11 +010031
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030032#include "mpconfig.h"
Damience89a212013-10-15 22:25:17 +010033#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010034#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000035#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000036#include "obj.h"
Damien George230fec72014-03-30 21:21:24 +010037#include "objtuple.h"
Damien Georgecaac5422014-03-25 14:18:18 +000038#include "objmodule.h"
Damien George20773972014-02-22 18:12:43 +000039#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000040#include "runtime0.h"
41#include "runtime.h"
Damien George2326d522014-03-27 23:26:35 +000042#include "emitglue.h"
Damien660365e2013-12-17 18:27:24 +000043#include "builtin.h"
Damien Georgecaac5422014-03-25 14:18:18 +000044#include "builtintables.h"
Damien George5fa93b62014-01-22 14:35:10 +000045#include "bc.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000046#include "smallint.h"
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +030047#include "objgenerator.h"
Damien660365e2013-12-17 18:27:24 +000048
Damien7f5dacf2013-10-10 11:24:39 +010049#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010050#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020051#define DEBUG_printf DEBUG_printf
Damien George41eb6082014-02-26 22:40:35 +000052#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
Damien7f5dacf2013-10-10 11:24:39 +010053#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000054#define DEBUG_printf(...) (void)0
55#define DEBUG_OP_printf(...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010056#endif
Damien429d7192013-10-04 19:53:11 +010057
Damieneb19efb2013-10-10 22:06:54 +010058// locals and globals need to be pointers because they can be the same in outer module scope
Damien George7efc5b32014-04-05 22:36:42 +010059STATIC mp_obj_dict_t *dict_locals;
60STATIC mp_obj_dict_t *dict_globals;
Damienbd254452013-10-16 20:39:12 +010061
Damien George7efc5b32014-04-05 22:36:42 +010062// dictionary for the __main__ module
Damien George8b0535e2014-04-05 21:53:54 +010063STATIC mp_obj_dict_t dict_main;
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030064
65const mp_obj_module_t mp_module___main__ = {
66 .base = { &mp_type_module },
67 .name = MP_QSTR___main__,
Damien George8b0535e2014-04-05 21:53:54 +010068 .globals = (mp_obj_dict_t*)&dict_main,
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030069};
70
Damien Georged17926d2014-03-30 13:35:08 +010071void mp_init(void) {
stijn72521a12014-05-03 11:28:29 +020072 // call port specific initialization if any
73#ifdef MICROPY_PORT_INIT_FUNC
74 MICROPY_PORT_INIT_FUNC;
75#endif
76
Damien George2326d522014-03-27 23:26:35 +000077 mp_emit_glue_init();
78
Damien Georgecaac5422014-03-25 14:18:18 +000079 // init global module stuff
80 mp_module_init();
Damien George0d028742014-01-22 23:59:20 +000081
Damien George7efc5b32014-04-05 22:36:42 +010082 // initialise the __main__ module
Damien George8b0535e2014-04-05 21:53:54 +010083 mp_obj_dict_init(&dict_main, 1);
Damien George8b0535e2014-04-05 21:53:54 +010084 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 +030085
86 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George7efc5b32014-04-05 22:36:42 +010087 dict_locals = dict_globals = &dict_main;
Damien429d7192013-10-04 19:53:11 +010088}
89
Damien Georged17926d2014-03-30 13:35:08 +010090void mp_deinit(void) {
Damien George7efc5b32014-04-05 22:36:42 +010091 //mp_obj_dict_free(&dict_main);
Damien George2326d522014-03-27 23:26:35 +000092 mp_module_deinit();
93 mp_emit_glue_deinit();
stijn5ed284a2014-05-08 10:56:33 +020094
95 // call port specific deinitialization if any
96#ifdef MICROPY_PORT_INIT_FUNC
97 MICROPY_PORT_DEINIT_FUNC;
98#endif
Damien429d7192013-10-04 19:53:11 +010099}
100
Damien Georged17926d2014-03-30 13:35:08 +0100101mp_obj_t mp_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000102 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +0000103 uint len;
104 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +0000105 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +0000106}
107
Damien Georged17926d2014-03-30 13:35:08 +0100108mp_obj_t mp_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100109 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +0000110 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +0100111}
112
Damien Georged17926d2014-03-30 13:35:08 +0100113mp_obj_t mp_load_const_bytes(qstr qstr) {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200114 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
115 uint len;
116 const byte *data = qstr_data(qstr, &len);
117 return mp_obj_new_bytes(data, len);
118}
119
Damien Georged17926d2014-03-30 13:35:08 +0100120mp_obj_t mp_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100121 // logic: search locals, globals, builtins
Damien George7efc5b32014-04-05 22:36:42 +0100122 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Paul Sokolovskya0d32992014-04-05 04:51:26 +0300123 // If we're at the outer scope (locals == globals), dispatch to load_global right away
Damien George7efc5b32014-04-05 22:36:42 +0100124 if (dict_locals != dict_globals) {
125 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 +0300126 if (elem != NULL) {
127 return elem->value;
128 }
Damiena3977762013-10-09 23:10:10 +0100129 }
Paul Sokolovskya0d32992014-04-05 04:51:26 +0300130 return mp_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100131}
132
Damien Georged17926d2014-03-30 13:35:08 +0100133mp_obj_t mp_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100134 // logic: search globals, builtins
135 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George7efc5b32014-04-05 22:36:42 +0100136 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 +0100137 if (elem == NULL) {
Damien George7efc5b32014-04-05 22:36:42 +0100138 // TODO lookup in dynamic table of builtins first
139 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 +0100140 if (elem == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100141 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 +0100142 }
143 }
144 return elem->value;
145}
146
Damien Georged17926d2014-03-30 13:35:08 +0100147mp_obj_t mp_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100148 DEBUG_OP_printf("load_build_class\n");
Damien George7efc5b32014-04-05 22:36:42 +0100149 // TODO lookup __build_class__ in dynamic table of builtins first
150 // ... else no user-defined __build_class__, return builtin one
151 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100152}
153
Damien Georged17926d2014-03-30 13:35:08 +0100154void mp_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100155 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George7efc5b32014-04-05 22:36:42 +0100156 mp_obj_dict_store(dict_locals, MP_OBJ_NEW_QSTR(qstr), obj);
Damiena3977762013-10-09 23:10:10 +0100157}
158
Damien Georged17926d2014-03-30 13:35:08 +0100159void mp_delete_name(qstr qstr) {
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200160 DEBUG_OP_printf("delete name %s\n", qstr_str(qstr));
Damien George1d24ea52014-04-08 21:11:49 +0100161 // TODO convert KeyError to NameError if qstr not found
162 mp_obj_dict_delete(dict_locals, MP_OBJ_NEW_QSTR(qstr));
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200163}
164
Damien Georged17926d2014-03-30 13:35:08 +0100165void mp_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100166 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George7efc5b32014-04-05 22:36:42 +0100167 mp_obj_dict_store(dict_globals, MP_OBJ_NEW_QSTR(qstr), obj);
Damien429d7192013-10-04 19:53:11 +0100168}
169
Damien George1d24ea52014-04-08 21:11:49 +0100170void mp_delete_global(qstr qstr) {
171 DEBUG_OP_printf("delete global %s\n", qstr_str(qstr));
172 // TODO convert KeyError to NameError if qstr not found
173 mp_obj_dict_delete(dict_globals, MP_OBJ_NEW_QSTR(qstr));
174}
175
Damien Georged17926d2014-03-30 13:35:08 +0100176mp_obj_t mp_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000177 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000178
Damiend99b0522013-12-21 18:17:45 +0000179 if (MP_OBJ_IS_SMALL_INT(arg)) {
180 mp_small_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000181 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100182 case MP_UNARY_OP_BOOL:
Damien George9d68e9c2014-03-12 15:38:15 +0000183 return MP_BOOL(val != 0);
Damien Georged17926d2014-03-30 13:35:08 +0100184 case MP_UNARY_OP_POSITIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000185 return arg;
Damien Georged17926d2014-03-30 13:35:08 +0100186 case MP_UNARY_OP_NEGATIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000187 // check for overflow
188 if (val == MP_SMALL_INT_MIN) {
189 return mp_obj_new_int(-val);
190 } else {
191 return MP_OBJ_NEW_SMALL_INT(-val);
192 }
Damien Georged17926d2014-03-30 13:35:08 +0100193 case MP_UNARY_OP_INVERT:
Damien George9d68e9c2014-03-12 15:38:15 +0000194 return MP_OBJ_NEW_SMALL_INT(~val);
195 default:
196 assert(0);
197 return arg;
Damien7410e442013-11-02 19:47:57 +0000198 }
Damien George1e708fe2014-01-23 18:27:51 +0000199 } else {
200 mp_obj_type_t *type = mp_obj_get_type(arg);
201 if (type->unary_op != NULL) {
202 mp_obj_t result = type->unary_op(op, arg);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100203 if (result != MP_OBJ_NOT_SUPPORTED) {
Damiend99b0522013-12-21 18:17:45 +0000204 return result;
205 }
Damien7410e442013-11-02 19:47:57 +0000206 }
Damiend99b0522013-12-21 18:17:45 +0000207 // TODO specify in error message what the operator is
Damien Georgeea13f402014-04-05 18:32:08 +0100208 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 +0000209 }
Damien429d7192013-10-04 19:53:11 +0100210}
211
Damien Georged17926d2014-03-30 13:35:08 +0100212mp_obj_t mp_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100213 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000214
215 // TODO correctly distinguish inplace operators for mutable objects
216 // lookup logic that CPython uses for +=:
217 // check for implemented +=
218 // then check for implemented +
219 // then check for implemented seq.inplace_concat
220 // then check for implemented seq.concat
221 // then fail
222 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
223
Damien George9aa2a522014-02-01 23:04:09 +0000224 // deal with is
Damien Georged17926d2014-03-30 13:35:08 +0100225 if (op == MP_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200226 return MP_BOOL(lhs == rhs);
227 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200228
Damien Georgebcbeea02014-01-11 10:47:22 +0000229 // deal with == and != for all types
Damien Georged17926d2014-03-30 13:35:08 +0100230 if (op == MP_BINARY_OP_EQUAL || op == MP_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000231 if (mp_obj_equal(lhs, rhs)) {
Damien Georged17926d2014-03-30 13:35:08 +0100232 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000233 return mp_const_true;
234 } else {
235 return mp_const_false;
236 }
237 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100238 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000239 return mp_const_false;
240 } else {
241 return mp_const_true;
242 }
243 }
244 }
245
246 // deal with exception_match for all types
Damien Georged17926d2014-03-30 13:35:08 +0100247 if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000248 // rhs must be issubclass(rhs, BaseException)
249 if (mp_obj_is_exception_type(rhs)) {
250 // if lhs is an instance of an exception, then extract and use its type
251 if (mp_obj_is_exception_instance(lhs)) {
252 lhs = mp_obj_get_type(lhs);
253 }
Damien George71510152014-03-03 22:38:13 +0000254 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000255 return mp_const_true;
256 } else {
257 return mp_const_false;
258 }
259 }
Paul Sokolovsky4b2b7ce2014-03-23 20:49:39 +0200260 assert(0);
261 return mp_const_false;
Damien Georgebcbeea02014-01-11 10:47:22 +0000262 }
263
Damien George1a9951d2014-01-06 22:13:00 +0000264 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damiend99b0522013-12-21 18:17:45 +0000265 mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000266 if (MP_OBJ_IS_SMALL_INT(rhs)) {
267 mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000268 // This is a binary operation: lhs_val op rhs_val
269 // We need to be careful to handle overflow; see CERT INT32-C
270 // Operations that can overflow:
271 // + result always fits in machine_int_t, then handled by SMALL_INT check
272 // - result always fits in machine_int_t, then handled by SMALL_INT check
273 // * checked explicitly
274 // / if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
275 // % if lhs=MIN and rhs=-1; result always fits in machine_int_t, then handled by SMALL_INT check
276 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000277 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100278 case MP_BINARY_OP_OR:
279 case MP_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
280 case MP_BINARY_OP_XOR:
281 case MP_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
282 case MP_BINARY_OP_AND:
283 case MP_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
284 case MP_BINARY_OP_LSHIFT:
285 case MP_BINARY_OP_INPLACE_LSHIFT: {
Damien George9d68e9c2014-03-12 15:38:15 +0000286 if (rhs_val < 0) {
287 // negative shift not allowed
Damien Georgeea13f402014-04-05 18:32:08 +0100288 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
Damien George9d68e9c2014-03-12 15:38:15 +0000289 } else if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
290 // left-shift will overflow, so use higher precision integer
291 lhs = mp_obj_new_int_from_ll(lhs_val);
292 goto generic_binary_op;
293 } else {
294 // use standard precision
295 lhs_val <<= rhs_val;
296 }
297 break;
298 }
Damien Georged17926d2014-03-30 13:35:08 +0100299 case MP_BINARY_OP_RSHIFT:
300 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000301 if (rhs_val < 0) {
302 // negative shift not allowed
Damien Georgeea13f402014-04-05 18:32:08 +0100303 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
Damien George9d68e9c2014-03-12 15:38:15 +0000304 } else {
305 // standard precision is enough for right-shift
306 lhs_val >>= rhs_val;
307 }
308 break;
Damien Georged17926d2014-03-30 13:35:08 +0100309 case MP_BINARY_OP_ADD:
310 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
311 case MP_BINARY_OP_SUBTRACT:
312 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
313 case MP_BINARY_OP_MULTIPLY:
314 case MP_BINARY_OP_INPLACE_MULTIPLY: {
Damien George9d68e9c2014-03-12 15:38:15 +0000315
316 // If long long type exists and is larger than machine_int_t, then
317 // we can use the following code to perform overflow-checked multiplication.
Damien Georgeecf5b772014-04-04 11:13:51 +0000318 // Otherwise (eg in x64 case) we must use mp_small_int_mul_overflow.
Damien George9d68e9c2014-03-12 15:38:15 +0000319 #if 0
320 // compute result using long long precision
321 long long res = (long long)lhs_val * (long long)rhs_val;
322 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
323 // result overflowed SMALL_INT, so return higher precision integer
324 return mp_obj_new_int_from_ll(res);
325 } else {
326 // use standard precision
327 lhs_val = (mp_small_int_t)res;
328 }
329 #endif
330
Damien Georgeecf5b772014-04-04 11:13:51 +0000331 if (mp_small_int_mul_overflow(lhs_val, rhs_val)) {
332 // use higher precision
333 lhs = mp_obj_new_int_from_ll(lhs_val);
334 goto generic_binary_op;
335 } else {
336 // use standard precision
337 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
338 }
Damien George9d68e9c2014-03-12 15:38:15 +0000339 break;
340 }
Damien Georged17926d2014-03-30 13:35:08 +0100341 case MP_BINARY_OP_FLOOR_DIVIDE:
342 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300343 if (rhs_val == 0) {
344 goto zero_division;
345 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000346 lhs_val = mp_small_int_floor_divide(lhs_val, rhs_val);
Rachel Dowdall56402792014-03-22 20:19:24 +0000347 break;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300348
Damien George9d68e9c2014-03-12 15:38:15 +0000349 #if MICROPY_ENABLE_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100350 case MP_BINARY_OP_TRUE_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300351 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
352 if (rhs_val == 0) {
Damien George70f33cd2014-04-02 17:06:05 +0100353 goto zero_division;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300354 }
355 return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000356 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000357
Damien Georged17926d2014-03-30 13:35:08 +0100358 case MP_BINARY_OP_MODULO:
Damien Georgeecf5b772014-04-04 11:13:51 +0000359 case MP_BINARY_OP_INPLACE_MODULO: {
360 lhs_val = mp_small_int_modulo(lhs_val, rhs_val);
Rachel Dowdallcde86312014-03-22 17:29:27 +0000361 break;
362 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000363
Damien Georged17926d2014-03-30 13:35:08 +0100364 case MP_BINARY_OP_POWER:
365 case MP_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000366 if (rhs_val < 0) {
367 #if MICROPY_ENABLE_FLOAT
368 lhs = mp_obj_new_float(lhs_val);
369 goto generic_binary_op;
370 #else
Damien Georgeea13f402014-04-05 18:32:08 +0100371 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
Damien George9d68e9c2014-03-12 15:38:15 +0000372 #endif
373 } else {
Damien George9d68e9c2014-03-12 15:38:15 +0000374 machine_int_t ans = 1;
375 while (rhs_val > 0) {
376 if (rhs_val & 1) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000377 if (mp_small_int_mul_overflow(ans, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100378 goto power_overflow;
379 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000380 ans *= lhs_val;
Damien George9d68e9c2014-03-12 15:38:15 +0000381 }
Damien George5bf565e2014-04-04 00:16:32 +0100382 if (rhs_val == 1) {
383 break;
384 }
Damien George9d68e9c2014-03-12 15:38:15 +0000385 rhs_val /= 2;
Damien Georgeecf5b772014-04-04 11:13:51 +0000386 if (mp_small_int_mul_overflow(lhs_val, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100387 goto power_overflow;
388 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000389 lhs_val *= lhs_val;
Damien George1a9951d2014-01-06 22:13:00 +0000390 }
Damien George9d68e9c2014-03-12 15:38:15 +0000391 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000392 }
Damien George1a9951d2014-01-06 22:13:00 +0000393 break;
Damien George5bf565e2014-04-04 00:16:32 +0100394
395 power_overflow:
396 // use higher precision
397 lhs = mp_obj_new_int_from_ll(MP_OBJ_SMALL_INT_VALUE(lhs));
398 goto generic_binary_op;
399
Damien Georged17926d2014-03-30 13:35:08 +0100400 case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
401 case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
402 case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
403 case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000404
Damien George8bcb9862014-04-17 16:26:50 +0100405 default:
406 goto unsupported_op;
Damien George1a9951d2014-01-06 22:13:00 +0000407 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200408 // TODO: We just should make mp_obj_new_int() inline and use that
409 if (MP_OBJ_FITS_SMALL_INT(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000410 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000411 } else {
412 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000413 }
Damien George3f759b72014-01-31 00:42:12 +0000414#if MICROPY_ENABLE_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000415 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien Georgeae491052014-04-10 20:08:11 +0100416 mp_obj_t res = mp_obj_float_binary_op(op, lhs_val, rhs);
417 if (res == MP_OBJ_NULL) {
418 goto unsupported_op;
419 } else {
420 return res;
421 }
Damien George0c36da02014-03-08 15:24:39 +0000422 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien Georgeae491052014-04-10 20:08:11 +0100423 mp_obj_t res = mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
424 if (res == MP_OBJ_NULL) {
425 goto unsupported_op;
426 } else {
427 return res;
428 }
Damien George3f759b72014-01-31 00:42:12 +0000429#endif
Damien429d7192013-10-04 19:53:11 +0100430 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000431 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000432
Damien George9aa2a522014-02-01 23:04:09 +0000433 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000434 *
435 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000436 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000437 */
Damien Georged17926d2014-03-30 13:35:08 +0100438 if (op == MP_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000439 mp_obj_type_t *type = mp_obj_get_type(rhs);
440 if (type->binary_op != NULL) {
441 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100442 if (res != MP_OBJ_NOT_SUPPORTED) {
Damien George5fa93b62014-01-22 14:35:10 +0000443 return res;
444 }
445 }
446 if (type->getiter != NULL) {
447 /* second attempt, walk the iterator */
448 mp_obj_t next = NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100449 mp_obj_t iter = mp_getiter(rhs);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100450 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien George5fa93b62014-01-22 14:35:10 +0000451 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000452 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000453 }
Damien7410e442013-11-02 19:47:57 +0000454 }
Damien George9aa2a522014-02-01 23:04:09 +0000455 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000456 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000457
Damien Georgeea13f402014-04-05 18:32:08 +0100458 nlr_raise(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000459 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000460 mp_obj_get_type_str(rhs)));
461 return mp_const_none;
462 }
463
Damien George5fa93b62014-01-22 14:35:10 +0000464 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000465 mp_obj_type_t *type;
466generic_binary_op:
467 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000468 if (type->binary_op != NULL) {
469 mp_obj_t result = type->binary_op(op, lhs, rhs);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100470 if (result != MP_OBJ_NOT_SUPPORTED) {
Damien George5fa93b62014-01-22 14:35:10 +0000471 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000472 }
Damien429d7192013-10-04 19:53:11 +0100473 }
Damiend99b0522013-12-21 18:17:45 +0000474
Damien George5fa93b62014-01-22 14:35:10 +0000475 // TODO implement dispatch for reverse binary ops
476
Damiend99b0522013-12-21 18:17:45 +0000477 // TODO specify in error message what the operator is
Damien Georgeae491052014-04-10 20:08:11 +0100478unsupported_op:
Damien Georgeea13f402014-04-05 18:32:08 +0100479 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200480 "unsupported operand types for binary operator: '%s', '%s'",
481 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000482 return mp_const_none;
Damien George70f33cd2014-04-02 17:06:05 +0100483
484zero_division:
Damien Georgeea13f402014-04-05 18:32:08 +0100485 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
Damien429d7192013-10-04 19:53:11 +0100486}
487
Damien Georged17926d2014-03-30 13:35:08 +0100488mp_obj_t mp_call_function_0(mp_obj_t fun) {
489 return mp_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100490}
491
Damien Georged17926d2014-03-30 13:35:08 +0100492mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
493 return mp_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100494}
495
Damien Georged17926d2014-03-30 13:35:08 +0100496mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
Damiend99b0522013-12-21 18:17:45 +0000497 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000498 args[0] = arg1;
499 args[1] = arg2;
Damien Georged17926d2014-03-30 13:35:08 +0100500 return mp_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100501}
502
Damien Georgecd82e022014-02-02 13:11:48 +0000503// wrapper that accepts n_args and n_kw in one argument
504// native emitter can only pass at most 3 arguments to a function
Damien Georged17926d2014-03-30 13:35:08 +0100505mp_obj_t mp_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
506 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 +0000507}
508
Damien George20006db2014-01-18 14:10:48 +0000509// args contains, eg: arg0 arg1 key0 value0 key1 value1
Damien Georged17926d2014-03-30 13:35:08 +0100510mp_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 +0000511 // TODO improve this: fun object can specify its type and we parse here the arguments,
512 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100513
John R. Lenton9c83ec02014-01-07 23:06:46 +0000514 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
515
Damien George8b56beb2014-01-31 23:49:49 +0000516 // get the type
517 mp_obj_type_t *type = mp_obj_get_type(fun_in);
518
519 // do the call
520 if (type->call != NULL) {
Paul Sokolovsky755565d2014-04-25 21:15:16 +0300521 mp_obj_t res = type->call(fun_in, n_args, n_kw, args);
522 if (res != NULL) {
523 return res;
524 }
John R. Lenton9c83ec02014-01-07 23:06:46 +0000525 }
Paul Sokolovsky755565d2014-04-25 21:15:16 +0300526
527 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 +0000528}
529
Damien George20006db2014-01-18 14:10:48 +0000530// 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)
531// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damien Georged17926d2014-03-30 13:35:08 +0100532mp_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 +0000533 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);
534 int adjust = (args[1] == NULL) ? 0 : 1;
Damien Georged17926d2014-03-30 13:35:08 +0100535 return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000536}
537
Damien George523b5752014-03-31 11:59:23 +0100538mp_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 +0100539 mp_obj_t fun = *args++;
540 mp_obj_t self = MP_OBJ_NULL;
541 if (have_self) {
542 self = *args++; // may be MP_OBJ_NULL
543 }
544 uint n_args = n_args_n_kw & 0xff;
545 uint n_kw = (n_args_n_kw >> 8) & 0xff;
Damien George523b5752014-03-31 11:59:23 +0100546 mp_obj_t pos_seq = args[n_args + 2 * n_kw]; // map be MP_OBJ_NULL
547 mp_obj_t kw_dict = args[n_args + 2 * n_kw + 1]; // map be MP_OBJ_NULL
Damien George230fec72014-03-30 21:21:24 +0100548
549 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);
550
551 // We need to create the following array of objects:
552 // args[0 .. n_args] unpacked(pos_seq) args[n_args .. n_args + 2 * n_kw] unpacked(kw_dict)
553 // TODO: optimize one day to avoid constructing new arg array? Will be hard.
554
555 // The new args array
556 mp_obj_t *args2;
557 uint args2_alloc;
558 uint args2_len = 0;
559
560 // Try to get a hint for the size of the kw_dict
561 uint kw_dict_len = 0;
562 if (kw_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
563 kw_dict_len = mp_obj_dict_len(kw_dict);
564 }
565
566 // Extract the pos_seq sequence to the new args array.
567 // Note that it can be arbitrary iterator.
568 if (pos_seq == MP_OBJ_NULL) {
569 // no sequence
570
571 // allocate memory for the new array of args
572 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len);
573 args2 = m_new(mp_obj_t, args2_alloc);
574
575 // copy the self
576 if (self != MP_OBJ_NULL) {
577 args2[args2_len++] = self;
578 }
579
580 // copy the fixed pos args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300581 mp_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100582 args2_len += n_args;
583
584 } else if (MP_OBJ_IS_TYPE(pos_seq, &mp_type_tuple) || MP_OBJ_IS_TYPE(pos_seq, &mp_type_list)) {
585 // optimise the case of a tuple and list
586
587 // get the items
588 uint len;
589 mp_obj_t *items;
590 mp_obj_get_array(pos_seq, &len, &items);
591
592 // allocate memory for the new array of args
593 args2_alloc = 1 + n_args + len + 2 * (n_kw + kw_dict_len);
594 args2 = m_new(mp_obj_t, args2_alloc);
595
596 // copy the self
597 if (self != MP_OBJ_NULL) {
598 args2[args2_len++] = self;
599 }
600
601 // copy the fixed and variable position args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300602 mp_seq_cat(args2 + args2_len, args, n_args, items, len, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100603 args2_len += n_args + len;
604
605 } else {
606 // generic iterator
607
608 // allocate memory for the new array of args
609 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len) + 3;
610 args2 = m_new(mp_obj_t, args2_alloc);
611
612 // copy the self
613 if (self != MP_OBJ_NULL) {
614 args2[args2_len++] = self;
615 }
616
617 // copy the fixed position args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300618 mp_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100619
620 // extract the variable position args from the iterator
621 mp_obj_t iterable = mp_getiter(pos_seq);
622 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100623 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George230fec72014-03-30 21:21:24 +0100624 if (args2_len >= args2_alloc) {
625 args2 = m_renew(mp_obj_t, args2, args2_alloc, args2_alloc * 2);
626 args2_alloc *= 2;
627 }
628 args2[args2_len++] = item;
629 }
630 }
631
632 // The size of the args2 array now is the number of positional args.
633 uint pos_args_len = args2_len;
634
635 // Copy the fixed kw args.
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300636 mp_seq_copy(args2 + args2_len, args + n_args, 2 * n_kw, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100637 args2_len += 2 * n_kw;
638
639 // Extract (key,value) pairs from kw_dict dictionary and append to args2.
640 // Note that it can be arbitrary iterator.
641 if (kw_dict == MP_OBJ_NULL) {
642 // pass
643 } else if (MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
644 // dictionary
645 mp_map_t *map = mp_obj_dict_get_map(kw_dict);
646 assert(args2_len + 2 * map->used <= args2_alloc); // should have enough, since kw_dict_len is in this case hinted correctly above
647 for (uint i = 0; i < map->alloc; i++) {
648 if (map->table[i].key != MP_OBJ_NULL) {
649 args2[args2_len++] = map->table[i].key;
650 args2[args2_len++] = map->table[i].value;
651 }
652 }
653 } else {
654 // generic mapping
655 // TODO is calling 'items' on the mapping the correct thing to do here?
656 mp_obj_t dest[2];
657 mp_load_method(kw_dict, MP_QSTR_items, dest);
658 mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest));
659 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100660 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George230fec72014-03-30 21:21:24 +0100661 if (args2_len + 1 >= args2_alloc) {
662 uint new_alloc = args2_alloc * 2;
663 if (new_alloc < 4) {
664 new_alloc = 4;
665 }
666 args2 = m_renew(mp_obj_t, args2, args2_alloc, new_alloc);
667 args2_alloc = new_alloc;
668 }
669 mp_obj_t *items;
670 mp_obj_get_array_fixed_n(item, 2, &items);
671 args2[args2_len++] = items[0];
672 args2[args2_len++] = items[1];
673 }
674 }
675
676 mp_obj_t res = mp_call_function_n_kw(fun, pos_args_len, (args2_len - pos_args_len) / 2, args2);
677 m_del(mp_obj_t, args2, args2_alloc);
678
679 return res;
680}
681
Damien George932bf1c2014-01-18 23:42:49 +0000682// unpacked items are stored in reverse order into the array pointed to by items
Damien Georged17926d2014-03-30 13:35:08 +0100683void mp_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200684 uint seq_len;
Damien George3e1a5c12014-03-29 13:43:38 +0000685 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 +0000686 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000687 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000688 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
689 } else {
690 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000691 }
Damiend99b0522013-12-21 18:17:45 +0000692 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200693 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000694 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200695 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000696 }
Damien George932bf1c2014-01-18 23:42:49 +0000697 for (uint i = 0; i < num; i++) {
698 items[i] = seq_items[num - 1 - i];
699 }
Damien86c7fc72013-11-26 15:16:41 +0000700 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100701 mp_obj_t iterable = mp_getiter(seq_in);
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200702
703 for (seq_len = 0; seq_len < num; seq_len++) {
Damien Georged17926d2014-03-30 13:35:08 +0100704 mp_obj_t el = mp_iternext(iterable);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100705 if (el == MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200706 goto too_short;
707 }
708 items[num - 1 - seq_len] = el;
709 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100710 if (mp_iternext(iterable) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200711 goto too_long;
712 }
Damien86c7fc72013-11-26 15:16:41 +0000713 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200714 return;
715
716too_short:
Damien Georgeea13f402014-04-05 18:32:08 +0100717 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 +0200718too_long:
Damien Georgeea13f402014-04-05 18:32:08 +0100719 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 +0000720}
721
Damien George495d7812014-04-08 17:51:47 +0100722// unpacked items are stored in reverse order into the array pointed to by items
723void mp_unpack_ex(mp_obj_t seq_in, uint num_in, mp_obj_t *items) {
724 uint num_left = num_in & 0xff;
725 uint num_right = (num_in >> 8) & 0xff;
726 DEBUG_OP_printf("unpack ex %d %d\n", num_left, num_right);
727 uint seq_len;
728 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
729 mp_obj_t *seq_items;
730 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
731 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
732 } else {
733 if (num_left == 0 && num_right == 0) {
734 // *a, = b # sets a to b if b is a list
735 items[0] = seq_in;
736 return;
737 }
738 mp_obj_list_get(seq_in, &seq_len, &seq_items);
739 }
740 if (seq_len < num_left + num_right) {
741 goto too_short;
742 }
743 for (uint i = 0; i < num_right; i++) {
744 items[i] = seq_items[seq_len - 1 - i];
745 }
746 items[num_right] = mp_obj_new_list(seq_len - num_left - num_right, seq_items + num_left);
747 for (uint i = 0; i < num_left; i++) {
748 items[num_right + 1 + i] = seq_items[num_left - 1 - i];
749 }
750 } else {
751 // Generic iterable; this gets a bit messy: we unpack known left length to the
752 // items destination array, then the rest to a dynamically created list. Once the
753 // iterable is exhausted, we take from this list for the right part of the items.
754 // TODO Improve to waste less memory in the dynamically created list.
755 mp_obj_t iterable = mp_getiter(seq_in);
756 mp_obj_t item;
757 for (seq_len = 0; seq_len < num_left; seq_len++) {
758 item = mp_iternext(iterable);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100759 if (item == MP_OBJ_STOP_ITERATION) {
Damien George495d7812014-04-08 17:51:47 +0100760 goto too_short;
761 }
762 items[num_left + num_right + 1 - 1 - seq_len] = item;
763 }
764 mp_obj_t rest = mp_obj_new_list(0, NULL);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100765 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George495d7812014-04-08 17:51:47 +0100766 mp_obj_list_append(rest, item);
767 }
768 uint rest_len;
769 mp_obj_t *rest_items;
770 mp_obj_list_get(rest, &rest_len, &rest_items);
771 if (rest_len < num_right) {
772 goto too_short;
773 }
774 items[num_right] = rest;
775 for (uint i = 0; i < num_right; i++) {
776 items[num_right - 1 - i] = rest_items[rest_len - num_right + i];
777 }
778 mp_obj_list_set_len(rest, rest_len - num_right);
779 }
780 return;
781
782too_short:
783 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", seq_len));
784}
785
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300786mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000787 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300788 // use load_method
Damien George062478e2014-01-09 20:57:50 +0000789 mp_obj_t dest[2];
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300790 mp_load_method(base, attr, dest);
791 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000792 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000793 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000794 } else {
795 // load_method returned a method, so build a bound method object
796 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000797 }
Damiend99b0522013-12-21 18:17:45 +0000798}
799
Damien George7c9c6672014-01-25 00:17:36 +0000800// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
801// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
802// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Damien Georgee44d26a2014-03-31 22:57:56 +0100803void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000804 // clear output to indicate no attribute/method found yet
805 dest[0] = MP_OBJ_NULL;
806 dest[1] = MP_OBJ_NULL;
807
808 // get the type
809 mp_obj_type_t *type = mp_obj_get_type(base);
810
Damien Georgee44d26a2014-03-31 22:57:56 +0100811 // look for built-in names
812 if (0) {
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +0300813#if MICROPY_CPYTHON_COMPAT
Damien Georgee44d26a2014-03-31 22:57:56 +0100814 } else if (attr == MP_QSTR___class__) {
815 // a.__class__ is equivalent to type(a)
816 dest[0] = type;
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +0300817#endif
Damien Georgee44d26a2014-03-31 22:57:56 +0100818
819 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
820 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
821 dest[1] = base;
822
823 } else if (type->load_attr != NULL) {
824 // this type can do its own load, so call it
825 type->load_attr(base, attr, dest);
826
827 } else if (type->locals_dict != NULL) {
828 // generic method lookup
829 // this is a lookup in the object (ie not class or type)
830 assert(MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)); // Micro Python restriction, for now
831 mp_map_t *locals_map = mp_obj_dict_get_map(type->locals_dict);
832 mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
833 if (elem != NULL) {
834 // check if the methods are functions, static or class methods
835 // see http://docs.python.org/3.3/howto/descriptor.html
836 if (MP_OBJ_IS_TYPE(elem->value, &mp_type_staticmethod)) {
837 // return just the function
838 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
839 } else if (MP_OBJ_IS_TYPE(elem->value, &mp_type_classmethod)) {
840 // return a bound method, with self being the type of this object
841 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
842 dest[1] = mp_obj_get_type(base);
Paul Sokolovsky9511f602014-05-11 03:12:36 +0300843 } else if (MP_OBJ_IS_TYPE(elem->value, &mp_type_type)) {
844 // Don't try to bind types
845 dest[0] = elem->value;
Damien Georgee44d26a2014-03-31 22:57:56 +0100846 } else if (mp_obj_is_callable(elem->value)) {
847 // return a bound method, with self being this object
848 dest[0] = elem->value;
849 dest[1] = base;
850 } else {
851 // class member is a value, so just return that value
852 dest[0] = elem->value;
Damiend57eba52013-11-02 23:58:14 +0000853 }
854 }
Damiena3977762013-10-09 23:10:10 +0100855 }
Damien George7c9c6672014-01-25 00:17:36 +0000856}
Damiena3977762013-10-09 23:10:10 +0100857
Damien Georged17926d2014-03-30 13:35:08 +0100858void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George7c9c6672014-01-25 00:17:36 +0000859 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
860
Damien Georged17926d2014-03-30 13:35:08 +0100861 mp_load_method_maybe(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000862
863 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000864 // no attribute/method called attr
865 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000866 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
Damien Georgeea13f402014-04-05 18:32:08 +0100867 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
Paul Sokolovsky7f8b3132014-03-25 00:55:39 +0200868 "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 +0000869 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100870 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 +0000871 }
872 }
Damiena3977762013-10-09 23:10:10 +0100873}
874
Damien Georged17926d2014-03-30 13:35:08 +0100875void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100876 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000877 mp_obj_type_t *type = mp_obj_get_type(base);
878 if (type->store_attr != NULL) {
879 if (type->store_attr(base, attr, value)) {
880 return;
881 }
Damiena3977762013-10-09 23:10:10 +0100882 }
Damien Georgeea13f402014-04-05 18:32:08 +0100883 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 +0100884}
885
Damien Georged17926d2014-03-30 13:35:08 +0100886mp_obj_t mp_getiter(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000887 mp_obj_type_t *type = mp_obj_get_type(o_in);
888 if (type->getiter != NULL) {
889 return type->getiter(o_in);
Damience89a212013-10-15 22:25:17 +0100890 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000891 // check for __iter__ method
Damien George7c9c6672014-01-25 00:17:36 +0000892 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100893 mp_load_method_maybe(o_in, MP_QSTR___iter__, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000894 if (dest[0] != MP_OBJ_NULL) {
Damien George9e6e9352014-03-26 18:37:06 +0000895 // __iter__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100896 return mp_call_method_n_kw(0, 0, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000897 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100898 mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000899 if (dest[0] != MP_OBJ_NULL) {
900 // __getitem__ exists, create an iterator
901 return mp_obj_new_getitem_iter(dest);
902 } else {
903 // object not iterable
Damien Georgeea13f402014-04-05 18:32:08 +0100904 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 +0000905 }
Damien George7c9c6672014-01-25 00:17:36 +0000906 }
Damience89a212013-10-15 22:25:17 +0100907 }
908}
909
Damien Georgeea8d06c2014-04-17 23:19:36 +0100910// may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration()
Damien George66eaf842014-03-26 19:27:58 +0000911// may also raise StopIteration()
Damien Georged17926d2014-03-30 13:35:08 +0100912mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000913 mp_obj_type_t *type = mp_obj_get_type(o_in);
914 if (type->iternext != NULL) {
915 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100916 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000917 // check for __next__ method
918 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100919 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000920 if (dest[0] != MP_OBJ_NULL) {
921 // __next__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100922 return mp_call_method_n_kw(0, 0, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000923 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100924 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 +0000925 }
Damien Georgec5966122014-02-15 16:10:44 +0000926 }
927}
928
Damien Georgeea8d06c2014-04-17 23:19:36 +0100929// will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration() (or any subclass thereof)
Damien George66eaf842014-03-26 19:27:58 +0000930// may raise other exceptions
Damien Georged17926d2014-03-30 13:35:08 +0100931mp_obj_t mp_iternext(mp_obj_t o_in) {
Damien George66eaf842014-03-26 19:27:58 +0000932 mp_obj_type_t *type = mp_obj_get_type(o_in);
933 if (type->iternext != NULL) {
934 return type->iternext(o_in);
935 } else {
936 // check for __next__ method
937 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100938 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George66eaf842014-03-26 19:27:58 +0000939 if (dest[0] != MP_OBJ_NULL) {
940 // __next__ exists, call it and return its result
941 nlr_buf_t nlr;
942 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100943 mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
Damien George66eaf842014-03-26 19:27:58 +0000944 nlr_pop();
945 return ret;
946 } else {
947 if (mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100948 return MP_OBJ_STOP_ITERATION;
Damien George66eaf842014-03-26 19:27:58 +0000949 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100950 nlr_raise(nlr.ret_val);
Damien George66eaf842014-03-26 19:27:58 +0000951 }
952 }
953 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100954 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 +0000955 }
956 }
957}
958
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300959// TODO: Unclear what to do with StopIterarion exception here.
960mp_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 +0300961 assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL));
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300962 mp_obj_type_t *type = mp_obj_get_type(self_in);
963
964 if (type == &mp_type_gen_instance) {
965 return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
966 }
967
968 if (type->iternext != NULL && send_value == mp_const_none) {
969 mp_obj_t ret = type->iternext(self_in);
970 if (ret != MP_OBJ_NULL) {
971 *ret_val = ret;
972 return MP_VM_RETURN_YIELD;
973 } else {
974 // Emulate raise StopIteration()
975 // Special case, handled in vm.c
976 *ret_val = MP_OBJ_NULL;
977 return MP_VM_RETURN_NORMAL;
978 }
979 }
980
981 mp_obj_t dest[3]; // Reserve slot for send() arg
982
983 if (send_value == mp_const_none) {
984 mp_load_method_maybe(self_in, MP_QSTR___next__, dest);
985 if (dest[0] != MP_OBJ_NULL) {
986 *ret_val = mp_call_method_n_kw(0, 0, dest);
987 return MP_VM_RETURN_YIELD;
988 }
989 }
990
991 if (send_value != MP_OBJ_NULL) {
992 mp_load_method(self_in, MP_QSTR_send, dest);
993 dest[2] = send_value;
994 *ret_val = mp_call_method_n_kw(1, 0, dest);
995 return MP_VM_RETURN_YIELD;
996 }
997
998 if (throw_value != MP_OBJ_NULL) {
999 if (mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) {
1000 mp_load_method_maybe(self_in, MP_QSTR_close, dest);
1001 if (dest[0] != MP_OBJ_NULL) {
1002 *ret_val = mp_call_method_n_kw(0, 0, dest);
1003 // We assume one can't "yield" from close()
1004 return MP_VM_RETURN_NORMAL;
1005 }
1006 }
Paul Sokolovskya2109d92014-03-31 04:14:30 +03001007 mp_load_method_maybe(self_in, MP_QSTR_throw, dest);
1008 if (dest[0] != MP_OBJ_NULL) {
1009 *ret_val = mp_call_method_n_kw(1, 0, &throw_value);
1010 // If .throw() method returned, we assume it's value to yield
Damien Georgeea13f402014-04-05 18:32:08 +01001011 // - any exception would be thrown with nlr_raise().
Paul Sokolovskya2109d92014-03-31 04:14:30 +03001012 return MP_VM_RETURN_YIELD;
1013 }
1014 // If there's nowhere to throw exception into, then we assume that object
1015 // is just incapable to handle it, so any exception thrown into it
1016 // will be propagated up. This behavior is approved by test_pep380.py
1017 // test_delegation_of_close_to_non_generator(),
1018 // test_delegating_throw_to_non_generator()
1019 *ret_val = throw_value;
1020 return MP_VM_RETURN_EXCEPTION;
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001021 }
1022
1023 assert(0);
1024 return MP_VM_RETURN_NORMAL; // Should be unreachable
1025}
1026
Damien Georged17926d2014-03-30 13:35:08 +01001027mp_obj_t mp_make_raise_obj(mp_obj_t o) {
Damien Georgec5966122014-02-15 16:10:44 +00001028 DEBUG_printf("raise %p\n", o);
1029 if (mp_obj_is_exception_type(o)) {
1030 // o is an exception type (it is derived from BaseException (or is BaseException))
1031 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +00001032 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
1033 // could have const instances in ROM which we return here instead
Damien Georged17926d2014-03-30 13:35:08 +01001034 return mp_call_function_n_kw(o, 0, 0, NULL);
Damien Georgec5966122014-02-15 16:10:44 +00001035 } else if (mp_obj_is_exception_instance(o)) {
1036 // o is an instance of an exception, so use it as the exception
1037 return o;
1038 } else {
1039 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
1040 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +01001041 }
1042}
1043
Damien Georged17926d2014-03-30 13:35:08 +01001044mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +00001045 DEBUG_printf("import name %s\n", qstr_str(name));
1046
Damiendb4c3612013-12-10 17:27:24 +00001047 // build args array
Damiend99b0522013-12-21 18:17:45 +00001048 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +00001049 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +00001050 args[1] = mp_const_none; // TODO should be globals
1051 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +00001052 args[3] = fromlist;
1053 args[4] = level; // must be 0; we don't yet support other values
1054
1055 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +00001056 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001057}
1058
Damien Georged17926d2014-03-30 13:35:08 +01001059mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001060 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1061
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001062 mp_obj_t dest[2];
1063
1064 mp_load_method_maybe(module, name, dest);
1065
1066 if (dest[1] != MP_OBJ_NULL) {
1067 // Hopefully we can't import bound method from an object
1068import_error:
Paul Sokolovsky42453dc2014-04-12 03:00:40 +03001069 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "cannot import name %s", qstr_str(name)));
Damiendb4c3612013-12-10 17:27:24 +00001070 }
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001071
1072 if (dest[0] != MP_OBJ_NULL) {
1073 return dest[0];
1074 }
1075
1076 // See if it's a package, then can try FS import
1077 mp_load_method_maybe(module, MP_QSTR___path__, dest);
1078 if (dest[0] == MP_OBJ_NULL) {
1079 goto import_error;
1080 }
1081
1082 mp_load_method_maybe(module, MP_QSTR___name__, dest);
1083 uint pkg_name_len;
1084 const char *pkg_name = mp_obj_str_get_data(dest[0], &pkg_name_len);
1085
stijn01d6be42014-05-05 12:18:27 +02001086 const uint dot_name_len = pkg_name_len + 1 + qstr_len(name);
1087 char *dot_name = alloca(dot_name_len);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001088 memcpy(dot_name, pkg_name, pkg_name_len);
1089 dot_name[pkg_name_len] = '.';
1090 memcpy(dot_name + pkg_name_len + 1, qstr_str(name), qstr_len(name));
stijn01d6be42014-05-05 12:18:27 +02001091 qstr dot_name_q = qstr_from_strn(dot_name, dot_name_len);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001092
1093 mp_obj_t args[5];
1094 args[0] = MP_OBJ_NEW_QSTR(dot_name_q);
1095 args[1] = mp_const_none; // TODO should be globals
1096 args[2] = mp_const_none; // TODO should be locals
1097 args[3] = mp_const_true; // Pass sentinel "non empty" value to force returning of leaf module
Paul Sokolovsky69f18672014-04-12 02:47:46 +03001098 args[4] = MP_OBJ_NEW_SMALL_INT(0);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001099
1100 // TODO lookup __import__ and call that instead of going straight to builtin implementation
1101 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001102}
1103
Damien Georged17926d2014-03-30 13:35:08 +01001104void mp_import_all(mp_obj_t module) {
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001105 DEBUG_printf("import all %p\n", module);
1106
Paul Sokolovsky599bbc12014-04-18 04:11:19 +03001107 // TODO: Support __all__
Damien George8b0535e2014-04-05 21:53:54 +01001108 mp_map_t *map = mp_obj_dict_get_map(mp_obj_module_get_globals(module));
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001109 for (uint i = 0; i < map->alloc; i++) {
Damien George8b0535e2014-04-05 21:53:54 +01001110 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
Paul Sokolovsky599bbc12014-04-18 04:11:19 +03001111 qstr name = MP_OBJ_QSTR_VALUE(map->table[i].key);
1112 if (*qstr_str(name) != '_') {
1113 mp_store_name(name, map->table[i].value);
1114 }
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001115 }
1116 }
1117}
1118
Damien George7efc5b32014-04-05 22:36:42 +01001119mp_obj_dict_t *mp_locals_get(void) {
1120 return dict_locals;
Damien George66028ab2014-01-03 14:03:48 +00001121}
1122
Damien George7efc5b32014-04-05 22:36:42 +01001123void mp_locals_set(mp_obj_dict_t *d) {
1124 DEBUG_OP_printf("mp_locals_set(%p)\n", d);
1125 dict_locals = d;
Damien George66028ab2014-01-03 14:03:48 +00001126}
1127
Damien George7efc5b32014-04-05 22:36:42 +01001128mp_obj_dict_t *mp_globals_get(void) {
1129 return dict_globals;
Damien George66028ab2014-01-03 14:03:48 +00001130}
1131
Damien George7efc5b32014-04-05 22:36:42 +01001132void mp_globals_set(mp_obj_dict_t *d) {
1133 DEBUG_OP_printf("mp_globals_set(%p)\n", d);
1134 dict_globals = d;
Damien George66028ab2014-01-03 14:03:48 +00001135}
1136
Damien George6902eed2014-04-04 10:52:59 +00001137void *m_malloc_fail(int num_bytes) {
1138 DEBUG_printf("memory allocation failed, allocating %d bytes\n", num_bytes);
Damien Georgeea13f402014-04-05 18:32:08 +01001139 nlr_raise((mp_obj_t)&mp_const_MemoryError_obj);
Damien George6902eed2014-04-04 10:52:59 +00001140}
1141
Damien6ba13142013-11-02 20:34:54 +00001142// these must correspond to the respective enum
Damien Georged17926d2014-03-30 13:35:08 +01001143void *const mp_fun_table[MP_F_NUMBER_OF] = {
1144 mp_load_const_dec,
Damien Georgea32c1e42014-05-07 18:30:52 +01001145 mp_obj_new_int_from_qstr,
Damien Georged17926d2014-03-30 13:35:08 +01001146 mp_load_const_str,
1147 mp_load_name,
1148 mp_load_global,
1149 mp_load_build_class,
1150 mp_load_attr,
1151 mp_load_method,
1152 mp_store_name,
1153 mp_store_attr,
Damien George729f7b42014-04-17 22:10:53 +01001154 mp_obj_subscr,
Damien Georged17926d2014-03-30 13:35:08 +01001155 mp_obj_is_true,
1156 mp_unary_op,
1157 mp_binary_op,
Damien George15d18062014-03-31 16:28:13 +01001158 mp_obj_new_tuple,
1159 mp_obj_new_list,
1160 mp_obj_list_append,
1161 mp_obj_new_dict,
1162 mp_obj_dict_store,
1163 mp_obj_new_set,
1164 mp_obj_set_store,
Damien Georgedf8127a2014-04-13 11:04:33 +01001165 mp_make_function_from_raw_code,
Damien Georged17926d2014-03-30 13:35:08 +01001166 mp_call_function_n_kw_for_native,
1167 mp_call_method_n_kw,
1168 mp_getiter,
Damien George36db6bc2014-05-07 17:24:22 +01001169 mp_iternext,
Damien Georgecdd96df2014-04-06 12:58:40 +01001170 mp_import_name,
1171 mp_import_from,
1172 mp_import_all,
1173 mp_obj_new_slice,
1174 mp_unpack_sequence,
Damien Georgea32c1e42014-05-07 18:30:52 +01001175 mp_unpack_ex,
Damien429d7192013-10-04 19:53:11 +01001176};
1177
1178/*
Damien Georged17926d2014-03-30 13:35:08 +01001179void mp_f_vector(mp_fun_kind_t fun_kind) {
1180 (mp_f_table[fun_kind])();
Damien429d7192013-10-04 19:53:11 +01001181}
1182*/