blob: ba4f4992fffab64c6db939a4fe0c6004f65d3690 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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 */
Alexander Steffen299bc622017-06-29 23:14:58 +020026#ifndef MICROPY_INCLUDED_PY_OBJ_H
27#define MICROPY_INCLUDED_PY_OBJ_H
Damien George9ddbe292014-12-29 01:02:19 +000028
29#include "py/mpconfig.h"
30#include "py/misc.h"
31#include "py/qstr.h"
Damien George7f9d1d62015-04-09 23:56:15 +010032#include "py/mpprint.h"
Damien George58321dd2017-08-29 13:04:01 +100033#include "py/runtime0.h"
Damien George04b91472014-05-03 23:27:38 +010034
Damien George41fceae2016-08-15 10:56:55 +100035// This is the definition of the opaque MicroPython object type.
36// All concrete objects have an encoding within this type and the
37// particular encoding is specified by MICROPY_OBJ_REPR.
Damien Georgeb8cfb0d2015-11-27 17:09:11 +000038#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
39typedef uint64_t mp_obj_t;
40typedef uint64_t mp_const_obj_t;
41#else
Damien George41fceae2016-08-15 10:56:55 +100042typedef void *mp_obj_t;
43typedef const void *mp_const_obj_t;
Damien Georgeb8cfb0d2015-11-27 17:09:11 +000044#endif
Damiend99b0522013-12-21 18:17:45 +000045
Damien George5b3f0b72016-01-03 15:55:55 +000046// This mp_obj_type_t struct is a concrete MicroPython object which holds info
47// about a type. See below for actual definition of the struct.
48typedef struct _mp_obj_type_t mp_obj_type_t;
Damien660365e2013-12-17 18:27:24 +000049
Damien George5b3f0b72016-01-03 15:55:55 +000050// Anything that wants to be a concrete MicroPython object must have mp_obj_base_t
51// as its first member (small ints, qstr objs and inline floats are not concrete).
Damiend99b0522013-12-21 18:17:45 +000052struct _mp_obj_base_t {
Damien George5b3f0b72016-01-03 15:55:55 +000053 const mp_obj_type_t *type MICROPY_OBJ_BASE_ALIGNMENT;
Damiend99b0522013-12-21 18:17:45 +000054};
ian-v7a16fad2014-01-06 09:52:29 -080055typedef struct _mp_obj_base_t mp_obj_base_t;
Damiend99b0522013-12-21 18:17:45 +000056
Damien Georgeea8d06c2014-04-17 23:19:36 +010057// These fake objects are used to indicate certain things in arguments or return
58// values, and should only be used when explicitly allowed.
59//
Damien George6ac5dce2014-05-21 19:42:43 +010060// - MP_OBJ_NULL : used to indicate the absence of an object, or unsupported operation.
Damien Georgeea8d06c2014-04-17 23:19:36 +010061// - MP_OBJ_STOP_ITERATION : used instead of throwing a StopIteration, for efficiency.
62// - MP_OBJ_SENTINEL : used for various internal purposes where one needs
63// an object which is unique from all other objects, including MP_OBJ_NULL.
64//
65// For debugging purposes they are all different. For non-debug mode, we alias
66// as many as we can to MP_OBJ_NULL because it's cheaper to load/compare 0.
Damiend99b0522013-12-21 18:17:45 +000067
Damien George02cc2882019-03-08 15:48:20 +110068#if MICROPY_DEBUG_MP_OBJ_SENTINELS
Damien George999cedb2015-11-27 17:01:44 +000069#define MP_OBJ_NULL (MP_OBJ_FROM_PTR((void*)0))
70#define MP_OBJ_STOP_ITERATION (MP_OBJ_FROM_PTR((void*)4))
71#define MP_OBJ_SENTINEL (MP_OBJ_FROM_PTR((void*)8))
Damien George02cc2882019-03-08 15:48:20 +110072#else
73#define MP_OBJ_NULL (MP_OBJ_FROM_PTR((void*)0))
74#define MP_OBJ_STOP_ITERATION (MP_OBJ_FROM_PTR((void*)0))
75#define MP_OBJ_SENTINEL (MP_OBJ_FROM_PTR((void*)4))
Damien Georgeea8d06c2014-04-17 23:19:36 +010076#endif
Damien George729f7b42014-04-17 22:10:53 +010077
Damien George567184e2015-03-29 14:05:46 +010078// These macros/inline functions operate on objects and depend on the
79// particular object representation. They are used to query, pack and
80// unpack small ints, qstrs and full object pointers.
Damiend99b0522013-12-21 18:17:45 +000081
Damien George567184e2015-03-29 14:05:46 +010082#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
Damien George38a2da62014-01-08 17:33:12 +000083
Damien Georgeeee1e882019-01-30 18:49:52 +110084static inline bool mp_obj_is_small_int(mp_const_obj_t o)
Damien George567184e2015-03-29 14:05:46 +010085 { return ((((mp_int_t)(o)) & 1) != 0); }
Damien George40f3c022014-07-03 13:25:24 +010086#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
Damien George2bddfd42016-04-26 09:51:37 +010087#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
Damien George38a2da62014-01-08 17:33:12 +000088
Damien Georgeeee1e882019-01-30 18:49:52 +110089static inline bool mp_obj_is_qstr(mp_const_obj_t o)
Damien George6f0c83f2020-01-08 23:58:40 +110090 { return ((((mp_int_t)(o)) & 7) == 2); }
91#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
92#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 2))
93
94static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o)
95 { return ((((mp_int_t)(o)) & 7) == 6); }
96#define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 3)
97#define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 3) | 6))
Damiend99b0522013-12-21 18:17:45 +000098
Damien Georgeaedb8592015-10-17 22:57:34 +010099#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgecbf76742015-11-27 13:38:15 +0000100#define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
101#define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
Damien Georgeaedb8592015-10-17 22:57:34 +0100102extern const struct _mp_obj_float_t mp_const_float_e_obj;
103extern const struct _mp_obj_float_t mp_const_float_pi_obj;
104
Damien Georgeeee1e882019-01-30 18:49:52 +1100105#define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
Damien Georgeaedb8592015-10-17 22:57:34 +0100106mp_float_t mp_obj_float_get(mp_obj_t self_in);
107mp_obj_t mp_obj_new_float(mp_float_t value);
108#endif
109
Damien Georgeeee1e882019-01-30 18:49:52 +1100110static inline bool mp_obj_is_obj(mp_const_obj_t o)
Damien George567184e2015-03-29 14:05:46 +0100111 { return ((((mp_int_t)(o)) & 3) == 0); }
112
113#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
114
Damien Georgeeee1e882019-01-30 18:49:52 +1100115static inline bool mp_obj_is_small_int(mp_const_obj_t o)
Damien George567184e2015-03-29 14:05:46 +0100116 { return ((((mp_int_t)(o)) & 3) == 1); }
117#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 2)
Damien George2bddfd42016-04-26 09:51:37 +0100118#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 2) | 1))
Damien George567184e2015-03-29 14:05:46 +0100119
Damien Georgeeee1e882019-01-30 18:49:52 +1100120static inline bool mp_obj_is_qstr(mp_const_obj_t o)
Damien George6f0c83f2020-01-08 23:58:40 +1100121 { return ((((mp_int_t)(o)) & 7) == 3); }
122#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
123#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 3))
124
125static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o)
126 { return ((((mp_int_t)(o)) & 7) == 7); }
127#define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 3)
128#define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 3) | 7))
Damien George567184e2015-03-29 14:05:46 +0100129
Damien Georgeaedb8592015-10-17 22:57:34 +0100130#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgecbf76742015-11-27 13:38:15 +0000131#define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
132#define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
Damien Georgeaedb8592015-10-17 22:57:34 +0100133extern const struct _mp_obj_float_t mp_const_float_e_obj;
134extern const struct _mp_obj_float_t mp_const_float_pi_obj;
135
Damien Georgeeee1e882019-01-30 18:49:52 +1100136#define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
Damien Georgeaedb8592015-10-17 22:57:34 +0100137mp_float_t mp_obj_float_get(mp_obj_t self_in);
138mp_obj_t mp_obj_new_float(mp_float_t value);
139#endif
140
Damien Georgeeee1e882019-01-30 18:49:52 +1100141static inline bool mp_obj_is_obj(mp_const_obj_t o)
Damien George567184e2015-03-29 14:05:46 +0100142 { return ((((mp_int_t)(o)) & 1) == 0); }
143
Damien George183edef2015-10-17 23:20:57 +0100144#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
145
Damien Georgeeee1e882019-01-30 18:49:52 +1100146static inline bool mp_obj_is_small_int(mp_const_obj_t o)
Damien George183edef2015-10-17 23:20:57 +0100147 { return ((((mp_int_t)(o)) & 1) != 0); }
148#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
Damien George2bddfd42016-04-26 09:51:37 +0100149#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
Damien George183edef2015-10-17 23:20:57 +0100150
Nicko van Somerend66c33c2018-07-02 14:52:53 -0600151#if MICROPY_PY_BUILTINS_FLOAT
Damien Georgefe03e7b2015-12-18 21:44:01 +0000152#define mp_const_float_e MP_ROM_PTR((mp_obj_t)(((0x402df854 & ~3) | 2) + 0x80800000))
153#define mp_const_float_pi MP_ROM_PTR((mp_obj_t)(((0x40490fdb & ~3) | 2) + 0x80800000))
Damien George183edef2015-10-17 23:20:57 +0100154
155static inline bool mp_obj_is_float(mp_const_obj_t o)
Damien George8b8d1892015-11-06 23:25:10 +0000156 { return (((mp_uint_t)(o)) & 3) == 2 && (((mp_uint_t)(o)) & 0xff800007) != 0x00000006; }
Damien George183edef2015-10-17 23:20:57 +0100157static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
158 union {
159 mp_float_t f;
160 mp_uint_t u;
Damien George8b8d1892015-11-06 23:25:10 +0000161 } num = {.u = ((mp_uint_t)o - 0x80800000) & ~3};
Damien George183edef2015-10-17 23:20:57 +0100162 return num.f;
163}
164static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
165 union {
166 mp_float_t f;
167 mp_uint_t u;
168 } num = {.f = f};
Damien George8b8d1892015-11-06 23:25:10 +0000169 return (mp_obj_t)(((num.u & ~0x3) | 2) + 0x80800000);
Damien George183edef2015-10-17 23:20:57 +0100170}
Nicko van Somerend66c33c2018-07-02 14:52:53 -0600171#endif
Damien George183edef2015-10-17 23:20:57 +0100172
Damien Georgeeee1e882019-01-30 18:49:52 +1100173static inline bool mp_obj_is_qstr(mp_const_obj_t o)
Damien George6f0c83f2020-01-08 23:58:40 +1100174 { return (((mp_uint_t)(o)) & 0xff80000f) == 0x00000006; }
175#define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 4)
176#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 4) | 0x00000006))
177
178static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o)
179 { return (((mp_uint_t)(o)) & 0xff80000f) == 0x0000000e; }
180#define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 4)
181#define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 4) | 0xe))
Damien George183edef2015-10-17 23:20:57 +0100182
Damien Georgeeee1e882019-01-30 18:49:52 +1100183static inline bool mp_obj_is_obj(mp_const_obj_t o)
Damien George183edef2015-10-17 23:20:57 +0100184 { return ((((mp_int_t)(o)) & 3) == 0); }
185
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000186#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
187
Damien Georgeeee1e882019-01-30 18:49:52 +1100188static inline bool mp_obj_is_small_int(mp_const_obj_t o)
Damien George69e79032018-10-01 16:36:46 +1000189 { return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0001000000000000); }
Damien George2759bec2017-12-11 22:39:12 +1100190#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)((o) << 16)) >> 17)
191#define MP_OBJ_NEW_SMALL_INT(small_int) (((((uint64_t)(small_int)) & 0x7fffffffffff) << 1) | 0x0001000000000001)
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000192
Damien Georgeeee1e882019-01-30 18:49:52 +1100193static inline bool mp_obj_is_qstr(mp_const_obj_t o)
Damien George69e79032018-10-01 16:36:46 +1000194 { return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0002000000000000); }
Damien George1fa6be52016-01-08 13:43:13 +0000195#define MP_OBJ_QSTR_VALUE(o) ((((uint32_t)(o)) >> 1) & 0xffffffff)
Damien Georged56bc6e2019-12-27 23:33:34 +1100196#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)(((uint64_t)(((uint32_t)(qst)) << 1)) | 0x0002000000000001))
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000197
Damien George6f0c83f2020-01-08 23:58:40 +1100198static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o)
199 { return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0003000000000000); }
200#define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) ((((uint32_t)(o)) >> 46) & 3)
201#define MP_OBJ_NEW_IMMEDIATE_OBJ(val) (((uint64_t)(val) << 46) | 0x0003000000000000)
202
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000203#if MICROPY_PY_BUILTINS_FLOAT
Damien George4a1edd82018-07-08 23:45:05 +1000204
205#if MICROPY_FLOAT_IMPL != MICROPY_FLOAT_IMPL_DOUBLE
206#error MICROPY_OBJ_REPR_D requires MICROPY_FLOAT_IMPL_DOUBLE
207#endif
208
Damien George6b4b6d32018-05-01 23:25:18 +1000209#define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b145769 + 0x8004000000000000))}
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000210#define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))}
211
212static inline bool mp_obj_is_float(mp_const_obj_t o) {
213 return ((uint64_t)(o) & 0xfffc000000000000) != 0;
214}
215static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
216 union {
217 mp_float_t f;
218 uint64_t r;
219 } num = {.r = o - 0x8004000000000000};
220 return num.f;
221}
222static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
223 union {
224 mp_float_t f;
225 uint64_t r;
226 } num = {.f = f};
227 return num.r + 0x8004000000000000;
228}
229#endif
230
Damien Georgeeee1e882019-01-30 18:49:52 +1100231static inline bool mp_obj_is_obj(mp_const_obj_t o)
Damien Georgeb8cfb0d2015-11-27 17:09:11 +0000232 { return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0000000000000000); }
233#define MP_OBJ_TO_PTR(o) ((void*)(uintptr_t)(o))
234#define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p)))
235
236// rom object storage needs special handling to widen 32-bit pointer to 64-bits
237typedef union _mp_rom_obj_t { uint64_t u64; struct { const void *lo, *hi; } u32; } mp_rom_obj_t;
238#define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
239#define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
240#if MP_ENDIANNESS_LITTLE
241#define MP_ROM_PTR(p) {.u32 = {.lo = (p), .hi = NULL}}
242#else
243#define MP_ROM_PTR(p) {.u32 = {.lo = NULL, .hi = (p)}}
244#endif
245
Damien George567184e2015-03-29 14:05:46 +0100246#endif
247
Paul Sokolovsky3d598252015-03-25 09:25:41 +0200248// Macros to convert between mp_obj_t and concrete object types.
249// These are identity operations in MicroPython, but ability to override
250// these operations are provided to experiment with other methods of
251// object representation and memory management.
252
253// Cast mp_obj_t to object pointer
Damien George999cedb2015-11-27 17:01:44 +0000254#ifndef MP_OBJ_TO_PTR
255#define MP_OBJ_TO_PTR(o) ((void*)o)
Paul Sokolovsky3d598252015-03-25 09:25:41 +0200256#endif
257
258// Cast object pointer to mp_obj_t
Damien George999cedb2015-11-27 17:01:44 +0000259#ifndef MP_OBJ_FROM_PTR
260#define MP_OBJ_FROM_PTR(p) ((mp_obj_t)p)
Paul Sokolovsky3d598252015-03-25 09:25:41 +0200261#endif
262
Damien Georgecbf76742015-11-27 13:38:15 +0000263// Macros to create objects that are stored in ROM.
264
Damien George09376f02019-12-16 15:40:05 +1100265#ifndef MP_ROM_NONE
Damien Georged96cfd12020-01-09 00:00:27 +1100266#if MICROPY_OBJ_IMMEDIATE_OBJS
267#define MP_ROM_NONE mp_const_none
268#else
Damien George09376f02019-12-16 15:40:05 +1100269#define MP_ROM_NONE MP_ROM_PTR(&mp_const_none_obj)
270#endif
Damien Georged96cfd12020-01-09 00:00:27 +1100271#endif
Damien George09376f02019-12-16 15:40:05 +1100272
Damien Georged97b40b2019-12-16 15:42:17 +1100273#ifndef MP_ROM_FALSE
Damien Georged96cfd12020-01-09 00:00:27 +1100274#if MICROPY_OBJ_IMMEDIATE_OBJS
275#define MP_ROM_FALSE mp_const_false
276#define MP_ROM_TRUE mp_const_true
277#else
Damien Georged97b40b2019-12-16 15:42:17 +1100278#define MP_ROM_FALSE MP_ROM_PTR(&mp_const_false_obj)
279#define MP_ROM_TRUE MP_ROM_PTR(&mp_const_true_obj)
280#endif
Damien Georged96cfd12020-01-09 00:00:27 +1100281#endif
Damien Georged97b40b2019-12-16 15:42:17 +1100282
Damien Georgecbf76742015-11-27 13:38:15 +0000283#ifndef MP_ROM_INT
284typedef mp_const_obj_t mp_rom_obj_t;
285#define MP_ROM_INT(i) MP_OBJ_NEW_SMALL_INT(i)
286#define MP_ROM_QSTR(q) MP_OBJ_NEW_QSTR(q)
287#define MP_ROM_PTR(p) (p)
288/* for testing
289typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
290#define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
291#define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
292#define MP_ROM_PTR(p) {.o = p}
293*/
294#endif
295
Damiend99b0522013-12-21 18:17:45 +0000296// These macros are used to declare and define constant function objects
297// You can put "static" in front of the definitions to make them local
298
Damien George571e6f22016-10-18 11:49:27 +1100299#define MP_DECLARE_CONST_FUN_OBJ_0(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
300#define MP_DECLARE_CONST_FUN_OBJ_1(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
301#define MP_DECLARE_CONST_FUN_OBJ_2(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
302#define MP_DECLARE_CONST_FUN_OBJ_3(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
303#define MP_DECLARE_CONST_FUN_OBJ_VAR(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
304#define MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
305#define MP_DECLARE_CONST_FUN_OBJ_KW(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
Damiend99b0522013-12-21 18:17:45 +0000306
Damien George9f241ef2018-09-14 13:39:17 +1000307#define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
Damien George5089b3f2019-01-25 16:03:05 +1100308#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)))
Damien George9f241ef2018-09-14 13:39:17 +1000309
Damien George1b0aab62016-01-03 11:53:44 +0000310#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
Damien George571e6f22016-10-18 11:49:27 +1100311 const mp_obj_fun_builtin_fixed_t obj_name = \
312 {{&mp_type_fun_builtin_0}, .fun._0 = fun_name}
Damien George1b0aab62016-01-03 11:53:44 +0000313#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
Damien George571e6f22016-10-18 11:49:27 +1100314 const mp_obj_fun_builtin_fixed_t obj_name = \
315 {{&mp_type_fun_builtin_1}, .fun._1 = fun_name}
Damien George1b0aab62016-01-03 11:53:44 +0000316#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
Damien George571e6f22016-10-18 11:49:27 +1100317 const mp_obj_fun_builtin_fixed_t obj_name = \
318 {{&mp_type_fun_builtin_2}, .fun._2 = fun_name}
Damien George1b0aab62016-01-03 11:53:44 +0000319#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
Damien George571e6f22016-10-18 11:49:27 +1100320 const mp_obj_fun_builtin_fixed_t obj_name = \
321 {{&mp_type_fun_builtin_3}, .fun._3 = fun_name}
Damien George1b0aab62016-01-03 11:53:44 +0000322#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
Damien George571e6f22016-10-18 11:49:27 +1100323 const mp_obj_fun_builtin_var_t obj_name = \
Damien George9f241ef2018-09-14 13:39:17 +1000324 {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, false), .fun.var = fun_name}
Damien George1b0aab62016-01-03 11:53:44 +0000325#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
Damien George571e6f22016-10-18 11:49:27 +1100326 const mp_obj_fun_builtin_var_t obj_name = \
Damien George9f241ef2018-09-14 13:39:17 +1000327 {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, false), .fun.var = fun_name}
Damien George1b0aab62016-01-03 11:53:44 +0000328#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
Damien George571e6f22016-10-18 11:49:27 +1100329 const mp_obj_fun_builtin_var_t obj_name = \
Damien George9f241ef2018-09-14 13:39:17 +1000330 {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, true), .fun.kw = fun_name}
John R. Lentonc06763a2014-01-07 17:29:16 +0000331
Damien George78d702c2014-12-09 16:19:48 +0000332// These macros are used to define constant map/dict objects
Damien George9b196cd2014-03-26 21:47:19 +0000333// You can put "static" in front of the definition to make it local
334
Damien George78d702c2014-12-09 16:19:48 +0000335#define MP_DEFINE_CONST_MAP(map_name, table_name) \
336 const mp_map_t map_name = { \
337 .all_keys_are_qstrs = 1, \
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200338 .is_fixed = 1, \
339 .is_ordered = 1, \
Damien George78d702c2014-12-09 16:19:48 +0000340 .used = MP_ARRAY_SIZE(table_name), \
341 .alloc = MP_ARRAY_SIZE(table_name), \
Damien Georgecbf76742015-11-27 13:38:15 +0000342 .table = (mp_map_elem_t*)(mp_rom_map_elem_t*)table_name, \
Damien George78d702c2014-12-09 16:19:48 +0000343 }
344
Damien George9b196cd2014-03-26 21:47:19 +0000345#define MP_DEFINE_CONST_DICT(dict_name, table_name) \
346 const mp_obj_dict_t dict_name = { \
Damien George3e1a5c12014-03-29 13:43:38 +0000347 .base = {&mp_type_dict}, \
Damien George9b196cd2014-03-26 21:47:19 +0000348 .map = { \
349 .all_keys_are_qstrs = 1, \
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200350 .is_fixed = 1, \
351 .is_ordered = 1, \
Damien George3b603f22014-11-29 14:39:27 +0000352 .used = MP_ARRAY_SIZE(table_name), \
353 .alloc = MP_ARRAY_SIZE(table_name), \
Damien Georgecbf76742015-11-27 13:38:15 +0000354 .table = (mp_map_elem_t*)(mp_rom_map_elem_t*)table_name, \
Damien George9b196cd2014-03-26 21:47:19 +0000355 }, \
356 }
357
Damien Georgeeae16442014-01-11 19:22:29 +0000358// These macros are used to declare and define constant staticmethond and classmethod objects
359// You can put "static" in front of the definitions to make them local
360
Damien Georgecbf76742015-11-27 13:38:15 +0000361#define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
362#define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
Damien Georgeeae16442014-01-11 19:22:29 +0000363
Damien Georgecbf76742015-11-27 13:38:15 +0000364#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}
365#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}
Damien Georgeeae16442014-01-11 19:22:29 +0000366
Andrew Leechcf22f472019-02-18 14:58:44 +1100367// Declare a module as a builtin, processed by makemoduledefs.py
368// param module_name: MP_QSTR_<module name>
369// param obj_module: mp_obj_module_t instance
370// prarm enabled_define: used as `#if (enabled_define) around entry`
371
372#define MP_REGISTER_MODULE(module_name, obj_module, enabled_define)
373
Damien Georgedf6567e2014-03-30 13:54:02 +0100374// Underlying map/hash table implementation (not dict object or map function)
375
376typedef struct _mp_map_elem_t {
377 mp_obj_t key;
378 mp_obj_t value;
379} mp_map_elem_t;
380
Damien Georgecbf76742015-11-27 13:38:15 +0000381typedef struct _mp_rom_map_elem_t {
382 mp_rom_obj_t key;
383 mp_rom_obj_t value;
384} mp_rom_map_elem_t;
385
Damien Georgedf6567e2014-03-30 13:54:02 +0100386// TODO maybe have a truncated mp_map_t for fixed tables, since alloc=used
387// put alloc last in the structure, so the truncated version does not need it
388// this would save 1 ROM word for all ROM objects that have a locals_dict
389// would also need a trucated dict structure
390
391typedef struct _mp_map_t {
Damien Georgeaf622eb2017-02-08 11:00:15 +1100392 size_t all_keys_are_qstrs : 1;
393 size_t is_fixed : 1; // a fixed array that can't be modified; must also be ordered
394 size_t is_ordered : 1; // an ordered array
395 size_t used : (8 * sizeof(size_t) - 3);
396 size_t alloc;
Damien Georgedf6567e2014-03-30 13:54:02 +0100397 mp_map_elem_t *table;
398} mp_map_t;
399
Damien Georged1cee022015-03-20 17:41:37 +0000400// mp_set_lookup requires these constants to have the values they do
Damien Georgedf6567e2014-03-30 13:54:02 +0100401typedef enum _mp_map_lookup_kind_t {
Damien Georged1cee022015-03-20 17:41:37 +0000402 MP_MAP_LOOKUP = 0,
403 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND = 1,
404 MP_MAP_LOOKUP_REMOVE_IF_FOUND = 2,
405 MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND = 3, // only valid for mp_set_lookup
Damien Georgedf6567e2014-03-30 13:54:02 +0100406} mp_map_lookup_kind_t;
407
Paul Sokolovskye5dbe1e2014-11-26 21:17:16 +0200408extern const mp_map_t mp_const_empty_map;
409
Damien George054dd332019-01-30 21:57:29 +1100410static inline bool mp_map_slot_is_filled(const mp_map_t *map, size_t pos) { return ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL); }
Damien George8b0535e2014-04-05 21:53:54 +0100411
Damien Georgeaf622eb2017-02-08 11:00:15 +1100412void mp_map_init(mp_map_t *map, size_t n);
413void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table);
414mp_map_t *mp_map_new(size_t n);
Damien Georgedf6567e2014-03-30 13:54:02 +0100415void mp_map_deinit(mp_map_t *map);
416void mp_map_free(mp_map_t *map);
Damien George2801e6f2015-04-04 15:53:11 +0100417mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
Damien Georgedf6567e2014-03-30 13:54:02 +0100418void mp_map_clear(mp_map_t *map);
Paul Sokolovskye3f58c82014-04-05 04:14:22 +0300419void mp_map_dump(mp_map_t *map);
Damien Georgedf6567e2014-03-30 13:54:02 +0100420
421// Underlying set implementation (not set object)
422
423typedef struct _mp_set_t {
Damien Georgeaf622eb2017-02-08 11:00:15 +1100424 size_t alloc;
425 size_t used;
Damien Georgedf6567e2014-03-30 13:54:02 +0100426 mp_obj_t *table;
427} mp_set_t;
428
Damien George054dd332019-01-30 21:57:29 +1100429static inline bool mp_set_slot_is_filled(const mp_set_t *set, size_t pos) { return ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL); }
Damien George8b0535e2014-04-05 21:53:54 +0100430
Damien Georgeaf622eb2017-02-08 11:00:15 +1100431void mp_set_init(mp_set_t *set, size_t n);
Damien Georgedf6567e2014-03-30 13:54:02 +0100432mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
Damien George95004e52014-04-05 17:17:19 +0100433mp_obj_t mp_set_remove_first(mp_set_t *set);
Damien Georgedf6567e2014-03-30 13:54:02 +0100434void mp_set_clear(mp_set_t *set);
Damiend99b0522013-12-21 18:17:45 +0000435
436// Type definitions for methods
437
438typedef mp_obj_t (*mp_fun_0_t)(void);
439typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
440typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
John R. Lenton45a87442014-01-04 01:15:01 +0000441typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
Damien George4b72b3a2016-01-03 14:21:40 +0000442typedef mp_obj_t (*mp_fun_var_t)(size_t n, const mp_obj_t *);
Paul Sokolovsky069654f2016-04-04 15:35:44 +0300443// mp_fun_kw_t takes mp_map_t* (and not const mp_map_t*) to ease passing
444// this arg to mp_map_lookup().
Damien George4b72b3a2016-01-03 14:21:40 +0000445typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *);
Damiend99b0522013-12-21 18:17:45 +0000446
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200447typedef enum {
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300448 PRINT_STR = 0,
449 PRINT_REPR = 1,
450 PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
Damien George612045f2014-09-17 22:56:34 +0100451 PRINT_JSON = 3,
Paul Sokolovskyef63ab52015-12-20 16:44:36 +0200452 PRINT_RAW = 4, // Special format for printing bytes as an undercorated string
Damien George612045f2014-09-17 22:56:34 +0100453 PRINT_EXC_SUBCLASS = 0x80, // Internal flag for printing exception subclasses
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200454} mp_print_kind_t;
455
Damien Georgeae8d8672016-01-09 23:14:54 +0000456typedef struct _mp_obj_iter_buf_t {
457 mp_obj_base_t base;
458 mp_obj_t buf[3];
459} mp_obj_iter_buf_t;
460
Damien George60656ea2017-03-23 16:36:08 +1100461// The number of slots that an mp_obj_iter_buf_t needs on the Python value stack.
462// It's rounded up in case mp_obj_base_t is smaller than mp_obj_t (eg for OBJ_REPR_D).
463#define MP_OBJ_ITER_BUF_NSLOTS ((sizeof(mp_obj_iter_buf_t) + sizeof(mp_obj_t) - 1) / sizeof(mp_obj_t))
464
Damien George7f9d1d62015-04-09 23:56:15 +0100465typedef void (*mp_print_fun_t)(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind);
Damien George5b3f0b72016-01-03 15:55:55 +0000466typedef 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);
Damien Georgea0c97812016-01-03 09:59:18 +0000467typedef mp_obj_t (*mp_call_fun_t)(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *args);
Damien George58321dd2017-08-29 13:04:01 +1000468typedef mp_obj_t (*mp_unary_op_fun_t)(mp_unary_op_t op, mp_obj_t);
469typedef mp_obj_t (*mp_binary_op_fun_t)(mp_binary_op_t op, mp_obj_t, mp_obj_t);
Damien Georgeb1bbe962015-04-01 14:10:50 +0000470typedef void (*mp_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
Damien George729f7b42014-04-17 22:10:53 +0100471typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
Damien Georgeae8d8672016-01-09 23:14:54 +0000472typedef mp_obj_t (*mp_getiter_fun_t)(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf);
Damiend99b0522013-12-21 18:17:45 +0000473
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200474// Buffer protocol
Damien George57a4b4f2014-04-18 22:29:21 +0100475typedef struct _mp_buffer_info_t {
Damien George42f3de92014-10-03 17:44:14 +0000476 void *buf; // can be NULL if len == 0
Damien George0d1f8862016-03-15 12:20:57 +0000477 size_t len; // in bytes
Damien George42f3de92014-10-03 17:44:14 +0000478 int typecode; // as per binary.h
Damien George57a4b4f2014-04-18 22:29:21 +0100479} mp_buffer_info_t;
480#define MP_BUFFER_READ (1)
481#define MP_BUFFER_WRITE (2)
482#define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200483typedef struct _mp_buffer_p_t {
Damien George4d917232014-08-30 14:28:06 +0100484 mp_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200485} mp_buffer_p_t;
Damien George4d917232014-08-30 14:28:06 +0100486bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
487void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200488
Damiend99b0522013-12-21 18:17:45 +0000489struct _mp_obj_type_t {
Damien Georgefc710162017-04-06 10:25:32 +1000490 // A type is an object so must start with this entry, which points to mp_type_type.
Damiend99b0522013-12-21 18:17:45 +0000491 mp_obj_base_t base;
Damien Georgefc710162017-04-06 10:25:32 +1000492
Damien Georgedb5d8c92018-05-25 16:48:19 +1000493 // Flags associated with this type.
494 uint16_t flags;
495
496 // The name of this type, a qstr.
497 uint16_t name;
Damien Georgefc710162017-04-06 10:25:32 +1000498
499 // Corresponds to __repr__ and __str__ special methods.
Damiend99b0522013-12-21 18:17:45 +0000500 mp_print_fun_t print;
501
Damien Georgefc710162017-04-06 10:25:32 +1000502 // Corresponds to __new__ and __init__ special methods, to make an instance of the type.
503 mp_make_new_fun_t make_new;
504
505 // Corresponds to __call__ special method, ie T(...).
Damien George20006db2014-01-18 14:10:48 +0000506 mp_call_fun_t call;
Damiend99b0522013-12-21 18:17:45 +0000507
Damien Georgefc710162017-04-06 10:25:32 +1000508 // Implements unary and binary operations.
509 // Can return MP_OBJ_NULL if the operation is not supported.
510 mp_unary_op_fun_t unary_op;
511 mp_binary_op_fun_t binary_op;
512
513 // Implements load, store and delete attribute.
Damien Georgeb1bbe962015-04-01 14:10:50 +0000514 //
515 // dest[0] = MP_OBJ_NULL means load
516 // return: for fail, do nothing
517 // for attr, dest[0] = value
518 // for method, dest[0] = method, dest[1] = self
519 //
520 // dest[0,1] = {MP_OBJ_SENTINEL, MP_OBJ_NULL} means delete
521 // dest[0,1] = {MP_OBJ_SENTINEL, object} means store
522 // return: for fail, do nothing
523 // for success set dest[0] = MP_OBJ_NULL
524 mp_attr_fun_t attr;
Damien Georgef4c9b332014-04-08 21:32:29 +0100525
Damien Georgefc710162017-04-06 10:25:32 +1000526 // Implements load, store and delete subscripting:
527 // - value = MP_OBJ_SENTINEL means load
528 // - value = MP_OBJ_NULL means delete
529 // - all other values mean store the value
530 // Can return MP_OBJ_NULL if operation not supported.
531 mp_subscr_fun_t subscr;
Damien Georgea71c83a2014-02-15 11:34:50 +0000532
Damien Georgefc710162017-04-06 10:25:32 +1000533 // Corresponds to __iter__ special method.
534 // Can use the given mp_obj_iter_buf_t to store iterator object,
535 // otherwise can return a pointer to an object on the heap.
Damien Georgeae8d8672016-01-09 23:14:54 +0000536 mp_getiter_fun_t getiter;
537
Damien Georgefc710162017-04-06 10:25:32 +1000538 // Corresponds to __next__ special method. May return MP_OBJ_STOP_ITERATION
539 // as an optimisation instead of raising StopIteration() with no args.
540 mp_fun_1_t iternext;
Damiend99b0522013-12-21 18:17:45 +0000541
Damien Georgefc710162017-04-06 10:25:32 +1000542 // Implements the buffer protocol if supported by this type.
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200543 mp_buffer_p_t buffer_p;
Damien Georgefc710162017-04-06 10:25:32 +1000544
Paul Sokolovsky07209f82016-06-18 18:19:24 +0300545 // One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
546 const void *protocol;
Paul Sokolovsky5b15daf2014-01-07 20:12:26 +0200547
Damien George816413e2017-04-06 12:09:01 +1000548 // A pointer to the parents of this type:
549 // - 0 parents: pointer is NULL (object is implicitly the single parent)
550 // - 1 parent: a pointer to the type of that parent
551 // - 2 or more parents: pointer to a tuple object containing the parent types
552 const void *parent;
Damien Georgefc710162017-04-06 10:25:32 +1000553
554 // A dict mapping qstrs to objects local methods/constants/etc.
Damien George999cedb2015-11-27 17:01:44 +0000555 struct _mp_obj_dict_t *locals_dict;
Damiend99b0522013-12-21 18:17:45 +0000556};
557
Damien Georgec5966122014-02-15 16:10:44 +0000558// Constant types, globally accessible
Damien Georgec5966122014-02-15 16:10:44 +0000559extern const mp_obj_type_t mp_type_type;
Damien George3e1a5c12014-03-29 13:43:38 +0000560extern const mp_obj_type_t mp_type_object;
561extern const mp_obj_type_t mp_type_NoneType;
562extern const mp_obj_type_t mp_type_bool;
563extern const mp_obj_type_t mp_type_int;
564extern const mp_obj_type_t mp_type_str;
565extern const mp_obj_type_t mp_type_bytes;
Paul Sokolovsky4dcb6052014-04-08 22:09:14 +0300566extern const mp_obj_type_t mp_type_bytearray;
Damien Georgedd4f4532014-10-23 13:34:35 +0100567extern const mp_obj_type_t mp_type_memoryview;
Damien George3e1a5c12014-03-29 13:43:38 +0000568extern const mp_obj_type_t mp_type_float;
569extern const mp_obj_type_t mp_type_complex;
570extern const mp_obj_type_t mp_type_tuple;
571extern const mp_obj_type_t mp_type_list;
572extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict implementation detail)
573extern const mp_obj_type_t mp_type_enumerate;
574extern const mp_obj_type_t mp_type_filter;
Paul Sokolovsky970eedc2018-02-06 00:06:42 +0200575extern const mp_obj_type_t mp_type_deque;
Damien George3e1a5c12014-03-29 13:43:38 +0000576extern const mp_obj_type_t mp_type_dict;
Paul Sokolovsky0ef01d02015-03-18 01:25:04 +0200577extern const mp_obj_type_t mp_type_ordereddict;
Damien George71d31122014-04-17 18:18:55 +0100578extern const mp_obj_type_t mp_type_range;
Damien George3e1a5c12014-03-29 13:43:38 +0000579extern const mp_obj_type_t mp_type_set;
Paul Sokolovskyb181b582014-05-10 16:02:17 +0300580extern const mp_obj_type_t mp_type_frozenset;
Damien George3e1a5c12014-03-29 13:43:38 +0000581extern const mp_obj_type_t mp_type_slice;
582extern const mp_obj_type_t mp_type_zip;
583extern const mp_obj_type_t mp_type_array;
584extern const mp_obj_type_t mp_type_super;
Damien Georgeb488a4a2018-06-29 16:32:58 +1000585extern const mp_obj_type_t mp_type_gen_wrap;
Damien Georgecc2bd632018-10-01 13:07:04 +1000586extern const mp_obj_type_t mp_type_native_gen_wrap;
Damien George3e1a5c12014-03-29 13:43:38 +0000587extern const mp_obj_type_t mp_type_gen_instance;
Damien George571e6f22016-10-18 11:49:27 +1100588extern const mp_obj_type_t mp_type_fun_builtin_0;
589extern const mp_obj_type_t mp_type_fun_builtin_1;
590extern const mp_obj_type_t mp_type_fun_builtin_2;
591extern const mp_obj_type_t mp_type_fun_builtin_3;
592extern const mp_obj_type_t mp_type_fun_builtin_var;
Damien George3e1a5c12014-03-29 13:43:38 +0000593extern const mp_obj_type_t mp_type_fun_bc;
594extern const mp_obj_type_t mp_type_module;
595extern const mp_obj_type_t mp_type_staticmethod;
596extern const mp_obj_type_t mp_type_classmethod;
Damien George777b0f32014-04-13 18:59:45 +0100597extern const mp_obj_type_t mp_type_property;
Paul Sokolovskycb9dc082014-04-26 20:26:14 +0300598extern const mp_obj_type_t mp_type_stringio;
Paul Sokolovskya47b64a2014-05-15 07:28:19 +0300599extern const mp_obj_type_t mp_type_bytesio;
Damien George4c03b3a2014-08-12 18:33:40 +0100600extern const mp_obj_type_t mp_type_reversed;
Paul Sokolovsky1a1ccea2015-12-14 23:48:12 +0200601extern const mp_obj_type_t mp_type_polymorph_iter;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000602
603// Exceptions
Damien Georgec5966122014-02-15 16:10:44 +0000604extern const mp_obj_type_t mp_type_BaseException;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000605extern const mp_obj_type_t mp_type_ArithmeticError;
606extern const mp_obj_type_t mp_type_AssertionError;
607extern const mp_obj_type_t mp_type_AttributeError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000608extern const mp_obj_type_t mp_type_EOFError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000609extern const mp_obj_type_t mp_type_Exception;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000610extern const mp_obj_type_t mp_type_GeneratorExit;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000611extern const mp_obj_type_t mp_type_ImportError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000612extern const mp_obj_type_t mp_type_IndentationError;
613extern const mp_obj_type_t mp_type_IndexError;
Damien George124df6f2014-10-25 18:19:55 +0100614extern const mp_obj_type_t mp_type_KeyboardInterrupt;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000615extern const mp_obj_type_t mp_type_KeyError;
616extern const mp_obj_type_t mp_type_LookupError;
617extern const mp_obj_type_t mp_type_MemoryError;
618extern const mp_obj_type_t mp_type_NameError;
619extern const mp_obj_type_t mp_type_NotImplementedError;
620extern const mp_obj_type_t mp_type_OSError;
Daniel Campora077812b2015-06-29 22:45:39 +0200621extern const mp_obj_type_t mp_type_TimeoutError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000622extern const mp_obj_type_t mp_type_OverflowError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000623extern const mp_obj_type_t mp_type_RuntimeError;
pohmelie81ebba72016-01-27 23:23:11 +0300624extern const mp_obj_type_t mp_type_StopAsyncIteration;
Damien Georgeffb5cfc2014-03-25 14:29:40 +0000625extern const mp_obj_type_t mp_type_StopIteration;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000626extern const mp_obj_type_t mp_type_SyntaxError;
Damien George7a4ddd22014-05-24 23:32:19 +0100627extern const mp_obj_type_t mp_type_SystemExit;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000628extern const mp_obj_type_t mp_type_TypeError;
Paul Sokolovsky71ebd4b2015-02-23 23:18:36 +0200629extern const mp_obj_type_t mp_type_UnicodeError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000630extern const mp_obj_type_t mp_type_ValueError;
Damien Georgec8b60f02015-04-20 13:29:31 +0000631extern const mp_obj_type_t mp_type_ViperTypeError;
Rachel Dowdall721c55d2014-03-22 15:28:16 +0000632extern const mp_obj_type_t mp_type_ZeroDivisionError;
633
Damien Georged96cfd12020-01-09 00:00:27 +1100634// Constant objects, globally accessible: None, False, True
635// These should always be accessed via the below macros.
636#if MICROPY_OBJ_IMMEDIATE_OBJS
637// None is even while False/True are odd so their types can be distinguished with 1 bit.
638#define mp_const_none MP_OBJ_NEW_IMMEDIATE_OBJ(0)
639#define mp_const_false MP_OBJ_NEW_IMMEDIATE_OBJ(1)
640#define mp_const_true MP_OBJ_NEW_IMMEDIATE_OBJ(3)
641#else
Damien George999cedb2015-11-27 17:01:44 +0000642#define mp_const_none (MP_OBJ_FROM_PTR(&mp_const_none_obj))
643#define mp_const_false (MP_OBJ_FROM_PTR(&mp_const_false_obj))
644#define mp_const_true (MP_OBJ_FROM_PTR(&mp_const_true_obj))
Damien George07ddab52014-03-29 13:15:08 +0000645extern const struct _mp_obj_none_t mp_const_none_obj;
646extern const struct _mp_obj_bool_t mp_const_false_obj;
647extern const struct _mp_obj_bool_t mp_const_true_obj;
Damien Georged96cfd12020-01-09 00:00:27 +1100648#endif
649
650// Constant objects, globally accessible: b'', (), Ellipsis, NotImplemented, GeneratorExit()
651// The below macros are for convenience only.
652#define mp_const_empty_bytes (MP_OBJ_FROM_PTR(&mp_const_empty_bytes_obj))
653#define mp_const_empty_tuple (MP_OBJ_FROM_PTR(&mp_const_empty_tuple_obj))
654#define mp_const_notimplemented (MP_OBJ_FROM_PTR(&mp_const_notimplemented_obj))
Damien George20f59e12014-10-11 17:56:43 +0100655extern const struct _mp_obj_str_t mp_const_empty_bytes_obj;
Damien George07ddab52014-03-29 13:15:08 +0000656extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
Paul Sokolovsky76677272015-05-05 22:18:07 +0300657extern const struct _mp_obj_singleton_t mp_const_ellipsis_obj;
658extern const struct _mp_obj_singleton_t mp_const_notimplemented_obj;
Damien George07ddab52014-03-29 13:15:08 +0000659extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
Damiend99b0522013-12-21 18:17:45 +0000660
Damiend99b0522013-12-21 18:17:45 +0000661// General API for objects
662
Damien Georgeeee1e882019-01-30 18:49:52 +1100663// These macros are derived from more primitive ones and are used to
664// check for more specific object types.
665// Note: these are kept as macros because inline functions sometimes use much
666// more code space than the equivalent macros, depending on the compiler.
667#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
668#define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_type(o, &mp_type_int))
669#define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_type(o, &mp_type_str))
670#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))
671#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))
672
Damien Georgea71c83a2014-02-15 11:34:50 +0000673mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
Paul Sokolovsky1b586f32015-10-11 12:09:43 +0300674static inline mp_obj_t mp_obj_new_bool(mp_int_t x) { return x ? mp_const_true : mp_const_false; }
Damien George6baf76e2013-12-30 22:32:17 +0000675mp_obj_t mp_obj_new_cell(mp_obj_t obj);
Damien George40f3c022014-07-03 13:25:24 +0100676mp_obj_t mp_obj_new_int(mp_int_t value);
677mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
Damien Georgeda36f522017-02-16 16:41:43 +1100678mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base);
Damien George9d68e9c2014-03-12 15:38:15 +0000679mp_obj_t mp_obj_new_int_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
Damien George95307432014-09-10 22:10:33 +0100680mp_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)
Damien George46017592017-11-16 13:17:51 +1100681mp_obj_t mp_obj_new_str(const char* data, size_t len);
682mp_obj_t mp_obj_new_str_via_qstr(const char* data, size_t len);
Damien George0b9ee862015-01-21 19:14:25 +0000683mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr);
Damien Georgec0d95002017-02-16 16:26:48 +1100684mp_obj_t mp_obj_new_bytes(const byte* data, size_t len);
Damien Georgeccc52542017-02-16 16:31:43 +1100685mp_obj_t mp_obj_new_bytearray(size_t n, void *items);
686mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items);
Damien Georgefb510b32014-06-01 13:32:54 +0100687#if MICROPY_PY_BUILTINS_FLOAT
Paul Sokolovsky5f680942014-12-30 00:34:54 +0200688mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
Damiend99b0522013-12-21 18:17:45 +0000689mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
690#endif
Damien Georgec5966122014-02-15 16:10:44 +0000691mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
Paul Sokolovskydec31bb2014-04-22 00:01:13 +0300692mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
Damien Georgefa5a5912017-02-16 16:38:14 +1100693mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args);
Damien Georgec5966122014-02-15 16:10:44 +0000694mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg);
695mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
Damien George713ea182015-10-23 01:23:11 +0100696mp_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);
697mp_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);
Damien George7bc71f52019-02-20 13:08:42 +1100698mp_obj_t mp_obj_new_fun_asm(size_t n_args, const void *fun_data, mp_uint_t type_sig);
Damien Georged0691cc2014-01-29 20:30:52 +0000699mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
Damien Georgeefa62902017-02-16 16:36:04 +1100700mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed, const mp_obj_t *closed);
Damien George22982392017-02-16 16:09:51 +1100701mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
Damien George58d9eeb2017-02-16 16:12:41 +1100702mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);
Damien George1ea2f7a2017-02-16 16:15:04 +1100703mp_obj_t mp_obj_new_dict(size_t n_args);
Damien George68cd3a92017-02-16 16:16:33 +1100704mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items);
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200705mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
Damien George20006db2014-01-18 14:10:48 +0000706mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
Damien Georgeae8d8672016-01-09 23:14:54 +0000707mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf);
Damien George28708622014-01-02 21:30:26 +0000708mp_obj_t mp_obj_new_module(qstr module_name);
Damien Georgeccc52542017-02-16 16:31:43 +1100709mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items);
Damiend99b0522013-12-21 18:17:45 +0000710
Damien Georgebfbd9442020-01-09 11:01:14 +1100711const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
Paul Sokolovsky7aca1ca2014-05-11 02:26:42 +0300712const char *mp_obj_get_type_str(mp_const_obj_t o_in);
Damien George9e6e9352014-03-26 18:37:06 +0000713bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
Paul Sokolovskyea970802014-05-11 03:16:04 +0300714mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type);
Damiend99b0522013-12-21 18:17:45 +0000715
Damien George7f9d1d62015-04-09 23:56:15 +0100716void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
Paul Sokolovsky76d982e2014-01-13 19:19:16 +0200717void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
Damien George7f9d1d62015-04-09 23:56:15 +0100718void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc);
Damiend99b0522013-12-21 18:17:45 +0000719
Damien George4d917232014-08-30 14:28:06 +0100720bool mp_obj_is_true(mp_obj_t arg);
Damiend99b0522013-12-21 18:17:45 +0000721bool mp_obj_is_callable(mp_obj_t o_in);
Damiend99b0522013-12-21 18:17:45 +0000722bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
Damiend99b0522013-12-21 18:17:45 +0000723
Damien Georgeeee1e882019-01-30 18:49:52 +1100724static inline bool mp_obj_is_integer(mp_const_obj_t o) { return mp_obj_is_int(o) || mp_obj_is_type(o, &mp_type_bool); } // returns true if o is bool, small int or long int
Damien George40f3c022014-07-03 13:25:24 +0100725mp_int_t mp_obj_get_int(mp_const_obj_t arg);
Damien Georgec50772d2015-05-12 23:05:53 +0100726mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
Damien George40f3c022014-07-03 13:25:24 +0100727bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
Damien Georgefb510b32014-06-01 13:32:54 +0100728#if MICROPY_PY_BUILTINS_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000729mp_float_t mp_obj_get_float(mp_obj_t self_in);
Paul Sokolovsky99508652017-09-02 21:19:01 +0300730bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value);
Damiend99b0522013-12-21 18:17:45 +0000731void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
732#endif
Damien George6213ad72017-03-25 19:35:08 +1100733void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items); // *items may point inside a GC block
Damien George3f810da2017-03-26 19:20:06 +1100734void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items); // *items may point inside a GC block
Damien Georgec88cfe12017-03-23 16:17:40 +1100735size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice);
Damien Georgec7687ad2014-08-22 21:48:30 +0100736mp_obj_t mp_obj_id(mp_obj_t o_in);
Damien George4c03b3a2014-08-12 18:33:40 +0100737mp_obj_t mp_obj_len(mp_obj_t o_in);
Damien George2801e6f2015-04-04 15:53:11 +0100738mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL
Damien George729f7b42014-04-17 22:10:53 +0100739mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
Damien George58321dd2017-08-29 13:04:01 +1000740mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in);
Damiend99b0522013-12-21 18:17:45 +0000741
Damiend99b0522013-12-21 18:17:45 +0000742// cell
743mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
744void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
745
Damien George71c51812014-01-04 20:21:15 +0000746// int
Damien George40f3c022014-07-03 13:25:24 +0100747// For long int, returns value truncated to mp_int_t
Damien Georgebe6d8be2014-12-05 23:13:52 +0000748mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
749// Will raise exception if value doesn't fit into mp_int_t
750mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
Yonatan Goldschmidt176ab992020-01-13 23:35:07 +0200751// Will raise exception if value is negative or doesn't fit into mp_uint_t
752mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in);
Damien George71c51812014-01-04 20:21:15 +0000753
Damienb86e3f92013-12-29 17:17:43 +0000754// exception
Paul Sokolovskyd8351ca2014-05-02 01:51:25 +0300755#define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
Damien Georgec5966122014-02-15 16:10:44 +0000756bool mp_obj_is_exception_type(mp_obj_t self_in);
757bool mp_obj_is_exception_instance(mp_obj_t self_in);
Damien George4bcd04b2014-09-24 14:05:40 +0100758bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
Damien Georgec5966122014-02-15 16:10:44 +0000759void mp_obj_exception_clear_traceback(mp_obj_t self_in);
Damien George3d2daa22016-01-02 22:04:12 +0000760void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
761void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
Paul Sokolovskyaf1ae302014-03-26 19:17:20 +0200762mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
Damien George5b3f0b72016-01-03 15:55:55 +0000763mp_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);
Dave Hylands5b7fd202014-07-01 23:46:53 -0700764mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
765void mp_init_emergency_exception_buf(void);
Damienb86e3f92013-12-29 17:17:43 +0000766
Damiend99b0522013-12-21 18:17:45 +0000767// str
Damien George5fa93b62014-01-22 14:35:10 +0000768bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
Damien Georgeb829b5c2014-01-25 13:51:19 +0000769qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
Damien George5fa93b62014-01-22 14:35:10 +0000770const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
Damien George6b341072017-03-25 19:48:18 +1100771const char *mp_obj_str_get_data(mp_obj_t self_in, size_t *len);
Paul Sokolovskyb4efac12014-06-08 01:13:35 +0300772mp_obj_t mp_obj_str_intern(mp_obj_t str);
Damien George32807882018-03-30 11:09:00 +1100773mp_obj_t mp_obj_str_intern_checked(mp_obj_t obj);
Damien Georgec0d95002017-02-16 16:26:48 +1100774void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, size_t str_len, bool is_bytes);
Damiend99b0522013-12-21 18:17:45 +0000775
Damien Georgefb510b32014-06-01 13:32:54 +0100776#if MICROPY_PY_BUILTINS_FLOAT
Damiend99b0522013-12-21 18:17:45 +0000777// float
Damien Georgea73501b2017-04-06 17:27:33 +1000778#if MICROPY_FLOAT_HIGH_QUALITY_HASH
779mp_int_t mp_float_hash(mp_float_t val);
780#else
Damien George19f2e472017-04-04 11:57:21 +1000781static inline mp_int_t mp_float_hash(mp_float_t val) { return (mp_int_t)val; }
Damien Georgea73501b2017-04-06 17:27:33 +1000782#endif
Damien George58321dd2017-08-29 13:04:01 +1000783mp_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
Damiend99b0522013-12-21 18:17:45 +0000784
785// complex
Damiend99b0522013-12-21 18:17:45 +0000786void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
Damien George58321dd2017-08-29 13:04:01 +1000787mp_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
Paul Sokolovskydc3eb552016-02-14 19:12:57 +0200788#else
789#define mp_obj_is_float(o) (false)
Damiend99b0522013-12-21 18:17:45 +0000790#endif
791
792// tuple
Damien George6213ad72017-03-25 19:35:08 +1100793void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
John R. Lenton07205ec2014-01-13 02:31:00 +0000794void mp_obj_tuple_del(mp_obj_t self_in);
Damien George40f3c022014-07-03 13:25:24 +0100795mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
Damiend99b0522013-12-21 18:17:45 +0000796
797// list
Damiend99b0522013-12-21 18:17:45 +0000798mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
Damien Georgeeff359e2015-02-21 14:47:02 +0000799mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value);
Damien George6213ad72017-03-25 19:35:08 +1100800void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
Damien George58d9eeb2017-02-16 16:12:41 +1100801void mp_obj_list_set_len(mp_obj_t self_in, size_t len);
Damiend99b0522013-12-21 18:17:45 +0000802void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
Damien George4b72b3a2016-01-03 14:21:40 +0000803mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
Damiend99b0522013-12-21 18:17:45 +0000804
805// dict
Damien Georgedf6567e2014-03-30 13:54:02 +0100806typedef struct _mp_obj_dict_t {
807 mp_obj_base_t base;
808 mp_map_t map;
809} mp_obj_dict_t;
Damien George1ea2f7a2017-02-16 16:15:04 +1100810void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args);
811size_t mp_obj_dict_len(mp_obj_t self_in);
Paul Sokolovsky75ce9252014-06-05 20:02:15 +0300812mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
Damiend99b0522013-12-21 18:17:45 +0000813mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
Damien George66edc5d2014-04-05 13:25:13 +0100814mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
Damien Georged9cdb882018-07-08 22:23:57 +1000815static inline mp_map_t *mp_obj_dict_get_map(mp_obj_t dict) {
816 return &((mp_obj_dict_t*)MP_OBJ_TO_PTR(dict))->map;
817}
Damiend99b0522013-12-21 18:17:45 +0000818
819// set
820void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
821
Nicko van Someren4c939552019-11-16 17:07:11 -0700822// slice indexes resolved to particular sequence
823typedef struct {
824 mp_int_t start;
825 mp_int_t stop;
826 mp_int_t step;
827} mp_bound_slice_t;
828
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200829// slice
Nicko van Someren10709842019-11-20 18:53:07 -0700830typedef struct _mp_obj_slice_t {
831 mp_obj_base_t base;
832 mp_obj_t start;
833 mp_obj_t stop;
834 mp_obj_t step;
835} mp_obj_slice_t;
Nicko van Someren4c939552019-11-16 17:07:11 -0700836void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result);
Paul Sokolovsky1c6de112014-01-03 02:41:17 +0200837
Damiend99b0522013-12-21 18:17:45 +0000838// functions
Damien George571e6f22016-10-18 11:49:27 +1100839
840typedef struct _mp_obj_fun_builtin_fixed_t {
Damiend99b0522013-12-21 18:17:45 +0000841 mp_obj_base_t base;
Damien George1b0aab62016-01-03 11:53:44 +0000842 union {
843 mp_fun_0_t _0;
844 mp_fun_1_t _1;
845 mp_fun_2_t _2;
846 mp_fun_3_t _3;
Damien George571e6f22016-10-18 11:49:27 +1100847 } fun;
848} mp_obj_fun_builtin_fixed_t;
849
Damien George571e6f22016-10-18 11:49:27 +1100850typedef struct _mp_obj_fun_builtin_var_t {
851 mp_obj_base_t base;
Damien George9f241ef2018-09-14 13:39:17 +1000852 uint32_t sig; // see MP_OBJ_FUN_MAKE_SIG
Damien George571e6f22016-10-18 11:49:27 +1100853 union {
Damien George1b0aab62016-01-03 11:53:44 +0000854 mp_fun_var_t var;
855 mp_fun_kw_t kw;
856 } fun;
Damien George571e6f22016-10-18 11:49:27 +1100857} mp_obj_fun_builtin_var_t;
Damien George97209d32014-01-07 15:58:30 +0000858
stijn3cc17c62015-02-14 18:44:31 +0100859qstr mp_obj_fun_get_name(mp_const_obj_t fun);
860qstr mp_obj_code_get_name(const byte *code_info);
Damien660365e2013-12-17 18:27:24 +0000861
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200862mp_obj_t mp_identity(mp_obj_t self);
Damien George4ebdb1f2016-10-18 11:06:20 +1100863MP_DECLARE_CONST_FUN_OBJ_1(mp_identity_obj);
Damien Georgeae8d8672016-01-09 23:14:54 +0000864mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf);
Paul Sokolovskydff3f892014-01-20 18:37:30 +0200865
Damien George28708622014-01-02 21:30:26 +0000866// module
Damien George0c36da02014-03-08 15:24:39 +0000867typedef struct _mp_obj_module_t {
868 mp_obj_base_t base;
Damien George8b0535e2014-04-05 21:53:54 +0100869 mp_obj_dict_t *globals;
Damien George0c36da02014-03-08 15:24:39 +0000870} mp_obj_module_t;
Damien George4cd853f2018-07-08 22:27:39 +1000871static inline mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t module) {
872 return ((mp_obj_module_t*)MP_OBJ_TO_PTR(module))->globals;
873}
Paul Sokolovskye5a37592014-10-25 21:04:13 +0300874// check if given module object is a package
875bool mp_obj_is_package(mp_obj_t module);
Damien Georgeeae16442014-01-11 19:22:29 +0000876
877// staticmethod and classmethod types; defined here so we can make const versions
Damien George64131f32014-02-06 20:31:44 +0000878// this structure is used for instances of both staticmethod and classmethod
879typedef struct _mp_obj_static_class_method_t {
Damien Georgeeae16442014-01-11 19:22:29 +0000880 mp_obj_base_t base;
881 mp_obj_t fun;
Damien George64131f32014-02-06 20:31:44 +0000882} mp_obj_static_class_method_t;
Damien Georgecbf76742015-11-27 13:38:15 +0000883typedef struct _mp_rom_obj_static_class_method_t {
884 mp_obj_base_t base;
885 mp_rom_obj_t fun;
886} mp_rom_obj_static_class_method_t;
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200887
Damien George777b0f32014-04-13 18:59:45 +0100888// property
889const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
890
Paul Sokolovsky439542f2014-01-21 00:19:19 +0200891// sequence helpers
Paul Sokolovskyde4b9322014-05-25 21:21:57 +0300892
Damien George507119f2017-03-23 16:23:20 +1100893void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest);
Damien Georgec49ddb92014-06-01 13:49:35 +0100894#if MICROPY_PY_BUILTINS_SLICE
Damien George40f3c022014-07-03 13:25:24 +0100895bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes);
Damien Georgec49ddb92014-06-01 13:49:35 +0100896#endif
Paul Sokolovskyd915a522014-05-10 21:36:33 +0300897#define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
898#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)); }
Damien George507119f2017-03-23 16:23:20 +1100899bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2);
900bool mp_seq_cmp_objs(mp_uint_t op, const mp_obj_t *items1, size_t len1, const mp_obj_t *items2, size_t len2);
901mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args);
902mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value);
903mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
Paul Sokolovskya2240672014-04-28 00:16:57 +0300904// Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
905#define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte*)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200906#define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_sz) \
907 /*printf("memcpy(%p, %p, %d)\n", dest + beg, slice, slice_len * (item_sz));*/ \
908 memcpy(((char*)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \
909 /*printf("memmove(%p, %p, %d)\n", dest + (beg + slice_len), dest + end, (dest_len - end) * (item_sz));*/ \
910 memmove(((char*)dest) + (beg + slice_len) * (item_sz), ((char*)dest) + (end) * (item_sz), (dest_len - end) * (item_sz));
Paul Sokolovsky2705f4c2014-05-25 02:36:12 +0300911
Damien Georgea5500a82017-04-02 17:28:24 +1000912// Note: dest and slice regions may overlap
Paul Sokolovskycefcbb22015-02-27 22:16:05 +0200913#define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_sz) \
914 /*printf("memmove(%p, %p, %d)\n", dest + beg + len_adj, dest + beg, (dest_len - beg) * (item_sz));*/ \
Damien Georgea5500a82017-04-02 17:28:24 +1000915 memmove(((char*)dest) + (beg + slice_len) * (item_sz), ((char*)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \
916 memmove(((char*)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));
Damien George9ddbe292014-12-29 01:02:19 +0000917
Damien Georgeeee1e882019-01-30 18:49:52 +1100918// Provide translation for legacy API
919#define MP_OBJ_IS_SMALL_INT mp_obj_is_small_int
920#define MP_OBJ_IS_QSTR mp_obj_is_qstr
921#define MP_OBJ_IS_OBJ mp_obj_is_obj
922#define MP_OBJ_IS_INT mp_obj_is_int
923#define MP_OBJ_IS_TYPE mp_obj_is_type
924#define MP_OBJ_IS_STR mp_obj_is_str
925#define MP_OBJ_IS_STR_OR_BYTES mp_obj_is_str_or_bytes
926#define MP_OBJ_IS_FUN mp_obj_is_fun
Damien George054dd332019-01-30 21:57:29 +1100927#define MP_MAP_SLOT_IS_FILLED mp_map_slot_is_filled
928#define MP_SET_SLOT_IS_FILLED mp_set_slot_is_filled
Damien Georgeeee1e882019-01-30 18:49:52 +1100929
Alexander Steffen299bc622017-06-29 23:14:58 +0200930#endif // MICROPY_INCLUDED_PY_OBJ_H