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