blob: 59e47c7ff79811d8850e9230d981df8db6c3927e [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>
30
Paul Sokolovskyf54bcbf2014-05-02 17:47:01 +030031#include "mpconfig.h"
Damience89a212013-10-15 22:25:17 +010032#include "nlr.h"
Damien429d7192013-10-04 19:53:11 +010033#include "misc.h"
Damien George55baff42014-01-21 21:40:13 +000034#include "qstr.h"
Damien660365e2013-12-17 18:27:24 +000035#include "obj.h"
Damien George230fec72014-03-30 21:21:24 +010036#include "objtuple.h"
Damien Georgecaac5422014-03-25 14:18:18 +000037#include "objmodule.h"
Damien George20773972014-02-22 18:12:43 +000038#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000039#include "runtime0.h"
40#include "runtime.h"
Damien George2326d522014-03-27 23:26:35 +000041#include "emitglue.h"
Damien660365e2013-12-17 18:27:24 +000042#include "builtin.h"
Damien Georgecaac5422014-03-25 14:18:18 +000043#include "builtintables.h"
Damien George5fa93b62014-01-22 14:35:10 +000044#include "bc.h"
Damien Georgeecf5b772014-04-04 11:13:51 +000045#include "smallint.h"
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +030046#include "objgenerator.h"
Paul Sokolovskyd3439d02014-06-02 19:37:55 +030047#include "lexer.h"
Paul Sokolovsky8a96ebe2014-06-27 20:54:22 +030048#include "stackctrl.h"
Damien660365e2013-12-17 18:27:24 +000049
Damien7f5dacf2013-10-10 11:24:39 +010050#if 0 // print debugging info
Damiena1ddfcc2013-10-10 23:25:50 +010051#define DEBUG_PRINT (1)
Paul Sokolovsky44739e22014-02-16 18:11:42 +020052#define DEBUG_printf DEBUG_printf
Damien George41eb6082014-02-26 22:40:35 +000053#define DEBUG_OP_printf(...) DEBUG_printf(__VA_ARGS__)
Damien7f5dacf2013-10-10 11:24:39 +010054#else // don't print debugging info
Damien George41eb6082014-02-26 22:40:35 +000055#define DEBUG_printf(...) (void)0
56#define DEBUG_OP_printf(...) (void)0
Damien7f5dacf2013-10-10 11:24:39 +010057#endif
Damien429d7192013-10-04 19:53:11 +010058
Damieneb19efb2013-10-10 22:06:54 +010059// locals and globals need to be pointers because they can be the same in outer module scope
Damien George7efc5b32014-04-05 22:36:42 +010060STATIC mp_obj_dict_t *dict_locals;
61STATIC mp_obj_dict_t *dict_globals;
Damienbd254452013-10-16 20:39:12 +010062
Damien George7efc5b32014-04-05 22:36:42 +010063// dictionary for the __main__ module
Damien George8b0535e2014-04-05 21:53:54 +010064STATIC mp_obj_dict_t dict_main;
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030065
66const mp_obj_module_t mp_module___main__ = {
67 .base = { &mp_type_module },
68 .name = MP_QSTR___main__,
Damien George8b0535e2014-04-05 21:53:54 +010069 .globals = (mp_obj_dict_t*)&dict_main,
Paul Sokolovskyc6813d92014-04-04 20:08:21 +030070};
71
Damien Georged17926d2014-03-30 13:35:08 +010072void mp_init(void) {
Damien George8dbbbbc2014-08-04 10:05:16 +010073 qstr_init();
Paul Sokolovskycaa73342014-07-01 02:13:42 +030074 mp_stack_ctrl_init();
Paul Sokolovsky8a96ebe2014-06-27 20:54:22 +030075
Damien George8dbbbbc2014-08-04 10:05:16 +010076#if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
77 mp_init_emergency_exception_buf();
78#endif
79
Damien George97f9a282014-05-12 23:07:34 +010080 // call port specific initialization if any
stijn72521a12014-05-03 11:28:29 +020081#ifdef MICROPY_PORT_INIT_FUNC
82 MICROPY_PORT_INIT_FUNC;
83#endif
84
Paul Sokolovskyd3439d02014-06-02 19:37:55 +030085 // optimization disabled by default
86 mp_optimise_value = 0;
Damien George97f9a282014-05-12 23:07:34 +010087
Damien Georgecaac5422014-03-25 14:18:18 +000088 // init global module stuff
89 mp_module_init();
Damien George0d028742014-01-22 23:59:20 +000090
Damien George7efc5b32014-04-05 22:36:42 +010091 // initialise the __main__ module
Damien George8b0535e2014-04-05 21:53:54 +010092 mp_obj_dict_init(&dict_main, 1);
Damien George8b0535e2014-04-05 21:53:54 +010093 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 +030094
95 // locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
Damien George7efc5b32014-04-05 22:36:42 +010096 dict_locals = dict_globals = &dict_main;
Damien429d7192013-10-04 19:53:11 +010097}
98
Damien Georged17926d2014-03-30 13:35:08 +010099void mp_deinit(void) {
Damien George7efc5b32014-04-05 22:36:42 +0100100 //mp_obj_dict_free(&dict_main);
Damien George2326d522014-03-27 23:26:35 +0000101 mp_module_deinit();
stijn5ed284a2014-05-08 10:56:33 +0200102
103 // call port specific deinitialization if any
104#ifdef MICROPY_PORT_INIT_FUNC
105 MICROPY_PORT_DEINIT_FUNC;
106#endif
Damien429d7192013-10-04 19:53:11 +0100107}
108
Damien George503d6112014-05-28 14:07:21 +0100109mp_obj_t mp_load_const_int(qstr qstr) {
110 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
111 uint len;
112 const byte* data = qstr_data(qstr, &len);
113 return mp_parse_num_integer((const char*)data, len, 0);
114}
115
Damien Georged17926d2014-03-30 13:35:08 +0100116mp_obj_t mp_load_const_dec(qstr qstr) {
Damien7410e442013-11-02 19:47:57 +0000117 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George20773972014-02-22 18:12:43 +0000118 uint len;
119 const byte* data = qstr_data(qstr, &len);
Damien George6e48f7f2014-03-21 11:45:46 +0000120 return mp_parse_num_decimal((const char*)data, len, true, false);
Damien7410e442013-11-02 19:47:57 +0000121}
122
Damien Georged17926d2014-03-30 13:35:08 +0100123mp_obj_t mp_load_const_str(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100124 DEBUG_OP_printf("load '%s'\n", qstr_str(qstr));
Damien George5fa93b62014-01-22 14:35:10 +0000125 return MP_OBJ_NEW_QSTR(qstr);
Damien429d7192013-10-04 19:53:11 +0100126}
127
Damien Georged17926d2014-03-30 13:35:08 +0100128mp_obj_t mp_load_const_bytes(qstr qstr) {
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200129 DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr));
130 uint len;
131 const byte *data = qstr_data(qstr, &len);
132 return mp_obj_new_bytes(data, len);
133}
134
Damien Georged17926d2014-03-30 13:35:08 +0100135mp_obj_t mp_load_name(qstr qstr) {
Damien429d7192013-10-04 19:53:11 +0100136 // logic: search locals, globals, builtins
Damien George7efc5b32014-04-05 22:36:42 +0100137 DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
Paul Sokolovskya0d32992014-04-05 04:51:26 +0300138 // If we're at the outer scope (locals == globals), dispatch to load_global right away
Damien George7efc5b32014-04-05 22:36:42 +0100139 if (dict_locals != dict_globals) {
140 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 +0300141 if (elem != NULL) {
142 return elem->value;
143 }
Damiena3977762013-10-09 23:10:10 +0100144 }
Paul Sokolovskya0d32992014-04-05 04:51:26 +0300145 return mp_load_global(qstr);
Damiena3977762013-10-09 23:10:10 +0100146}
147
Damien Georged17926d2014-03-30 13:35:08 +0100148mp_obj_t mp_load_global(qstr qstr) {
Damiena3977762013-10-09 23:10:10 +0100149 // logic: search globals, builtins
150 DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
Damien George7efc5b32014-04-05 22:36:42 +0100151 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 +0100152 if (elem == NULL) {
Damien George7efc5b32014-04-05 22:36:42 +0100153 // TODO lookup in dynamic table of builtins first
154 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 +0100155 if (elem == NULL) {
Damien Georgeea13f402014-04-05 18:32:08 +0100156 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 +0100157 }
158 }
159 return elem->value;
160}
161
Damien Georged17926d2014-03-30 13:35:08 +0100162mp_obj_t mp_load_build_class(void) {
Damien429d7192013-10-04 19:53:11 +0100163 DEBUG_OP_printf("load_build_class\n");
Damien George7efc5b32014-04-05 22:36:42 +0100164 // TODO lookup __build_class__ in dynamic table of builtins first
165 // ... else no user-defined __build_class__, return builtin one
166 return (mp_obj_t)&mp_builtin___build_class___obj;
Damien429d7192013-10-04 19:53:11 +0100167}
168
Damien Georged17926d2014-03-30 13:35:08 +0100169void mp_store_name(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100170 DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
Damien George7efc5b32014-04-05 22:36:42 +0100171 mp_obj_dict_store(dict_locals, MP_OBJ_NEW_QSTR(qstr), obj);
Damiena3977762013-10-09 23:10:10 +0100172}
173
Damien Georged17926d2014-03-30 13:35:08 +0100174void mp_delete_name(qstr qstr) {
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200175 DEBUG_OP_printf("delete name %s\n", qstr_str(qstr));
Damien George1d24ea52014-04-08 21:11:49 +0100176 // TODO convert KeyError to NameError if qstr not found
177 mp_obj_dict_delete(dict_locals, MP_OBJ_NEW_QSTR(qstr));
Paul Sokolovskyf9090342014-03-23 21:19:02 +0200178}
179
Damien Georged17926d2014-03-30 13:35:08 +0100180void mp_store_global(qstr qstr, mp_obj_t obj) {
Damiena3977762013-10-09 23:10:10 +0100181 DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
Damien George7efc5b32014-04-05 22:36:42 +0100182 mp_obj_dict_store(dict_globals, MP_OBJ_NEW_QSTR(qstr), obj);
Damien429d7192013-10-04 19:53:11 +0100183}
184
Damien George1d24ea52014-04-08 21:11:49 +0100185void mp_delete_global(qstr qstr) {
186 DEBUG_OP_printf("delete global %s\n", qstr_str(qstr));
187 // TODO convert KeyError to NameError if qstr not found
188 mp_obj_dict_delete(dict_globals, MP_OBJ_NEW_QSTR(qstr));
189}
190
Damien Georged17926d2014-03-30 13:35:08 +0100191mp_obj_t mp_unary_op(int op, mp_obj_t arg) {
Damien7410e442013-11-02 19:47:57 +0000192 DEBUG_OP_printf("unary %d %p\n", op, arg);
Damien George9aa2a522014-02-01 23:04:09 +0000193
Damiend99b0522013-12-21 18:17:45 +0000194 if (MP_OBJ_IS_SMALL_INT(arg)) {
Damien George40f3c022014-07-03 13:25:24 +0100195 mp_int_t val = MP_OBJ_SMALL_INT_VALUE(arg);
Damien7410e442013-11-02 19:47:57 +0000196 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100197 case MP_UNARY_OP_BOOL:
Damien George9d68e9c2014-03-12 15:38:15 +0000198 return MP_BOOL(val != 0);
Damien Georged17926d2014-03-30 13:35:08 +0100199 case MP_UNARY_OP_POSITIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000200 return arg;
Damien Georged17926d2014-03-30 13:35:08 +0100201 case MP_UNARY_OP_NEGATIVE:
Damien George9d68e9c2014-03-12 15:38:15 +0000202 // check for overflow
203 if (val == MP_SMALL_INT_MIN) {
204 return mp_obj_new_int(-val);
205 } else {
206 return MP_OBJ_NEW_SMALL_INT(-val);
207 }
Damien Georged17926d2014-03-30 13:35:08 +0100208 case MP_UNARY_OP_INVERT:
Damien George9d68e9c2014-03-12 15:38:15 +0000209 return MP_OBJ_NEW_SMALL_INT(~val);
210 default:
211 assert(0);
212 return arg;
Damien7410e442013-11-02 19:47:57 +0000213 }
Damien George1e708fe2014-01-23 18:27:51 +0000214 } else {
215 mp_obj_type_t *type = mp_obj_get_type(arg);
216 if (type->unary_op != NULL) {
217 mp_obj_t result = type->unary_op(op, arg);
Damien George6ac5dce2014-05-21 19:42:43 +0100218 if (result != MP_OBJ_NULL) {
Damiend99b0522013-12-21 18:17:45 +0000219 return result;
220 }
Damien7410e442013-11-02 19:47:57 +0000221 }
Damiend99b0522013-12-21 18:17:45 +0000222 // TODO specify in error message what the operator is
Damien Georgeea13f402014-04-05 18:32:08 +0100223 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 +0000224 }
Damien429d7192013-10-04 19:53:11 +0100225}
226
Damien Georged17926d2014-03-30 13:35:08 +0100227mp_obj_t mp_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
Damien429d7192013-10-04 19:53:11 +0100228 DEBUG_OP_printf("binary %d %p %p\n", op, lhs, rhs);
Damien George14f945c2014-01-03 14:09:31 +0000229
230 // TODO correctly distinguish inplace operators for mutable objects
231 // lookup logic that CPython uses for +=:
232 // check for implemented +=
233 // then check for implemented +
234 // then check for implemented seq.inplace_concat
235 // then check for implemented seq.concat
236 // then fail
237 // note that list does not implement + or +=, so that inplace_concat is reached first for +=
238
Damien George9aa2a522014-02-01 23:04:09 +0000239 // deal with is
Damien Georged17926d2014-03-30 13:35:08 +0100240 if (op == MP_BINARY_OP_IS) {
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200241 return MP_BOOL(lhs == rhs);
242 }
Paul Sokolovsky8bc96472014-01-15 00:31:28 +0200243
Damien Georgebcbeea02014-01-11 10:47:22 +0000244 // deal with == and != for all types
Damien Georged17926d2014-03-30 13:35:08 +0100245 if (op == MP_BINARY_OP_EQUAL || op == MP_BINARY_OP_NOT_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000246 if (mp_obj_equal(lhs, rhs)) {
Damien Georged17926d2014-03-30 13:35:08 +0100247 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000248 return mp_const_true;
249 } else {
250 return mp_const_false;
251 }
252 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100253 if (op == MP_BINARY_OP_EQUAL) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000254 return mp_const_false;
255 } else {
256 return mp_const_true;
257 }
258 }
259 }
260
261 // deal with exception_match for all types
Damien Georged17926d2014-03-30 13:35:08 +0100262 if (op == MP_BINARY_OP_EXCEPTION_MATCH) {
Damien Georgec5966122014-02-15 16:10:44 +0000263 // rhs must be issubclass(rhs, BaseException)
264 if (mp_obj_is_exception_type(rhs)) {
265 // if lhs is an instance of an exception, then extract and use its type
266 if (mp_obj_is_exception_instance(lhs)) {
267 lhs = mp_obj_get_type(lhs);
268 }
Damien George71510152014-03-03 22:38:13 +0000269 if (mp_obj_is_subclass_fast(lhs, rhs)) {
Damien Georgebcbeea02014-01-11 10:47:22 +0000270 return mp_const_true;
271 } else {
272 return mp_const_false;
273 }
274 }
Paul Sokolovsky4b2b7ce2014-03-23 20:49:39 +0200275 assert(0);
276 return mp_const_false;
Damien Georgebcbeea02014-01-11 10:47:22 +0000277 }
278
Damien George1a9951d2014-01-06 22:13:00 +0000279 if (MP_OBJ_IS_SMALL_INT(lhs)) {
Damien George40f3c022014-07-03 13:25:24 +0100280 mp_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs);
Damien George1a9951d2014-01-06 22:13:00 +0000281 if (MP_OBJ_IS_SMALL_INT(rhs)) {
Damien George40f3c022014-07-03 13:25:24 +0100282 mp_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs);
Damien George9d68e9c2014-03-12 15:38:15 +0000283 // This is a binary operation: lhs_val op rhs_val
284 // We need to be careful to handle overflow; see CERT INT32-C
285 // Operations that can overflow:
Damien George40f3c022014-07-03 13:25:24 +0100286 // + result always fits in mp_int_t, then handled by SMALL_INT check
287 // - result always fits in mp_int_t, then handled by SMALL_INT check
Damien George9d68e9c2014-03-12 15:38:15 +0000288 // * checked explicitly
Damien George40f3c022014-07-03 13:25:24 +0100289 // / if lhs=MIN and rhs=-1; result always fits in mp_int_t, then handled by SMALL_INT check
290 // % if lhs=MIN and rhs=-1; result always fits in mp_int_t, then handled by SMALL_INT check
Damien George9d68e9c2014-03-12 15:38:15 +0000291 // << checked explicitly
Damien George1a9951d2014-01-06 22:13:00 +0000292 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100293 case MP_BINARY_OP_OR:
294 case MP_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break;
295 case MP_BINARY_OP_XOR:
296 case MP_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break;
297 case MP_BINARY_OP_AND:
298 case MP_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break;
299 case MP_BINARY_OP_LSHIFT:
300 case MP_BINARY_OP_INPLACE_LSHIFT: {
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 if (rhs_val >= BITS_PER_WORD || lhs_val > (MP_SMALL_INT_MAX >> rhs_val) || lhs_val < (MP_SMALL_INT_MIN >> rhs_val)) {
305 // left-shift will overflow, so use higher precision integer
306 lhs = mp_obj_new_int_from_ll(lhs_val);
307 goto generic_binary_op;
308 } else {
309 // use standard precision
310 lhs_val <<= rhs_val;
311 }
312 break;
313 }
Damien Georged17926d2014-03-30 13:35:08 +0100314 case MP_BINARY_OP_RSHIFT:
315 case MP_BINARY_OP_INPLACE_RSHIFT:
Damien George9d68e9c2014-03-12 15:38:15 +0000316 if (rhs_val < 0) {
317 // negative shift not allowed
Damien Georgeea13f402014-04-05 18:32:08 +0100318 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative shift count"));
Damien George9d68e9c2014-03-12 15:38:15 +0000319 } else {
320 // standard precision is enough for right-shift
321 lhs_val >>= rhs_val;
322 }
323 break;
Damien Georged17926d2014-03-30 13:35:08 +0100324 case MP_BINARY_OP_ADD:
325 case MP_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break;
326 case MP_BINARY_OP_SUBTRACT:
327 case MP_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break;
328 case MP_BINARY_OP_MULTIPLY:
329 case MP_BINARY_OP_INPLACE_MULTIPLY: {
Damien George9d68e9c2014-03-12 15:38:15 +0000330
Damien George40f3c022014-07-03 13:25:24 +0100331 // If long long type exists and is larger than mp_int_t, then
Damien George9d68e9c2014-03-12 15:38:15 +0000332 // we can use the following code to perform overflow-checked multiplication.
Damien Georgeecf5b772014-04-04 11:13:51 +0000333 // Otherwise (eg in x64 case) we must use mp_small_int_mul_overflow.
Damien George9d68e9c2014-03-12 15:38:15 +0000334 #if 0
335 // compute result using long long precision
336 long long res = (long long)lhs_val * (long long)rhs_val;
337 if (res > MP_SMALL_INT_MAX || res < MP_SMALL_INT_MIN) {
338 // result overflowed SMALL_INT, so return higher precision integer
339 return mp_obj_new_int_from_ll(res);
340 } else {
341 // use standard precision
Damien George40f3c022014-07-03 13:25:24 +0100342 lhs_val = (mp_int_t)res;
Damien George9d68e9c2014-03-12 15:38:15 +0000343 }
344 #endif
345
Damien Georgeecf5b772014-04-04 11:13:51 +0000346 if (mp_small_int_mul_overflow(lhs_val, rhs_val)) {
347 // use higher precision
348 lhs = mp_obj_new_int_from_ll(lhs_val);
349 goto generic_binary_op;
350 } else {
351 // use standard precision
352 return MP_OBJ_NEW_SMALL_INT(lhs_val * rhs_val);
353 }
Damien George9d68e9c2014-03-12 15:38:15 +0000354 break;
355 }
Damien Georged17926d2014-03-30 13:35:08 +0100356 case MP_BINARY_OP_FLOOR_DIVIDE:
357 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300358 if (rhs_val == 0) {
359 goto zero_division;
360 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000361 lhs_val = mp_small_int_floor_divide(lhs_val, rhs_val);
Rachel Dowdall56402792014-03-22 20:19:24 +0000362 break;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300363
Damien Georgefb510b32014-06-01 13:32:54 +0100364 #if MICROPY_PY_BUILTINS_FLOAT
Damien Georged17926d2014-03-30 13:35:08 +0100365 case MP_BINARY_OP_TRUE_DIVIDE:
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300366 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
367 if (rhs_val == 0) {
Damien George70f33cd2014-04-02 17:06:05 +0100368 goto zero_division;
Paul Sokolovsky6ded55a2014-03-31 02:20:00 +0300369 }
370 return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000371 #endif
Damiena3dcd9e2013-12-17 21:35:38 +0000372
Damien Georged17926d2014-03-30 13:35:08 +0100373 case MP_BINARY_OP_MODULO:
Damien Georgeecf5b772014-04-04 11:13:51 +0000374 case MP_BINARY_OP_INPLACE_MODULO: {
375 lhs_val = mp_small_int_modulo(lhs_val, rhs_val);
Rachel Dowdallcde86312014-03-22 17:29:27 +0000376 break;
377 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000378
Damien Georged17926d2014-03-30 13:35:08 +0100379 case MP_BINARY_OP_POWER:
380 case MP_BINARY_OP_INPLACE_POWER:
Damien George9d68e9c2014-03-12 15:38:15 +0000381 if (rhs_val < 0) {
Damien Georgefb510b32014-06-01 13:32:54 +0100382 #if MICROPY_PY_BUILTINS_FLOAT
Damien George9d68e9c2014-03-12 15:38:15 +0000383 lhs = mp_obj_new_float(lhs_val);
384 goto generic_binary_op;
385 #else
Damien Georgeea13f402014-04-05 18:32:08 +0100386 nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "negative power with no float support"));
Damien George9d68e9c2014-03-12 15:38:15 +0000387 #endif
388 } else {
Damien George40f3c022014-07-03 13:25:24 +0100389 mp_int_t ans = 1;
Damien George9d68e9c2014-03-12 15:38:15 +0000390 while (rhs_val > 0) {
391 if (rhs_val & 1) {
Damien Georgeecf5b772014-04-04 11:13:51 +0000392 if (mp_small_int_mul_overflow(ans, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100393 goto power_overflow;
394 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000395 ans *= lhs_val;
Damien George9d68e9c2014-03-12 15:38:15 +0000396 }
Damien George5bf565e2014-04-04 00:16:32 +0100397 if (rhs_val == 1) {
398 break;
399 }
Damien George9d68e9c2014-03-12 15:38:15 +0000400 rhs_val /= 2;
Damien Georgeecf5b772014-04-04 11:13:51 +0000401 if (mp_small_int_mul_overflow(lhs_val, lhs_val)) {
Damien George5bf565e2014-04-04 00:16:32 +0100402 goto power_overflow;
403 }
Damien Georgeecf5b772014-04-04 11:13:51 +0000404 lhs_val *= lhs_val;
Damien George1a9951d2014-01-06 22:13:00 +0000405 }
Damien George9d68e9c2014-03-12 15:38:15 +0000406 lhs_val = ans;
Damiena3dcd9e2013-12-17 21:35:38 +0000407 }
Damien George1a9951d2014-01-06 22:13:00 +0000408 break;
Damien George5bf565e2014-04-04 00:16:32 +0100409
410 power_overflow:
411 // use higher precision
412 lhs = mp_obj_new_int_from_ll(MP_OBJ_SMALL_INT_VALUE(lhs));
413 goto generic_binary_op;
414
Damien Georged17926d2014-03-30 13:35:08 +0100415 case MP_BINARY_OP_LESS: return MP_BOOL(lhs_val < rhs_val); break;
416 case MP_BINARY_OP_MORE: return MP_BOOL(lhs_val > rhs_val); break;
417 case MP_BINARY_OP_LESS_EQUAL: return MP_BOOL(lhs_val <= rhs_val); break;
418 case MP_BINARY_OP_MORE_EQUAL: return MP_BOOL(lhs_val >= rhs_val); break;
Damiena3dcd9e2013-12-17 21:35:38 +0000419
Damien George8bcb9862014-04-17 16:26:50 +0100420 default:
421 goto unsupported_op;
Damien George1a9951d2014-01-06 22:13:00 +0000422 }
Paul Sokolovsky757ac812014-01-12 17:06:25 +0200423 // TODO: We just should make mp_obj_new_int() inline and use that
Damien Georged1e355e2014-05-28 14:51:12 +0100424 if (MP_SMALL_INT_FITS(lhs_val)) {
Damien George1a9951d2014-01-06 22:13:00 +0000425 return MP_OBJ_NEW_SMALL_INT(lhs_val);
Damien George9d68e9c2014-03-12 15:38:15 +0000426 } else {
427 return mp_obj_new_int(lhs_val);
Damien George1a9951d2014-01-06 22:13:00 +0000428 }
Damien Georgefb510b32014-06-01 13:32:54 +0100429#if MICROPY_PY_BUILTINS_FLOAT
Damien George0c36da02014-03-08 15:24:39 +0000430 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_float)) {
Damien Georgeae491052014-04-10 20:08:11 +0100431 mp_obj_t res = mp_obj_float_binary_op(op, lhs_val, rhs);
432 if (res == MP_OBJ_NULL) {
433 goto unsupported_op;
434 } else {
435 return res;
436 }
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300437#if MICROPY_PY_BUILTINS_COMPLEX
Damien George0c36da02014-03-08 15:24:39 +0000438 } else if (MP_OBJ_IS_TYPE(rhs, &mp_type_complex)) {
Damien Georgeae491052014-04-10 20:08:11 +0100439 mp_obj_t res = mp_obj_complex_binary_op(op, lhs_val, 0, rhs);
440 if (res == MP_OBJ_NULL) {
441 goto unsupported_op;
442 } else {
443 return res;
444 }
Damien George3f759b72014-01-31 00:42:12 +0000445#endif
Paul Sokolovsky3b6f7b92014-06-20 01:48:35 +0300446#endif
Damien429d7192013-10-04 19:53:11 +0100447 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000448 }
John R. Lentonb8698fc2014-01-11 00:58:59 +0000449
Damien George9aa2a522014-02-01 23:04:09 +0000450 /* deal with `in`
John R. Lentonc1bef212014-01-11 12:39:33 +0000451 *
452 * NOTE `a in b` is `b.__contains__(a)`, hence why the generic dispatch
Damien George48697f12014-02-01 23:32:29 +0000453 * needs to go below with swapped arguments
John R. Lentonc1bef212014-01-11 12:39:33 +0000454 */
Damien Georged17926d2014-03-30 13:35:08 +0100455 if (op == MP_BINARY_OP_IN) {
Damien George5fa93b62014-01-22 14:35:10 +0000456 mp_obj_type_t *type = mp_obj_get_type(rhs);
457 if (type->binary_op != NULL) {
458 mp_obj_t res = type->binary_op(op, rhs, lhs);
Damien George6ac5dce2014-05-21 19:42:43 +0100459 if (res != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000460 return res;
461 }
462 }
463 if (type->getiter != NULL) {
464 /* second attempt, walk the iterator */
465 mp_obj_t next = NULL;
Damien Georged17926d2014-03-30 13:35:08 +0100466 mp_obj_t iter = mp_getiter(rhs);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100467 while ((next = mp_iternext(iter)) != MP_OBJ_STOP_ITERATION) {
Damien George5fa93b62014-01-22 14:35:10 +0000468 if (mp_obj_equal(next, lhs)) {
Damien George9aa2a522014-02-01 23:04:09 +0000469 return mp_const_true;
John R. Lentonb8698fc2014-01-11 00:58:59 +0000470 }
Damien7410e442013-11-02 19:47:57 +0000471 }
Damien George9aa2a522014-02-01 23:04:09 +0000472 return mp_const_false;
Damien7410e442013-11-02 19:47:57 +0000473 }
John R. Lentonc1bef212014-01-11 12:39:33 +0000474
Damien Georgeea13f402014-04-05 18:32:08 +0100475 nlr_raise(mp_obj_new_exception_msg_varg(
Damien Georgec5966122014-02-15 16:10:44 +0000476 &mp_type_TypeError, "'%s' object is not iterable",
John R. Lentonc1bef212014-01-11 12:39:33 +0000477 mp_obj_get_type_str(rhs)));
478 return mp_const_none;
479 }
480
Damien George5fa93b62014-01-22 14:35:10 +0000481 // generic binary_op supplied by type
Damien George9d68e9c2014-03-12 15:38:15 +0000482 mp_obj_type_t *type;
483generic_binary_op:
484 type = mp_obj_get_type(lhs);
Damien George5fa93b62014-01-22 14:35:10 +0000485 if (type->binary_op != NULL) {
486 mp_obj_t result = type->binary_op(op, lhs, rhs);
Damien George6ac5dce2014-05-21 19:42:43 +0100487 if (result != MP_OBJ_NULL) {
Damien George5fa93b62014-01-22 14:35:10 +0000488 return result;
John R. Lentonc1bef212014-01-11 12:39:33 +0000489 }
Damien429d7192013-10-04 19:53:11 +0100490 }
Damiend99b0522013-12-21 18:17:45 +0000491
Damien George5fa93b62014-01-22 14:35:10 +0000492 // TODO implement dispatch for reverse binary ops
493
Damiend99b0522013-12-21 18:17:45 +0000494 // TODO specify in error message what the operator is
Damien Georgeae491052014-04-10 20:08:11 +0100495unsupported_op:
Damien Georgeea13f402014-04-05 18:32:08 +0100496 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
Paul Sokolovskybab5cfb2014-01-10 17:32:22 +0200497 "unsupported operand types for binary operator: '%s', '%s'",
498 mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
John R. Lentonc1bef212014-01-11 12:39:33 +0000499 return mp_const_none;
Damien George70f33cd2014-04-02 17:06:05 +0100500
501zero_division:
Damien Georgeea13f402014-04-05 18:32:08 +0100502 nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
Damien429d7192013-10-04 19:53:11 +0100503}
504
Damien Georged17926d2014-03-30 13:35:08 +0100505mp_obj_t mp_call_function_0(mp_obj_t fun) {
506 return mp_call_function_n_kw(fun, 0, 0, NULL);
Damieneb19efb2013-10-10 22:06:54 +0100507}
508
Damien Georged17926d2014-03-30 13:35:08 +0100509mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg) {
510 return mp_call_function_n_kw(fun, 1, 0, &arg);
Damieneb19efb2013-10-10 22:06:54 +0100511}
512
Damien Georged17926d2014-03-30 13:35:08 +0100513mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) {
Damiend99b0522013-12-21 18:17:45 +0000514 mp_obj_t args[2];
Damien George20006db2014-01-18 14:10:48 +0000515 args[0] = arg1;
516 args[1] = arg2;
Damien Georged17926d2014-03-30 13:35:08 +0100517 return mp_call_function_n_kw(fun, 2, 0, args);
Damieneb19efb2013-10-10 22:06:54 +0100518}
519
Damien Georgecd82e022014-02-02 13:11:48 +0000520// wrapper that accepts n_args and n_kw in one argument
521// native emitter can only pass at most 3 arguments to a function
Damien Georged17926d2014-03-30 13:35:08 +0100522mp_obj_t mp_call_function_n_kw_for_native(mp_obj_t fun_in, uint n_args_kw, const mp_obj_t *args) {
523 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 +0000524}
525
Damien George20006db2014-01-18 14:10:48 +0000526// args contains, eg: arg0 arg1 key0 value0 key1 value1
Damien Georged17926d2014-03-30 13:35:08 +0100527mp_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 +0000528 // TODO improve this: fun object can specify its type and we parse here the arguments,
529 // passing to the function arrays of fixed and keyword arguments
Damieneb19efb2013-10-10 22:06:54 +0100530
John R. Lenton9c83ec02014-01-07 23:06:46 +0000531 DEBUG_OP_printf("calling function %p(n_args=%d, n_kw=%d, args=%p)\n", fun_in, n_args, n_kw, args);
532
Damien George8b56beb2014-01-31 23:49:49 +0000533 // get the type
534 mp_obj_type_t *type = mp_obj_get_type(fun_in);
535
536 // do the call
537 if (type->call != NULL) {
Damien George3f522622014-06-03 13:40:16 +0100538 return type->call(fun_in, n_args, n_kw, args);
John R. Lenton9c83ec02014-01-07 23:06:46 +0000539 }
Paul Sokolovsky755565d2014-04-25 21:15:16 +0300540
541 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 +0000542}
543
Damien George20006db2014-01-18 14:10:48 +0000544// 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)
545// if n_args==0 and n_kw==0 then there are only fun and self/NULL
Damien Georged17926d2014-03-30 13:35:08 +0100546mp_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 +0000547 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);
548 int adjust = (args[1] == NULL) ? 0 : 1;
Damien Georged17926d2014-03-30 13:35:08 +0100549 return mp_call_function_n_kw(args[0], n_args + adjust, n_kw, args + 2 - adjust);
Damien86c7fc72013-11-26 15:16:41 +0000550}
551
Damien George523b5752014-03-31 11:59:23 +0100552mp_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 +0100553 mp_obj_t fun = *args++;
554 mp_obj_t self = MP_OBJ_NULL;
555 if (have_self) {
556 self = *args++; // may be MP_OBJ_NULL
557 }
558 uint n_args = n_args_n_kw & 0xff;
559 uint n_kw = (n_args_n_kw >> 8) & 0xff;
Damien George523b5752014-03-31 11:59:23 +0100560 mp_obj_t pos_seq = args[n_args + 2 * n_kw]; // map be MP_OBJ_NULL
561 mp_obj_t kw_dict = args[n_args + 2 * n_kw + 1]; // map be MP_OBJ_NULL
Damien George230fec72014-03-30 21:21:24 +0100562
563 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);
564
565 // We need to create the following array of objects:
566 // args[0 .. n_args] unpacked(pos_seq) args[n_args .. n_args + 2 * n_kw] unpacked(kw_dict)
567 // TODO: optimize one day to avoid constructing new arg array? Will be hard.
568
569 // The new args array
570 mp_obj_t *args2;
571 uint args2_alloc;
572 uint args2_len = 0;
573
574 // Try to get a hint for the size of the kw_dict
575 uint kw_dict_len = 0;
576 if (kw_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
577 kw_dict_len = mp_obj_dict_len(kw_dict);
578 }
579
580 // Extract the pos_seq sequence to the new args array.
581 // Note that it can be arbitrary iterator.
582 if (pos_seq == MP_OBJ_NULL) {
583 // no sequence
584
585 // allocate memory for the new array of args
586 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len);
587 args2 = m_new(mp_obj_t, args2_alloc);
588
589 // copy the self
590 if (self != MP_OBJ_NULL) {
591 args2[args2_len++] = self;
592 }
593
594 // copy the fixed pos args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300595 mp_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100596 args2_len += n_args;
597
598 } else if (MP_OBJ_IS_TYPE(pos_seq, &mp_type_tuple) || MP_OBJ_IS_TYPE(pos_seq, &mp_type_list)) {
599 // optimise the case of a tuple and list
600
601 // get the items
602 uint len;
603 mp_obj_t *items;
604 mp_obj_get_array(pos_seq, &len, &items);
605
606 // allocate memory for the new array of args
607 args2_alloc = 1 + n_args + len + 2 * (n_kw + kw_dict_len);
608 args2 = m_new(mp_obj_t, args2_alloc);
609
610 // copy the self
611 if (self != MP_OBJ_NULL) {
612 args2[args2_len++] = self;
613 }
614
615 // copy the fixed and variable position args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300616 mp_seq_cat(args2 + args2_len, args, n_args, items, len, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100617 args2_len += n_args + len;
618
619 } else {
620 // generic iterator
621
622 // allocate memory for the new array of args
623 args2_alloc = 1 + n_args + 2 * (n_kw + kw_dict_len) + 3;
624 args2 = m_new(mp_obj_t, args2_alloc);
625
626 // copy the self
627 if (self != MP_OBJ_NULL) {
628 args2[args2_len++] = self;
629 }
630
631 // copy the fixed position args
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300632 mp_seq_copy(args2 + args2_len, args, n_args, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100633
634 // extract the variable position args from the iterator
635 mp_obj_t iterable = mp_getiter(pos_seq);
636 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100637 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George230fec72014-03-30 21:21:24 +0100638 if (args2_len >= args2_alloc) {
639 args2 = m_renew(mp_obj_t, args2, args2_alloc, args2_alloc * 2);
640 args2_alloc *= 2;
641 }
642 args2[args2_len++] = item;
643 }
644 }
645
646 // The size of the args2 array now is the number of positional args.
647 uint pos_args_len = args2_len;
648
649 // Copy the fixed kw args.
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300650 mp_seq_copy(args2 + args2_len, args + n_args, 2 * n_kw, mp_obj_t);
Damien George230fec72014-03-30 21:21:24 +0100651 args2_len += 2 * n_kw;
652
653 // Extract (key,value) pairs from kw_dict dictionary and append to args2.
654 // Note that it can be arbitrary iterator.
655 if (kw_dict == MP_OBJ_NULL) {
656 // pass
657 } else if (MP_OBJ_IS_TYPE(kw_dict, &mp_type_dict)) {
658 // dictionary
659 mp_map_t *map = mp_obj_dict_get_map(kw_dict);
660 assert(args2_len + 2 * map->used <= args2_alloc); // should have enough, since kw_dict_len is in this case hinted correctly above
661 for (uint i = 0; i < map->alloc; i++) {
662 if (map->table[i].key != MP_OBJ_NULL) {
663 args2[args2_len++] = map->table[i].key;
664 args2[args2_len++] = map->table[i].value;
665 }
666 }
667 } else {
668 // generic mapping
669 // TODO is calling 'items' on the mapping the correct thing to do here?
670 mp_obj_t dest[2];
671 mp_load_method(kw_dict, MP_QSTR_items, dest);
672 mp_obj_t iterable = mp_getiter(mp_call_method_n_kw(0, 0, dest));
673 mp_obj_t item;
Damien Georgeea8d06c2014-04-17 23:19:36 +0100674 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George230fec72014-03-30 21:21:24 +0100675 if (args2_len + 1 >= args2_alloc) {
676 uint new_alloc = args2_alloc * 2;
677 if (new_alloc < 4) {
678 new_alloc = 4;
679 }
680 args2 = m_renew(mp_obj_t, args2, args2_alloc, new_alloc);
681 args2_alloc = new_alloc;
682 }
683 mp_obj_t *items;
684 mp_obj_get_array_fixed_n(item, 2, &items);
685 args2[args2_len++] = items[0];
686 args2[args2_len++] = items[1];
687 }
688 }
689
690 mp_obj_t res = mp_call_function_n_kw(fun, pos_args_len, (args2_len - pos_args_len) / 2, args2);
691 m_del(mp_obj_t, args2, args2_alloc);
692
693 return res;
694}
695
Damien George932bf1c2014-01-18 23:42:49 +0000696// unpacked items are stored in reverse order into the array pointed to by items
Damien Georged17926d2014-03-30 13:35:08 +0100697void mp_unpack_sequence(mp_obj_t seq_in, uint num, mp_obj_t *items) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200698 uint seq_len;
Damien George3e1a5c12014-03-29 13:43:38 +0000699 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 +0000700 mp_obj_t *seq_items;
Damien George07ddab52014-03-29 13:15:08 +0000701 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
Damiend99b0522013-12-21 18:17:45 +0000702 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
703 } else {
704 mp_obj_list_get(seq_in, &seq_len, &seq_items);
Damien86c7fc72013-11-26 15:16:41 +0000705 }
Damiend99b0522013-12-21 18:17:45 +0000706 if (seq_len < num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200707 goto too_short;
Damiend99b0522013-12-21 18:17:45 +0000708 } else if (seq_len > num) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200709 goto too_long;
Damiend99b0522013-12-21 18:17:45 +0000710 }
Damien George932bf1c2014-01-18 23:42:49 +0000711 for (uint i = 0; i < num; i++) {
712 items[i] = seq_items[num - 1 - i];
713 }
Damien86c7fc72013-11-26 15:16:41 +0000714 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100715 mp_obj_t iterable = mp_getiter(seq_in);
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200716
717 for (seq_len = 0; seq_len < num; seq_len++) {
Damien Georged17926d2014-03-30 13:35:08 +0100718 mp_obj_t el = mp_iternext(iterable);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100719 if (el == MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200720 goto too_short;
721 }
722 items[num - 1 - seq_len] = el;
723 }
Damien Georgeea8d06c2014-04-17 23:19:36 +0100724 if (mp_iternext(iterable) != MP_OBJ_STOP_ITERATION) {
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200725 goto too_long;
726 }
Damien86c7fc72013-11-26 15:16:41 +0000727 }
Paul Sokolovskyedbdf712014-02-02 00:54:06 +0200728 return;
729
730too_short:
Damien Georgeea13f402014-04-05 18:32:08 +0100731 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 +0200732too_long:
Damien Georgeea13f402014-04-05 18:32:08 +0100733 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 +0000734}
735
Damien George495d7812014-04-08 17:51:47 +0100736// unpacked items are stored in reverse order into the array pointed to by items
737void mp_unpack_ex(mp_obj_t seq_in, uint num_in, mp_obj_t *items) {
738 uint num_left = num_in & 0xff;
739 uint num_right = (num_in >> 8) & 0xff;
740 DEBUG_OP_printf("unpack ex %d %d\n", num_left, num_right);
741 uint seq_len;
742 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(seq_in, &mp_type_list)) {
743 mp_obj_t *seq_items;
744 if (MP_OBJ_IS_TYPE(seq_in, &mp_type_tuple)) {
745 mp_obj_tuple_get(seq_in, &seq_len, &seq_items);
746 } else {
747 if (num_left == 0 && num_right == 0) {
748 // *a, = b # sets a to b if b is a list
749 items[0] = seq_in;
750 return;
751 }
752 mp_obj_list_get(seq_in, &seq_len, &seq_items);
753 }
754 if (seq_len < num_left + num_right) {
755 goto too_short;
756 }
757 for (uint i = 0; i < num_right; i++) {
758 items[i] = seq_items[seq_len - 1 - i];
759 }
760 items[num_right] = mp_obj_new_list(seq_len - num_left - num_right, seq_items + num_left);
761 for (uint i = 0; i < num_left; i++) {
762 items[num_right + 1 + i] = seq_items[num_left - 1 - i];
763 }
764 } else {
765 // Generic iterable; this gets a bit messy: we unpack known left length to the
766 // items destination array, then the rest to a dynamically created list. Once the
767 // iterable is exhausted, we take from this list for the right part of the items.
768 // TODO Improve to waste less memory in the dynamically created list.
769 mp_obj_t iterable = mp_getiter(seq_in);
770 mp_obj_t item;
771 for (seq_len = 0; seq_len < num_left; seq_len++) {
772 item = mp_iternext(iterable);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100773 if (item == MP_OBJ_STOP_ITERATION) {
Damien George495d7812014-04-08 17:51:47 +0100774 goto too_short;
775 }
776 items[num_left + num_right + 1 - 1 - seq_len] = item;
777 }
778 mp_obj_t rest = mp_obj_new_list(0, NULL);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100779 while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
Damien George495d7812014-04-08 17:51:47 +0100780 mp_obj_list_append(rest, item);
781 }
782 uint rest_len;
783 mp_obj_t *rest_items;
784 mp_obj_list_get(rest, &rest_len, &rest_items);
785 if (rest_len < num_right) {
786 goto too_short;
787 }
788 items[num_right] = rest;
789 for (uint i = 0; i < num_right; i++) {
790 items[num_right - 1 - i] = rest_items[rest_len - num_right + i];
791 }
792 mp_obj_list_set_len(rest, rest_len - num_right);
793 }
794 return;
795
796too_short:
797 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", seq_len));
798}
799
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300800mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
Damien George062478e2014-01-09 20:57:50 +0000801 DEBUG_OP_printf("load attr %p.%s\n", base, qstr_str(attr));
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300802 // use load_method
Damien George062478e2014-01-09 20:57:50 +0000803 mp_obj_t dest[2];
Paul Sokolovsky36dd19a2014-04-06 02:12:03 +0300804 mp_load_method(base, attr, dest);
805 if (dest[1] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000806 // load_method returned just a normal attribute
Damien George20006db2014-01-18 14:10:48 +0000807 return dest[0];
Damien George062478e2014-01-09 20:57:50 +0000808 } else {
809 // load_method returned a method, so build a bound method object
810 return mp_obj_new_bound_meth(dest[0], dest[1]);
Damiend99b0522013-12-21 18:17:45 +0000811 }
Damiend99b0522013-12-21 18:17:45 +0000812}
813
Damien George7c9c6672014-01-25 00:17:36 +0000814// no attribute found, returns: dest[0] == MP_OBJ_NULL, dest[1] == MP_OBJ_NULL
815// normal attribute found, returns: dest[0] == <attribute>, dest[1] == MP_OBJ_NULL
816// method attribute found, returns: dest[0] == <method>, dest[1] == <self>
Damien Georgee44d26a2014-03-31 22:57:56 +0100817void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George062478e2014-01-09 20:57:50 +0000818 // clear output to indicate no attribute/method found yet
819 dest[0] = MP_OBJ_NULL;
820 dest[1] = MP_OBJ_NULL;
821
822 // get the type
823 mp_obj_type_t *type = mp_obj_get_type(base);
824
Damien Georgee44d26a2014-03-31 22:57:56 +0100825 // look for built-in names
826 if (0) {
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +0300827#if MICROPY_CPYTHON_COMPAT
Damien Georgee44d26a2014-03-31 22:57:56 +0100828 } else if (attr == MP_QSTR___class__) {
829 // a.__class__ is equivalent to type(a)
830 dest[0] = type;
Paul Sokolovsky6ce78c42014-03-31 20:30:08 +0300831#endif
Damien Georgee44d26a2014-03-31 22:57:56 +0100832
833 } else if (attr == MP_QSTR___next__ && type->iternext != NULL) {
834 dest[0] = (mp_obj_t)&mp_builtin_next_obj;
835 dest[1] = base;
836
837 } else if (type->load_attr != NULL) {
838 // this type can do its own load, so call it
839 type->load_attr(base, attr, dest);
840
841 } else if (type->locals_dict != NULL) {
842 // generic method lookup
843 // this is a lookup in the object (ie not class or type)
844 assert(MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)); // Micro Python restriction, for now
845 mp_map_t *locals_map = mp_obj_dict_get_map(type->locals_dict);
846 mp_map_elem_t *elem = mp_map_lookup(locals_map, MP_OBJ_NEW_QSTR(attr), MP_MAP_LOOKUP);
847 if (elem != NULL) {
848 // check if the methods are functions, static or class methods
Chris Angelicodaf973a2014-06-06 03:51:03 +1000849 // see http://docs.python.org/3/howto/descriptor.html
Damien Georgee44d26a2014-03-31 22:57:56 +0100850 if (MP_OBJ_IS_TYPE(elem->value, &mp_type_staticmethod)) {
851 // return just the function
852 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
853 } else if (MP_OBJ_IS_TYPE(elem->value, &mp_type_classmethod)) {
854 // return a bound method, with self being the type of this object
855 dest[0] = ((mp_obj_static_class_method_t*)elem->value)->fun;
856 dest[1] = mp_obj_get_type(base);
Paul Sokolovsky9511f602014-05-11 03:12:36 +0300857 } else if (MP_OBJ_IS_TYPE(elem->value, &mp_type_type)) {
858 // Don't try to bind types
859 dest[0] = elem->value;
Damien Georgee44d26a2014-03-31 22:57:56 +0100860 } else if (mp_obj_is_callable(elem->value)) {
861 // return a bound method, with self being this object
862 dest[0] = elem->value;
863 dest[1] = base;
864 } else {
865 // class member is a value, so just return that value
866 dest[0] = elem->value;
Damiend57eba52013-11-02 23:58:14 +0000867 }
868 }
Damiena3977762013-10-09 23:10:10 +0100869 }
Damien George7c9c6672014-01-25 00:17:36 +0000870}
Damiena3977762013-10-09 23:10:10 +0100871
Damien Georged17926d2014-03-30 13:35:08 +0100872void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
Damien George7c9c6672014-01-25 00:17:36 +0000873 DEBUG_OP_printf("load method %p.%s\n", base, qstr_str(attr));
874
Damien Georged17926d2014-03-30 13:35:08 +0100875 mp_load_method_maybe(base, attr, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000876
877 if (dest[0] == MP_OBJ_NULL) {
Damien George062478e2014-01-09 20:57:50 +0000878 // no attribute/method called attr
879 // following CPython, we give a more detailed error message for type objects
Damien Georgec5966122014-02-15 16:10:44 +0000880 if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
Damien Georgeea13f402014-04-05 18:32:08 +0100881 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
Paul Sokolovsky7f8b3132014-03-25 00:55:39 +0200882 "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 +0000883 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100884 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 +0000885 }
886 }
Damiena3977762013-10-09 23:10:10 +0100887}
888
Damien Georged17926d2014-03-30 13:35:08 +0100889void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
Damien5ac1b2e2013-10-18 19:58:12 +0100890 DEBUG_OP_printf("store attr %p.%s <- %p\n", base, qstr_str(attr), value);
Damien George062478e2014-01-09 20:57:50 +0000891 mp_obj_type_t *type = mp_obj_get_type(base);
892 if (type->store_attr != NULL) {
893 if (type->store_attr(base, attr, value)) {
894 return;
895 }
Damiena3977762013-10-09 23:10:10 +0100896 }
Damien Georgeea13f402014-04-05 18:32:08 +0100897 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 +0100898}
899
Damien Georged17926d2014-03-30 13:35:08 +0100900mp_obj_t mp_getiter(mp_obj_t o_in) {
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +0300901 assert(o_in);
Damien George5fa93b62014-01-22 14:35:10 +0000902 mp_obj_type_t *type = mp_obj_get_type(o_in);
903 if (type->getiter != NULL) {
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +0300904 mp_obj_t iter = type->getiter(o_in);
905 if (iter == MP_OBJ_NULL) {
906 goto not_iterable;
907 }
908 return iter;
Damience89a212013-10-15 22:25:17 +0100909 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000910 // check for __iter__ method
Damien George7c9c6672014-01-25 00:17:36 +0000911 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100912 mp_load_method_maybe(o_in, MP_QSTR___iter__, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000913 if (dest[0] != MP_OBJ_NULL) {
Damien George9e6e9352014-03-26 18:37:06 +0000914 // __iter__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100915 return mp_call_method_n_kw(0, 0, dest);
Damien George7c9c6672014-01-25 00:17:36 +0000916 } else {
Damien Georged17926d2014-03-30 13:35:08 +0100917 mp_load_method_maybe(o_in, MP_QSTR___getitem__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000918 if (dest[0] != MP_OBJ_NULL) {
919 // __getitem__ exists, create an iterator
920 return mp_obj_new_getitem_iter(dest);
921 } else {
922 // object not iterable
Paul Sokolovskyc48d6f72014-05-11 20:32:39 +0300923not_iterable:
Damien Georgeea13f402014-04-05 18:32:08 +0100924 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 +0000925 }
Damien George7c9c6672014-01-25 00:17:36 +0000926 }
Damience89a212013-10-15 22:25:17 +0100927 }
928}
929
Damien Georgeea8d06c2014-04-17 23:19:36 +0100930// may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration()
Damien George66eaf842014-03-26 19:27:58 +0000931// may also raise StopIteration()
Damien Georged17926d2014-03-30 13:35:08 +0100932mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
Damien George5fa93b62014-01-22 14:35:10 +0000933 mp_obj_type_t *type = mp_obj_get_type(o_in);
934 if (type->iternext != NULL) {
935 return type->iternext(o_in);
Damience89a212013-10-15 22:25:17 +0100936 } else {
Damien George9e6e9352014-03-26 18:37:06 +0000937 // check for __next__ method
938 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100939 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000940 if (dest[0] != MP_OBJ_NULL) {
941 // __next__ exists, call it and return its result
Damien Georged17926d2014-03-30 13:35:08 +0100942 return mp_call_method_n_kw(0, 0, dest);
Damien George9e6e9352014-03-26 18:37:06 +0000943 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100944 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 +0000945 }
Damien Georgec5966122014-02-15 16:10:44 +0000946 }
947}
948
Damien Georgeea8d06c2014-04-17 23:19:36 +0100949// will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration() (or any subclass thereof)
Damien George66eaf842014-03-26 19:27:58 +0000950// may raise other exceptions
Damien Georged17926d2014-03-30 13:35:08 +0100951mp_obj_t mp_iternext(mp_obj_t o_in) {
Damien George66eaf842014-03-26 19:27:58 +0000952 mp_obj_type_t *type = mp_obj_get_type(o_in);
953 if (type->iternext != NULL) {
954 return type->iternext(o_in);
955 } else {
956 // check for __next__ method
957 mp_obj_t dest[2];
Damien Georged17926d2014-03-30 13:35:08 +0100958 mp_load_method_maybe(o_in, MP_QSTR___next__, dest);
Damien George66eaf842014-03-26 19:27:58 +0000959 if (dest[0] != MP_OBJ_NULL) {
960 // __next__ exists, call it and return its result
961 nlr_buf_t nlr;
962 if (nlr_push(&nlr) == 0) {
Damien Georged17926d2014-03-30 13:35:08 +0100963 mp_obj_t ret = mp_call_method_n_kw(0, 0, dest);
Damien George66eaf842014-03-26 19:27:58 +0000964 nlr_pop();
965 return ret;
966 } else {
967 if (mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
Damien Georgeea8d06c2014-04-17 23:19:36 +0100968 return MP_OBJ_STOP_ITERATION;
Damien George66eaf842014-03-26 19:27:58 +0000969 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100970 nlr_raise(nlr.ret_val);
Damien George66eaf842014-03-26 19:27:58 +0000971 }
972 }
973 } else {
Damien Georgeea13f402014-04-05 18:32:08 +0100974 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 +0000975 }
976 }
977}
978
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300979// TODO: Unclear what to do with StopIterarion exception here.
980mp_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 +0300981 assert((send_value != MP_OBJ_NULL) ^ (throw_value != MP_OBJ_NULL));
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300982 mp_obj_type_t *type = mp_obj_get_type(self_in);
983
984 if (type == &mp_type_gen_instance) {
985 return mp_obj_gen_resume(self_in, send_value, throw_value, ret_val);
986 }
987
988 if (type->iternext != NULL && send_value == mp_const_none) {
989 mp_obj_t ret = type->iternext(self_in);
990 if (ret != MP_OBJ_NULL) {
991 *ret_val = ret;
992 return MP_VM_RETURN_YIELD;
993 } else {
994 // Emulate raise StopIteration()
995 // Special case, handled in vm.c
996 *ret_val = MP_OBJ_NULL;
997 return MP_VM_RETURN_NORMAL;
998 }
999 }
1000
1001 mp_obj_t dest[3]; // Reserve slot for send() arg
1002
1003 if (send_value == mp_const_none) {
1004 mp_load_method_maybe(self_in, MP_QSTR___next__, dest);
1005 if (dest[0] != MP_OBJ_NULL) {
1006 *ret_val = mp_call_method_n_kw(0, 0, dest);
1007 return MP_VM_RETURN_YIELD;
1008 }
1009 }
1010
1011 if (send_value != MP_OBJ_NULL) {
1012 mp_load_method(self_in, MP_QSTR_send, dest);
1013 dest[2] = send_value;
1014 *ret_val = mp_call_method_n_kw(1, 0, dest);
1015 return MP_VM_RETURN_YIELD;
1016 }
1017
1018 if (throw_value != MP_OBJ_NULL) {
1019 if (mp_obj_is_subclass_fast(mp_obj_get_type(throw_value), &mp_type_GeneratorExit)) {
1020 mp_load_method_maybe(self_in, MP_QSTR_close, dest);
1021 if (dest[0] != MP_OBJ_NULL) {
1022 *ret_val = mp_call_method_n_kw(0, 0, dest);
1023 // We assume one can't "yield" from close()
1024 return MP_VM_RETURN_NORMAL;
1025 }
1026 }
Paul Sokolovskya2109d92014-03-31 04:14:30 +03001027 mp_load_method_maybe(self_in, MP_QSTR_throw, dest);
1028 if (dest[0] != MP_OBJ_NULL) {
1029 *ret_val = mp_call_method_n_kw(1, 0, &throw_value);
1030 // If .throw() method returned, we assume it's value to yield
Damien Georgeea13f402014-04-05 18:32:08 +01001031 // - any exception would be thrown with nlr_raise().
Paul Sokolovskya2109d92014-03-31 04:14:30 +03001032 return MP_VM_RETURN_YIELD;
1033 }
1034 // If there's nowhere to throw exception into, then we assume that object
1035 // is just incapable to handle it, so any exception thrown into it
1036 // will be propagated up. This behavior is approved by test_pep380.py
1037 // test_delegation_of_close_to_non_generator(),
1038 // test_delegating_throw_to_non_generator()
1039 *ret_val = throw_value;
1040 return MP_VM_RETURN_EXCEPTION;
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +03001041 }
1042
1043 assert(0);
1044 return MP_VM_RETURN_NORMAL; // Should be unreachable
1045}
1046
Damien Georged17926d2014-03-30 13:35:08 +01001047mp_obj_t mp_make_raise_obj(mp_obj_t o) {
Damien Georgec5966122014-02-15 16:10:44 +00001048 DEBUG_printf("raise %p\n", o);
1049 if (mp_obj_is_exception_type(o)) {
1050 // o is an exception type (it is derived from BaseException (or is BaseException))
1051 // create and return a new exception instance by calling o
Damien George22a08652014-02-15 21:05:25 +00001052 // TODO could have an option to disable traceback, then builtin exceptions (eg TypeError)
1053 // could have const instances in ROM which we return here instead
Damien Georged17926d2014-03-30 13:35:08 +01001054 return mp_call_function_n_kw(o, 0, 0, NULL);
Damien Georgec5966122014-02-15 16:10:44 +00001055 } else if (mp_obj_is_exception_instance(o)) {
1056 // o is an instance of an exception, so use it as the exception
1057 return o;
1058 } else {
1059 // o cannot be used as an exception, so return a type error (which will be raised by the caller)
1060 return mp_obj_new_exception_msg(&mp_type_TypeError, "exceptions must derive from BaseException");
Damience89a212013-10-15 22:25:17 +01001061 }
1062}
1063
Damien Georged17926d2014-03-30 13:35:08 +01001064mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level) {
Damien George64131f32014-02-06 20:31:44 +00001065 DEBUG_printf("import name %s\n", qstr_str(name));
1066
Damiendb4c3612013-12-10 17:27:24 +00001067 // build args array
Damiend99b0522013-12-21 18:17:45 +00001068 mp_obj_t args[5];
Damien George5fa93b62014-01-22 14:35:10 +00001069 args[0] = MP_OBJ_NEW_QSTR(name);
Damiend99b0522013-12-21 18:17:45 +00001070 args[1] = mp_const_none; // TODO should be globals
1071 args[2] = mp_const_none; // TODO should be locals
Damiendb4c3612013-12-10 17:27:24 +00001072 args[3] = fromlist;
1073 args[4] = level; // must be 0; we don't yet support other values
1074
1075 // TODO lookup __import__ and call that instead of going straight to builtin implementation
Damiend99b0522013-12-21 18:17:45 +00001076 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001077}
1078
Damien Georged17926d2014-03-30 13:35:08 +01001079mp_obj_t mp_import_from(mp_obj_t module, qstr name) {
Damien George64131f32014-02-06 20:31:44 +00001080 DEBUG_printf("import from %p %s\n", module, qstr_str(name));
1081
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001082 mp_obj_t dest[2];
1083
1084 mp_load_method_maybe(module, name, dest);
1085
1086 if (dest[1] != MP_OBJ_NULL) {
1087 // Hopefully we can't import bound method from an object
1088import_error:
Paul Sokolovsky42453dc2014-04-12 03:00:40 +03001089 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ImportError, "cannot import name %s", qstr_str(name)));
Damiendb4c3612013-12-10 17:27:24 +00001090 }
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001091
1092 if (dest[0] != MP_OBJ_NULL) {
1093 return dest[0];
1094 }
1095
1096 // See if it's a package, then can try FS import
1097 mp_load_method_maybe(module, MP_QSTR___path__, dest);
1098 if (dest[0] == MP_OBJ_NULL) {
1099 goto import_error;
1100 }
1101
1102 mp_load_method_maybe(module, MP_QSTR___name__, dest);
1103 uint pkg_name_len;
1104 const char *pkg_name = mp_obj_str_get_data(dest[0], &pkg_name_len);
1105
stijn01d6be42014-05-05 12:18:27 +02001106 const uint dot_name_len = pkg_name_len + 1 + qstr_len(name);
1107 char *dot_name = alloca(dot_name_len);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001108 memcpy(dot_name, pkg_name, pkg_name_len);
1109 dot_name[pkg_name_len] = '.';
1110 memcpy(dot_name + pkg_name_len + 1, qstr_str(name), qstr_len(name));
stijn01d6be42014-05-05 12:18:27 +02001111 qstr dot_name_q = qstr_from_strn(dot_name, dot_name_len);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001112
1113 mp_obj_t args[5];
1114 args[0] = MP_OBJ_NEW_QSTR(dot_name_q);
1115 args[1] = mp_const_none; // TODO should be globals
1116 args[2] = mp_const_none; // TODO should be locals
1117 args[3] = mp_const_true; // Pass sentinel "non empty" value to force returning of leaf module
Paul Sokolovsky69f18672014-04-12 02:47:46 +03001118 args[4] = MP_OBJ_NEW_SMALL_INT(0);
Paul Sokolovskyaf620ab2014-04-12 00:15:19 +03001119
1120 // TODO lookup __import__ and call that instead of going straight to builtin implementation
1121 return mp_builtin___import__(5, args);
Damiendb4c3612013-12-10 17:27:24 +00001122}
1123
Damien Georged17926d2014-03-30 13:35:08 +01001124void mp_import_all(mp_obj_t module) {
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001125 DEBUG_printf("import all %p\n", module);
1126
Paul Sokolovsky599bbc12014-04-18 04:11:19 +03001127 // TODO: Support __all__
Damien George8b0535e2014-04-05 21:53:54 +01001128 mp_map_t *map = mp_obj_dict_get_map(mp_obj_module_get_globals(module));
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001129 for (uint i = 0; i < map->alloc; i++) {
Damien George8b0535e2014-04-05 21:53:54 +01001130 if (MP_MAP_SLOT_IS_FILLED(map, i)) {
Paul Sokolovsky599bbc12014-04-18 04:11:19 +03001131 qstr name = MP_OBJ_QSTR_VALUE(map->table[i].key);
1132 if (*qstr_str(name) != '_') {
1133 mp_store_name(name, map->table[i].value);
1134 }
Paul Sokolovskyda1ce932014-02-14 00:22:06 +02001135 }
1136 }
1137}
1138
Damien George7efc5b32014-04-05 22:36:42 +01001139mp_obj_dict_t *mp_locals_get(void) {
1140 return dict_locals;
Damien George66028ab2014-01-03 14:03:48 +00001141}
1142
Damien George7efc5b32014-04-05 22:36:42 +01001143void mp_locals_set(mp_obj_dict_t *d) {
1144 DEBUG_OP_printf("mp_locals_set(%p)\n", d);
1145 dict_locals = d;
Damien George66028ab2014-01-03 14:03:48 +00001146}
1147
Damien George7efc5b32014-04-05 22:36:42 +01001148mp_obj_dict_t *mp_globals_get(void) {
1149 return dict_globals;
Damien George66028ab2014-01-03 14:03:48 +00001150}
1151
Damien George7efc5b32014-04-05 22:36:42 +01001152void mp_globals_set(mp_obj_dict_t *d) {
1153 DEBUG_OP_printf("mp_globals_set(%p)\n", d);
1154 dict_globals = d;
Damien George66028ab2014-01-03 14:03:48 +00001155}
1156
Damien George6902eed2014-04-04 10:52:59 +00001157void *m_malloc_fail(int num_bytes) {
1158 DEBUG_printf("memory allocation failed, allocating %d bytes\n", num_bytes);
Damien Georgeea13f402014-04-05 18:32:08 +01001159 nlr_raise((mp_obj_t)&mp_const_MemoryError_obj);
Damien George6902eed2014-04-04 10:52:59 +00001160}
1161
Paul Sokolovsky7e4a2b02014-06-07 23:22:41 +03001162NORETURN void mp_not_implemented(const char *msg) {
1163 nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError, msg));
1164}
1165
Damien6ba13142013-11-02 20:34:54 +00001166// these must correspond to the respective enum
Damien Georged17926d2014-03-30 13:35:08 +01001167void *const mp_fun_table[MP_F_NUMBER_OF] = {
Damien George503d6112014-05-28 14:07:21 +01001168 mp_load_const_int,
Damien Georged17926d2014-03-30 13:35:08 +01001169 mp_load_const_dec,
1170 mp_load_const_str,
1171 mp_load_name,
1172 mp_load_global,
1173 mp_load_build_class,
1174 mp_load_attr,
1175 mp_load_method,
1176 mp_store_name,
1177 mp_store_attr,
Damien George729f7b42014-04-17 22:10:53 +01001178 mp_obj_subscr,
Damien Georged17926d2014-03-30 13:35:08 +01001179 mp_obj_is_true,
1180 mp_unary_op,
1181 mp_binary_op,
Damien George15d18062014-03-31 16:28:13 +01001182 mp_obj_new_tuple,
1183 mp_obj_new_list,
1184 mp_obj_list_append,
1185 mp_obj_new_dict,
1186 mp_obj_dict_store,
Damien George3ebd4d02014-06-01 13:46:47 +01001187#if MICROPY_PY_BUILTINS_SET
Damien George15d18062014-03-31 16:28:13 +01001188 mp_obj_new_set,
1189 mp_obj_set_store,
Damien George3ebd4d02014-06-01 13:46:47 +01001190#endif
Damien Georgedf8127a2014-04-13 11:04:33 +01001191 mp_make_function_from_raw_code,
Damien Georged17926d2014-03-30 13:35:08 +01001192 mp_call_function_n_kw_for_native,
1193 mp_call_method_n_kw,
1194 mp_getiter,
Damien George36db6bc2014-05-07 17:24:22 +01001195 mp_iternext,
Damien Georgecdd96df2014-04-06 12:58:40 +01001196 mp_import_name,
1197 mp_import_from,
1198 mp_import_all,
Damien Georgec49ddb92014-06-01 13:49:35 +01001199#if MICROPY_PY_BUILTINS_SLICE
Damien Georgecdd96df2014-04-06 12:58:40 +01001200 mp_obj_new_slice,
Damien Georgec49ddb92014-06-01 13:49:35 +01001201#endif
Damien Georgecdd96df2014-04-06 12:58:40 +01001202 mp_unpack_sequence,
Damien Georgea32c1e42014-05-07 18:30:52 +01001203 mp_unpack_ex,
Damien429d7192013-10-04 19:53:11 +01001204};
1205
1206/*
Damien Georged17926d2014-03-30 13:35:08 +01001207void mp_f_vector(mp_fun_kind_t fun_kind) {
1208 (mp_f_table[fun_kind])();
Damien429d7192013-10-04 19:53:11 +01001209}
1210*/