Branch data Line data Source code
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 : :
86 : 171688340 : static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
87 [ + + + + : 105566013 : 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 : :
92 : 12020509 : static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
93 [ + + + + : 11924169 : 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 : :
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 : : #if MICROPY_PY_MATH_CONSTANTS
108 : : #define mp_const_float_tau MP_ROM_PTR(&mp_const_float_tau_obj)
109 : : #define mp_const_float_inf MP_ROM_PTR(&mp_const_float_inf_obj)
110 : : #define mp_const_float_nan MP_ROM_PTR(&mp_const_float_nan_obj)
111 : : #endif
112 : : extern const struct _mp_obj_float_t mp_const_float_e_obj;
113 : : extern const struct _mp_obj_float_t mp_const_float_pi_obj;
114 : : #if MICROPY_PY_MATH_CONSTANTS
115 : : extern const struct _mp_obj_float_t mp_const_float_tau_obj;
116 : : extern const struct _mp_obj_float_t mp_const_float_inf_obj;
117 : : extern const struct _mp_obj_float_t mp_const_float_nan_obj;
118 : : #endif
119 : :
120 : : #define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
121 : : mp_float_t mp_obj_float_get(mp_obj_t self_in);
122 : : mp_obj_t mp_obj_new_float(mp_float_t value);
123 : : #endif
124 : :
125 : 91258965 : static inline bool mp_obj_is_obj(mp_const_obj_t o) {
126 [ + + + + : 91172517 : return (((mp_int_t)(o)) & 3) == 0;
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
- - + + -
+ - + - ]
127 : : }
128 : :
129 : : #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
130 : :
131 : : static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
132 : : return (((mp_int_t)(o)) & 3) == 1;
133 : : }
134 : : #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 2)
135 : : #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 2) | 1))
136 : :
137 : : static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
138 : : return (((mp_int_t)(o)) & 7) == 3;
139 : : }
140 : : #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
141 : : #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 3))
142 : :
143 : : static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
144 : : return (((mp_int_t)(o)) & 7) == 7;
145 : : }
146 : : #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 3)
147 : : #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 3) | 7))
148 : :
149 : : #if MICROPY_PY_BUILTINS_FLOAT
150 : : #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
151 : : #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
152 : : #if MICROPY_PY_MATH_CONSTANTS
153 : : #define mp_const_float_tau MP_ROM_PTR(&mp_const_float_tau_obj)
154 : : #define mp_const_float_inf MP_ROM_PTR(&mp_const_float_inf_obj)
155 : : #define mp_const_float_nan MP_ROM_PTR(&mp_const_float_nan_obj)
156 : : #endif
157 : : extern const struct _mp_obj_float_t mp_const_float_e_obj;
158 : : extern const struct _mp_obj_float_t mp_const_float_pi_obj;
159 : : #if MICROPY_PY_MATH_CONSTANTS
160 : : extern const struct _mp_obj_float_t mp_const_float_tau_obj;
161 : : extern const struct _mp_obj_float_t mp_const_float_inf_obj;
162 : : extern const struct _mp_obj_float_t mp_const_float_nan_obj;
163 : : #endif
164 : :
165 : : #define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
166 : : mp_float_t mp_obj_float_get(mp_obj_t self_in);
167 : : mp_obj_t mp_obj_new_float(mp_float_t value);
168 : : #endif
169 : :
170 : : static inline bool mp_obj_is_obj(mp_const_obj_t o) {
171 : : return (((mp_int_t)(o)) & 1) == 0;
172 : : }
173 : :
174 : : #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
175 : :
176 : : #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_NONE
177 : : #error "MICROPY_OBJ_REPR_C requires float to be enabled."
178 : : #endif
179 : :
180 : : static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
181 : : return (((mp_int_t)(o)) & 1) != 0;
182 : : }
183 : : #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
184 : : #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
185 : :
186 : : #if MICROPY_PY_BUILTINS_FLOAT
187 : : #define mp_const_float_e MP_ROM_PTR((mp_obj_t)(((0x402df854 & ~3) | 2) + 0x80800000))
188 : : #define mp_const_float_pi MP_ROM_PTR((mp_obj_t)(((0x40490fdb & ~3) | 2) + 0x80800000))
189 : : #if MICROPY_PY_MATH_CONSTANTS
190 : : #define mp_const_float_tau MP_ROM_PTR((mp_obj_t)(((0x40c90fdb & ~3) | 2) + 0x80800000))
191 : : #define mp_const_float_inf MP_ROM_PTR((mp_obj_t)(((0x7f800000 & ~3) | 2) + 0x80800000))
192 : : #define mp_const_float_nan MP_ROM_PTR((mp_obj_t)(((0xffc00000 & ~3) | 2) + 0x80800000))
193 : : #endif
194 : :
195 : : static inline bool mp_obj_is_float(mp_const_obj_t o) {
196 : : // Ensure that 32-bit arch can only use single precision.
197 : : MP_STATIC_ASSERT(sizeof(mp_float_t) <= sizeof(mp_obj_t));
198 : :
199 : : return (((mp_uint_t)(o)) & 3) == 2 && (((mp_uint_t)(o)) & 0xff800007) != 0x00000006;
200 : : }
201 : : static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
202 : : union {
203 : : mp_float_t f;
204 : : mp_uint_t u;
205 : : } num = {.u = ((mp_uint_t)o - 0x80800000) & ~3};
206 : : return num.f;
207 : : }
208 : : static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
209 : : union {
210 : : mp_float_t f;
211 : : mp_uint_t u;
212 : : } num = {.f = f};
213 : : return (mp_obj_t)(((num.u & ~0x3) | 2) + 0x80800000);
214 : : }
215 : : #endif
216 : :
217 : : static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
218 : : return (((mp_uint_t)(o)) & 0xff80000f) == 0x00000006;
219 : : }
220 : : #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 4)
221 : : #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 4) | 0x00000006))
222 : :
223 : : static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
224 : : return (((mp_uint_t)(o)) & 0xff80000f) == 0x0000000e;
225 : : }
226 : : #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 4)
227 : : #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 4) | 0xe))
228 : :
229 : : static inline bool mp_obj_is_obj(mp_const_obj_t o) {
230 : : return (((mp_int_t)(o)) & 3) == 0;
231 : : }
232 : :
233 : : #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
234 : :
235 : : static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
236 : : return (((uint64_t)(o)) & 0xffff000000000000) == 0x0001000000000000;
237 : : }
238 : : #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)((o) << 16)) >> 17)
239 : : #define MP_OBJ_NEW_SMALL_INT(small_int) (((((uint64_t)(small_int)) & 0x7fffffffffff) << 1) | 0x0001000000000001)
240 : :
241 : : static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
242 : : return (((uint64_t)(o)) & 0xffff000000000000) == 0x0002000000000000;
243 : : }
244 : : #define MP_OBJ_QSTR_VALUE(o) ((((uint32_t)(o)) >> 1) & 0xffffffff)
245 : : #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)(((uint64_t)(((uint32_t)(qst)) << 1)) | 0x0002000000000001))
246 : :
247 : : static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
248 : : return (((uint64_t)(o)) & 0xffff000000000000) == 0x0003000000000000;
249 : : }
250 : : #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) ((((uint32_t)(o)) >> 46) & 3)
251 : : #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) (((uint64_t)(val) << 46) | 0x0003000000000000)
252 : :
253 : : #if MICROPY_PY_BUILTINS_FLOAT
254 : :
255 : : #if MICROPY_FLOAT_IMPL != MICROPY_FLOAT_IMPL_DOUBLE
256 : : #error MICROPY_OBJ_REPR_D requires MICROPY_FLOAT_IMPL_DOUBLE
257 : : #endif
258 : :
259 : : #define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b145769 + 0x8004000000000000))}
260 : : #define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))}
261 : : #if MICROPY_PY_MATH_CONSTANTS
262 : : #define mp_const_float_tau {((mp_obj_t)((uint64_t)0x401921fb54442d18 + 0x8004000000000000))}
263 : : #define mp_const_float_inf {((mp_obj_t)((uint64_t)0x7ff0000000000000 + 0x8004000000000000))}
264 : : #define mp_const_float_nan {((mp_obj_t)((uint64_t)0xfff8000000000000 + 0x8004000000000000))}
265 : : #endif
266 : :
267 : : static inline bool mp_obj_is_float(mp_const_obj_t o) {
268 : : return ((uint64_t)(o) & 0xfffc000000000000) != 0;
269 : : }
270 : : static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
271 : : union {
272 : : mp_float_t f;
273 : : uint64_t r;
274 : : } num = {.r = o - 0x8004000000000000};
275 : : return num.f;
276 : : }
277 : : static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
278 : : union {
279 : : mp_float_t f;
280 : : uint64_t r;
281 : : } num = {.f = f};
282 : : return num.r + 0x8004000000000000;
283 : : }
284 : : #endif
285 : :
286 : : static inline bool mp_obj_is_obj(mp_const_obj_t o) {
287 : : return (((uint64_t)(o)) & 0xffff000000000000) == 0x0000000000000000;
288 : : }
289 : : #define MP_OBJ_TO_PTR(o) ((void *)(uintptr_t)(o))
290 : : #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p)))
291 : :
292 : : // rom object storage needs special handling to widen 32-bit pointer to 64-bits
293 : : typedef union _mp_rom_obj_t {
294 : : uint64_t u64;
295 : : struct {
296 : : const void *lo, *hi;
297 : : } u32;
298 : : } mp_rom_obj_t;
299 : : #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
300 : : #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
301 : : #if MP_ENDIANNESS_LITTLE
302 : : #define MP_ROM_PTR(p) {.u32 = {.lo = (p), .hi = NULL}}
303 : : #else
304 : : #define MP_ROM_PTR(p) {.u32 = {.lo = NULL, .hi = (p)}}
305 : : #endif
306 : :
307 : : #endif
308 : :
309 : : // Macros to convert between mp_obj_t and concrete object types.
310 : : // These are identity operations in MicroPython, but ability to override
311 : : // these operations are provided to experiment with other methods of
312 : : // object representation and memory management.
313 : :
314 : : // Cast mp_obj_t to object pointer
315 : : #ifndef MP_OBJ_TO_PTR
316 : : #define MP_OBJ_TO_PTR(o) ((void *)(o))
317 : : #endif
318 : :
319 : : // Cast object pointer to mp_obj_t
320 : : #ifndef MP_OBJ_FROM_PTR
321 : : #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)(p))
322 : : #endif
323 : :
324 : : // Macros to create objects that are stored in ROM.
325 : :
326 : : #ifndef MP_ROM_NONE
327 : : #if MICROPY_OBJ_IMMEDIATE_OBJS
328 : : #define MP_ROM_NONE mp_const_none
329 : : #else
330 : : #define MP_ROM_NONE MP_ROM_PTR(&mp_const_none_obj)
331 : : #endif
332 : : #endif
333 : :
334 : : #ifndef MP_ROM_FALSE
335 : : #if MICROPY_OBJ_IMMEDIATE_OBJS
336 : : #define MP_ROM_FALSE mp_const_false
337 : : #define MP_ROM_TRUE mp_const_true
338 : : #else
339 : : #define MP_ROM_FALSE MP_ROM_PTR(&mp_const_false_obj)
340 : : #define MP_ROM_TRUE MP_ROM_PTR(&mp_const_true_obj)
341 : : #endif
342 : : #endif
343 : :
344 : : #ifndef MP_ROM_INT
345 : : typedef mp_const_obj_t mp_rom_obj_t;
346 : : #define MP_ROM_INT(i) MP_OBJ_NEW_SMALL_INT(i)
347 : : #define MP_ROM_QSTR(q) MP_OBJ_NEW_QSTR(q)
348 : : #define MP_ROM_PTR(p) (p)
349 : : /* for testing
350 : : typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
351 : : #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
352 : : #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
353 : : #define MP_ROM_PTR(p) {.o = p}
354 : : */
355 : : #endif
356 : :
357 : : // These macros are used to declare and define constant function objects
358 : : // You can put "static" in front of the definitions to make them local
359 : :
360 : : #define MP_DECLARE_CONST_FUN_OBJ_0(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
361 : : #define MP_DECLARE_CONST_FUN_OBJ_1(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
362 : : #define MP_DECLARE_CONST_FUN_OBJ_2(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
363 : : #define MP_DECLARE_CONST_FUN_OBJ_3(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
364 : : #define MP_DECLARE_CONST_FUN_OBJ_VAR(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
365 : : #define MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
366 : : #define MP_DECLARE_CONST_FUN_OBJ_KW(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
367 : :
368 : : #define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
369 : : #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)))
370 : :
371 : : #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
372 : : const mp_obj_fun_builtin_fixed_t obj_name = \
373 : : {{&mp_type_fun_builtin_0}, .fun._0 = fun_name}
374 : : #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
375 : : const mp_obj_fun_builtin_fixed_t obj_name = \
376 : : {{&mp_type_fun_builtin_1}, .fun._1 = fun_name}
377 : : #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
378 : : const mp_obj_fun_builtin_fixed_t obj_name = \
379 : : {{&mp_type_fun_builtin_2}, .fun._2 = fun_name}
380 : : #define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
381 : : const mp_obj_fun_builtin_fixed_t obj_name = \
382 : : {{&mp_type_fun_builtin_3}, .fun._3 = fun_name}
383 : : #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
384 : : const mp_obj_fun_builtin_var_t obj_name = \
385 : : {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, false), .fun.var = fun_name}
386 : : #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
387 : : const mp_obj_fun_builtin_var_t obj_name = \
388 : : {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, false), .fun.var = fun_name}
389 : : #define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
390 : : const mp_obj_fun_builtin_var_t obj_name = \
391 : : {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, true), .fun.kw = fun_name}
392 : :
393 : : // These macros are used to define constant map/dict objects
394 : : // You can put "static" in front of the definition to make it local
395 : :
396 : : #define MP_DEFINE_CONST_MAP(map_name, table_name) \
397 : : const mp_map_t map_name = { \
398 : : .all_keys_are_qstrs = 1, \
399 : : .is_fixed = 1, \
400 : : .is_ordered = 1, \
401 : : .used = MP_ARRAY_SIZE(table_name), \
402 : : .alloc = MP_ARRAY_SIZE(table_name), \
403 : : .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)table_name, \
404 : : }
405 : :
406 : : #define MP_DEFINE_CONST_DICT_WITH_SIZE(dict_name, table_name, n) \
407 : : const mp_obj_dict_t dict_name = { \
408 : : .base = {&mp_type_dict}, \
409 : : .map = { \
410 : : .all_keys_are_qstrs = 1, \
411 : : .is_fixed = 1, \
412 : : .is_ordered = 1, \
413 : : .used = n, \
414 : : .alloc = n, \
415 : : .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)table_name, \
416 : : }, \
417 : : }
418 : :
419 : : #define MP_DEFINE_CONST_DICT(dict_name, table_name) MP_DEFINE_CONST_DICT_WITH_SIZE(dict_name, table_name, MP_ARRAY_SIZE(table_name))
420 : :
421 : : // These macros are used to declare and define constant staticmethond and classmethod objects
422 : : // You can put "static" in front of the definitions to make them local
423 : :
424 : : #define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
425 : : #define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
426 : :
427 : : #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}
428 : : #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}
429 : :
430 : : #ifndef NO_QSTR
431 : :
432 : : // Declare a module as a builtin, processed by makemoduledefs.py
433 : : // param module_name: MP_QSTR_<module name>
434 : : // param obj_module: mp_obj_module_t instance
435 : : #define MP_REGISTER_MODULE(module_name, obj_module)
436 : :
437 : : // Declare a root pointer (to avoid garbage collection of a global static variable).
438 : : // param variable_declaration: a valid C variable declaration
439 : : #define MP_REGISTER_ROOT_POINTER(variable_declaration)
440 : :
441 : : #endif // NO_QSTR
442 : :
443 : : // Underlying map/hash table implementation (not dict object or map function)
444 : :
445 : : typedef struct _mp_map_elem_t {
446 : : mp_obj_t key;
447 : : mp_obj_t value;
448 : : } mp_map_elem_t;
449 : :
450 : : typedef struct _mp_rom_map_elem_t {
451 : : mp_rom_obj_t key;
452 : : mp_rom_obj_t value;
453 : : } mp_rom_map_elem_t;
454 : :
455 : : typedef struct _mp_map_t {
456 : : size_t all_keys_are_qstrs : 1;
457 : : size_t is_fixed : 1; // if set, table is fixed/read-only and can't be modified
458 : : size_t is_ordered : 1; // if set, table is an ordered array, not a hash map
459 : : size_t used : (8 * sizeof(size_t) - 3);
460 : : size_t alloc;
461 : : mp_map_elem_t *table;
462 : : } mp_map_t;
463 : :
464 : : // mp_set_lookup requires these constants to have the values they do
465 : : typedef enum _mp_map_lookup_kind_t {
466 : : MP_MAP_LOOKUP = 0,
467 : : MP_MAP_LOOKUP_ADD_IF_NOT_FOUND = 1,
468 : : MP_MAP_LOOKUP_REMOVE_IF_FOUND = 2,
469 : : MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND = 3, // only valid for mp_set_lookup
470 : : } mp_map_lookup_kind_t;
471 : :
472 : 50566 : static inline bool mp_map_slot_is_filled(const mp_map_t *map, size_t pos) {
473 [ - + ]: 50566 : assert(pos < map->alloc);
474 : 50566 : return (map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL;
475 : : }
476 : :
477 : : void mp_map_init(mp_map_t *map, size_t n);
478 : : void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table);
479 : : mp_map_t *mp_map_new(size_t n);
480 : : void mp_map_deinit(mp_map_t *map);
481 : : void mp_map_free(mp_map_t *map);
482 : : mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
483 : : void mp_map_clear(mp_map_t *map);
484 : : void mp_map_dump(mp_map_t *map);
485 : :
486 : : // Underlying set implementation (not set object)
487 : :
488 : : typedef struct _mp_set_t {
489 : : size_t alloc;
490 : : size_t used;
491 : : mp_obj_t *table;
492 : : } mp_set_t;
493 : :
494 : 49907 : static inline bool mp_set_slot_is_filled(const mp_set_t *set, size_t pos) {
495 [ + + + + : 49907 : return (set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL;
+ + ]
496 : : }
497 : :
498 : : void mp_set_init(mp_set_t *set, size_t n);
499 : : mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
500 : : mp_obj_t mp_set_remove_first(mp_set_t *set);
501 : : void mp_set_clear(mp_set_t *set);
502 : :
503 : : // Type definitions for methods
504 : :
505 : : typedef mp_obj_t (*mp_fun_0_t)(void);
506 : : typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
507 : : typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
508 : : typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
509 : : typedef mp_obj_t (*mp_fun_var_t)(size_t n, const mp_obj_t *);
510 : : // mp_fun_kw_t takes mp_map_t* (and not const mp_map_t*) to ease passing
511 : : // this arg to mp_map_lookup().
512 : : typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *);
513 : :
514 : : // Flags for type behaviour (mp_obj_type_t.flags)
515 : : // If MP_TYPE_FLAG_EQ_NOT_REFLEXIVE is clear then __eq__ is reflexive (A==A returns True).
516 : : // If MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE is clear then the type can't be equal to an
517 : : // instance of any different class that also clears this flag. If this flag is set
518 : : // then the type may check for equality against a different type.
519 : : // If MP_TYPE_FLAG_EQ_HAS_NEQ_TEST is clear then the type only implements the __eq__
520 : : // operator and not the __ne__ operator. If it's set then __ne__ may be implemented.
521 : : // If MP_TYPE_FLAG_BINDS_SELF is set then the type as a method binds self as the first arg.
522 : : // If MP_TYPE_FLAG_BUILTIN_FUN is set then the type is a built-in function type.
523 : : // MP_TYPE_FLAG_ITER_IS_GETITER is a no-op flag that means the default behaviour for the
524 : : // iter slot and it's the getiter function.
525 : : // If MP_TYPE_FLAG_ITER_IS_ITERNEXT is set then the "iter" slot is the iternext
526 : : // function and getiter will be automatically implemented as "return self".
527 : : // If MP_TYPE_FLAG_ITER_IS_CUSTOM is set then the "iter" slot is a pointer to a
528 : : // mp_getiter_iternext_custom_t struct instance (with both .getiter and .iternext set).
529 : : // If MP_TYPE_FLAG_ITER_IS_STREAM is set then the type implicitly gets a "return self"
530 : : // getiter, and mp_stream_unbuffered_iter for iternext.
531 : : // If MP_TYPE_FLAG_INSTANCE_TYPE is set then this is an instance type (i.e. defined in Python).
532 : : #define MP_TYPE_FLAG_NONE (0x0000)
533 : : #define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001)
534 : : #define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002)
535 : : #define MP_TYPE_FLAG_EQ_NOT_REFLEXIVE (0x0004)
536 : : #define MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE (0x0008)
537 : : #define MP_TYPE_FLAG_EQ_HAS_NEQ_TEST (0x0010)
538 : : #define MP_TYPE_FLAG_BINDS_SELF (0x0020)
539 : : #define MP_TYPE_FLAG_BUILTIN_FUN (0x0040)
540 : : #define MP_TYPE_FLAG_ITER_IS_GETITER (0x0000)
541 : : #define MP_TYPE_FLAG_ITER_IS_ITERNEXT (0x0080)
542 : : #define MP_TYPE_FLAG_ITER_IS_CUSTOM (0x0100)
543 : : #define MP_TYPE_FLAG_ITER_IS_STREAM (MP_TYPE_FLAG_ITER_IS_ITERNEXT | MP_TYPE_FLAG_ITER_IS_CUSTOM)
544 : : #define MP_TYPE_FLAG_INSTANCE_TYPE (0x0200)
545 : :
546 : : typedef enum {
547 : : PRINT_STR = 0,
548 : : PRINT_REPR = 1,
549 : : PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
550 : : PRINT_JSON = 3,
551 : : PRINT_RAW = 4, // Special format for printing bytes as an undercorated string
552 : : PRINT_EXC_SUBCLASS = 0x80, // Internal flag for printing exception subclasses
553 : : } mp_print_kind_t;
554 : :
555 : : typedef struct _mp_obj_iter_buf_t {
556 : : mp_obj_base_t base;
557 : : mp_obj_t buf[3];
558 : : } mp_obj_iter_buf_t;
559 : :
560 : : // The number of slots that an mp_obj_iter_buf_t needs on the Python value stack.
561 : : // It's rounded up in case mp_obj_base_t is smaller than mp_obj_t (eg for OBJ_REPR_D).
562 : : #define MP_OBJ_ITER_BUF_NSLOTS ((sizeof(mp_obj_iter_buf_t) + sizeof(mp_obj_t) - 1) / sizeof(mp_obj_t))
563 : :
564 : : typedef void (*mp_print_fun_t)(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind);
565 : : 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);
566 : : 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);
567 : : typedef mp_obj_t (*mp_unary_op_fun_t)(mp_unary_op_t op, mp_obj_t);
568 : : typedef mp_obj_t (*mp_binary_op_fun_t)(mp_binary_op_t op, mp_obj_t, mp_obj_t);
569 : : typedef void (*mp_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
570 : : typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
571 : : typedef mp_obj_t (*mp_getiter_fun_t)(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf);
572 : : typedef mp_fun_1_t mp_iternext_fun_t;
573 : :
574 : : // For MP_TYPE_FLAG_ITER_IS_CUSTOM, the "getiter" slot points to an instance of this type.
575 : : typedef struct _mp_getiter_iternext_custom_t {
576 : : mp_getiter_fun_t getiter;
577 : : mp_iternext_fun_t iternext;
578 : : } mp_getiter_iternext_custom_t;
579 : :
580 : : // Buffer protocol
581 : : typedef struct _mp_buffer_info_t {
582 : : void *buf; // can be NULL if len == 0
583 : : size_t len; // in bytes
584 : : int typecode; // as per binary.h
585 : : } mp_buffer_info_t;
586 : : #define MP_BUFFER_READ (1)
587 : : #define MP_BUFFER_WRITE (2)
588 : : #define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
589 : : typedef mp_int_t (*mp_buffer_fun_t)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
590 : : bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
591 : : void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
592 : :
593 : : // This struct will be updated to become a variable sized struct. In order to
594 : : // use this as a member, or allocate dynamically, use the mp_obj_empty_type_t
595 : : // or mp_obj_full_type_t structs below (which must be kept in sync).
596 : : struct _mp_obj_type_t {
597 : : // A type is an object so must start with this entry, which points to mp_type_type.
598 : : mp_obj_base_t base;
599 : :
600 : : // Flags associated with this type.
601 : : uint16_t flags;
602 : :
603 : : // The name of this type, a qstr.
604 : : uint16_t name;
605 : :
606 : : // Slots: For the rest of the fields, the slot index points to the
607 : : // relevant function in the variable-length "slots" field. Ideally these
608 : : // would be only 4 bits, but the extra overhead of accessing them adds
609 : : // more code, and we also need to be able to take the address of them for
610 : : // mp_obj_class_lookup.
611 : :
612 : : // Corresponds to __new__ and __init__ special methods, to make an instance of the type.
613 : : uint8_t slot_index_make_new;
614 : :
615 : : // Corresponds to __repr__ and __str__ special methods.
616 : : uint8_t slot_index_print;
617 : :
618 : : // Corresponds to __call__ special method, ie T(...).
619 : : uint8_t slot_index_call;
620 : :
621 : : // Implements unary and binary operations.
622 : : // Can return MP_OBJ_NULL if the operation is not supported.
623 : : uint8_t slot_index_unary_op;
624 : : uint8_t slot_index_binary_op;
625 : :
626 : : // Implements load, store and delete attribute.
627 : : //
628 : : // dest[0] = MP_OBJ_NULL means load
629 : : // return: for fail, do nothing
630 : : // for fail but continue lookup in locals_dict, dest[1] = MP_OBJ_SENTINEL
631 : : // for attr, dest[0] = value
632 : : // for method, dest[0] = method, dest[1] = self
633 : : //
634 : : // dest[0,1] = {MP_OBJ_SENTINEL, MP_OBJ_NULL} means delete
635 : : // dest[0,1] = {MP_OBJ_SENTINEL, object} means store
636 : : // return: for fail, do nothing
637 : : // for success set dest[0] = MP_OBJ_NULL
638 : : uint8_t slot_index_attr;
639 : :
640 : : // Implements load, store and delete subscripting:
641 : : // - value = MP_OBJ_SENTINEL means load
642 : : // - value = MP_OBJ_NULL means delete
643 : : // - all other values mean store the value
644 : : // Can return MP_OBJ_NULL if operation not supported.
645 : : uint8_t slot_index_subscr;
646 : :
647 : : // This slot's behaviour depends on the MP_TYPE_FLAG_ITER_IS_* flags above.
648 : : // - If MP_TYPE_FLAG_ITER_IS_GETITER flag is set, then this corresponds to the __iter__
649 : : // special method (of type mp_getiter_fun_t). Can use the given mp_obj_iter_buf_t
650 : : // to store the iterator object, otherwise can return a pointer to an object on the heap.
651 : : // - If MP_TYPE_FLAG_ITER_IS_ITERNEXT is set, then this corresponds to __next__ special method.
652 : : // May return MP_OBJ_STOP_ITERATION as an optimisation instead of raising StopIteration()
653 : : // with no args. The type will implicitly implement getiter as "return self".
654 : : // - If MP_TYPE_FLAG_ITER_IS_CUSTOM is set, then this slot must point to an
655 : : // mp_getiter_iternext_custom_t instance with both the getiter and iternext fields set.
656 : : // - If MP_TYPE_FLAG_ITER_IS_STREAM is set, this this slot should be unset.
657 : : uint8_t slot_index_iter;
658 : :
659 : : // Implements the buffer protocol if supported by this type.
660 : : uint8_t slot_index_buffer;
661 : :
662 : : // One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
663 : : uint8_t slot_index_protocol;
664 : :
665 : : // A pointer to the parents of this type:
666 : : // - 0 parents: pointer is NULL (object is implicitly the single parent)
667 : : // - 1 parent: a pointer to the type of that parent
668 : : // - 2 or more parents: pointer to a tuple object containing the parent types
669 : : uint8_t slot_index_parent;
670 : :
671 : : // A dict mapping qstrs to objects local methods/constants/etc.
672 : : uint8_t slot_index_locals_dict;
673 : :
674 : : const void *slots[];
675 : : };
676 : :
677 : : // Non-variable sized versions of mp_obj_type_t to be used as a member
678 : : // in other structs or for dynamic allocation. The fields are exactly
679 : : // as in mp_obj_type_t, but with a fixed size for the flexible array
680 : : // members.
681 : : typedef struct _mp_obj_empty_type_t {
682 : : mp_obj_base_t base;
683 : : uint16_t flags;
684 : : uint16_t name;
685 : :
686 : : uint8_t slot_index_make_new;
687 : : uint8_t slot_index_print;
688 : : uint8_t slot_index_call;
689 : : uint8_t slot_index_unary_op;
690 : : uint8_t slot_index_binary_op;
691 : : uint8_t slot_index_attr;
692 : : uint8_t slot_index_subscr;
693 : : uint8_t slot_index_iter;
694 : : uint8_t slot_index_buffer;
695 : : uint8_t slot_index_protocol;
696 : : uint8_t slot_index_parent;
697 : : uint8_t slot_index_locals_dict;
698 : :
699 : : // No slots member.
700 : : } mp_obj_empty_type_t;
701 : :
702 : : typedef struct _mp_obj_full_type_t {
703 : : mp_obj_base_t base;
704 : : uint16_t flags;
705 : : uint16_t name;
706 : :
707 : : uint8_t slot_index_make_new;
708 : : uint8_t slot_index_print;
709 : : uint8_t slot_index_call;
710 : : uint8_t slot_index_unary_op;
711 : : uint8_t slot_index_binary_op;
712 : : uint8_t slot_index_attr;
713 : : uint8_t slot_index_subscr;
714 : : uint8_t slot_index_iter;
715 : : uint8_t slot_index_buffer;
716 : : uint8_t slot_index_protocol;
717 : : uint8_t slot_index_parent;
718 : : uint8_t slot_index_locals_dict;
719 : :
720 : : // Explicitly add 12 slots.
721 : : const void *slots[11];
722 : : } mp_obj_full_type_t;
723 : :
724 : : #define _MP_OBJ_TYPE_SLOT_TYPE_make_new (mp_make_new_fun_t)
725 : : #define _MP_OBJ_TYPE_SLOT_TYPE_print (mp_print_fun_t)
726 : : #define _MP_OBJ_TYPE_SLOT_TYPE_call (mp_call_fun_t)
727 : : #define _MP_OBJ_TYPE_SLOT_TYPE_unary_op (mp_unary_op_fun_t)
728 : : #define _MP_OBJ_TYPE_SLOT_TYPE_binary_op (mp_binary_op_fun_t)
729 : : #define _MP_OBJ_TYPE_SLOT_TYPE_attr (mp_attr_fun_t)
730 : : #define _MP_OBJ_TYPE_SLOT_TYPE_subscr (mp_subscr_fun_t)
731 : : #define _MP_OBJ_TYPE_SLOT_TYPE_iter (const void *)
732 : : #define _MP_OBJ_TYPE_SLOT_TYPE_buffer (mp_buffer_fun_t)
733 : : #define _MP_OBJ_TYPE_SLOT_TYPE_protocol (const void *)
734 : : #define _MP_OBJ_TYPE_SLOT_TYPE_parent (const void *)
735 : : #define _MP_OBJ_TYPE_SLOT_TYPE_locals_dict (struct _mp_obj_dict_t *)
736 : :
737 : : // Implementation of MP_DEFINE_CONST_OBJ_TYPE for each number of arguments.
738 : : // Do not use these directly, instead use MP_DEFINE_CONST_OBJ_TYPE.
739 : : // Generated with:
740 : : // for i in range(13):
741 : : // print(f"#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_{i}(_struct_type, _typename, _name, _flags{''.join(f', f{j+1}, v{j+1}' for j in range(i))}) const _struct_type _typename = {{ .base = {{ &mp_type_type }}, .name = _name, .flags = _flags{''.join(f', .slot_index_##f{j+1} = {j+1}' for j in range(i))}{', .slots = { ' + ''.join(f'v{j+1}, ' for j in range(i)) + '}' if i else '' } }}")
742 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_0(_struct_type, _typename, _name, _flags) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags }
743 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_1(_struct_type, _typename, _name, _flags, f1, v1) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slots = { v1, } }
744 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_2(_struct_type, _typename, _name, _flags, f1, v1, f2, v2) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slots = { v1, v2, } }
745 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_3(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slots = { v1, v2, v3, } }
746 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_4(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slots = { v1, v2, v3, v4, } }
747 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_5(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slots = { v1, v2, v3, v4, v5, } }
748 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_6(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slots = { v1, v2, v3, v4, v5, v6, } }
749 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_7(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slots = { v1, v2, v3, v4, v5, v6, v7, } }
750 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_8(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, } }
751 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_9(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, } }
752 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_10(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, } }
753 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_11(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, } }
754 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_12(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slot_index_##f12 = 12, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, } }
755 : :
756 : : // Because the mp_obj_type_t instances are in (zero-initialised) ROM, we take
757 : : // slot_index_foo=0 to mean that the slot is unset. This also simplifies checking
758 : : // if the slot is set. That means that we need to store index+1 in slot_index_foo
759 : : // though and then access it as slots[slot_index_foo - 1]. This is an implementation
760 : : // detail, the user of these macros doesn't need to be aware of it, and when using
761 : : // MP_OBJ_TYPE_OFFSETOF_SLOT you should use zero-based indexing.
762 : : #define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->slot_index_##f)
763 : : #define MP_OBJ_TYPE_GET_SLOT(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(t)->slots[(t)->slot_index_##f - 1])
764 : : #define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(MP_OBJ_TYPE_HAS_SLOT(t, f) ? MP_OBJ_TYPE_GET_SLOT(t, f) : NULL))
765 : : #define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->slot_index_##f = (n) + 1, (t)->slots[(n)] = (void *)v)
766 : : #define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, slot_index_##f))
767 : : #define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(uint8_t *)((char *)(t) + (offset)) != 0)
768 : :
769 : : // Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked
770 : : #define MP_DEFINE_CONST_OBJ_TYPE_EXPAND(x) x
771 : :
772 : : // This macro evaluates to MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N, where N is the value
773 : : // of the 29th argument (29 is 13*2 + 3).
774 : : #define MP_DEFINE_CONST_OBJ_TYPE_NARGS(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, N, ...) MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N
775 : :
776 : : // This macros is used to define a object type in ROM.
777 : : // Invoke as MP_DEFINE_CONST_OBJ_TYPE(_typename, _name, _flags, _make_new [, slot, func]*)
778 : : // It uses the number of arguments to select which MP_DEFINE_CONST_OBJ_TYPE_*
779 : : // macro to use based on the number of arguments. It works by shifting the
780 : : // numeric values 12, 11, ... 0 by the number of arguments, such that the
781 : : // 29th argument ends up being the number to use. The _INV values are
782 : : // placeholders because the slot arguments come in pairs.
783 : : #define MP_DEFINE_CONST_OBJ_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_EXPAND(MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_type_t, __VA_ARGS__))
784 : :
785 : : // Constant types, globally accessible
786 : : extern const mp_obj_type_t mp_type_type;
787 : : extern const mp_obj_type_t mp_type_object;
788 : : extern const mp_obj_type_t mp_type_NoneType;
789 : : extern const mp_obj_type_t mp_type_bool;
790 : : extern const mp_obj_type_t mp_type_int;
791 : : extern const mp_obj_type_t mp_type_str;
792 : : extern const mp_obj_type_t mp_type_bytes;
793 : : extern const mp_obj_type_t mp_type_bytearray;
794 : : extern const mp_obj_type_t mp_type_memoryview;
795 : : extern const mp_obj_type_t mp_type_float;
796 : : extern const mp_obj_type_t mp_type_complex;
797 : : extern const mp_obj_type_t mp_type_tuple;
798 : : extern const mp_obj_type_t mp_type_list;
799 : : extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict implementation detail)
800 : : extern const mp_obj_type_t mp_type_enumerate;
801 : : extern const mp_obj_type_t mp_type_filter;
802 : : extern const mp_obj_type_t mp_type_deque;
803 : : extern const mp_obj_type_t mp_type_dict;
804 : : extern const mp_obj_type_t mp_type_ordereddict;
805 : : extern const mp_obj_type_t mp_type_range;
806 : : extern const mp_obj_type_t mp_type_set;
807 : : extern const mp_obj_type_t mp_type_frozenset;
808 : : extern const mp_obj_type_t mp_type_slice;
809 : : extern const mp_obj_type_t mp_type_zip;
810 : : extern const mp_obj_type_t mp_type_array;
811 : : extern const mp_obj_type_t mp_type_super;
812 : : extern const mp_obj_type_t mp_type_gen_wrap;
813 : : extern const mp_obj_type_t mp_type_native_gen_wrap;
814 : : extern const mp_obj_type_t mp_type_gen_instance;
815 : : extern const mp_obj_type_t mp_type_fun_builtin_0;
816 : : extern const mp_obj_type_t mp_type_fun_builtin_1;
817 : : extern const mp_obj_type_t mp_type_fun_builtin_2;
818 : : extern const mp_obj_type_t mp_type_fun_builtin_3;
819 : : extern const mp_obj_type_t mp_type_fun_builtin_var;
820 : : extern const mp_obj_type_t mp_type_fun_bc;
821 : : extern const mp_obj_type_t mp_type_module;
822 : : extern const mp_obj_type_t mp_type_staticmethod;
823 : : extern const mp_obj_type_t mp_type_classmethod;
824 : : extern const mp_obj_type_t mp_type_property;
825 : : extern const mp_obj_type_t mp_type_stringio;
826 : : extern const mp_obj_type_t mp_type_bytesio;
827 : : extern const mp_obj_type_t mp_type_reversed;
828 : : extern const mp_obj_type_t mp_type_polymorph_iter;
829 : : #if MICROPY_ENABLE_FINALISER
830 : : extern const mp_obj_type_t mp_type_polymorph_iter_with_finaliser;
831 : : #endif
832 : :
833 : : // Exceptions
834 : : extern const mp_obj_type_t mp_type_BaseException;
835 : : extern const mp_obj_type_t mp_type_ArithmeticError;
836 : : extern const mp_obj_type_t mp_type_AssertionError;
837 : : extern const mp_obj_type_t mp_type_AttributeError;
838 : : extern const mp_obj_type_t mp_type_EOFError;
839 : : extern const mp_obj_type_t mp_type_Exception;
840 : : extern const mp_obj_type_t mp_type_GeneratorExit;
841 : : extern const mp_obj_type_t mp_type_ImportError;
842 : : extern const mp_obj_type_t mp_type_IndentationError;
843 : : extern const mp_obj_type_t mp_type_IndexError;
844 : : extern const mp_obj_type_t mp_type_KeyboardInterrupt;
845 : : extern const mp_obj_type_t mp_type_KeyError;
846 : : extern const mp_obj_type_t mp_type_LookupError;
847 : : extern const mp_obj_type_t mp_type_MemoryError;
848 : : extern const mp_obj_type_t mp_type_NameError;
849 : : extern const mp_obj_type_t mp_type_NotImplementedError;
850 : : extern const mp_obj_type_t mp_type_OSError;
851 : : extern const mp_obj_type_t mp_type_OverflowError;
852 : : extern const mp_obj_type_t mp_type_RuntimeError;
853 : : extern const mp_obj_type_t mp_type_StopAsyncIteration;
854 : : extern const mp_obj_type_t mp_type_StopIteration;
855 : : extern const mp_obj_type_t mp_type_SyntaxError;
856 : : extern const mp_obj_type_t mp_type_SystemExit;
857 : : extern const mp_obj_type_t mp_type_TypeError;
858 : : extern const mp_obj_type_t mp_type_UnicodeError;
859 : : extern const mp_obj_type_t mp_type_ValueError;
860 : : extern const mp_obj_type_t mp_type_ViperTypeError;
861 : : extern const mp_obj_type_t mp_type_ZeroDivisionError;
862 : :
863 : : // Constant objects, globally accessible: None, False, True
864 : : // These should always be accessed via the below macros.
865 : : #if MICROPY_OBJ_IMMEDIATE_OBJS
866 : : // None is even while False/True are odd so their types can be distinguished with 1 bit.
867 : : #define mp_const_none MP_OBJ_NEW_IMMEDIATE_OBJ(0)
868 : : #define mp_const_false MP_OBJ_NEW_IMMEDIATE_OBJ(1)
869 : : #define mp_const_true MP_OBJ_NEW_IMMEDIATE_OBJ(3)
870 : : #else
871 : : #define mp_const_none (MP_OBJ_FROM_PTR(&mp_const_none_obj))
872 : : #define mp_const_false (MP_OBJ_FROM_PTR(&mp_const_false_obj))
873 : : #define mp_const_true (MP_OBJ_FROM_PTR(&mp_const_true_obj))
874 : : extern const struct _mp_obj_none_t mp_const_none_obj;
875 : : extern const struct _mp_obj_bool_t mp_const_false_obj;
876 : : extern const struct _mp_obj_bool_t mp_const_true_obj;
877 : : #endif
878 : :
879 : : // Constant objects, globally accessible: b'', (), {}, Ellipsis, NotImplemented, GeneratorExit()
880 : : // The below macros are for convenience only.
881 : : #define mp_const_empty_bytes (MP_OBJ_FROM_PTR(&mp_const_empty_bytes_obj))
882 : : #define mp_const_empty_tuple (MP_OBJ_FROM_PTR(&mp_const_empty_tuple_obj))
883 : : #define mp_const_notimplemented (MP_OBJ_FROM_PTR(&mp_const_notimplemented_obj))
884 : : extern const struct _mp_obj_str_t mp_const_empty_bytes_obj;
885 : : extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
886 : : extern const struct _mp_obj_dict_t mp_const_empty_dict_obj;
887 : : extern const struct _mp_obj_singleton_t mp_const_ellipsis_obj;
888 : : extern const struct _mp_obj_singleton_t mp_const_notimplemented_obj;
889 : : extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
890 : :
891 : : // Fixed empty map. Useful when calling keyword-receiving functions
892 : : // without any keywords from C, etc.
893 : : #define mp_const_empty_map (mp_const_empty_dict_obj.map)
894 : :
895 : : // General API for objects
896 : :
897 : : // Helper versions of m_new_obj when you need to immediately set base.type.
898 : : // Implementing this as a call rather than inline saves 8 bytes per usage.
899 : : #define mp_obj_malloc(struct_type, obj_type) ((struct_type *)mp_obj_malloc_helper(sizeof(struct_type), obj_type))
900 : : #define mp_obj_malloc_var(struct_type, var_type, var_num, obj_type) ((struct_type *)mp_obj_malloc_helper(sizeof(struct_type) + sizeof(var_type) * (var_num), obj_type))
901 : : void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type);
902 : :
903 : : // These macros are derived from more primitive ones and are used to
904 : : // check for more specific object types.
905 : : // Note: these are kept as macros because inline functions sometimes use much
906 : : // more code space than the equivalent macros, depending on the compiler.
907 : : // don't use mp_obj_is_exact_type directly; use mp_obj_is_type which provides additional safety checks.
908 : : // use the former only if you need to bypass these checks (because you've already checked everything else)
909 : : #define mp_obj_is_exact_type(o, t) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type == (t)))
910 : :
911 : : // Type checks are split to a separate, constant result macro. This is so it doesn't hinder the compilers's
912 : : // optimizations (other tricks like using ({ expr; exper; }) or (exp, expr, expr) in mp_obj_is_type() result
913 : : // in missed optimizations)
914 : : #define mp_type_assert_not_bool_int_str_nonetype(t) ( \
915 : : MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_bool), assert((t) != &mp_type_bool), \
916 : : MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_int), assert((t) != &mp_type_int), \
917 : : MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_str), assert((t) != &mp_type_str), \
918 : : MP_STATIC_ASSERT_NOT_MSC((t) != &mp_type_NoneType), assert((t) != &mp_type_NoneType), \
919 : : 1)
920 : :
921 : : #define mp_obj_is_type(o, t) (mp_type_assert_not_bool_int_str_nonetype(t) && mp_obj_is_exact_type(o, t))
922 : : #if MICROPY_OBJ_IMMEDIATE_OBJS
923 : : // bool's are immediates, not real objects, so test for the 2 possible values.
924 : : #define mp_obj_is_bool(o) ((o) == mp_const_false || (o) == mp_const_true)
925 : : #else
926 : : #define mp_obj_is_bool(o) mp_obj_is_exact_type(o, &mp_type_bool)
927 : : #endif
928 : : #define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_exact_type(o, &mp_type_int))
929 : : #define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_exact_type(o, &mp_type_str))
930 : : #define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, binary_op) == mp_obj_str_binary_op))
931 : : bool mp_obj_is_dict_or_ordereddict(mp_obj_t o);
932 : : #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))
933 : :
934 : : mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
935 : 18154002 : static inline mp_obj_t mp_obj_new_bool(mp_int_t x) {
936 [ + + + + : 18154002 : return x ? mp_const_true : mp_const_false;
+ + + + +
+ + + +
- ]
937 : : }
938 : : mp_obj_t mp_obj_new_cell(mp_obj_t obj);
939 : : mp_obj_t mp_obj_new_int(mp_int_t value);
940 : : mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
941 : : mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base);
942 : : 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)
943 : : 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)
944 : : mp_obj_t mp_obj_new_str(const char *data, size_t len); // will check utf-8 (raises UnicodeError)
945 : : mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len); // input data must be valid utf-8
946 : : mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr); // will check utf-8 (raises UnicodeError)
947 : : #if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK
948 : : mp_obj_t mp_obj_new_str_from_utf8_vstr(vstr_t *vstr); // input data must be valid utf-8
949 : : #else
950 : : #define mp_obj_new_str_from_utf8_vstr mp_obj_new_str_from_vstr
951 : : #endif
952 : : mp_obj_t mp_obj_new_bytes_from_vstr(vstr_t *vstr);
953 : : mp_obj_t mp_obj_new_bytes(const byte *data, size_t len);
954 : : mp_obj_t mp_obj_new_bytearray(size_t n, const void *items);
955 : : mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items);
956 : : #if MICROPY_PY_BUILTINS_FLOAT
957 : : mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
958 : : mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
959 : : #endif
960 : : mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
961 : : mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args);
962 : : #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
963 : : #define mp_obj_new_exception_msg(exc_type, msg) mp_obj_new_exception(exc_type)
964 : : #define mp_obj_new_exception_msg_varg(exc_type, ...) mp_obj_new_exception(exc_type)
965 : : #else
966 : : mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
967 : : 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!)
968 : : #endif
969 : : #ifdef va_start
970 : : 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
971 : : #endif
972 : : mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
973 : : mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed, const mp_obj_t *closed);
974 : : mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
975 : : mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);
976 : : mp_obj_t mp_obj_new_dict(size_t n_args);
977 : : mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items);
978 : : mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
979 : : mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
980 : : mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf);
981 : : mp_obj_t mp_obj_new_module(qstr module_name);
982 : : mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items);
983 : :
984 : : const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
985 : : const char *mp_obj_get_type_str(mp_const_obj_t o_in);
986 : : bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
987 : : mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type);
988 : :
989 : : void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
990 : : void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
991 : : void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc);
992 : :
993 : : bool mp_obj_is_true(mp_obj_t arg);
994 : : bool mp_obj_is_callable(mp_obj_t o_in);
995 : : mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2);
996 : : bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
997 : :
998 : : // returns true if o is bool, small int or long int
999 : 4 : static inline bool mp_obj_is_integer(mp_const_obj_t o) {
1000 [ + - + - : 6 : return mp_obj_is_int(o) || mp_obj_is_bool(o);
+ + - + ]
1001 : : }
1002 : :
1003 : : mp_int_t mp_obj_get_int(mp_const_obj_t arg);
1004 : : mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
1005 : : bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
1006 : : #if MICROPY_PY_BUILTINS_FLOAT
1007 : : mp_float_t mp_obj_get_float(mp_obj_t self_in);
1008 : : bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value);
1009 : : void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
1010 : : bool mp_obj_get_complex_maybe(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
1011 : : #endif
1012 : : void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items); // *items may point inside a GC block
1013 : : 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
1014 : : size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice);
1015 : : mp_obj_t mp_obj_id(mp_obj_t o_in);
1016 : : mp_obj_t mp_obj_len(mp_obj_t o_in);
1017 : : mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL
1018 : : mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
1019 : : mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in);
1020 : :
1021 : : // cell
1022 : :
1023 : : typedef struct _mp_obj_cell_t {
1024 : : mp_obj_base_t base;
1025 : : mp_obj_t obj;
1026 : : } mp_obj_cell_t;
1027 : :
1028 : 48495 : static inline mp_obj_t mp_obj_cell_get(mp_obj_t self_in) {
1029 : 48495 : mp_obj_cell_t *self = (mp_obj_cell_t *)MP_OBJ_TO_PTR(self_in);
1030 [ + + ]: 48495 : return self->obj;
1031 : : }
1032 : :
1033 : 1366 : static inline void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) {
1034 : 1366 : mp_obj_cell_t *self = (mp_obj_cell_t *)MP_OBJ_TO_PTR(self_in);
1035 : 1366 : self->obj = obj;
1036 : 54 : }
1037 : :
1038 : : // int
1039 : : // For long int, returns value truncated to mp_int_t
1040 : : mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
1041 : : // Will raise exception if value doesn't fit into mp_int_t
1042 : : mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
1043 : : // Will raise exception if value is negative or doesn't fit into mp_uint_t
1044 : : mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in);
1045 : :
1046 : : // exception
1047 : : bool mp_obj_is_native_exception_instance(mp_obj_t self_in);
1048 : : bool mp_obj_is_exception_type(mp_obj_t self_in);
1049 : : bool mp_obj_is_exception_instance(mp_obj_t self_in);
1050 : : bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
1051 : : void mp_obj_exception_clear_traceback(mp_obj_t self_in);
1052 : : void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
1053 : : void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
1054 : : mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
1055 : : 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);
1056 : : mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
1057 : : void mp_init_emergency_exception_buf(void);
1058 : 570 : static inline mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
1059 [ + - - + ]: 570 : assert(MP_OBJ_TYPE_GET_SLOT_OR_NULL(exc_type, make_new) == mp_obj_exception_make_new);
1060 : 570 : return mp_obj_exception_make_new(exc_type, 1, 0, &arg);
1061 : : }
1062 : :
1063 : : // str
1064 : : bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
1065 : : qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
1066 : : const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
1067 : : const char *mp_obj_str_get_data(mp_obj_t self_in, size_t *len);
1068 : : mp_obj_t mp_obj_str_intern(mp_obj_t str);
1069 : : mp_obj_t mp_obj_str_intern_checked(mp_obj_t obj);
1070 : : void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, size_t str_len, bool is_bytes);
1071 : :
1072 : : #if MICROPY_PY_BUILTINS_FLOAT
1073 : : // float
1074 : : #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
1075 : : static inline float mp_obj_get_float_to_f(mp_obj_t o) {
1076 : : return mp_obj_get_float(o);
1077 : : }
1078 : :
1079 : : static inline double mp_obj_get_float_to_d(mp_obj_t o) {
1080 : : return (double)mp_obj_get_float(o);
1081 : : }
1082 : :
1083 : : static inline mp_obj_t mp_obj_new_float_from_f(float o) {
1084 : : return mp_obj_new_float(o);
1085 : : }
1086 : :
1087 : : static inline mp_obj_t mp_obj_new_float_from_d(double o) {
1088 : : return mp_obj_new_float((mp_float_t)o);
1089 : : }
1090 : : #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
1091 : 96 : static inline float mp_obj_get_float_to_f(mp_obj_t o) {
1092 : 96 : return (float)mp_obj_get_float(o);
1093 : : }
1094 : :
1095 : 74 : static inline double mp_obj_get_float_to_d(mp_obj_t o) {
1096 : 74 : return mp_obj_get_float(o);
1097 : : }
1098 : :
1099 : 78 : static inline mp_obj_t mp_obj_new_float_from_f(float o) {
1100 : 78 : return mp_obj_new_float((mp_float_t)o);
1101 : : }
1102 : :
1103 : 76 : static inline mp_obj_t mp_obj_new_float_from_d(double o) {
1104 : 76 : return mp_obj_new_float(o);
1105 : : }
1106 : : #endif
1107 : : #if MICROPY_FLOAT_HIGH_QUALITY_HASH
1108 : : mp_int_t mp_float_hash(mp_float_t val);
1109 : : #else
1110 : : static inline mp_int_t mp_float_hash(mp_float_t val) {
1111 : : return (mp_int_t)val;
1112 : : }
1113 : : #endif
1114 : : 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
1115 : :
1116 : : // complex
1117 : : void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
1118 : : 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
1119 : : #else
1120 : : #define mp_obj_is_float(o) (false)
1121 : : #endif
1122 : :
1123 : : // tuple
1124 : : void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
1125 : : void mp_obj_tuple_del(mp_obj_t self_in);
1126 : : mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
1127 : :
1128 : : // list
1129 : : mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
1130 : : mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value);
1131 : : void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
1132 : : void mp_obj_list_set_len(mp_obj_t self_in, size_t len);
1133 : : void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
1134 : : mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
1135 : :
1136 : : // dict
1137 : : typedef struct _mp_obj_dict_t {
1138 : : mp_obj_base_t base;
1139 : : mp_map_t map;
1140 : : } mp_obj_dict_t;
1141 : : 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);
1142 : : void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args);
1143 : : size_t mp_obj_dict_len(mp_obj_t self_in);
1144 : : mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
1145 : : mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
1146 : : mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
1147 : : mp_obj_t mp_obj_dict_copy(mp_obj_t self_in);
1148 : 876 : static inline mp_map_t *mp_obj_dict_get_map(mp_obj_t dict) {
1149 [ - + ]: 876 : return &((mp_obj_dict_t *)MP_OBJ_TO_PTR(dict))->map;
1150 : : }
1151 : :
1152 : : // set
1153 : : void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
1154 : :
1155 : : // slice indexes resolved to particular sequence
1156 : : typedef struct {
1157 : : mp_int_t start;
1158 : : mp_int_t stop;
1159 : : mp_int_t step;
1160 : : } mp_bound_slice_t;
1161 : :
1162 : : // slice
1163 : : typedef struct _mp_obj_slice_t {
1164 : : mp_obj_base_t base;
1165 : : mp_obj_t start;
1166 : : mp_obj_t stop;
1167 : : mp_obj_t step;
1168 : : } mp_obj_slice_t;
1169 : : void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result);
1170 : :
1171 : : // functions
1172 : :
1173 : : typedef struct _mp_obj_fun_builtin_fixed_t {
1174 : : mp_obj_base_t base;
1175 : : union {
1176 : : mp_fun_0_t _0;
1177 : : mp_fun_1_t _1;
1178 : : mp_fun_2_t _2;
1179 : : mp_fun_3_t _3;
1180 : : } fun;
1181 : : } mp_obj_fun_builtin_fixed_t;
1182 : :
1183 : : typedef struct _mp_obj_fun_builtin_var_t {
1184 : : mp_obj_base_t base;
1185 : : uint32_t sig; // see MP_OBJ_FUN_MAKE_SIG
1186 : : union {
1187 : : mp_fun_var_t var;
1188 : : mp_fun_kw_t kw;
1189 : : } fun;
1190 : : } mp_obj_fun_builtin_var_t;
1191 : :
1192 : : qstr mp_obj_fun_get_name(mp_const_obj_t fun);
1193 : :
1194 : : mp_obj_t mp_identity(mp_obj_t self);
1195 : : MP_DECLARE_CONST_FUN_OBJ_1(mp_identity_obj);
1196 : :
1197 : : // module
1198 : : typedef struct _mp_obj_module_t {
1199 : : mp_obj_base_t base;
1200 : : mp_obj_dict_t *globals;
1201 : : } mp_obj_module_t;
1202 : 82 : static inline mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t module) {
1203 : 82 : return ((mp_obj_module_t *)MP_OBJ_TO_PTR(module))->globals;
1204 : : }
1205 : : // check if given module object is a package
1206 : : bool mp_obj_is_package(mp_obj_t module);
1207 : :
1208 : : // staticmethod and classmethod types; defined here so we can make const versions
1209 : : // this structure is used for instances of both staticmethod and classmethod
1210 : : typedef struct _mp_obj_static_class_method_t {
1211 : : mp_obj_base_t base;
1212 : : mp_obj_t fun;
1213 : : } mp_obj_static_class_method_t;
1214 : : typedef struct _mp_rom_obj_static_class_method_t {
1215 : : mp_obj_base_t base;
1216 : : mp_rom_obj_t fun;
1217 : : } mp_rom_obj_static_class_method_t;
1218 : :
1219 : : // property
1220 : : const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
1221 : :
1222 : : // sequence helpers
1223 : :
1224 : : void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest);
1225 : : #if MICROPY_PY_BUILTINS_SLICE
1226 : : bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes);
1227 : : #endif
1228 : : #define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
1229 : : #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)); }
1230 : : bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2);
1231 : : 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);
1232 : : mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args);
1233 : : mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value);
1234 : : mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
1235 : :
1236 : : // Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
1237 : : #define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte *)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
1238 : :
1239 : : // Note: dest and slice regions may overlap
1240 : : #define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_sz) \
1241 : : memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \
1242 : : memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), (dest_len - end) * (item_sz));
1243 : :
1244 : : // Note: dest and slice regions may overlap
1245 : : #define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_sz) \
1246 : : memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \
1247 : : memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));
1248 : :
1249 : : // Provide translation for legacy API
1250 : : #define MP_OBJ_IS_SMALL_INT mp_obj_is_small_int
1251 : : #define MP_OBJ_IS_QSTR mp_obj_is_qstr
1252 : : #define MP_OBJ_IS_OBJ mp_obj_is_obj
1253 : : #define MP_OBJ_IS_INT mp_obj_is_int
1254 : : #define MP_OBJ_IS_TYPE mp_obj_is_type
1255 : : #define MP_OBJ_IS_STR mp_obj_is_str
1256 : : #define MP_OBJ_IS_STR_OR_BYTES mp_obj_is_str_or_bytes
1257 : : #define MP_OBJ_IS_FUN mp_obj_is_fun
1258 : : #define MP_MAP_SLOT_IS_FILLED mp_map_slot_is_filled
1259 : : #define MP_SET_SLOT_IS_FILLED mp_set_slot_is_filled
1260 : :
1261 : : #endif // MICROPY_INCLUDED_PY_OBJ_H
|