1 /*
2  * This file is part of the MicroPython 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 #ifndef MICROPY_INCLUDED_PY_OBJ_H
27 #define MICROPY_INCLUDED_PY_OBJ_H
28 
29 #include <assert.h>
30 
31 #include "py/mpconfig.h"
32 #include "py/misc.h"
33 #include "py/qstr.h"
34 #include "py/mpprint.h"
35 #include "py/runtime0.h"
36 
37 // This is the definition of the opaque MicroPython object type.
38 // All concrete objects have an encoding within this type and the
39 // particular encoding is specified by MICROPY_OBJ_REPR.
40 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
41 typedef uint64_t mp_obj_t;
42 typedef uint64_t mp_const_obj_t;
43 #else
44 typedef void *mp_obj_t;
45 typedef const void *mp_const_obj_t;
46 #endif
47 
48 // This mp_obj_type_t struct is a concrete MicroPython object which holds info
49 // about a type.  See below for actual definition of the struct.
50 typedef struct _mp_obj_type_t mp_obj_type_t;
51 
52 // Anything that wants to be a concrete MicroPython object must have mp_obj_base_t
53 // as its first member (small ints, qstr objs and inline floats are not concrete).
54 struct _mp_obj_base_t {
55     const mp_obj_type_t *type MICROPY_OBJ_BASE_ALIGNMENT;
56 };
57 typedef struct _mp_obj_base_t mp_obj_base_t;
58 
59 // These fake objects are used to indicate certain things in arguments or return
60 // values, and should only be used when explicitly allowed.
61 //
62 //  - MP_OBJ_NULL : used to indicate the absence of an object, or unsupported operation.
63 //  - MP_OBJ_STOP_ITERATION : used instead of throwing a StopIteration, for efficiency.
64 //  - MP_OBJ_SENTINEL : used for various internal purposes where one needs
65 //    an object which is unique from all other objects, including MP_OBJ_NULL.
66 //
67 // For debugging purposes they are all different.  For non-debug mode, we alias
68 // as many as we can to MP_OBJ_NULL because it's cheaper to load/compare 0.
69 
70 #if MICROPY_DEBUG_MP_OBJ_SENTINELS
71 #define MP_OBJ_NULL             (MP_OBJ_FROM_PTR((void *)0))
72 #define MP_OBJ_STOP_ITERATION   (MP_OBJ_FROM_PTR((void *)4))
73 #define MP_OBJ_SENTINEL         (MP_OBJ_FROM_PTR((void *)8))
74 #else
75 #define MP_OBJ_NULL             (MP_OBJ_FROM_PTR((void *)0))
76 #define MP_OBJ_STOP_ITERATION   (MP_OBJ_FROM_PTR((void *)0))
77 #define MP_OBJ_SENTINEL         (MP_OBJ_FROM_PTR((void *)4))
78 #endif
79 
80 // These macros/inline functions operate on objects and depend on the
81 // particular object representation.  They are used to query, pack and
82 // unpack small ints, qstrs and full object pointers.
83 
84 #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
85 
mp_obj_is_small_int(mp_const_obj_t o)86 static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
87     return (((mp_int_t)(o)) & 1) != 0;
88 }
89 #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
90 #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
91 
mp_obj_is_qstr(mp_const_obj_t o)92 static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
93     return (((mp_int_t)(o)) & 7) == 2;
94 }
95 #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
96 #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 2))
97 
mp_obj_is_immediate_obj(mp_const_obj_t o)98 static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
99     return (((mp_int_t)(o)) & 7) == 6;
100 }
101 #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 3)
102 #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 3) | 6))
103 
104 #if MICROPY_PY_BUILTINS_FLOAT
105 #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
106 #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
107 extern const struct _mp_obj_float_t mp_const_float_e_obj;
108 extern const struct _mp_obj_float_t mp_const_float_pi_obj;
109 
110 #define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
111 mp_float_t mp_obj_float_get(mp_obj_t self_in);
112 mp_obj_t mp_obj_new_float(mp_float_t value);
113 #endif
114 
mp_obj_is_obj(mp_const_obj_t o)115 static inline bool mp_obj_is_obj(mp_const_obj_t o) {
116     return (((mp_int_t)(o)) & 3) == 0;
117 }
118 
119 #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
120 
mp_obj_is_small_int(mp_const_obj_t o)121 static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
122     return (((mp_int_t)(o)) & 3) == 1;
123 }
124 #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 2)
125 #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 2) | 1))
126 
mp_obj_is_qstr(mp_const_obj_t o)127 static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
128     return (((mp_int_t)(o)) & 7) == 3;
129 }
130 #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
131 #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 3))
132 
mp_obj_is_immediate_obj(mp_const_obj_t o)133 static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
134     return (((mp_int_t)(o)) & 7) == 7;
135 }
136 #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 3)
137 #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 3) | 7))
138 
139 #if MICROPY_PY_BUILTINS_FLOAT
140 #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
141 #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
142 extern const struct _mp_obj_float_t mp_const_float_e_obj;
143 extern const struct _mp_obj_float_t mp_const_float_pi_obj;
144 
145 #define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
146 mp_float_t mp_obj_float_get(mp_obj_t self_in);
147 mp_obj_t mp_obj_new_float(mp_float_t value);
148 #endif
149 
mp_obj_is_obj(mp_const_obj_t o)150 static inline bool mp_obj_is_obj(mp_const_obj_t o) {
151     return (((mp_int_t)(o)) & 1) == 0;
152 }
153 
154 #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
155 
mp_obj_is_small_int(mp_const_obj_t o)156 static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
157     return (((mp_int_t)(o)) & 1) != 0;
158 }
159 #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
160 #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
161 
162 #if MICROPY_PY_BUILTINS_FLOAT
163 #define mp_const_float_e MP_ROM_PTR((mp_obj_t)(((0x402df854 & ~3) | 2) + 0x80800000))
164 #define mp_const_float_pi MP_ROM_PTR((mp_obj_t)(((0x40490fdb & ~3) | 2) + 0x80800000))
165 
mp_obj_is_float(mp_const_obj_t o)166 static inline bool mp_obj_is_float(mp_const_obj_t o) {
167     return (((mp_uint_t)(o)) & 3) == 2 && (((mp_uint_t)(o)) & 0xff800007) != 0x00000006;
168 }
mp_obj_float_get(mp_const_obj_t o)169 static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
170     union {
171         mp_float_t f;
172         mp_uint_t u;
173     } num = {.u = ((mp_uint_t)o - 0x80800000) & ~3};
174     return num.f;
175 }
mp_obj_new_float(mp_float_t f)176 static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
177     union {
178         mp_float_t f;
179         mp_uint_t u;
180     } num = {.f = f};
181     return (mp_obj_t)(((num.u & ~0x3) | 2) + 0x80800000);
182 }
183 #endif
184 
mp_obj_is_qstr(mp_const_obj_t o)185 static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
186     return (((mp_uint_t)(o)) & 0xff80000f) == 0x00000006;
187 }
188 #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 4)
189 #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 4) | 0x00000006))
190 
mp_obj_is_immediate_obj(mp_const_obj_t o)191 static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
192     return (((mp_uint_t)(o)) & 0xff80000f) == 0x0000000e;
193 }
194 #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 4)
195 #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 4) | 0xe))
196 
mp_obj_is_obj(mp_const_obj_t o)197 static inline bool mp_obj_is_obj(mp_const_obj_t o) {
198     return (((mp_int_t)(o)) & 3) == 0;
199 }
200 
201 #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
202 
mp_obj_is_small_int(mp_const_obj_t o)203 static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
204     return (((uint64_t)(o)) & 0xffff000000000000) == 0x0001000000000000;
205 }
206 #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)((o) << 16)) >> 17)
207 #define MP_OBJ_NEW_SMALL_INT(small_int) (((((uint64_t)(small_int)) & 0x7fffffffffff) << 1) | 0x0001000000000001)
208 
mp_obj_is_qstr(mp_const_obj_t o)209 static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
210     return (((uint64_t)(o)) & 0xffff000000000000) == 0x0002000000000000;
211 }
212 #define MP_OBJ_QSTR_VALUE(o) ((((uint32_t)(o)) >> 1) & 0xffffffff)
213 #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)(((uint64_t)(((uint32_t)(qst)) << 1)) | 0x0002000000000001))
214 
mp_obj_is_immediate_obj(mp_const_obj_t o)215 static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
216     return (((uint64_t)(o)) & 0xffff000000000000) == 0x0003000000000000;
217 }
218 #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) ((((uint32_t)(o)) >> 46) & 3)
219 #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) (((uint64_t)(val) << 46) | 0x0003000000000000)
220 
221 #if MICROPY_PY_BUILTINS_FLOAT
222 
223 #if MICROPY_FLOAT_IMPL != MICROPY_FLOAT_IMPL_DOUBLE
224 #error MICROPY_OBJ_REPR_D requires MICROPY_FLOAT_IMPL_DOUBLE
225 #endif
226 
227 #define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b145769 + 0x8004000000000000))}
228 #define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))}
229 
mp_obj_is_float(mp_const_obj_t o)230 static inline bool mp_obj_is_float(mp_const_obj_t o) {
231     return ((uint64_t)(o) & 0xfffc000000000000) != 0;
232 }
mp_obj_float_get(mp_const_obj_t o)233 static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
234     union {
235         mp_float_t f;
236         uint64_t r;
237     } num = {.r = o - 0x8004000000000000};
238     return num.f;
239 }
mp_obj_new_float(mp_float_t f)240 static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
241     union {
242         mp_float_t f;
243         uint64_t r;
244     } num = {.f = f};
245     return num.r + 0x8004000000000000;
246 }
247 #endif
248 
mp_obj_is_obj(mp_const_obj_t o)249 static inline bool mp_obj_is_obj(mp_const_obj_t o) {
250     return (((uint64_t)(o)) & 0xffff000000000000) == 0x0000000000000000;
251 }
252 #define MP_OBJ_TO_PTR(o) ((void *)(uintptr_t)(o))
253 #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p)))
254 
255 // rom object storage needs special handling to widen 32-bit pointer to 64-bits
256 typedef union _mp_rom_obj_t { uint64_t u64;
257                               struct { const void *lo, *hi;
258                               } u32;
259 } mp_rom_obj_t;
260 #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
261 #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
262 #if MP_ENDIANNESS_LITTLE
263 #define MP_ROM_PTR(p) {.u32 = {.lo = (p), .hi = NULL}}
264 #else
265 #define MP_ROM_PTR(p) {.u32 = {.lo = NULL, .hi = (p)}}
266 #endif
267 
268 #endif
269 
270 // Macros to convert between mp_obj_t and concrete object types.
271 // These are identity operations in MicroPython, but ability to override
272 // these operations are provided to experiment with other methods of
273 // object representation and memory management.
274 
275 // Cast mp_obj_t to object pointer
276 #ifndef MP_OBJ_TO_PTR
277 #define MP_OBJ_TO_PTR(o) ((void *)o)
278 #endif
279 
280 // Cast object pointer to mp_obj_t
281 #ifndef MP_OBJ_FROM_PTR
282 #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)p)
283 #endif
284 
285 // Macros to create objects that are stored in ROM.
286 
287 #ifndef MP_ROM_NONE
288 #if MICROPY_OBJ_IMMEDIATE_OBJS
289 #define MP_ROM_NONE mp_const_none
290 #else
291 #define MP_ROM_NONE MP_ROM_PTR(&mp_const_none_obj)
292 #endif
293 #endif
294 
295 #ifndef MP_ROM_FALSE
296 #if MICROPY_OBJ_IMMEDIATE_OBJS
297 #define MP_ROM_FALSE mp_const_false
298 #define MP_ROM_TRUE mp_const_true
299 #else
300 #define MP_ROM_FALSE MP_ROM_PTR(&mp_const_false_obj)
301 #define MP_ROM_TRUE MP_ROM_PTR(&mp_const_true_obj)
302 #endif
303 #endif
304 
305 #ifndef MP_ROM_INT
306 typedef mp_const_obj_t mp_rom_obj_t;
307 #define MP_ROM_INT(i) MP_OBJ_NEW_SMALL_INT(i)
308 #define MP_ROM_QSTR(q) MP_OBJ_NEW_QSTR(q)
309 #define MP_ROM_PTR(p) (p)
310 /* for testing
311 typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
312 #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
313 #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
314 #define MP_ROM_PTR(p) {.o = p}
315 */
316 #endif
317 
318 // These macros are used to declare and define constant function objects
319 // You can put "static" in front of the definitions to make them local
320 
321 #define MP_DECLARE_CONST_FUN_OBJ_0(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
322 #define MP_DECLARE_CONST_FUN_OBJ_1(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
323 #define MP_DECLARE_CONST_FUN_OBJ_2(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
324 #define MP_DECLARE_CONST_FUN_OBJ_3(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
325 #define MP_DECLARE_CONST_FUN_OBJ_VAR(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
326 #define MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
327 #define MP_DECLARE_CONST_FUN_OBJ_KW(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
328 
329 #define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
330 #define MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, takes_kw) ((uint32_t)((((uint32_t)(n_args_min)) << 17) | (((uint32_t)(n_args_max)) << 1) | ((takes_kw) ? 1 : 0)))
331 
332 #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
333     const mp_obj_fun_builtin_fixed_t obj_name = \
334     {{&mp_type_fun_builtin_0}, .fun._0 = fun_name}
335 #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
336     const mp_obj_fun_builtin_fixed_t obj_name = \
337     {{&mp_type_fun_builtin_1}, .fun._1 = fun_name}
338 #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
339     const mp_obj_fun_builtin_fixed_t obj_name = \
340     {{&mp_type_fun_builtin_2}, .fun._2 = fun_name}
341 #define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
342     const mp_obj_fun_builtin_fixed_t obj_name = \
343     {{&mp_type_fun_builtin_3}, .fun._3 = fun_name}
344 #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
345     const mp_obj_fun_builtin_var_t obj_name = \
346     {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, false), .fun.var = fun_name}
347 #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
348     const mp_obj_fun_builtin_var_t obj_name = \
349     {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, false), .fun.var = fun_name}
350 #define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
351     const mp_obj_fun_builtin_var_t obj_name = \
352     {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, true), .fun.kw = fun_name}
353 
354 // These macros are used to define constant map/dict objects
355 // You can put "static" in front of the definition to make it local
356 
357 #define MP_DEFINE_CONST_MAP(map_name, table_name) \
358     const mp_map_t map_name = { \
359         .all_keys_are_qstrs = 1, \
360         .is_fixed = 1, \
361         .is_ordered = 1, \
362         .used = MP_ARRAY_SIZE(table_name), \
363         .alloc = MP_ARRAY_SIZE(table_name), \
364         .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)table_name, \
365     }
366 
367 #define MP_DEFINE_CONST_DICT(dict_name, table_name) \
368     const mp_obj_dict_t dict_name = { \
369         .base = {&mp_type_dict}, \
370         .map = { \
371             .all_keys_are_qstrs = 1, \
372             .is_fixed = 1, \
373             .is_ordered = 1, \
374             .used = MP_ARRAY_SIZE(table_name), \
375             .alloc = MP_ARRAY_SIZE(table_name), \
376             .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)table_name, \
377         }, \
378     }
379 
380 // These macros are used to declare and define constant staticmethond and classmethod objects
381 // You can put "static" in front of the definitions to make them local
382 
383 #define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
384 #define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
385 
386 #define MP_DEFINE_CONST_STATICMETHOD_OBJ(obj_name, fun_name) const mp_rom_obj_static_class_method_t obj_name = {{&mp_type_staticmethod}, fun_name}
387 #define MP_DEFINE_CONST_CLASSMETHOD_OBJ(obj_name, fun_name) const mp_rom_obj_static_class_method_t obj_name = {{&mp_type_classmethod}, fun_name}
388 
389 // Declare a module as a builtin, processed by makemoduledefs.py
390 // param module_name: MP_QSTR_<module name>
391 // param obj_module: mp_obj_module_t instance
392 // prarm enabled_define: used as `#if (enabled_define) around entry`
393 
394 #define MP_REGISTER_MODULE(module_name, obj_module, enabled_define)
395 
396 // Underlying map/hash table implementation (not dict object or map function)
397 
398 typedef struct _mp_map_elem_t {
399     mp_obj_t key;
400     mp_obj_t value;
401 } mp_map_elem_t;
402 
403 typedef struct _mp_rom_map_elem_t {
404     mp_rom_obj_t key;
405     mp_rom_obj_t value;
406 } mp_rom_map_elem_t;
407 
408 typedef struct _mp_map_t {
409     size_t all_keys_are_qstrs : 1;
410     size_t is_fixed : 1;    // if set, table is fixed/read-only and can't be modified
411     size_t is_ordered : 1;  // if set, table is an ordered array, not a hash map
412     size_t used : (8 * sizeof(size_t) - 3);
413     size_t alloc;
414     mp_map_elem_t *table;
415 } mp_map_t;
416 
417 // mp_set_lookup requires these constants to have the values they do
418 typedef enum _mp_map_lookup_kind_t {
419     MP_MAP_LOOKUP = 0,
420     MP_MAP_LOOKUP_ADD_IF_NOT_FOUND = 1,
421     MP_MAP_LOOKUP_REMOVE_IF_FOUND = 2,
422     MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND = 3, // only valid for mp_set_lookup
423 } mp_map_lookup_kind_t;
424 
mp_map_slot_is_filled(const mp_map_t * map,size_t pos)425 static inline bool mp_map_slot_is_filled(const mp_map_t *map, size_t pos) {
426     assert(pos < map->alloc);
427     return (map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL;
428 }
429 
430 void mp_map_init(mp_map_t *map, size_t n);
431 void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table);
432 mp_map_t *mp_map_new(size_t n);
433 void mp_map_deinit(mp_map_t *map);
434 void mp_map_free(mp_map_t *map);
435 mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
436 void mp_map_clear(mp_map_t *map);
437 void mp_map_dump(mp_map_t *map);
438 
439 // Underlying set implementation (not set object)
440 
441 typedef struct _mp_set_t {
442     size_t alloc;
443     size_t used;
444     mp_obj_t *table;
445 } mp_set_t;
446 
mp_set_slot_is_filled(const mp_set_t * set,size_t pos)447 static inline bool mp_set_slot_is_filled(const mp_set_t *set, size_t pos) {
448     return (set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL;
449 }
450 
451 void mp_set_init(mp_set_t *set, size_t n);
452 mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
453 mp_obj_t mp_set_remove_first(mp_set_t *set);
454 void mp_set_clear(mp_set_t *set);
455 
456 // Type definitions for methods
457 
458 typedef mp_obj_t (*mp_fun_0_t)(void);
459 typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
460 typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
461 typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
462 typedef mp_obj_t (*mp_fun_var_t)(size_t n, const mp_obj_t *);
463 // mp_fun_kw_t takes mp_map_t* (and not const mp_map_t*) to ease passing
464 // this arg to mp_map_lookup().
465 typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *);
466 
467 // Flags for type behaviour (mp_obj_type_t.flags)
468 // If MP_TYPE_FLAG_EQ_NOT_REFLEXIVE is clear then __eq__ is reflexive (A==A returns True).
469 // If MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE is clear then the type can't be equal to an
470 // instance of any different class that also clears this flag.  If this flag is set
471 // then the type may check for equality against a different type.
472 // If MP_TYPE_FLAG_EQ_HAS_NEQ_TEST is clear then the type only implements the __eq__
473 // operator and not the __ne__ operator.  If it's set then __ne__ may be implemented.
474 // If MP_TYPE_FLAG_BINDS_SELF is set then the type as a method binds self as the first arg.
475 // If MP_TYPE_FLAG_BUILTIN_FUN is set then the type is a built-in function type.
476 #define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001)
477 #define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002)
478 #define MP_TYPE_FLAG_EQ_NOT_REFLEXIVE (0x0004)
479 #define MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE (0x0008)
480 #define MP_TYPE_FLAG_EQ_HAS_NEQ_TEST (0x0010)
481 #define MP_TYPE_FLAG_BINDS_SELF (0x0020)
482 #define MP_TYPE_FLAG_BUILTIN_FUN (0x0040)
483 
484 typedef enum {
485     PRINT_STR = 0,
486     PRINT_REPR = 1,
487     PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
488     PRINT_JSON = 3,
489     PRINT_RAW = 4, // Special format for printing bytes as an undercorated string
490     PRINT_EXC_SUBCLASS = 0x80, // Internal flag for printing exception subclasses
491 } mp_print_kind_t;
492 
493 typedef struct _mp_obj_iter_buf_t {
494     mp_obj_base_t base;
495     mp_obj_t buf[3];
496 } mp_obj_iter_buf_t;
497 
498 // The number of slots that an mp_obj_iter_buf_t needs on the Python value stack.
499 // It's rounded up in case mp_obj_base_t is smaller than mp_obj_t (eg for OBJ_REPR_D).
500 #define MP_OBJ_ITER_BUF_NSLOTS ((sizeof(mp_obj_iter_buf_t) + sizeof(mp_obj_t) - 1) / sizeof(mp_obj_t))
501 
502 typedef void (*mp_print_fun_t)(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind);
503 typedef mp_obj_t (*mp_make_new_fun_t)(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
504 typedef mp_obj_t (*mp_call_fun_t)(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *args);
505 typedef mp_obj_t (*mp_unary_op_fun_t)(mp_unary_op_t op, mp_obj_t);
506 typedef mp_obj_t (*mp_binary_op_fun_t)(mp_binary_op_t op, mp_obj_t, mp_obj_t);
507 typedef void (*mp_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
508 typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
509 typedef mp_obj_t (*mp_getiter_fun_t)(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf);
510 
511 // Buffer protocol
512 typedef struct _mp_buffer_info_t {
513     void *buf;      // can be NULL if len == 0
514     size_t len;     // in bytes
515     int typecode;   // as per binary.h
516 } mp_buffer_info_t;
517 #define MP_BUFFER_READ  (1)
518 #define MP_BUFFER_WRITE (2)
519 #define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
520 typedef struct _mp_buffer_p_t {
521     mp_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
522 } mp_buffer_p_t;
523 bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
524 void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
525 
526 struct _mp_obj_type_t {
527     // A type is an object so must start with this entry, which points to mp_type_type.
528     mp_obj_base_t base;
529 
530     // Flags associated with this type.
531     uint16_t flags;
532 
533     // The name of this type, a qstr.
534     uint16_t name;
535 
536     // Corresponds to __repr__ and __str__ special methods.
537     mp_print_fun_t print;
538 
539     // Corresponds to __new__ and __init__ special methods, to make an instance of the type.
540     mp_make_new_fun_t make_new;
541 
542     // Corresponds to __call__ special method, ie T(...).
543     mp_call_fun_t call;
544 
545     // Implements unary and binary operations.
546     // Can return MP_OBJ_NULL if the operation is not supported.
547     mp_unary_op_fun_t unary_op;
548     mp_binary_op_fun_t binary_op;
549 
550     // Implements load, store and delete attribute.
551     //
552     // dest[0] = MP_OBJ_NULL means load
553     //  return: for fail, do nothing
554     //          for attr, dest[0] = value
555     //          for method, dest[0] = method, dest[1] = self
556     //
557     // dest[0,1] = {MP_OBJ_SENTINEL, MP_OBJ_NULL} means delete
558     // dest[0,1] = {MP_OBJ_SENTINEL, object} means store
559     //  return: for fail, do nothing
560     //          for success set dest[0] = MP_OBJ_NULL
561     mp_attr_fun_t attr;
562 
563     // Implements load, store and delete subscripting:
564     //  - value = MP_OBJ_SENTINEL means load
565     //  - value = MP_OBJ_NULL means delete
566     //  - all other values mean store the value
567     // Can return MP_OBJ_NULL if operation not supported.
568     mp_subscr_fun_t subscr;
569 
570     // Corresponds to __iter__ special method.
571     // Can use the given mp_obj_iter_buf_t to store iterator object,
572     // otherwise can return a pointer to an object on the heap.
573     mp_getiter_fun_t getiter;
574 
575     // Corresponds to __next__ special method.  May return MP_OBJ_STOP_ITERATION
576     // as an optimisation instead of raising StopIteration() with no args.
577     mp_fun_1_t iternext;
578 
579     // Implements the buffer protocol if supported by this type.
580     mp_buffer_p_t buffer_p;
581 
582     // One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
583     const void *protocol;
584 
585     // A pointer to the parents of this type:
586     //  - 0 parents: pointer is NULL (object is implicitly the single parent)
587     //  - 1 parent: a pointer to the type of that parent
588     //  - 2 or more parents: pointer to a tuple object containing the parent types
589     const void *parent;
590 
591     // A dict mapping qstrs to objects local methods/constants/etc.
592     struct _mp_obj_dict_t *locals_dict;
593 };
594 
595 // Constant types, globally accessible
596 extern const mp_obj_type_t mp_type_type;
597 extern const mp_obj_type_t mp_type_object;
598 extern const mp_obj_type_t mp_type_NoneType;
599 extern const mp_obj_type_t mp_type_bool;
600 extern const mp_obj_type_t mp_type_int;
601 extern const mp_obj_type_t mp_type_str;
602 extern const mp_obj_type_t mp_type_bytes;
603 extern const mp_obj_type_t mp_type_bytearray;
604 extern const mp_obj_type_t mp_type_memoryview;
605 extern const mp_obj_type_t mp_type_float;
606 extern const mp_obj_type_t mp_type_complex;
607 extern const mp_obj_type_t mp_type_tuple;
608 extern const mp_obj_type_t mp_type_list;
609 extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict implementation detail)
610 extern const mp_obj_type_t mp_type_enumerate;
611 extern const mp_obj_type_t mp_type_filter;
612 extern const mp_obj_type_t mp_type_deque;
613 extern const mp_obj_type_t mp_type_dict;
614 extern const mp_obj_type_t mp_type_ordereddict;
615 extern const mp_obj_type_t mp_type_range;
616 extern const mp_obj_type_t mp_type_set;
617 extern const mp_obj_type_t mp_type_frozenset;
618 extern const mp_obj_type_t mp_type_slice;
619 extern const mp_obj_type_t mp_type_zip;
620 extern const mp_obj_type_t mp_type_array;
621 extern const mp_obj_type_t mp_type_super;
622 extern const mp_obj_type_t mp_type_gen_wrap;
623 extern const mp_obj_type_t mp_type_native_gen_wrap;
624 extern const mp_obj_type_t mp_type_gen_instance;
625 extern const mp_obj_type_t mp_type_fun_builtin_0;
626 extern const mp_obj_type_t mp_type_fun_builtin_1;
627 extern const mp_obj_type_t mp_type_fun_builtin_2;
628 extern const mp_obj_type_t mp_type_fun_builtin_3;
629 extern const mp_obj_type_t mp_type_fun_builtin_var;
630 extern const mp_obj_type_t mp_type_fun_bc;
631 extern const mp_obj_type_t mp_type_module;
632 extern const mp_obj_type_t mp_type_staticmethod;
633 extern const mp_obj_type_t mp_type_classmethod;
634 extern const mp_obj_type_t mp_type_property;
635 extern const mp_obj_type_t mp_type_stringio;
636 extern const mp_obj_type_t mp_type_bytesio;
637 extern const mp_obj_type_t mp_type_reversed;
638 extern const mp_obj_type_t mp_type_polymorph_iter;
639 
640 // Exceptions
641 extern const mp_obj_type_t mp_type_BaseException;
642 extern const mp_obj_type_t mp_type_ArithmeticError;
643 extern const mp_obj_type_t mp_type_AssertionError;
644 extern const mp_obj_type_t mp_type_AttributeError;
645 extern const mp_obj_type_t mp_type_EOFError;
646 extern const mp_obj_type_t mp_type_Exception;
647 extern const mp_obj_type_t mp_type_GeneratorExit;
648 extern const mp_obj_type_t mp_type_ImportError;
649 extern const mp_obj_type_t mp_type_IndentationError;
650 extern const mp_obj_type_t mp_type_IndexError;
651 extern const mp_obj_type_t mp_type_KeyboardInterrupt;
652 extern const mp_obj_type_t mp_type_KeyError;
653 extern const mp_obj_type_t mp_type_LookupError;
654 extern const mp_obj_type_t mp_type_MemoryError;
655 extern const mp_obj_type_t mp_type_NameError;
656 extern const mp_obj_type_t mp_type_NotImplementedError;
657 extern const mp_obj_type_t mp_type_OSError;
658 extern const mp_obj_type_t mp_type_OverflowError;
659 extern const mp_obj_type_t mp_type_RuntimeError;
660 extern const mp_obj_type_t mp_type_StopAsyncIteration;
661 extern const mp_obj_type_t mp_type_StopIteration;
662 extern const mp_obj_type_t mp_type_SyntaxError;
663 extern const mp_obj_type_t mp_type_SystemExit;
664 extern const mp_obj_type_t mp_type_TypeError;
665 extern const mp_obj_type_t mp_type_UnicodeError;
666 extern const mp_obj_type_t mp_type_ValueError;
667 extern const mp_obj_type_t mp_type_ViperTypeError;
668 extern const mp_obj_type_t mp_type_ZeroDivisionError;
669 
670 // Constant objects, globally accessible: None, False, True
671 // These should always be accessed via the below macros.
672 #if MICROPY_OBJ_IMMEDIATE_OBJS
673 // None is even while False/True are odd so their types can be distinguished with 1 bit.
674 #define mp_const_none MP_OBJ_NEW_IMMEDIATE_OBJ(0)
675 #define mp_const_false MP_OBJ_NEW_IMMEDIATE_OBJ(1)
676 #define mp_const_true MP_OBJ_NEW_IMMEDIATE_OBJ(3)
677 #else
678 #define mp_const_none (MP_OBJ_FROM_PTR(&mp_const_none_obj))
679 #define mp_const_false (MP_OBJ_FROM_PTR(&mp_const_false_obj))
680 #define mp_const_true (MP_OBJ_FROM_PTR(&mp_const_true_obj))
681 extern const struct _mp_obj_none_t mp_const_none_obj;
682 extern const struct _mp_obj_bool_t mp_const_false_obj;
683 extern const struct _mp_obj_bool_t mp_const_true_obj;
684 #endif
685 
686 // Constant objects, globally accessible: b'', (), {}, Ellipsis, NotImplemented, GeneratorExit()
687 // The below macros are for convenience only.
688 #define mp_const_empty_bytes (MP_OBJ_FROM_PTR(&mp_const_empty_bytes_obj))
689 #define mp_const_empty_tuple (MP_OBJ_FROM_PTR(&mp_const_empty_tuple_obj))
690 #define mp_const_notimplemented (MP_OBJ_FROM_PTR(&mp_const_notimplemented_obj))
691 extern const struct _mp_obj_str_t mp_const_empty_bytes_obj;
692 extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
693 extern const struct _mp_obj_dict_t mp_const_empty_dict_obj;
694 extern const struct _mp_obj_singleton_t mp_const_ellipsis_obj;
695 extern const struct _mp_obj_singleton_t mp_const_notimplemented_obj;
696 extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
697 
698 // Fixed empty map. Useful when calling keyword-receiving functions
699 // without any keywords from C, etc.
700 #define mp_const_empty_map (mp_const_empty_dict_obj.map)
701 
702 // General API for objects
703 
704 // These macros are derived from more primitive ones and are used to
705 // check for more specific object types.
706 // Note: these are kept as macros because inline functions sometimes use much
707 // more code space than the equivalent macros, depending on the compiler.
708 #define mp_obj_is_type(o, t) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type == (t))) // this does not work for checking int, str or fun; use below macros for that
709 #if MICROPY_OBJ_IMMEDIATE_OBJS
710 // bool's are immediates, not real objects, so test for the 2 possible values.
711 #define mp_obj_is_bool(o) ((o) == mp_const_false || (o) == mp_const_true)
712 #else
713 #define mp_obj_is_bool(o) mp_obj_is_type(o, &mp_type_bool)
714 #endif
715 #define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_type(o, &mp_type_int))
716 #define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_type(o, &mp_type_str))
717 #define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->binary_op == mp_obj_str_binary_op))
718 #define mp_obj_is_dict_or_ordereddict(o) (mp_obj_is_obj(o) && ((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->make_new == mp_obj_dict_make_new)
719 #define mp_obj_is_fun(o) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type->name == MP_QSTR_function))
720 
721 mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
mp_obj_new_bool(mp_int_t x)722 static inline mp_obj_t mp_obj_new_bool(mp_int_t x) {
723     return x ? mp_const_true : mp_const_false;
724 }
725 mp_obj_t mp_obj_new_cell(mp_obj_t obj);
726 mp_obj_t mp_obj_new_int(mp_int_t value);
727 mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
728 mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base);
729 mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
730 mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception)
731 mp_obj_t mp_obj_new_str(const char *data, size_t len);
732 mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len);
733 mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr);
734 mp_obj_t mp_obj_new_bytes(const byte *data, size_t len);
735 mp_obj_t mp_obj_new_bytearray(size_t n, void *items);
736 mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items);
737 #if MICROPY_PY_BUILTINS_FLOAT
738 mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
739 mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
740 #endif
741 mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
742 mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args);
743 #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
744 #define mp_obj_new_exception_msg(exc_type, msg) mp_obj_new_exception(exc_type)
745 #define mp_obj_new_exception_msg_varg(exc_type, ...) mp_obj_new_exception(exc_type)
746 #else
747 mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
748 mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
749 #endif
750 #ifdef va_start
751 mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, va_list arg); // same fmt restrictions as above
752 #endif
753 mp_obj_t mp_obj_new_fun_bc(mp_obj_t def_args, mp_obj_t def_kw_args, const byte *code, const mp_uint_t *const_table);
754 mp_obj_t mp_obj_new_fun_native(mp_obj_t def_args_in, mp_obj_t def_kw_args, const void *fun_data, const mp_uint_t *const_table);
755 mp_obj_t mp_obj_new_fun_asm(size_t n_args, const void *fun_data, mp_uint_t type_sig);
756 mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
757 mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed, const mp_obj_t *closed);
758 mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
759 mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);
760 mp_obj_t mp_obj_new_dict(size_t n_args);
761 mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items);
762 mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
763 mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
764 mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf);
765 mp_obj_t mp_obj_new_module(qstr module_name);
766 mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items);
767 
768 const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
769 const char *mp_obj_get_type_str(mp_const_obj_t o_in);
770 bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
771 mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type);
772 
773 void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
774 void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
775 void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc);
776 
777 bool mp_obj_is_true(mp_obj_t arg);
778 bool mp_obj_is_callable(mp_obj_t o_in);
779 mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2);
780 bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
781 
782 // returns true if o is bool, small int or long int
mp_obj_is_integer(mp_const_obj_t o)783 static inline bool mp_obj_is_integer(mp_const_obj_t o) {
784     return mp_obj_is_int(o) || mp_obj_is_bool(o);
785 }
786 
787 mp_int_t mp_obj_get_int(mp_const_obj_t arg);
788 mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
789 bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
790 #if MICROPY_PY_BUILTINS_FLOAT
791 mp_float_t mp_obj_get_float(mp_obj_t self_in);
792 bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value);
793 void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
794 bool mp_obj_get_complex_maybe(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
795 #endif
796 void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items); // *items may point inside a GC block
797 void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items); // *items may point inside a GC block
798 size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice);
799 mp_obj_t mp_obj_id(mp_obj_t o_in);
800 mp_obj_t mp_obj_len(mp_obj_t o_in);
801 mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL
802 mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
803 mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in);
804 
805 // cell
806 mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
807 void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
808 
809 // int
810 // For long int, returns value truncated to mp_int_t
811 mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
812 // Will raise exception if value doesn't fit into mp_int_t
813 mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
814 // Will raise exception if value is negative or doesn't fit into mp_uint_t
815 mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in);
816 
817 // exception
818 #define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
819 bool mp_obj_is_exception_type(mp_obj_t self_in);
820 bool mp_obj_is_exception_instance(mp_obj_t self_in);
821 bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
822 void mp_obj_exception_clear_traceback(mp_obj_t self_in);
823 void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
824 void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
825 mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
826 mp_obj_t mp_obj_exception_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
827 mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
828 void mp_init_emergency_exception_buf(void);
mp_obj_new_exception_arg1(const mp_obj_type_t * exc_type,mp_obj_t arg)829 static inline mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
830     assert(exc_type->make_new == mp_obj_exception_make_new);
831     return mp_obj_exception_make_new(exc_type, 1, 0, &arg);
832 }
833 
834 // str
835 bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
836 qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
837 const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
838 const char *mp_obj_str_get_data(mp_obj_t self_in, size_t *len);
839 mp_obj_t mp_obj_str_intern(mp_obj_t str);
840 mp_obj_t mp_obj_str_intern_checked(mp_obj_t obj);
841 void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, size_t str_len, bool is_bytes);
842 
843 #if MICROPY_PY_BUILTINS_FLOAT
844 // float
845 #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
mp_obj_get_float_to_f(mp_obj_t o)846 static inline float mp_obj_get_float_to_f(mp_obj_t o) {
847     return mp_obj_get_float(o);
848 }
849 
mp_obj_get_float_to_d(mp_obj_t o)850 static inline double mp_obj_get_float_to_d(mp_obj_t o) {
851     return (double)mp_obj_get_float(o);
852 }
853 
mp_obj_new_float_from_f(float o)854 static inline mp_obj_t mp_obj_new_float_from_f(float o) {
855     return mp_obj_new_float(o);
856 }
857 
mp_obj_new_float_from_d(double o)858 static inline mp_obj_t mp_obj_new_float_from_d(double o) {
859     return mp_obj_new_float((mp_float_t)o);
860 }
861 #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
mp_obj_get_float_to_f(mp_obj_t o)862 static inline float mp_obj_get_float_to_f(mp_obj_t o) {
863     return (float)mp_obj_get_float(o);
864 }
865 
mp_obj_get_float_to_d(mp_obj_t o)866 static inline double mp_obj_get_float_to_d(mp_obj_t o) {
867     return mp_obj_get_float(o);
868 }
869 
mp_obj_new_float_from_f(float o)870 static inline mp_obj_t mp_obj_new_float_from_f(float o) {
871     return mp_obj_new_float((mp_float_t)o);
872 }
873 
mp_obj_new_float_from_d(double o)874 static inline mp_obj_t mp_obj_new_float_from_d(double o) {
875     return mp_obj_new_float(o);
876 }
877 #endif
878 #if MICROPY_FLOAT_HIGH_QUALITY_HASH
879 mp_int_t mp_float_hash(mp_float_t val);
880 #else
mp_float_hash(mp_float_t val)881 static inline mp_int_t mp_float_hash(mp_float_t val) {
882     return (mp_int_t)val;
883 }
884 #endif
885 mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
886 
887 // complex
888 void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
889 mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t 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
890 #else
891 #define mp_obj_is_float(o) (false)
892 #endif
893 
894 // tuple
895 void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
896 void mp_obj_tuple_del(mp_obj_t self_in);
897 mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
898 
899 // list
900 mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
901 mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value);
902 void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
903 void mp_obj_list_set_len(mp_obj_t self_in, size_t len);
904 void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
905 mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
906 
907 // dict
908 typedef struct _mp_obj_dict_t {
909     mp_obj_base_t base;
910     mp_map_t map;
911 } mp_obj_dict_t;
912 mp_obj_t mp_obj_dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
913 void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args);
914 size_t mp_obj_dict_len(mp_obj_t self_in);
915 mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
916 mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
917 mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
918 mp_obj_t mp_obj_dict_copy(mp_obj_t self_in);
mp_obj_dict_get_map(mp_obj_t dict)919 static inline mp_map_t *mp_obj_dict_get_map(mp_obj_t dict) {
920     return &((mp_obj_dict_t *)MP_OBJ_TO_PTR(dict))->map;
921 }
922 
923 // set
924 void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
925 
926 // slice indexes resolved to particular sequence
927 typedef struct {
928     mp_int_t start;
929     mp_int_t stop;
930     mp_int_t step;
931 } mp_bound_slice_t;
932 
933 // slice
934 typedef struct _mp_obj_slice_t {
935     mp_obj_base_t base;
936     mp_obj_t start;
937     mp_obj_t stop;
938     mp_obj_t step;
939 } mp_obj_slice_t;
940 void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result);
941 
942 // functions
943 
944 typedef struct _mp_obj_fun_builtin_fixed_t {
945     mp_obj_base_t base;
946     union {
947         mp_fun_0_t _0;
948         mp_fun_1_t _1;
949         mp_fun_2_t _2;
950         mp_fun_3_t _3;
951     } fun;
952 } mp_obj_fun_builtin_fixed_t;
953 
954 typedef struct _mp_obj_fun_builtin_var_t {
955     mp_obj_base_t base;
956     uint32_t sig; // see MP_OBJ_FUN_MAKE_SIG
957     union {
958         mp_fun_var_t var;
959         mp_fun_kw_t kw;
960     } fun;
961 } mp_obj_fun_builtin_var_t;
962 
963 qstr mp_obj_fun_get_name(mp_const_obj_t fun);
964 qstr mp_obj_code_get_name(const byte *code_info);
965 
966 mp_obj_t mp_identity(mp_obj_t self);
967 MP_DECLARE_CONST_FUN_OBJ_1(mp_identity_obj);
968 mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf);
969 
970 // module
971 typedef struct _mp_obj_module_t {
972     mp_obj_base_t base;
973     mp_obj_dict_t *globals;
974 } mp_obj_module_t;
mp_obj_module_get_globals(mp_obj_t module)975 static inline mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t module) {
976     return ((mp_obj_module_t *)MP_OBJ_TO_PTR(module))->globals;
977 }
978 // check if given module object is a package
979 bool mp_obj_is_package(mp_obj_t module);
980 
981 // staticmethod and classmethod types; defined here so we can make const versions
982 // this structure is used for instances of both staticmethod and classmethod
983 typedef struct _mp_obj_static_class_method_t {
984     mp_obj_base_t base;
985     mp_obj_t fun;
986 } mp_obj_static_class_method_t;
987 typedef struct _mp_rom_obj_static_class_method_t {
988     mp_obj_base_t base;
989     mp_rom_obj_t fun;
990 } mp_rom_obj_static_class_method_t;
991 
992 // property
993 const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
994 
995 // sequence helpers
996 
997 void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest);
998 #if MICROPY_PY_BUILTINS_SLICE
999 bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes);
1000 #endif
1001 #define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
1002 #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)); }
1003 bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2);
1004 bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, size_t len1, const mp_obj_t *items2, size_t len2);
1005 mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args);
1006 mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value);
1007 mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
1008 
1009 // Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
1010 #define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte *)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
1011 
1012 // Note: dest and slice regions may overlap
1013 #define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_sz) \
1014     memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \
1015     memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), (dest_len - end) * (item_sz));
1016 
1017 // Note: dest and slice regions may overlap
1018 #define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_sz) \
1019     memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \
1020     memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));
1021 
1022 // Provide translation for legacy API
1023 #define MP_OBJ_IS_SMALL_INT mp_obj_is_small_int
1024 #define MP_OBJ_IS_QSTR mp_obj_is_qstr
1025 #define MP_OBJ_IS_OBJ mp_obj_is_obj
1026 #define MP_OBJ_IS_INT mp_obj_is_int
1027 #define MP_OBJ_IS_TYPE mp_obj_is_type
1028 #define MP_OBJ_IS_STR mp_obj_is_str
1029 #define MP_OBJ_IS_STR_OR_BYTES mp_obj_is_str_or_bytes
1030 #define MP_OBJ_IS_FUN mp_obj_is_fun
1031 #define MP_MAP_SLOT_IS_FILLED mp_map_slot_is_filled
1032 #define MP_SET_SLOT_IS_FILLED mp_set_slot_is_filled
1033 
1034 #endif // MICROPY_INCLUDED_PY_OBJ_H
1035