obj.h 38 KB

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