obj.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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. #include "py/mpconfig.h"
  29. #include "py/misc.h"
  30. #include "py/qstr.h"
  31. #include "py/mpprint.h"
  32. #include "py/runtime0.h"
  33. // This is the definition of the opaque MicroPython object type.
  34. // All concrete objects have an encoding within this type and the
  35. // particular encoding is specified by MICROPY_OBJ_REPR.
  36. #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
  37. typedef uint64_t mp_obj_t;
  38. typedef uint64_t mp_const_obj_t;
  39. #else
  40. typedef void *mp_obj_t;
  41. typedef const void *mp_const_obj_t;
  42. #endif
  43. // This mp_obj_type_t struct is a concrete MicroPython object which holds info
  44. // about a type. See below for actual definition of the struct.
  45. typedef struct _mp_obj_type_t mp_obj_type_t;
  46. // Anything that wants to be a concrete MicroPython object must have mp_obj_base_t
  47. // as its first member (small ints, qstr objs and inline floats are not concrete).
  48. struct _mp_obj_base_t {
  49. const mp_obj_type_t *type MICROPY_OBJ_BASE_ALIGNMENT;
  50. };
  51. typedef struct _mp_obj_base_t mp_obj_base_t;
  52. // These fake objects are used to indicate certain things in arguments or return
  53. // values, and should only be used when explicitly allowed.
  54. //
  55. // - MP_OBJ_NULL : used to indicate the absence of an object, or unsupported operation.
  56. // - MP_OBJ_STOP_ITERATION : used instead of throwing a StopIteration, for efficiency.
  57. // - MP_OBJ_SENTINEL : used for various internal purposes where one needs
  58. // an object which is unique from all other objects, including MP_OBJ_NULL.
  59. //
  60. // For debugging purposes they are all different. For non-debug mode, we alias
  61. // as many as we can to MP_OBJ_NULL because it's cheaper to load/compare 0.
  62. #ifdef NDEBUG
  63. #define MP_OBJ_NULL (MP_OBJ_FROM_PTR((void*)0))
  64. #define MP_OBJ_STOP_ITERATION (MP_OBJ_FROM_PTR((void*)0))
  65. #define MP_OBJ_SENTINEL (MP_OBJ_FROM_PTR((void*)4))
  66. #else
  67. #define MP_OBJ_NULL (MP_OBJ_FROM_PTR((void*)0))
  68. #define MP_OBJ_STOP_ITERATION (MP_OBJ_FROM_PTR((void*)4))
  69. #define MP_OBJ_SENTINEL (MP_OBJ_FROM_PTR((void*)8))
  70. #endif
  71. // These macros/inline functions operate on objects and depend on the
  72. // particular object representation. They are used to query, pack and
  73. // unpack small ints, qstrs and full object pointers.
  74. #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
  75. static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
  76. { return ((((mp_int_t)(o)) & 1) != 0); }
  77. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
  78. #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
  79. static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
  80. { return ((((mp_int_t)(o)) & 3) == 2); }
  81. #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
  82. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2))
  83. #if MICROPY_PY_BUILTINS_FLOAT
  84. #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
  85. #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
  86. extern const struct _mp_obj_float_t mp_const_float_e_obj;
  87. extern const struct _mp_obj_float_t mp_const_float_pi_obj;
  88. #define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
  89. mp_float_t mp_obj_float_get(mp_obj_t self_in);
  90. mp_obj_t mp_obj_new_float(mp_float_t value);
  91. #endif
  92. static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
  93. { return ((((mp_int_t)(o)) & 3) == 0); }
  94. #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
  95. static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
  96. { return ((((mp_int_t)(o)) & 3) == 1); }
  97. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 2)
  98. #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 2) | 1))
  99. static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
  100. { return ((((mp_int_t)(o)) & 3) == 3); }
  101. #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 2)
  102. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 3))
  103. #if MICROPY_PY_BUILTINS_FLOAT
  104. #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
  105. #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
  106. extern const struct _mp_obj_float_t mp_const_float_e_obj;
  107. extern const struct _mp_obj_float_t mp_const_float_pi_obj;
  108. #define mp_obj_is_float(o) MP_OBJ_IS_TYPE((o), &mp_type_float)
  109. mp_float_t mp_obj_float_get(mp_obj_t self_in);
  110. mp_obj_t mp_obj_new_float(mp_float_t value);
  111. #endif
  112. static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
  113. { return ((((mp_int_t)(o)) & 1) == 0); }
  114. #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
  115. static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
  116. { return ((((mp_int_t)(o)) & 1) != 0); }
  117. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
  118. #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
  119. #if MICROPY_PY_BUILTINS_FLOAT
  120. #define mp_const_float_e MP_ROM_PTR((mp_obj_t)(((0x402df854 & ~3) | 2) + 0x80800000))
  121. #define mp_const_float_pi MP_ROM_PTR((mp_obj_t)(((0x40490fdb & ~3) | 2) + 0x80800000))
  122. static inline bool mp_obj_is_float(mp_const_obj_t o)
  123. { return (((mp_uint_t)(o)) & 3) == 2 && (((mp_uint_t)(o)) & 0xff800007) != 0x00000006; }
  124. static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
  125. union {
  126. mp_float_t f;
  127. mp_uint_t u;
  128. } num = {.u = ((mp_uint_t)o - 0x80800000) & ~3};
  129. return num.f;
  130. }
  131. static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
  132. union {
  133. mp_float_t f;
  134. mp_uint_t u;
  135. } num = {.f = f};
  136. return (mp_obj_t)(((num.u & ~0x3) | 2) + 0x80800000);
  137. }
  138. #endif
  139. static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
  140. { return (((mp_uint_t)(o)) & 0xff800007) == 0x00000006; }
  141. #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
  142. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 0x00000006))
  143. static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
  144. { return ((((mp_int_t)(o)) & 3) == 0); }
  145. #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
  146. static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o)
  147. { return ((((mp_int_t)(o)) & 0xffff000000000000) == 0x0001000000000000); }
  148. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)((o) << 16)) >> 17)
  149. #define MP_OBJ_NEW_SMALL_INT(small_int) (((((uint64_t)(small_int)) & 0x7fffffffffff) << 1) | 0x0001000000000001)
  150. static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o)
  151. { return ((((mp_int_t)(o)) & 0xffff000000000000) == 0x0002000000000000); }
  152. #define MP_OBJ_QSTR_VALUE(o) ((((uint32_t)(o)) >> 1) & 0xffffffff)
  153. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 1) | 0x0002000000000001))
  154. #if MICROPY_PY_BUILTINS_FLOAT
  155. #if MICROPY_FLOAT_IMPL != MICROPY_FLOAT_IMPL_DOUBLE
  156. #error MICROPY_OBJ_REPR_D requires MICROPY_FLOAT_IMPL_DOUBLE
  157. #endif
  158. #define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b145769 + 0x8004000000000000))}
  159. #define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))}
  160. static inline bool mp_obj_is_float(mp_const_obj_t o) {
  161. return ((uint64_t)(o) & 0xfffc000000000000) != 0;
  162. }
  163. static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
  164. union {
  165. mp_float_t f;
  166. uint64_t r;
  167. } num = {.r = o - 0x8004000000000000};
  168. return num.f;
  169. }
  170. static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
  171. union {
  172. mp_float_t f;
  173. uint64_t r;
  174. } num = {.f = f};
  175. return num.r + 0x8004000000000000;
  176. }
  177. #endif
  178. static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o)
  179. { return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0000000000000000); }
  180. #define MP_OBJ_TO_PTR(o) ((void*)(uintptr_t)(o))
  181. #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p)))
  182. // rom object storage needs special handling to widen 32-bit pointer to 64-bits
  183. typedef union _mp_rom_obj_t { uint64_t u64; struct { const void *lo, *hi; } u32; } mp_rom_obj_t;
  184. #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
  185. #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
  186. #if MP_ENDIANNESS_LITTLE
  187. #define MP_ROM_PTR(p) {.u32 = {.lo = (p), .hi = NULL}}
  188. #else
  189. #define MP_ROM_PTR(p) {.u32 = {.lo = NULL, .hi = (p)}}
  190. #endif
  191. #endif
  192. // Macros to convert between mp_obj_t and concrete object types.
  193. // These are identity operations in MicroPython, but ability to override
  194. // these operations are provided to experiment with other methods of
  195. // object representation and memory management.
  196. // Cast mp_obj_t to object pointer
  197. #ifndef MP_OBJ_TO_PTR
  198. #define MP_OBJ_TO_PTR(o) ((void*)o)
  199. #endif
  200. // Cast object pointer to mp_obj_t
  201. #ifndef MP_OBJ_FROM_PTR
  202. #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)p)
  203. #endif
  204. // Macros to create objects that are stored in ROM.
  205. #ifndef MP_ROM_INT
  206. typedef mp_const_obj_t mp_rom_obj_t;
  207. #define MP_ROM_INT(i) MP_OBJ_NEW_SMALL_INT(i)
  208. #define MP_ROM_QSTR(q) MP_OBJ_NEW_QSTR(q)
  209. #define MP_ROM_PTR(p) (p)
  210. /* for testing
  211. typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
  212. #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
  213. #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
  214. #define MP_ROM_PTR(p) {.o = p}
  215. */
  216. #endif
  217. // The macros below are derived from the ones above and are used to
  218. // check for more specific object types.
  219. // Note: these are kept as macros because inline functions sometimes use much
  220. // more code space than the equivalent macros, depending on the compiler.
  221. #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
  222. #define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int))
  223. #define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str))
  224. #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))
  225. #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))
  226. // These macros are used to declare and define constant function objects
  227. // You can put "static" in front of the definitions to make them local
  228. #define MP_DECLARE_CONST_FUN_OBJ_0(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  229. #define MP_DECLARE_CONST_FUN_OBJ_1(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  230. #define MP_DECLARE_CONST_FUN_OBJ_2(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  231. #define MP_DECLARE_CONST_FUN_OBJ_3(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  232. #define MP_DECLARE_CONST_FUN_OBJ_VAR(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
  233. #define MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
  234. #define MP_DECLARE_CONST_FUN_OBJ_KW(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
  235. #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
  236. const mp_obj_fun_builtin_fixed_t obj_name = \
  237. {{&mp_type_fun_builtin_0}, .fun._0 = fun_name}
  238. #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
  239. const mp_obj_fun_builtin_fixed_t obj_name = \
  240. {{&mp_type_fun_builtin_1}, .fun._1 = fun_name}
  241. #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
  242. const mp_obj_fun_builtin_fixed_t obj_name = \
  243. {{&mp_type_fun_builtin_2}, .fun._2 = fun_name}
  244. #define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
  245. const mp_obj_fun_builtin_fixed_t obj_name = \
  246. {{&mp_type_fun_builtin_3}, .fun._3 = fun_name}
  247. #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
  248. const mp_obj_fun_builtin_var_t obj_name = \
  249. {{&mp_type_fun_builtin_var}, false, n_args_min, MP_OBJ_FUN_ARGS_MAX, .fun.var = fun_name}
  250. #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
  251. const mp_obj_fun_builtin_var_t obj_name = \
  252. {{&mp_type_fun_builtin_var}, false, n_args_min, n_args_max, .fun.var = fun_name}
  253. #define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
  254. const mp_obj_fun_builtin_var_t obj_name = \
  255. {{&mp_type_fun_builtin_var}, true, n_args_min, MP_OBJ_FUN_ARGS_MAX, .fun.kw = fun_name}
  256. // These macros are used to define constant map/dict objects
  257. // You can put "static" in front of the definition to make it local
  258. #define MP_DEFINE_CONST_MAP(map_name, table_name) \
  259. const mp_map_t map_name = { \
  260. .all_keys_are_qstrs = 1, \
  261. .is_fixed = 1, \
  262. .is_ordered = 1, \
  263. .used = MP_ARRAY_SIZE(table_name), \
  264. .alloc = MP_ARRAY_SIZE(table_name), \
  265. .table = (mp_map_elem_t*)(mp_rom_map_elem_t*)table_name, \
  266. }
  267. #define MP_DEFINE_CONST_DICT(dict_name, table_name) \
  268. const mp_obj_dict_t dict_name = { \
  269. .base = {&mp_type_dict}, \
  270. .map = { \
  271. .all_keys_are_qstrs = 1, \
  272. .is_fixed = 1, \
  273. .is_ordered = 1, \
  274. .used = MP_ARRAY_SIZE(table_name), \
  275. .alloc = MP_ARRAY_SIZE(table_name), \
  276. .table = (mp_map_elem_t*)(mp_rom_map_elem_t*)table_name, \
  277. }, \
  278. }
  279. // These macros are used to declare and define constant staticmethond and classmethod objects
  280. // You can put "static" in front of the definitions to make them local
  281. #define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
  282. #define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
  283. #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}
  284. #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}
  285. // Underlying map/hash table implementation (not dict object or map function)
  286. typedef struct _mp_map_elem_t {
  287. mp_obj_t key;
  288. mp_obj_t value;
  289. } mp_map_elem_t;
  290. typedef struct _mp_rom_map_elem_t {
  291. mp_rom_obj_t key;
  292. mp_rom_obj_t value;
  293. } mp_rom_map_elem_t;
  294. // TODO maybe have a truncated mp_map_t for fixed tables, since alloc=used
  295. // put alloc last in the structure, so the truncated version does not need it
  296. // this would save 1 ROM word for all ROM objects that have a locals_dict
  297. // would also need a trucated dict structure
  298. typedef struct _mp_map_t {
  299. size_t all_keys_are_qstrs : 1;
  300. size_t is_fixed : 1; // a fixed array that can't be modified; must also be ordered
  301. size_t is_ordered : 1; // an ordered array
  302. size_t used : (8 * sizeof(size_t) - 3);
  303. size_t alloc;
  304. mp_map_elem_t *table;
  305. } mp_map_t;
  306. // mp_set_lookup requires these constants to have the values they do
  307. typedef enum _mp_map_lookup_kind_t {
  308. MP_MAP_LOOKUP = 0,
  309. MP_MAP_LOOKUP_ADD_IF_NOT_FOUND = 1,
  310. MP_MAP_LOOKUP_REMOVE_IF_FOUND = 2,
  311. MP_MAP_LOOKUP_ADD_IF_NOT_FOUND_OR_REMOVE_IF_FOUND = 3, // only valid for mp_set_lookup
  312. } mp_map_lookup_kind_t;
  313. extern const mp_map_t mp_const_empty_map;
  314. static 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); }
  315. void mp_map_init(mp_map_t *map, size_t n);
  316. void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table);
  317. mp_map_t *mp_map_new(size_t n);
  318. void mp_map_deinit(mp_map_t *map);
  319. void mp_map_free(mp_map_t *map);
  320. mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
  321. void mp_map_clear(mp_map_t *map);
  322. void mp_map_dump(mp_map_t *map);
  323. // Underlying set implementation (not set object)
  324. typedef struct _mp_set_t {
  325. size_t alloc;
  326. size_t used;
  327. mp_obj_t *table;
  328. } mp_set_t;
  329. static 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); }
  330. void mp_set_init(mp_set_t *set, size_t n);
  331. mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
  332. mp_obj_t mp_set_remove_first(mp_set_t *set);
  333. void mp_set_clear(mp_set_t *set);
  334. // Type definitions for methods
  335. typedef mp_obj_t (*mp_fun_0_t)(void);
  336. typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
  337. typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
  338. typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
  339. typedef mp_obj_t (*mp_fun_var_t)(size_t n, const mp_obj_t *);
  340. // mp_fun_kw_t takes mp_map_t* (and not const mp_map_t*) to ease passing
  341. // this arg to mp_map_lookup().
  342. typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *);
  343. typedef enum {
  344. PRINT_STR = 0,
  345. PRINT_REPR = 1,
  346. PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
  347. PRINT_JSON = 3,
  348. PRINT_RAW = 4, // Special format for printing bytes as an undercorated string
  349. PRINT_EXC_SUBCLASS = 0x80, // Internal flag for printing exception subclasses
  350. } mp_print_kind_t;
  351. typedef struct _mp_obj_iter_buf_t {
  352. mp_obj_base_t base;
  353. mp_obj_t buf[3];
  354. } mp_obj_iter_buf_t;
  355. // The number of slots that an mp_obj_iter_buf_t needs on the Python value stack.
  356. // It's rounded up in case mp_obj_base_t is smaller than mp_obj_t (eg for OBJ_REPR_D).
  357. #define MP_OBJ_ITER_BUF_NSLOTS ((sizeof(mp_obj_iter_buf_t) + sizeof(mp_obj_t) - 1) / sizeof(mp_obj_t))
  358. typedef void (*mp_print_fun_t)(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind);
  359. 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);
  360. 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);
  361. typedef mp_obj_t (*mp_unary_op_fun_t)(mp_unary_op_t op, mp_obj_t);
  362. typedef mp_obj_t (*mp_binary_op_fun_t)(mp_binary_op_t op, mp_obj_t, mp_obj_t);
  363. typedef void (*mp_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
  364. typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
  365. typedef mp_obj_t (*mp_getiter_fun_t)(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf);
  366. // Buffer protocol
  367. typedef struct _mp_buffer_info_t {
  368. // if we'd bother to support various versions of structure
  369. // (with different number of fields), we can distinguish
  370. // them with ver = sizeof(struct). Cons: overkill for *micro*?
  371. //int ver; // ?
  372. void *buf; // can be NULL if len == 0
  373. size_t len; // in bytes
  374. int typecode; // as per binary.h
  375. // Rationale: to load arbitrary-sized sprites directly to LCD
  376. // Cons: a bit adhoc usecase
  377. // int stride;
  378. } mp_buffer_info_t;
  379. #define MP_BUFFER_READ (1)
  380. #define MP_BUFFER_WRITE (2)
  381. #define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
  382. typedef struct _mp_buffer_p_t {
  383. mp_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
  384. } mp_buffer_p_t;
  385. bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
  386. void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
  387. struct _mp_obj_type_t {
  388. // A type is an object so must start with this entry, which points to mp_type_type.
  389. mp_obj_base_t base;
  390. // Flags associated with this type.
  391. uint16_t flags;
  392. // The name of this type, a qstr.
  393. uint16_t name;
  394. // Corresponds to __repr__ and __str__ special methods.
  395. mp_print_fun_t print;
  396. // Corresponds to __new__ and __init__ special methods, to make an instance of the type.
  397. mp_make_new_fun_t make_new;
  398. // Corresponds to __call__ special method, ie T(...).
  399. mp_call_fun_t call;
  400. // Implements unary and binary operations.
  401. // Can return MP_OBJ_NULL if the operation is not supported.
  402. mp_unary_op_fun_t unary_op;
  403. mp_binary_op_fun_t binary_op;
  404. // Implements load, store and delete attribute.
  405. //
  406. // dest[0] = MP_OBJ_NULL means load
  407. // return: for fail, do nothing
  408. // for attr, dest[0] = value
  409. // for method, dest[0] = method, dest[1] = self
  410. //
  411. // dest[0,1] = {MP_OBJ_SENTINEL, MP_OBJ_NULL} means delete
  412. // dest[0,1] = {MP_OBJ_SENTINEL, object} means store
  413. // return: for fail, do nothing
  414. // for success set dest[0] = MP_OBJ_NULL
  415. mp_attr_fun_t attr;
  416. // Implements load, store and delete subscripting:
  417. // - value = MP_OBJ_SENTINEL means load
  418. // - value = MP_OBJ_NULL means delete
  419. // - all other values mean store the value
  420. // Can return MP_OBJ_NULL if operation not supported.
  421. mp_subscr_fun_t subscr;
  422. // Corresponds to __iter__ special method.
  423. // Can use the given mp_obj_iter_buf_t to store iterator object,
  424. // otherwise can return a pointer to an object on the heap.
  425. mp_getiter_fun_t getiter;
  426. // Corresponds to __next__ special method. May return MP_OBJ_STOP_ITERATION
  427. // as an optimisation instead of raising StopIteration() with no args.
  428. mp_fun_1_t iternext;
  429. // Implements the buffer protocol if supported by this type.
  430. mp_buffer_p_t buffer_p;
  431. // One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
  432. const void *protocol;
  433. // A pointer to the parents of this type:
  434. // - 0 parents: pointer is NULL (object is implicitly the single parent)
  435. // - 1 parent: a pointer to the type of that parent
  436. // - 2 or more parents: pointer to a tuple object containing the parent types
  437. const void *parent;
  438. // A dict mapping qstrs to objects local methods/constants/etc.
  439. struct _mp_obj_dict_t *locals_dict;
  440. };
  441. // Constant types, globally accessible
  442. extern const mp_obj_type_t mp_type_type;
  443. extern const mp_obj_type_t mp_type_object;
  444. extern const mp_obj_type_t mp_type_NoneType;
  445. extern const mp_obj_type_t mp_type_bool;
  446. extern const mp_obj_type_t mp_type_int;
  447. extern const mp_obj_type_t mp_type_str;
  448. extern const mp_obj_type_t mp_type_bytes;
  449. extern const mp_obj_type_t mp_type_bytearray;
  450. extern const mp_obj_type_t mp_type_memoryview;
  451. extern const mp_obj_type_t mp_type_float;
  452. extern const mp_obj_type_t mp_type_complex;
  453. extern const mp_obj_type_t mp_type_tuple;
  454. extern const mp_obj_type_t mp_type_list;
  455. extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict implementation detail)
  456. extern const mp_obj_type_t mp_type_enumerate;
  457. extern const mp_obj_type_t mp_type_filter;
  458. extern const mp_obj_type_t mp_type_deque;
  459. extern const mp_obj_type_t mp_type_dict;
  460. extern const mp_obj_type_t mp_type_ordereddict;
  461. extern const mp_obj_type_t mp_type_range;
  462. extern const mp_obj_type_t mp_type_set;
  463. extern const mp_obj_type_t mp_type_frozenset;
  464. extern const mp_obj_type_t mp_type_slice;
  465. extern const mp_obj_type_t mp_type_zip;
  466. extern const mp_obj_type_t mp_type_array;
  467. extern const mp_obj_type_t mp_type_super;
  468. extern const mp_obj_type_t mp_type_gen_wrap;
  469. extern const mp_obj_type_t mp_type_gen_instance;
  470. extern const mp_obj_type_t mp_type_fun_builtin_0;
  471. extern const mp_obj_type_t mp_type_fun_builtin_1;
  472. extern const mp_obj_type_t mp_type_fun_builtin_2;
  473. extern const mp_obj_type_t mp_type_fun_builtin_3;
  474. extern const mp_obj_type_t mp_type_fun_builtin_var;
  475. extern const mp_obj_type_t mp_type_fun_bc;
  476. extern const mp_obj_type_t mp_type_module;
  477. extern const mp_obj_type_t mp_type_staticmethod;
  478. extern const mp_obj_type_t mp_type_classmethod;
  479. extern const mp_obj_type_t mp_type_property;
  480. extern const mp_obj_type_t mp_type_stringio;
  481. extern const mp_obj_type_t mp_type_bytesio;
  482. extern const mp_obj_type_t mp_type_reversed;
  483. extern const mp_obj_type_t mp_type_polymorph_iter;
  484. // Exceptions
  485. extern const mp_obj_type_t mp_type_BaseException;
  486. extern const mp_obj_type_t mp_type_ArithmeticError;
  487. extern const mp_obj_type_t mp_type_AssertionError;
  488. extern const mp_obj_type_t mp_type_AttributeError;
  489. extern const mp_obj_type_t mp_type_EOFError;
  490. extern const mp_obj_type_t mp_type_Exception;
  491. extern const mp_obj_type_t mp_type_GeneratorExit;
  492. extern const mp_obj_type_t mp_type_ImportError;
  493. extern const mp_obj_type_t mp_type_IndentationError;
  494. extern const mp_obj_type_t mp_type_IndexError;
  495. extern const mp_obj_type_t mp_type_KeyboardInterrupt;
  496. extern const mp_obj_type_t mp_type_KeyError;
  497. extern const mp_obj_type_t mp_type_LookupError;
  498. extern const mp_obj_type_t mp_type_MemoryError;
  499. extern const mp_obj_type_t mp_type_NameError;
  500. extern const mp_obj_type_t mp_type_NotImplementedError;
  501. extern const mp_obj_type_t mp_type_OSError;
  502. extern const mp_obj_type_t mp_type_TimeoutError;
  503. extern const mp_obj_type_t mp_type_OverflowError;
  504. extern const mp_obj_type_t mp_type_RuntimeError;
  505. extern const mp_obj_type_t mp_type_StopAsyncIteration;
  506. extern const mp_obj_type_t mp_type_StopIteration;
  507. extern const mp_obj_type_t mp_type_SyntaxError;
  508. extern const mp_obj_type_t mp_type_SystemExit;
  509. extern const mp_obj_type_t mp_type_TypeError;
  510. extern const mp_obj_type_t mp_type_UnicodeError;
  511. extern const mp_obj_type_t mp_type_ValueError;
  512. extern const mp_obj_type_t mp_type_ViperTypeError;
  513. extern const mp_obj_type_t mp_type_ZeroDivisionError;
  514. // Constant objects, globally accessible
  515. // The macros are for convenience only
  516. #define mp_const_none (MP_OBJ_FROM_PTR(&mp_const_none_obj))
  517. #define mp_const_false (MP_OBJ_FROM_PTR(&mp_const_false_obj))
  518. #define mp_const_true (MP_OBJ_FROM_PTR(&mp_const_true_obj))
  519. #define mp_const_empty_bytes (MP_OBJ_FROM_PTR(&mp_const_empty_bytes_obj))
  520. #define mp_const_empty_tuple (MP_OBJ_FROM_PTR(&mp_const_empty_tuple_obj))
  521. #define mp_const_notimplemented (MP_OBJ_FROM_PTR(&mp_const_notimplemented_obj))
  522. extern const struct _mp_obj_none_t mp_const_none_obj;
  523. extern const struct _mp_obj_bool_t mp_const_false_obj;
  524. extern const struct _mp_obj_bool_t mp_const_true_obj;
  525. extern const struct _mp_obj_str_t mp_const_empty_bytes_obj;
  526. extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
  527. extern const struct _mp_obj_singleton_t mp_const_ellipsis_obj;
  528. extern const struct _mp_obj_singleton_t mp_const_notimplemented_obj;
  529. extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
  530. // General API for objects
  531. mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
  532. static inline mp_obj_t mp_obj_new_bool(mp_int_t x) { return x ? mp_const_true : mp_const_false; }
  533. mp_obj_t mp_obj_new_cell(mp_obj_t obj);
  534. mp_obj_t mp_obj_new_int(mp_int_t value);
  535. mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
  536. mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base);
  537. 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)
  538. 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)
  539. mp_obj_t mp_obj_new_str(const char* data, size_t len);
  540. mp_obj_t mp_obj_new_str_via_qstr(const char* data, size_t len);
  541. mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr);
  542. mp_obj_t mp_obj_new_bytes(const byte* data, size_t len);
  543. mp_obj_t mp_obj_new_bytearray(size_t n, void *items);
  544. mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items);
  545. #if MICROPY_PY_BUILTINS_FLOAT
  546. mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
  547. mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
  548. #endif
  549. mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
  550. mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
  551. mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args);
  552. mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, const char *msg);
  553. mp_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!)
  554. 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);
  555. 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);
  556. mp_obj_t mp_obj_new_fun_viper(size_t n_args, void *fun_data, mp_uint_t type_sig);
  557. mp_obj_t mp_obj_new_fun_asm(size_t n_args, void *fun_data, mp_uint_t type_sig);
  558. mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
  559. mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed, const mp_obj_t *closed);
  560. mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
  561. mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);
  562. mp_obj_t mp_obj_new_dict(size_t n_args);
  563. mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items);
  564. mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
  565. mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
  566. mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf);
  567. mp_obj_t mp_obj_new_module(qstr module_name);
  568. mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items);
  569. mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
  570. const char *mp_obj_get_type_str(mp_const_obj_t o_in);
  571. bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
  572. mp_obj_t mp_instance_cast_to_native_base(mp_const_obj_t self_in, mp_const_obj_t native_type);
  573. void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
  574. void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
  575. void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc);
  576. bool mp_obj_is_true(mp_obj_t arg);
  577. bool mp_obj_is_callable(mp_obj_t o_in);
  578. bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
  579. static 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
  580. mp_int_t mp_obj_get_int(mp_const_obj_t arg);
  581. mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
  582. bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
  583. #if MICROPY_PY_BUILTINS_FLOAT
  584. mp_float_t mp_obj_get_float(mp_obj_t self_in);
  585. bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value);
  586. void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  587. #endif
  588. //qstr mp_obj_get_qstr(mp_obj_t arg);
  589. void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items); // *items may point inside a GC block
  590. 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
  591. size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice);
  592. mp_obj_t mp_obj_id(mp_obj_t o_in);
  593. mp_obj_t mp_obj_len(mp_obj_t o_in);
  594. mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL
  595. mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
  596. mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in);
  597. // cell
  598. mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
  599. void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj);
  600. // int
  601. // For long int, returns value truncated to mp_int_t
  602. mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
  603. // Will raise exception if value doesn't fit into mp_int_t
  604. mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
  605. // exception
  606. #define mp_obj_is_native_exception_instance(o) (mp_obj_get_type(o)->make_new == mp_obj_exception_make_new)
  607. bool mp_obj_is_exception_type(mp_obj_t self_in);
  608. bool mp_obj_is_exception_instance(mp_obj_t self_in);
  609. bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
  610. void mp_obj_exception_clear_traceback(mp_obj_t self_in);
  611. void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
  612. void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
  613. mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
  614. 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);
  615. mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
  616. void mp_init_emergency_exception_buf(void);
  617. // str
  618. bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
  619. qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
  620. const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
  621. const char *mp_obj_str_get_data(mp_obj_t self_in, size_t *len);
  622. mp_obj_t mp_obj_str_intern(mp_obj_t str);
  623. mp_obj_t mp_obj_str_intern_checked(mp_obj_t obj);
  624. void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, size_t str_len, bool is_bytes);
  625. #if MICROPY_PY_BUILTINS_FLOAT
  626. // float
  627. #if MICROPY_FLOAT_HIGH_QUALITY_HASH
  628. mp_int_t mp_float_hash(mp_float_t val);
  629. #else
  630. static inline mp_int_t mp_float_hash(mp_float_t val) { return (mp_int_t)val; }
  631. #endif
  632. 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
  633. // complex
  634. void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  635. 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
  636. #else
  637. #define mp_obj_is_float(o) (false)
  638. #endif
  639. // tuple
  640. void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
  641. void mp_obj_tuple_del(mp_obj_t self_in);
  642. mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
  643. // list
  644. mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
  645. mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value);
  646. void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
  647. void mp_obj_list_set_len(mp_obj_t self_in, size_t len);
  648. void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
  649. mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
  650. // dict
  651. typedef struct _mp_obj_dict_t {
  652. mp_obj_base_t base;
  653. mp_map_t map;
  654. } mp_obj_dict_t;
  655. void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args);
  656. size_t mp_obj_dict_len(mp_obj_t self_in);
  657. mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
  658. mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
  659. mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
  660. static inline mp_map_t *mp_obj_dict_get_map(mp_obj_t dict) {
  661. return &((mp_obj_dict_t*)MP_OBJ_TO_PTR(dict))->map;
  662. }
  663. // set
  664. void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
  665. // slice
  666. void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step);
  667. // functions
  668. typedef struct _mp_obj_fun_builtin_fixed_t {
  669. mp_obj_base_t base;
  670. union {
  671. mp_fun_0_t _0;
  672. mp_fun_1_t _1;
  673. mp_fun_2_t _2;
  674. mp_fun_3_t _3;
  675. } fun;
  676. } mp_obj_fun_builtin_fixed_t;
  677. #define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
  678. typedef struct _mp_obj_fun_builtin_var_t {
  679. mp_obj_base_t base;
  680. bool is_kw : 1;
  681. mp_uint_t n_args_min : 15; // inclusive
  682. mp_uint_t n_args_max : 16; // inclusive
  683. union {
  684. mp_fun_var_t var;
  685. mp_fun_kw_t kw;
  686. } fun;
  687. } mp_obj_fun_builtin_var_t;
  688. qstr mp_obj_fun_get_name(mp_const_obj_t fun);
  689. qstr mp_obj_code_get_name(const byte *code_info);
  690. mp_obj_t mp_identity(mp_obj_t self);
  691. MP_DECLARE_CONST_FUN_OBJ_1(mp_identity_obj);
  692. mp_obj_t mp_identity_getiter(mp_obj_t self, mp_obj_iter_buf_t *iter_buf);
  693. // module
  694. typedef struct _mp_obj_module_t {
  695. mp_obj_base_t base;
  696. mp_obj_dict_t *globals;
  697. } mp_obj_module_t;
  698. static inline mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t module) {
  699. return ((mp_obj_module_t*)MP_OBJ_TO_PTR(module))->globals;
  700. }
  701. // check if given module object is a package
  702. bool mp_obj_is_package(mp_obj_t module);
  703. // staticmethod and classmethod types; defined here so we can make const versions
  704. // this structure is used for instances of both staticmethod and classmethod
  705. typedef struct _mp_obj_static_class_method_t {
  706. mp_obj_base_t base;
  707. mp_obj_t fun;
  708. } mp_obj_static_class_method_t;
  709. typedef struct _mp_rom_obj_static_class_method_t {
  710. mp_obj_base_t base;
  711. mp_rom_obj_t fun;
  712. } mp_rom_obj_static_class_method_t;
  713. // property
  714. const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
  715. // sequence helpers
  716. // slice indexes resolved to particular sequence
  717. typedef struct {
  718. mp_uint_t start;
  719. mp_uint_t stop;
  720. mp_int_t step;
  721. } mp_bound_slice_t;
  722. void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest);
  723. #if MICROPY_PY_BUILTINS_SLICE
  724. bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes);
  725. #endif
  726. #define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
  727. #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)); }
  728. bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2);
  729. 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);
  730. mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args);
  731. mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value);
  732. mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
  733. // Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
  734. #define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte*)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
  735. #define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_sz) \
  736. /*printf("memcpy(%p, %p, %d)\n", dest + beg, slice, slice_len * (item_sz));*/ \
  737. memcpy(((char*)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \
  738. /*printf("memmove(%p, %p, %d)\n", dest + (beg + slice_len), dest + end, (dest_len - end) * (item_sz));*/ \
  739. memmove(((char*)dest) + (beg + slice_len) * (item_sz), ((char*)dest) + (end) * (item_sz), (dest_len - end) * (item_sz));
  740. // Note: dest and slice regions may overlap
  741. #define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_sz) \
  742. /*printf("memmove(%p, %p, %d)\n", dest + beg + len_adj, dest + beg, (dest_len - beg) * (item_sz));*/ \
  743. memmove(((char*)dest) + (beg + slice_len) * (item_sz), ((char*)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \
  744. memmove(((char*)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));
  745. #endif // MICROPY_INCLUDED_PY_OBJ_H