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