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