blob: 2a9ed3acd3f642b76dc044f2099dc786acff6672 [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
Damien Georgeea8d06c2014-04-17 23:19:36 +010027// A Micro Python object is a machine word having the following form:
28// - xxxx...xxx1 : a small int, bits 1 and above are the value
29// - xxxx...xx10 : a qstr, bits 2 and above are the value
30// - xxxx...xx00 : a pointer to an mp_obj_base_t (unless a fake object)
31
Damiend99b0522013-12-21 18:17:45 +000032// All Micro Python objects are at least this type
33// It must be of pointer size
34
35typedef machine_ptr_t mp_obj_t;
36typedef machine_const_ptr_t mp_const_obj_t;
37
38// Integers that fit in a pointer have this type
39// (do we need to expose this in the public API?)
40
41typedef machine_int_t mp_small_int_t;
42
Damien George97209d32014-01-07 15:58:30 +000043// Anything that wants to be a Micro Python object must have
Damien Georgeea8d06c2014-04-17 23:19:36 +010044// mp_obj_base_t as its first member (except small ints and qstrs)
Damien660365e2013-12-17 18:27:24 +000045
ian-v7a16fad2014-01-06 09:52:29 -080046struct _mp_obj_type_t;
Damiend99b0522013-12-21 18:17:45 +000047struct _mp_obj_base_t {
ian-v7a16fad2014-01-06 09:52:29 -080048 const struct _mp_obj_type_t *type;
Damiend99b0522013-12-21 18:17:45 +000049};
ian-v7a16fad2014-01-06 09:52:29 -080050typedef struct _mp_obj_base_t mp_obj_base_t;
Damiend99b0522013-12-21 18:17:45 +000051
Damien Georgeea8d06c2014-04-17 23:19:36 +010052// These fake objects are used to indicate certain things in arguments or return
53// values, and should only be used when explicitly allowed.
54//
Damien George6ac5dce2014-05-21 19:42:43 +010055// - MP_OBJ_NULL : used to indicate the absence of an object, or unsupported operation.
Damien Georgeea8d06c2014-04-17 23:19:36 +010056// - MP_OBJ_STOP_ITERATION : used instead of throwing a StopIteration, for efficiency.
57// - MP_OBJ_SENTINEL : used for various internal purposes where one needs
58// an object which is unique from all other objects, including MP_OBJ_NULL.
59//
60// For debugging purposes they are all different. For non-debug mode, we alias
61// as many as we can to MP_OBJ_NULL because it's cheaper to load/compare 0.
Damiend99b0522013-12-21 18:17:45 +000062
Damien Georgeea8d06c2014-04-17 23:19:36 +010063#if NDEBUG
64#define MP_OBJ_NULL ((mp_obj_t)0)
Damien Georgeea8d06c2014-04-17 23:19:36 +010065#define MP_OBJ_STOP_ITERATION ((mp_obj_t)0)
66#define MP_OBJ_SENTINEL ((mp_obj_t)4)
67#else
68#define MP_OBJ_NULL ((mp_obj_t)0)
Damien George6ac5dce2014-05-21 19:42:43 +010069#define MP_OBJ_STOP_ITERATION ((mp_obj_t)4)
70#define MP_OBJ_SENTINEL ((mp_obj_t)8)
Damien Georgeea8d06c2014-04-17 23:19:36 +010071#endif
Damien George729f7b42014-04-17 22:10:53 +010072
Damien George38a2da62014-01-08 17:33:12 +000073// These macros check for small int, qstr or object, and access small int and qstr values
Damiend99b0522013-12-21 18:17:45 +000074
Paul Sokolovsky757ac812014-01-12 17:06:25 +020075// In SMALL_INT, next-to-highest bits is used as sign, so both must match for value in range
Damien George9d68e9c2014-03-12 15:38:15 +000076#define MP_SMALL_INT_MIN ((mp_small_int_t)(((machine_int_t)WORD_MSBIT_HIGH) >> 1))
77#define MP_SMALL_INT_MAX ((mp_small_int_t)(~(MP_SMALL_INT_MIN)))
Paul Sokolovsky757ac812014-01-12 17:06:25 +020078#define MP_OBJ_FITS_SMALL_INT(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0)
Damien Georgea5c82a82014-04-11 11:16:53 +000079// these macros have now become inline functions; see below
80//#define MP_OBJ_IS_SMALL_INT(o) ((((mp_small_int_t)(o)) & 1) != 0)
81//#define MP_OBJ_IS_QSTR(o) ((((mp_small_int_t)(o)) & 3) == 2)
82//#define MP_OBJ_IS_OBJ(o) ((((mp_small_int_t)(o)) & 3) == 0)
Damien George11840942014-04-11 22:30:09 +010083#define MP_OBJ_IS_TYPE(o, t) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type == (t))) // this does not work for checking a string, use below macro for that
84#define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int))
85#define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str))
Damien George38a2da62014-01-08 17:33:12 +000086
Damiend99b0522013-12-21 18:17:45 +000087#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_small_int_t)(o)) >> 1)
Damien George38a2da62014-01-08 17:33:12 +000088#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)(((small_int) << 1) | 1))
89
90#define MP_OBJ_QSTR_VALUE(o) (((mp_small_int_t)(o)) >> 2)
91#define MP_OBJ_NEW_QSTR(qstr) ((mp_obj_t)((((machine_uint_t)qstr) << 2) | 2))
Damiend99b0522013-12-21 18:17:45 +000092
93// These macros are used to declare and define constant function objects
94// You can put "static" in front of the definitions to make them local
95
96#define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name
97
Damien George3e1a5c12014-03-29 13:43:38 +000098#define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, is_kw, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&mp_type_fun_native}, is_kw, n_args_min, n_args_max, (void *)fun_name}
John R. Lenton270112f2014-01-07 18:01:08 +000099#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 0, 0, (mp_fun_0_t)fun_name)
100#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 1, 1, (mp_fun_1_t)fun_name)
101#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 2, 2, (mp_fun_2_t)fun_name)
102#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 3, 3, (mp_fun_3_t)fun_name)
Damien Georged5e81822014-02-26 17:47:05 +0000103#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, MP_OBJ_FUN_ARGS_MAX, (mp_fun_var_t)fun_name)
John R. Lenton270112f2014-01-07 18:01:08 +0000104#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, n_args_max, (mp_fun_var_t)fun_name)
Damien Georged5e81822014-02-26 17:47:05 +0000105#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, n_args_min, MP_OBJ_FUN_ARGS_MAX, (mp_fun_kw_t)fun_name)
John R. Lentonc06763a2014-01-07 17:29:16 +0000106
Damien George9b196cd2014-03-26 21:47:19 +0000107// This macro is used to define constant dict objects
108// You can put "static" in front of the definition to make it local
109
110#define MP_DEFINE_CONST_DICT(dict_name, table_name) \
111 const mp_obj_dict_t dict_name = { \
Damien George3e1a5c12014-03-29 13:43:38 +0000112 .base = {&mp_type_dict}, \
Damien George9b196cd2014-03-26 21:47:19 +0000113 .map = { \
114 .all_keys_are_qstrs = 1, \
115 .table_is_fixed_array = 1, \
116 .used = sizeof(table_name) / sizeof(mp_map_elem_t), \
117 .alloc = sizeof(table_name) / sizeof(mp_map_elem_t), \
118 .table = (mp_map_elem_t*)table_name, \
119 }, \
120 }
121
Damien Georgeeae16442014-01-11 19:22:29 +0000122// These macros are used to declare and define constant staticmethond and classmethod objects
123// You can put "static" in front of the definitions to make them local
124
Damien George64131f32014-02-06 20:31:44 +0000125#define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_obj_static_class_method_t obj_name
126#define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_obj_static_class_method_t obj_name
Damien Georgeeae16442014-01-11 19:22:29 +0000127
Damien George64131f32014-02-06 20:31:44 +0000128#define MP_DEFINE_CONST_STATICMETHOD_OBJ(obj_name, fun_name) const mp_obj_static_class_method_t obj_name = {{&mp_type_staticmethod}, fun_name}
129#define MP_DEFINE_CONST_CLASSMETHOD_OBJ(obj_name, fun_name) const mp_obj_static_class_method_t obj_name = {{&mp_type_classmethod}, fun_name}
Damien Georgeeae16442014-01-11 19:22:29 +0000130
Damien Georgedf6567e2014-03-30 13:54:02 +0100131// Underlying map/hash table implementation (not dict object or map function)
132
133typedef struct _mp_map_elem_t {
134 mp_obj_t key;
135 mp_obj_t value;
136} mp_map_elem_t;
137
138// TODO maybe have a truncated mp_map_t for fixed tables, since alloc=used
139// put alloc last in the structure, so the truncated version does not need it
140// this would save 1 ROM word for all ROM objects that have a locals_dict
141// would also need a trucated dict structure
142
143typedef struct _mp_map_t {
144 machine_uint_t all_keys_are_qstrs : 1;
145 machine_uint_t table_is_fixed_array : 1;
146 machine_uint_t used : (8 * sizeof(machine_uint_t) - 2);
147 machine_uint_t alloc;
148 mp_map_elem_t *table;
149} mp_map_t;
150
Damien George95004e52014-04-05 17:17:19 +0100151// These can be or'd together
Damien Georgedf6567e2014-03-30 13:54:02 +0100152typedef enum _mp_map_lookup_kind_t {
153 MP_MAP_LOOKUP, // 0
154 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND, // 1
155 MP_MAP_LOOKUP_REMOVE_IF_FOUND, // 2
Damien Georgedf6567e2014-03-30 13:54:02 +0100156} mp_map_lookup_kind_t;
157
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300158static inline bool MP_MAP_SLOT_IS_FILLED(const mp_map_t *map, machine_uint_t pos) { return ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL); }
Damien George8b0535e2014-04-05 21:53:54 +0100159
Damien Georgedf6567e2014-03-30 13:54:02 +0100160void mp_map_init(mp_map_t *map, int n);
161void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table);
162mp_map_t *mp_map_new(int n);
163void mp_map_deinit(mp_map_t *map);
164void mp_map_free(mp_map_t *map);
165mp_map_elem_t* mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
166void mp_map_clear(mp_map_t *map);
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300167void mp_map_dump(mp_map_t *map);
Damien Georgedf6567e2014-03-30 13:54:02 +0100168
169// Underlying set implementation (not set object)
170
171typedef struct _mp_set_t {
172 machine_uint_t alloc;
173 machine_uint_t used;
174 mp_obj_t *table;
175} mp_set_t;
176
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300177static inline bool MP_SET_SLOT_IS_FILLED(const mp_set_t *set, machine_uint_t pos) { return ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL); }
Damien George8b0535e2014-04-05 21:53:54 +0100178
Damien Georgedf6567e2014-03-30 13:54:02 +0100179void mp_set_init(mp_set_t *set, int n);
180mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
Damien George95004e52014-04-05 17:17:19 +0100181mp_obj_t mp_set_remove_first(mp_set_t *set);
Damien Georgedf6567e2014-03-30 13:54:02 +0100182void mp_set_clear(mp_set_t *set);
Damiend99b0522013-12-21 18:17:45 +0000183
184// Type definitions for methods
185
186typedef mp_obj_t (*mp_fun_0_t)(void);
187typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
188typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
John R. Lenton45a87442014-01-04 01:15:01 +0000189typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
Damiend99b0522013-12-21 18:17:45 +0000190typedef mp_obj_t (*mp_fun_t)(void);
Damien Georgea11ceca2014-01-19 16:02:09 +0000191typedef mp_obj_t (*mp_fun_var_t)(uint n, const mp_obj_t *);
Damien Georgedf6567e2014-03-30 13:54:02 +0100192typedef mp_obj_t (*mp_fun_kw_t)(uint n, const mp_obj_t *, mp_map_t *);
Damiend99b0522013-12-21 18:17:45 +0000193
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200194typedef enum {
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300195 PRINT_STR = 0,
196 PRINT_REPR = 1,
197 PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
198 PRINT_EXC_SUBCLASS = 4, // Internal flag for printing exception subclasses
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200199} mp_print_kind_t;
200
201typedef void (*mp_print_fun_t)(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o, mp_print_kind_t kind);
Damien George20006db2014-01-18 14:10:48 +0000202typedef mp_obj_t (*mp_make_new_fun_t)(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
203typedef mp_obj_t (*mp_call_fun_t)(mp_obj_t fun, uint n_args, uint n_kw, const mp_obj_t *args);
Damiend99b0522013-12-21 18:17:45 +0000204typedef mp_obj_t (*mp_unary_op_fun_t)(int op, mp_obj_t);
205typedef mp_obj_t (*mp_binary_op_fun_t)(int op, mp_obj_t, mp_obj_t);
Damien George20006db2014-01-18 14:10:48 +0000206typedef void (*mp_load_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest); // for fail, do nothing; for attr, dest[0] = value; for method, dest[0] = method, dest[1] = self
Damien Georgef4c9b332014-04-08 21:32:29 +0100207typedef bool (*mp_store_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t value); // return true if store succeeded; if value==MP_OBJ_NULL then delete
Damien George729f7b42014-04-17 22:10:53 +0100208typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
Damiend99b0522013-12-21 18:17:45 +0000209
210typedef struct _mp_method_t {
Damien Georgec12b2212014-03-26 20:15:40 +0000211 qstr name;
Damiend99b0522013-12-21 18:17:45 +0000212 mp_const_obj_t fun;
213} mp_method_t;
214
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200215// Buffer protocol
Damien George57a4b4f2014-04-18 22:29:21 +0100216typedef struct _mp_buffer_info_t {
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200217 // if we'd bother to support various versions of structure
218 // (with different number of fields), we can distinguish
219 // them with ver = sizeof(struct). Cons: overkill for *micro*?
220 //int ver; // ?
221
222 void *buf;
Damien George57a4b4f2014-04-18 22:29:21 +0100223 machine_int_t len; // in bytes
224 int typecode; // as per binary.h
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200225
226 // Rationale: to load arbitrary-sized sprites directly to LCD
227 // Cons: a bit adhoc usecase
228 // int stride;
Damien George57a4b4f2014-04-18 22:29:21 +0100229} mp_buffer_info_t;
230#define MP_BUFFER_READ (1)
231#define MP_BUFFER_WRITE (2)
232#define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200233typedef struct _mp_buffer_p_t {
Damien George57a4b4f2014-04-18 22:29:21 +0100234 machine_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200235} mp_buffer_p_t;
Damien Georgeb11b85a2014-04-18 22:59:24 +0100236bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
237void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200238
239// Stream protocol
240typedef struct _mp_stream_p_t {
241 // On error, functions should return -1 and fill in *errcode (values are
242 // implementation-dependent, but will be exposed to user, e.g. via exception).
243 machine_int_t (*read)(mp_obj_t obj, void *buf, machine_uint_t size, int *errcode);
244 machine_int_t (*write)(mp_obj_t obj, const void *buf, machine_uint_t size, int *errcode);
245 // add seek() ?
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300246 int is_bytes : 1;
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200247} mp_stream_p_t;
248
Damiend99b0522013-12-21 18:17:45 +0000249struct _mp_obj_type_t {
250 mp_obj_base_t base;
Damien Georgea71c83a2014-02-15 11:34:50 +0000251 qstr name;
Damiend99b0522013-12-21 18:17:45 +0000252 mp_print_fun_t print;
Damien George71c51812014-01-04 20:21:15 +0000253 mp_make_new_fun_t make_new; // to make an instance of the type
Damiend99b0522013-12-21 18:17:45 +0000254
Damien George20006db2014-01-18 14:10:48 +0000255 mp_call_fun_t call;
Damien George6ac5dce2014-05-21 19:42:43 +0100256 mp_unary_op_fun_t unary_op; // can return MP_OBJ_NULL if op not supported
257 mp_binary_op_fun_t binary_op; // can return MP_OBJ_NULL if op not supported
Damiend99b0522013-12-21 18:17:45 +0000258
Damien Georgea71c83a2014-02-15 11:34:50 +0000259 mp_load_attr_fun_t load_attr;
Damien George1d24ea52014-04-08 21:11:49 +0100260 mp_store_attr_fun_t store_attr; // if value is MP_OBJ_NULL, then delete that attribute
Damien Georgef4c9b332014-04-08 21:32:29 +0100261
Damien George729f7b42014-04-17 22:10:53 +0100262 mp_subscr_fun_t subscr; // implements load, store, delete subscripting
263 // value=MP_OBJ_NULL means delete, value=MP_OBJ_SENTINEL means load, else store
Damien George6ac5dce2014-05-21 19:42:43 +0100264 // can return MP_OBJ_NULL if op not supported
Damien Georgea71c83a2014-02-15 11:34:50 +0000265
Damiend99b0522013-12-21 18:17:45 +0000266 mp_fun_1_t getiter;
Damien Georged0a5bf32014-05-10 13:55:11 +0100267 mp_fun_1_t iternext; // may return MP_OBJ_STOP_ITERATION as an optimisation instead of raising StopIteration() (with no args)
Damiend99b0522013-12-21 18:17:45 +0000268
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200269 mp_buffer_p_t buffer_p;
Damien George27e735f2014-04-05 23:02:23 +0100270 const mp_stream_p_t *stream_p;
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200271
Damien George004cdce2014-01-09 21:43:51 +0000272 // these are for dynamically created types (classes)
273 mp_obj_t bases_tuple;
274 mp_obj_t locals_dict;
Damien George062478e2014-01-09 20:57:50 +0000275
Damiend99b0522013-12-21 18:17:45 +0000276 /*
277 What we might need to add here:
278
Damiend99b0522013-12-21 18:17:45 +0000279 len str tuple list map
280 abs float complex
281 hash bool int none str
282 equal int str
Damiend99b0522013-12-21 18:17:45 +0000283
284 unpack seq list tuple
Damiend99b0522013-12-21 18:17:45 +0000285 */
286};
287
ian-v7a16fad2014-01-06 09:52:29 -0800288typedef struct _mp_obj_type_t mp_obj_type_t;
Damiend99b0522013-12-21 18:17:45 +0000289
Damien Georgec5966122014-02-15 16:10:44 +0000290// Constant types, globally accessible
Damien Georgec5966122014-02-15 16:10:44 +0000291extern const mp_obj_type_t mp_type_type;
Damien George3e1a5c12014-03-29 13:43:38 +0000292extern const mp_obj_type_t mp_type_object;
293extern const mp_obj_type_t mp_type_NoneType;
294extern const mp_obj_type_t mp_type_bool;
295extern const mp_obj_type_t mp_type_int;
296extern const mp_obj_type_t mp_type_str;
297extern const mp_obj_type_t mp_type_bytes;
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300298extern const mp_obj_type_t mp_type_bytearray;
Damien George3e1a5c12014-03-29 13:43:38 +0000299extern const mp_obj_type_t mp_type_float;
300extern const mp_obj_type_t mp_type_complex;
301extern const mp_obj_type_t mp_type_tuple;
302extern const mp_obj_type_t mp_type_list;
303extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict implementation detail)
304extern const mp_obj_type_t mp_type_enumerate;
305extern const mp_obj_type_t mp_type_filter;
306extern const mp_obj_type_t mp_type_dict;
Damien George71d31122014-04-17 18:18:55 +0100307extern const mp_obj_type_t mp_type_range;
Damien George3e1a5c12014-03-29 13:43:38 +0000308extern const mp_obj_type_t mp_type_set;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300309extern const mp_obj_type_t mp_type_frozenset;
Damien George3e1a5c12014-03-29 13:43:38 +0000310extern const mp_obj_type_t mp_type_slice;
311extern const mp_obj_type_t mp_type_zip;
312extern const mp_obj_type_t mp_type_array;
313extern const mp_obj_type_t mp_type_super;
314extern const mp_obj_type_t mp_type_gen_instance;
315extern const mp_obj_type_t mp_type_fun_native;
316extern const mp_obj_type_t mp_type_fun_bc;
317extern const mp_obj_type_t mp_type_module;
318extern const mp_obj_type_t mp_type_staticmethod;
319extern const mp_obj_type_t mp_type_classmethod;
Damien George777b0f32014-04-13 18:59:45 +0100320extern const mp_obj_type_t mp_type_property;
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300321extern const mp_obj_type_t mp_type_stringio;
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300322extern const mp_obj_type_t mp_type_bytesio;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000323
324// Exceptions
Damien Georgec5966122014-02-15 16:10:44 +0000325extern const mp_obj_type_t mp_type_BaseException;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000326extern const mp_obj_type_t mp_type_ArithmeticError;
327extern const mp_obj_type_t mp_type_AssertionError;
328extern const mp_obj_type_t mp_type_AttributeError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000329extern const mp_obj_type_t mp_type_EOFError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000330extern const mp_obj_type_t mp_type_Exception;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000331extern const mp_obj_type_t mp_type_GeneratorExit;
332extern const mp_obj_type_t mp_type_IOError;
333extern const mp_obj_type_t mp_type_ImportError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000334extern const mp_obj_type_t mp_type_IndentationError;
335extern const mp_obj_type_t mp_type_IndexError;
336extern const mp_obj_type_t mp_type_KeyError;
337extern const mp_obj_type_t mp_type_LookupError;
338extern const mp_obj_type_t mp_type_MemoryError;
339extern const mp_obj_type_t mp_type_NameError;
340extern const mp_obj_type_t mp_type_NotImplementedError;
341extern const mp_obj_type_t mp_type_OSError;
342extern const mp_obj_type_t mp_type_OverflowError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000343extern const mp_obj_type_t mp_type_RuntimeError;
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000344extern const mp_obj_type_t mp_type_StopIteration;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000345extern const mp_obj_type_t mp_type_SyntaxError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000346extern const mp_obj_type_t mp_type_SystemError;
Damien George7a4ddd22014-05-24 23:32:19 +0100347extern const mp_obj_type_t mp_type_SystemExit;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000348extern const mp_obj_type_t mp_type_TypeError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000349extern const mp_obj_type_t mp_type_ValueError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000350extern const mp_obj_type_t mp_type_ZeroDivisionError;
351
Damiend99b0522013-12-21 18:17:45 +0000352// Constant objects, globally accessible
Damien George07ddab52014-03-29 13:15:08 +0000353// The macros are for convenience only
354#define mp_const_none ((mp_obj_t)&mp_const_none_obj)
355#define mp_const_false ((mp_obj_t)&mp_const_false_obj)
356#define mp_const_true ((mp_obj_t)&mp_const_true_obj)
357#define mp_const_empty_tuple ((mp_obj_t)&mp_const_empty_tuple_obj)
358extern const struct _mp_obj_none_t mp_const_none_obj;
359extern const struct _mp_obj_bool_t mp_const_false_obj;
360extern const struct _mp_obj_bool_t mp_const_true_obj;
361extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
362extern const struct _mp_obj_ellipsis_t mp_const_ellipsis_obj;
Damien George6902eed2014-04-04 10:52:59 +0000363extern const struct _mp_obj_exception_t mp_const_MemoryError_obj;
Damien George07ddab52014-03-29 13:15:08 +0000364extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
Damiend99b0522013-12-21 18:17:45 +0000365
Damiend99b0522013-12-21 18:17:45 +0000366// General API for objects
367
Damien Georgea71c83a2014-02-15 11:34:50 +0000368mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
Damiend99b0522013-12-21 18:17:45 +0000369mp_obj_t mp_obj_new_none(void);
370mp_obj_t mp_obj_new_bool(bool value);
Damien George6baf76e2013-12-30 22:32:17 +0000371mp_obj_t mp_obj_new_cell(mp_obj_t obj);
Damiend99b0522013-12-21 18:17:45 +0000372mp_obj_t mp_obj_new_int(machine_int_t value);
Paul Sokolovsky48b35722014-01-12 17:30:48 +0200373mp_obj_t mp_obj_new_int_from_uint(machine_uint_t value);
Damien Georgea32c1e42014-05-07 18:30:52 +0100374mp_obj_t mp_obj_new_int_from_qstr(qstr qst);
Damien George9d68e9c2014-03-12 15:38:15 +0000375mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
Damien George5fa93b62014-01-22 14:35:10 +0000376mp_obj_t mp_obj_new_str(const byte* data, uint len, bool make_qstr_if_not_already);
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200377mp_obj_t mp_obj_new_bytes(const byte* data, uint len);
Damiend99b0522013-12-21 18:17:45 +0000378#if MICROPY_ENABLE_FLOAT
379mp_obj_t mp_obj_new_float(mp_float_t val);
380mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
381#endif
Damien Georgec5966122014-02-15 16:10:44 +0000382mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
Paul Sokolovskydec31bb2014-04-22 00:01:13 +0300383mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200384mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, uint n_args, const mp_obj_t *args);
Damien Georgec5966122014-02-15 16:10:44 +0000385mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg);
386mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
Damien George2827d622014-04-27 15:50:52 +0100387mp_obj_t mp_obj_new_fun_bc(uint scope_flags, qstr *args, uint n_pos_args, uint n_kwonly_args, mp_obj_t def_args, const byte *code);
Damiend99b0522013-12-21 18:17:45 +0000388mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun);
Damien Georged0691cc2014-01-29 20:30:52 +0000389mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
Damien George3558f622014-04-20 17:50:40 +0100390mp_obj_t mp_obj_new_closure(mp_obj_t fun, uint n_closed, const mp_obj_t *closed);
John R. Lentonc06763a2014-01-07 17:29:16 +0000391mp_obj_t mp_obj_new_tuple(uint n, const mp_obj_t *items);
Damiend99b0522013-12-21 18:17:45 +0000392mp_obj_t mp_obj_new_list(uint n, mp_obj_t *items);
Damiend99b0522013-12-21 18:17:45 +0000393mp_obj_t mp_obj_new_dict(int n_args);
394mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items);
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200395mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
Damien George35e2a4e2014-02-05 00:51:47 +0000396mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj);
Damien George20006db2014-01-18 14:10:48 +0000397mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
Damien George7c9c6672014-01-25 00:17:36 +0000398mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args);
Damien George28708622014-01-02 21:30:26 +0000399mp_obj_t mp_obj_new_module(qstr module_name);
Damiend99b0522013-12-21 18:17:45 +0000400
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +0300401mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
402const char *mp_obj_get_type_str(mp_const_obj_t o_in);
Damien George9e6e9352014-03-26 18:37:06 +0000403bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
Paul Sokolovskyea970802014-05-11 03:16:04 +0300404mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type);
Damiend99b0522013-12-21 18:17:45 +0000405
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200406void mp_obj_print_helper(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind);
407void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
Damien George136b1492014-01-19 12:38:49 +0000408void mp_obj_print_exception(mp_obj_t exc);
Damiend99b0522013-12-21 18:17:45 +0000409
Damien Georged17926d2014-03-30 13:35:08 +0100410int mp_obj_is_true(mp_obj_t arg);
Damien Georgea5c82a82014-04-11 11:16:53 +0000411
412// TODO make these all lower case when they have proven themselves
Damien Georgedb049c22014-04-12 00:08:40 +0100413static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o) { return ((((mp_small_int_t)(o)) & 3) == 0); }
414static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o) { return ((((mp_small_int_t)(o)) & 1) != 0); }
415//static inline bool MP_OBJ_IS_TYPE(mp_const_obj_t o, const mp_obj_type_t *t) { return (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type == (t))); } // this does not work for checking a string, use below macro for that
416//static inline bool MP_OBJ_IS_INT(mp_const_obj_t o) { return (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int)); } // returns true if o is a small int or long int
417static inline bool mp_obj_is_integer(mp_const_obj_t o) { return MP_OBJ_IS_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_bool); } // returns true if o is bool, small int or long int
418static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o) { return ((((mp_small_int_t)(o)) & 3) == 2); }
419//static inline bool MP_OBJ_IS_STR(mp_const_obj_t o) { return (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str)); }
Damien Georgea5c82a82014-04-11 11:16:53 +0000420
Damiend99b0522013-12-21 18:17:45 +0000421bool mp_obj_is_callable(mp_obj_t o_in);
422machine_int_t mp_obj_hash(mp_obj_t o_in);
423bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
Damiend99b0522013-12-21 18:17:45 +0000424
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300425machine_int_t mp_obj_get_int(mp_const_obj_t arg);
426bool mp_obj_get_int_maybe(mp_const_obj_t arg, machine_int_t *value);
Damiend99b0522013-12-21 18:17:45 +0000427#if MICROPY_ENABLE_FLOAT
428mp_float_t mp_obj_get_float(mp_obj_t self_in);
429void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
430#endif
Damien George5fa93b62014-01-22 14:35:10 +0000431//qstr mp_obj_get_qstr(mp_obj_t arg);
Damien George24ff0632014-03-24 10:47:13 +0000432void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items);
433void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items);
xbe9e1e8cd2014-03-12 22:57:16 -0700434uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice);
Damien Georgea5c82a82014-04-11 11:16:53 +0000435mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return MP_OBJ_NULL */
Damien George729f7b42014-04-17 22:10:53 +0100436mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
Damiend99b0522013-12-21 18:17:45 +0000437
Damiend99b0522013-12-21 18:17:45 +0000438// bool
Damien Georgea5c82a82014-04-11 11:16:53 +0000439// TODO make lower case when it has proven itself
Damien George2813cb62014-04-12 17:53:05 +0100440static inline mp_obj_t MP_BOOL(machine_int_t x) { return x ? mp_const_true : mp_const_false; }
Damiend99b0522013-12-21 18:17:45 +0000441
442// cell
443mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
444void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
445
Damien George71c51812014-01-04 20:21:15 +0000446// int
Paul Sokolovskyd26b3792014-01-18 16:07:16 +0200447// For long int, returns value truncated to machine_int_t
448machine_int_t mp_obj_int_get(mp_obj_t self_in);
Damien Georgeeabdf672014-03-22 20:54:01 +0000449#if MICROPY_ENABLE_FLOAT
450mp_float_t mp_obj_int_as_float(mp_obj_t self_in);
451#endif
Paul Sokolovsky4602b9a2014-04-19 03:07:34 +0300452// Will raise exception if value doesn't fit into machine_int_t
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300453machine_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
Damien George71c51812014-01-04 20:21:15 +0000454
Damienb86e3f92013-12-29 17:17:43 +0000455// exception
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300456#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
Damien Georgec5966122014-02-15 16:10:44 +0000457bool mp_obj_is_exception_type(mp_obj_t self_in);
458bool mp_obj_is_exception_instance(mp_obj_t self_in);
Paul Sokolovsky962b1cd2014-03-23 21:48:29 +0200459bool mp_obj_exception_match(mp_obj_t exc, const mp_obj_type_t *exc_type);
Damien Georgec5966122014-02-15 16:10:44 +0000460void mp_obj_exception_clear_traceback(mp_obj_t self_in);
Damien George136b1492014-01-19 12:38:49 +0000461void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, machine_uint_t line, qstr block);
462void mp_obj_exception_get_traceback(mp_obj_t self_in, machine_uint_t *n, machine_uint_t **values);
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200463mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300464mp_obj_t mp_obj_exception_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args);
Damienb86e3f92013-12-29 17:17:43 +0000465
Damiend99b0522013-12-21 18:17:45 +0000466// str
Paul Sokolovsky91fb1c92014-01-24 22:50:40 +0200467mp_obj_t mp_obj_str_builder_start(const mp_obj_type_t *type, uint len, byte **data);
Damien George5fa93b62014-01-22 14:35:10 +0000468mp_obj_t mp_obj_str_builder_end(mp_obj_t o_in);
469bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
470uint mp_obj_str_get_hash(mp_obj_t self_in);
471uint mp_obj_str_get_len(mp_obj_t self_in);
Damien Georgeb829b5c2014-01-25 13:51:19 +0000472qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
Damien George5fa93b62014-01-22 14:35:10 +0000473const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
Damien George698ec212014-02-08 18:17:23 +0000474const char *mp_obj_str_get_data(mp_obj_t self_in, uint *len);
Paul Sokolovsky0b7e29c2014-01-28 03:40:06 +0200475void mp_str_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len);
Damiend99b0522013-12-21 18:17:45 +0000476
477#if MICROPY_ENABLE_FLOAT
478// float
Damien George0c36da02014-03-08 15:24:39 +0000479typedef struct _mp_obj_float_t {
480 mp_obj_base_t base;
481 mp_float_t value;
482} mp_obj_float_t;
Damiend99b0522013-12-21 18:17:45 +0000483mp_float_t mp_obj_float_get(mp_obj_t self_in);
Damien George6ac5dce2014-05-21 19:42:43 +0100484mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
Damiend99b0522013-12-21 18:17:45 +0000485
486// complex
Damiend99b0522013-12-21 18:17:45 +0000487void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
Damien George6ac5dce2014-05-21 19:42:43 +0100488mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); // can return MP_OBJ_NULL if op not supported
Damiend99b0522013-12-21 18:17:45 +0000489#endif
490
491// tuple
Damiend99b0522013-12-21 18:17:45 +0000492void mp_obj_tuple_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
John R. Lenton07205ec2014-01-13 02:31:00 +0000493void mp_obj_tuple_del(mp_obj_t self_in);
Damien George7f8be592014-03-20 19:20:59 +0000494machine_int_t mp_obj_tuple_hash(mp_obj_t self_in);
Damiend99b0522013-12-21 18:17:45 +0000495
496// list
Paul Sokolovsky18bef252014-04-13 06:17:29 +0300497struct _mp_obj_list_t;
498void mp_obj_list_init(struct _mp_obj_list_t *o, uint n);
Damiend99b0522013-12-21 18:17:45 +0000499mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
500void mp_obj_list_get(mp_obj_t self_in, uint *len, mp_obj_t **items);
Damien George495d7812014-04-08 17:51:47 +0100501void mp_obj_list_set_len(mp_obj_t self_in, uint len);
Damiend99b0522013-12-21 18:17:45 +0000502void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
Damien Georgedf6567e2014-03-30 13:54:02 +0100503mp_obj_t mp_obj_list_sort(uint n_args, const mp_obj_t *args, mp_map_t *kwargs);
Damiend99b0522013-12-21 18:17:45 +0000504
505// dict
Damien Georgedf6567e2014-03-30 13:54:02 +0100506typedef struct _mp_obj_dict_t {
507 mp_obj_base_t base;
508 mp_map_t map;
509} mp_obj_dict_t;
Damien George8b0535e2014-04-05 21:53:54 +0100510void mp_obj_dict_init(mp_obj_dict_t *dict, int n_args);
Damiendae7eb72013-12-29 22:32:51 +0000511uint mp_obj_dict_len(mp_obj_t self_in);
Damiend99b0522013-12-21 18:17:45 +0000512mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
Damien George66edc5d2014-04-05 13:25:13 +0100513mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
Damien Georgedf6567e2014-03-30 13:54:02 +0100514mp_map_t *mp_obj_dict_get_map(mp_obj_t self_in);
Damiend99b0522013-12-21 18:17:45 +0000515
516// set
517void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
518
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200519// slice
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200520void mp_obj_slice_get(mp_obj_t self_in, machine_int_t *start, machine_int_t *stop, machine_int_t *step);
521
Paul Sokolovsky427905c2014-01-18 19:24:47 +0200522// array
Paul Sokolovsky33996682014-01-21 23:30:10 +0200523uint mp_obj_array_len(mp_obj_t self_in);
Paul Sokolovsky7f11c792014-01-29 00:21:41 +0200524mp_obj_t mp_obj_new_bytearray_by_ref(uint n, void *items);
John R. Lenton07205ec2014-01-13 02:31:00 +0000525
Damiend99b0522013-12-21 18:17:45 +0000526// functions
Damien Georged5e81822014-02-26 17:47:05 +0000527#define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
Damien George66028ab2014-01-03 14:03:48 +0000528typedef struct _mp_obj_fun_native_t { // need this so we can define const objects (to go in ROM)
Damiend99b0522013-12-21 18:17:45 +0000529 mp_obj_base_t base;
Damien George51047752014-02-26 17:40:52 +0000530 bool is_kw : 1;
Damien Georged5e81822014-02-26 17:47:05 +0000531 uint n_args_min : 15; // inclusive
532 uint n_args_max : 16; // inclusive
Damien660365e2013-12-17 18:27:24 +0000533 void *fun;
Damien George66028ab2014-01-03 14:03:48 +0000534 // TODO add mp_map_t *globals
535 // for const function objects, make an empty, const map
536 // such functions won't be able to access the global scope, but that's probably okay
Damiend99b0522013-12-21 18:17:45 +0000537} mp_obj_fun_native_t;
Damien George97209d32014-01-07 15:58:30 +0000538
Paul Sokolovsky7fafb282014-03-30 20:21:28 +0300539bool mp_obj_fun_prepare_simple_args(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args,
540 uint *out_args1_len, const mp_obj_t **out_args1, uint *out_args2_len, const mp_obj_t **out_args2);
Paul Sokolovskyab7bf282014-05-17 11:08:33 +0300541const char *mp_obj_fun_get_name(mp_const_obj_t fun);
Paul Sokolovskyc3103b52014-05-01 22:20:07 +0300542const char *mp_obj_code_get_name(const byte *code_info);
Damien660365e2013-12-17 18:27:24 +0000543
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200544mp_obj_t mp_identity(mp_obj_t self);
Paul Sokolovsky557c9d52014-02-08 21:57:19 +0200545MP_DECLARE_CONST_FUN_OBJ(mp_identity_obj);
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200546
Damien George28708622014-01-02 21:30:26 +0000547// module
Damien George0c36da02014-03-08 15:24:39 +0000548typedef struct _mp_obj_module_t {
549 mp_obj_base_t base;
550 qstr name;
Damien George8b0535e2014-04-05 21:53:54 +0100551 mp_obj_dict_t *globals;
Damien George0c36da02014-03-08 15:24:39 +0000552} mp_obj_module_t;
Damien George8b0535e2014-04-05 21:53:54 +0100553mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t self_in);
Damien Georgeeae16442014-01-11 19:22:29 +0000554
555// staticmethod and classmethod types; defined here so we can make const versions
Damien George64131f32014-02-06 20:31:44 +0000556// this structure is used for instances of both staticmethod and classmethod
557typedef struct _mp_obj_static_class_method_t {
Damien Georgeeae16442014-01-11 19:22:29 +0000558 mp_obj_base_t base;
559 mp_obj_t fun;
Damien George64131f32014-02-06 20:31:44 +0000560} mp_obj_static_class_method_t;
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200561
Damien George777b0f32014-04-13 18:59:45 +0100562// property
563const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
564
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200565// sequence helpers
566void mp_seq_multiply(const void *items, uint item_sz, uint len, uint times, void *dest);
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300567bool mp_seq_get_fast_slice_indexes(machine_uint_t len, mp_obj_t slice, machine_uint_t *begin, machine_uint_t *end);
568#define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
569#define mp_seq_cat(dest, src1, len1, src2, len2, item_t) { memcpy(dest, src1, (len1) * sizeof(item_t)); memcpy(dest + (len1), src2, (len2) * sizeof(item_t)); }
Paul Sokolovsky87e85b72014-02-02 08:24:07 +0200570bool mp_seq_cmp_bytes(int op, const byte *data1, uint len1, const byte *data2, uint len2);
Paul Sokolovsky1a996c42014-02-08 22:49:46 +0200571bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *items2, uint len2);
Paul Sokolovsky0cd1dc02014-02-10 06:37:11 +0200572mp_obj_t mp_seq_index_obj(const mp_obj_t *items, uint len, uint n_args, const mp_obj_t *args);
Paul Sokolovskyac0134d2014-02-10 07:10:55 +0200573mp_obj_t mp_seq_count_obj(const mp_obj_t *items, uint len, mp_obj_t value);
Paul Sokolovskya2240672014-04-28 00:16:57 +0300574// Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
575#define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte*)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
Paul Sokolovsky94d82462014-05-10 22:23:00 +0300576#define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_t) \
577 /*printf("memcpy(%p, %p, %d)\n", dest + beg, slice, slice_len * sizeof(item_t));*/ \
578 memcpy(dest + beg, slice, slice_len * sizeof(item_t)); \
579 /*printf("memcpy(%p, %p, %d)\n", dest + (beg + slice_len), dest + end, (dest_len - end) * sizeof(item_t));*/ \
580 memcpy(dest + (beg + slice_len), dest + end, (dest_len - end) * sizeof(item_t));