obj.h 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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 <assert.h>
  29. #include "py/mpconfig.h"
  30. #include "py/misc.h"
  31. #include "py/qstr.h"
  32. #include "py/mpprint.h"
  33. #include "py/runtime0.h"
  34. // This is the definition of the opaque MicroPython object type.
  35. // All concrete objects have an encoding within this type and the
  36. // particular encoding is specified by MICROPY_OBJ_REPR.
  37. #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
  38. typedef uint64_t mp_obj_t;
  39. typedef uint64_t mp_const_obj_t;
  40. #else
  41. typedef void *mp_obj_t;
  42. typedef const void *mp_const_obj_t;
  43. #endif
  44. // This mp_obj_type_t struct is a concrete MicroPython object which holds info
  45. // about a type. See below for actual definition of the struct.
  46. typedef struct _mp_obj_type_t mp_obj_type_t;
  47. // Anything that wants to be a concrete MicroPython object must have mp_obj_base_t
  48. // as its first member (small ints, qstr objs and inline floats are not concrete).
  49. struct _mp_obj_base_t {
  50. const mp_obj_type_t *type MICROPY_OBJ_BASE_ALIGNMENT;
  51. };
  52. typedef struct _mp_obj_base_t mp_obj_base_t;
  53. // These fake objects are used to indicate certain things in arguments or return
  54. // values, and should only be used when explicitly allowed.
  55. //
  56. // - MP_OBJ_NULL : used to indicate the absence of an object, or unsupported operation.
  57. // - MP_OBJ_STOP_ITERATION : used instead of throwing a StopIteration, for efficiency.
  58. // - MP_OBJ_SENTINEL : used for various internal purposes where one needs
  59. // an object which is unique from all other objects, including MP_OBJ_NULL.
  60. //
  61. // For debugging purposes they are all different. For non-debug mode, we alias
  62. // as many as we can to MP_OBJ_NULL because it's cheaper to load/compare 0.
  63. #if MICROPY_DEBUG_MP_OBJ_SENTINELS
  64. #define MP_OBJ_NULL (MP_OBJ_FROM_PTR((void *)0))
  65. #define MP_OBJ_STOP_ITERATION (MP_OBJ_FROM_PTR((void *)4))
  66. #define MP_OBJ_SENTINEL (MP_OBJ_FROM_PTR((void *)8))
  67. #else
  68. #define MP_OBJ_NULL (MP_OBJ_FROM_PTR((void *)0))
  69. #define MP_OBJ_STOP_ITERATION (MP_OBJ_FROM_PTR((void *)0))
  70. #define MP_OBJ_SENTINEL (MP_OBJ_FROM_PTR((void *)4))
  71. #endif
  72. // These macros/inline functions operate on objects and depend on the
  73. // particular object representation. They are used to query, pack and
  74. // unpack small ints, qstrs and full object pointers.
  75. #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_A
  76. static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
  77. return (((mp_int_t)(o)) & 1) != 0;
  78. }
  79. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
  80. #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
  81. static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
  82. return (((mp_int_t)(o)) & 7) == 2;
  83. }
  84. #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 3)
  85. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 3) | 2))
  86. static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
  87. return (((mp_int_t)(o)) & 7) == 6;
  88. }
  89. #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 3)
  90. #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 3) | 6))
  91. #if MICROPY_PY_BUILTINS_FLOAT
  92. #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
  93. #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
  94. #if MICROPY_PY_MATH_CONSTANTS
  95. #define mp_const_float_tau MP_ROM_PTR(&mp_const_float_tau_obj)
  96. #define mp_const_float_inf MP_ROM_PTR(&mp_const_float_inf_obj)
  97. #define mp_const_float_nan MP_ROM_PTR(&mp_const_float_nan_obj)
  98. #endif
  99. extern const struct _mp_obj_float_t mp_const_float_e_obj;
  100. extern const struct _mp_obj_float_t mp_const_float_pi_obj;
  101. #if MICROPY_PY_MATH_CONSTANTS
  102. extern const struct _mp_obj_float_t mp_const_float_tau_obj;
  103. extern const struct _mp_obj_float_t mp_const_float_inf_obj;
  104. extern const struct _mp_obj_float_t mp_const_float_nan_obj;
  105. #endif
  106. #define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
  107. mp_float_t mp_obj_float_get(mp_obj_t self_in);
  108. mp_obj_t mp_obj_new_float(mp_float_t value);
  109. #endif
  110. static inline bool mp_obj_is_obj(mp_const_obj_t o) {
  111. return (((mp_int_t)(o)) & 3) == 0;
  112. }
  113. #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B
  114. static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
  115. return (((mp_int_t)(o)) & 3) == 1;
  116. }
  117. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 2)
  118. #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 2) | 1))
  119. static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
  120. return (((mp_int_t)(o)) & 7) == 3;
  121. }
  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. static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
  125. return (((mp_int_t)(o)) & 7) == 7;
  126. }
  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))
  129. #if MICROPY_PY_BUILTINS_FLOAT
  130. #define mp_const_float_e MP_ROM_PTR(&mp_const_float_e_obj)
  131. #define mp_const_float_pi MP_ROM_PTR(&mp_const_float_pi_obj)
  132. #if MICROPY_PY_MATH_CONSTANTS
  133. #define mp_const_float_tau MP_ROM_PTR(&mp_const_float_tau_obj)
  134. #define mp_const_float_inf MP_ROM_PTR(&mp_const_float_inf_obj)
  135. #define mp_const_float_nan MP_ROM_PTR(&mp_const_float_nan_obj)
  136. #endif
  137. extern const struct _mp_obj_float_t mp_const_float_e_obj;
  138. extern const struct _mp_obj_float_t mp_const_float_pi_obj;
  139. #if MICROPY_PY_MATH_CONSTANTS
  140. extern const struct _mp_obj_float_t mp_const_float_tau_obj;
  141. extern const struct _mp_obj_float_t mp_const_float_inf_obj;
  142. extern const struct _mp_obj_float_t mp_const_float_nan_obj;
  143. #endif
  144. #define mp_obj_is_float(o) mp_obj_is_type((o), &mp_type_float)
  145. mp_float_t mp_obj_float_get(mp_obj_t self_in);
  146. mp_obj_t mp_obj_new_float(mp_float_t value);
  147. #endif
  148. static inline bool mp_obj_is_obj(mp_const_obj_t o) {
  149. return (((mp_int_t)(o)) & 1) == 0;
  150. }
  151. #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
  152. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_NONE
  153. #error "MICROPY_OBJ_REPR_C requires float to be enabled."
  154. #endif
  155. static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
  156. return (((mp_int_t)(o)) & 1) != 0;
  157. }
  158. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)(o)) >> 1)
  159. #define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)((((mp_uint_t)(small_int)) << 1) | 1))
  160. #if MICROPY_PY_BUILTINS_FLOAT
  161. #define mp_const_float_e MP_ROM_PTR((mp_obj_t)(((0x402df854 & ~3) | 2) + 0x80800000))
  162. #define mp_const_float_pi MP_ROM_PTR((mp_obj_t)(((0x40490fdb & ~3) | 2) + 0x80800000))
  163. #if MICROPY_PY_MATH_CONSTANTS
  164. #define mp_const_float_tau MP_ROM_PTR((mp_obj_t)(((0x40c90fdb & ~3) | 2) + 0x80800000))
  165. #define mp_const_float_inf MP_ROM_PTR((mp_obj_t)(((0x7f800000 & ~3) | 2) + 0x80800000))
  166. #define mp_const_float_nan MP_ROM_PTR((mp_obj_t)(((0xffc00000 & ~3) | 2) + 0x80800000))
  167. #endif
  168. static inline bool mp_obj_is_float(mp_const_obj_t o) {
  169. // Ensure that 32-bit arch can only use single precision.
  170. MP_STATIC_ASSERT(sizeof(mp_float_t) <= sizeof(mp_obj_t));
  171. return (((mp_uint_t)(o)) & 3) == 2 && (((mp_uint_t)(o)) & 0xff800007) != 0x00000006;
  172. }
  173. static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
  174. union {
  175. mp_float_t f;
  176. mp_uint_t u;
  177. } num = {.u = ((mp_uint_t)o - 0x80800000) & ~3};
  178. return num.f;
  179. }
  180. static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
  181. union {
  182. mp_float_t f;
  183. mp_uint_t u;
  184. } num = {.f = f};
  185. return (mp_obj_t)(((num.u & ~0x3) | 2) + 0x80800000);
  186. }
  187. #endif
  188. static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
  189. return (((mp_uint_t)(o)) & 0xff80000f) == 0x00000006;
  190. }
  191. #define MP_OBJ_QSTR_VALUE(o) (((mp_uint_t)(o)) >> 4)
  192. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 4) | 0x00000006))
  193. static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
  194. return (((mp_uint_t)(o)) & 0xff80000f) == 0x0000000e;
  195. }
  196. #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) (((mp_uint_t)(o)) >> 4)
  197. #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) ((mp_obj_t)(((val) << 4) | 0xe))
  198. static inline bool mp_obj_is_obj(mp_const_obj_t o) {
  199. return (((mp_int_t)(o)) & 3) == 0;
  200. }
  201. #elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D
  202. static inline bool mp_obj_is_small_int(mp_const_obj_t o) {
  203. return (((uint64_t)(o)) & 0xffff000000000000) == 0x0001000000000000;
  204. }
  205. #define MP_OBJ_SMALL_INT_VALUE(o) (((mp_int_t)((o) << 16)) >> 17)
  206. #define MP_OBJ_NEW_SMALL_INT(small_int) (((((uint64_t)(small_int)) & 0x7fffffffffff) << 1) | 0x0001000000000001)
  207. static inline bool mp_obj_is_qstr(mp_const_obj_t o) {
  208. return (((uint64_t)(o)) & 0xffff000000000000) == 0x0002000000000000;
  209. }
  210. #define MP_OBJ_QSTR_VALUE(o) ((((uint32_t)(o)) >> 1) & 0xffffffff)
  211. #define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)(((uint64_t)(((uint32_t)(qst)) << 1)) | 0x0002000000000001))
  212. static inline bool mp_obj_is_immediate_obj(mp_const_obj_t o) {
  213. return (((uint64_t)(o)) & 0xffff000000000000) == 0x0003000000000000;
  214. }
  215. #define MP_OBJ_IMMEDIATE_OBJ_VALUE(o) ((((uint32_t)(o)) >> 46) & 3)
  216. #define MP_OBJ_NEW_IMMEDIATE_OBJ(val) (((uint64_t)(val) << 46) | 0x0003000000000000)
  217. #if MICROPY_PY_BUILTINS_FLOAT
  218. #if MICROPY_FLOAT_IMPL != MICROPY_FLOAT_IMPL_DOUBLE
  219. #error MICROPY_OBJ_REPR_D requires MICROPY_FLOAT_IMPL_DOUBLE
  220. #endif
  221. #define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b145769 + 0x8004000000000000))}
  222. #define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))}
  223. #if MICROPY_PY_MATH_CONSTANTS
  224. #define mp_const_float_tau {((mp_obj_t)((uint64_t)0x401921fb54442d18 + 0x8004000000000000))}
  225. #define mp_const_float_inf {((mp_obj_t)((uint64_t)0x7ff0000000000000 + 0x8004000000000000))}
  226. #define mp_const_float_nan {((mp_obj_t)((uint64_t)0xfff8000000000000 + 0x8004000000000000))}
  227. #endif
  228. static inline bool mp_obj_is_float(mp_const_obj_t o) {
  229. return ((uint64_t)(o) & 0xfffc000000000000) != 0;
  230. }
  231. static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) {
  232. union {
  233. mp_float_t f;
  234. uint64_t r;
  235. } num = {.r = o - 0x8004000000000000};
  236. return num.f;
  237. }
  238. static inline mp_obj_t mp_obj_new_float(mp_float_t f) {
  239. union {
  240. mp_float_t f;
  241. uint64_t r;
  242. } num = {.f = f};
  243. return num.r + 0x8004000000000000;
  244. }
  245. #endif
  246. static inline bool mp_obj_is_obj(mp_const_obj_t o) {
  247. return (((uint64_t)(o)) & 0xffff000000000000) == 0x0000000000000000;
  248. }
  249. #define MP_OBJ_TO_PTR(o) ((void *)(uintptr_t)(o))
  250. #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p)))
  251. // rom object storage needs special handling to widen 32-bit pointer to 64-bits
  252. typedef union _mp_rom_obj_t {
  253. uint64_t u64;
  254. struct {
  255. const void *lo, *hi;
  256. } u32;
  257. } mp_rom_obj_t;
  258. #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
  259. #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
  260. #if MP_ENDIANNESS_LITTLE
  261. #define MP_ROM_PTR(p) {.u32 = {.lo = (p), .hi = NULL}}
  262. #else
  263. #define MP_ROM_PTR(p) {.u32 = {.lo = NULL, .hi = (p)}}
  264. #endif
  265. #endif
  266. // Macros to convert between mp_obj_t and concrete object types.
  267. // These are identity operations in MicroPython, but ability to override
  268. // these operations are provided to experiment with other methods of
  269. // object representation and memory management.
  270. // Cast mp_obj_t to object pointer
  271. #ifndef MP_OBJ_TO_PTR
  272. #define MP_OBJ_TO_PTR(o) ((void *)(o))
  273. #endif
  274. // Cast object pointer to mp_obj_t
  275. #ifndef MP_OBJ_FROM_PTR
  276. #define MP_OBJ_FROM_PTR(p) ((mp_obj_t)(p))
  277. #endif
  278. // Macros to create objects that are stored in ROM.
  279. #ifndef MP_ROM_NONE
  280. #if MICROPY_OBJ_IMMEDIATE_OBJS
  281. #define MP_ROM_NONE mp_const_none
  282. #else
  283. #define MP_ROM_NONE MP_ROM_PTR(&mp_const_none_obj)
  284. #endif
  285. #endif
  286. #ifndef MP_ROM_FALSE
  287. #if MICROPY_OBJ_IMMEDIATE_OBJS
  288. #define MP_ROM_FALSE mp_const_false
  289. #define MP_ROM_TRUE mp_const_true
  290. #else
  291. #define MP_ROM_FALSE MP_ROM_PTR(&mp_const_false_obj)
  292. #define MP_ROM_TRUE MP_ROM_PTR(&mp_const_true_obj)
  293. #endif
  294. #endif
  295. #ifndef MP_ROM_INT
  296. typedef mp_const_obj_t mp_rom_obj_t;
  297. #define MP_ROM_INT(i) MP_OBJ_NEW_SMALL_INT(i)
  298. #define MP_ROM_QSTR(q) MP_OBJ_NEW_QSTR(q)
  299. #define MP_ROM_PTR(p) (p)
  300. /* for testing
  301. typedef struct _mp_rom_obj_t { mp_const_obj_t o; } mp_rom_obj_t;
  302. #define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)}
  303. #define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)}
  304. #define MP_ROM_PTR(p) {.o = p}
  305. */
  306. #endif
  307. // These macros are used to declare and define constant function objects
  308. // You can put "static" in front of the definitions to make them local
  309. #define MP_DECLARE_CONST_FUN_OBJ_0(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  310. #define MP_DECLARE_CONST_FUN_OBJ_1(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  311. #define MP_DECLARE_CONST_FUN_OBJ_2(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  312. #define MP_DECLARE_CONST_FUN_OBJ_3(obj_name) extern const mp_obj_fun_builtin_fixed_t obj_name
  313. #define MP_DECLARE_CONST_FUN_OBJ_VAR(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
  314. #define MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
  315. #define MP_DECLARE_CONST_FUN_OBJ_KW(obj_name) extern const mp_obj_fun_builtin_var_t obj_name
  316. #define MP_OBJ_FUN_ARGS_MAX (0xffff) // to set maximum value in n_args_max below
  317. #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)))
  318. #define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) \
  319. const mp_obj_fun_builtin_fixed_t obj_name = \
  320. {{&mp_type_fun_builtin_0}, .fun._0 = fun_name}
  321. #define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) \
  322. const mp_obj_fun_builtin_fixed_t obj_name = \
  323. {{&mp_type_fun_builtin_1}, .fun._1 = fun_name}
  324. #define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) \
  325. const mp_obj_fun_builtin_fixed_t obj_name = \
  326. {{&mp_type_fun_builtin_2}, .fun._2 = fun_name}
  327. #define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) \
  328. const mp_obj_fun_builtin_fixed_t obj_name = \
  329. {{&mp_type_fun_builtin_3}, .fun._3 = fun_name}
  330. #define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) \
  331. const mp_obj_fun_builtin_var_t obj_name = \
  332. {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, false), .fun.var = fun_name}
  333. #define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) \
  334. const mp_obj_fun_builtin_var_t obj_name = \
  335. {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, false), .fun.var = fun_name}
  336. #define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, n_args_min, fun_name) \
  337. const mp_obj_fun_builtin_var_t obj_name = \
  338. {{&mp_type_fun_builtin_var}, MP_OBJ_FUN_MAKE_SIG(n_args_min, MP_OBJ_FUN_ARGS_MAX, true), .fun.kw = fun_name}
  339. // These macros are used to define constant map/dict objects
  340. // You can put "static" in front of the definition to make it local
  341. #define MP_DEFINE_CONST_MAP(map_name, table_name) \
  342. const mp_map_t map_name = { \
  343. .all_keys_are_qstrs = 1, \
  344. .is_fixed = 1, \
  345. .is_ordered = 1, \
  346. .used = MP_ARRAY_SIZE(table_name), \
  347. .alloc = MP_ARRAY_SIZE(table_name), \
  348. .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)table_name, \
  349. }
  350. #define MP_DEFINE_CONST_DICT_WITH_SIZE(dict_name, table_name, n) \
  351. const mp_obj_dict_t dict_name = { \
  352. .base = {&mp_type_dict}, \
  353. .map = { \
  354. .all_keys_are_qstrs = 1, \
  355. .is_fixed = 1, \
  356. .is_ordered = 1, \
  357. .used = n, \
  358. .alloc = n, \
  359. .table = (mp_map_elem_t *)(mp_rom_map_elem_t *)table_name, \
  360. }, \
  361. }
  362. #define MP_DEFINE_CONST_DICT(dict_name, table_name) MP_DEFINE_CONST_DICT_WITH_SIZE(dict_name, table_name, MP_ARRAY_SIZE(table_name))
  363. // These macros are used to declare and define constant staticmethod and classmethod objects
  364. // You can put "static" in front of the definitions to make them local
  365. #define MP_DECLARE_CONST_STATICMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
  366. #define MP_DECLARE_CONST_CLASSMETHOD_OBJ(obj_name) extern const mp_rom_obj_static_class_method_t obj_name
  367. #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}
  368. #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}
  369. #ifndef NO_QSTR
  370. // Declare a module as a builtin, processed by makemoduledefs.py
  371. // param module_name: MP_QSTR_<module name>
  372. // param obj_module: mp_obj_module_t instance
  373. #define MP_REGISTER_MODULE(module_name, obj_module)
  374. // As above, but allow this module to be extended from the filesystem.
  375. #define MP_REGISTER_EXTENSIBLE_MODULE(module_name, obj_module)
  376. // Add a custom handler for a builtin module that will be called to delegate
  377. // failed attribute lookups.
  378. #define MP_REGISTER_MODULE_DELEGATION(obj_module, fun_name)
  379. // Declare a root pointer (to avoid garbage collection of a global static variable).
  380. // param variable_declaration: a valid C variable declaration
  381. #define MP_REGISTER_ROOT_POINTER(variable_declaration)
  382. #endif // NO_QSTR
  383. // Underlying map/hash table implementation (not dict object or map function)
  384. typedef struct _mp_map_elem_t {
  385. mp_obj_t key;
  386. mp_obj_t value;
  387. } mp_map_elem_t;
  388. typedef struct _mp_rom_map_elem_t {
  389. mp_rom_obj_t key;
  390. mp_rom_obj_t value;
  391. } mp_rom_map_elem_t;
  392. typedef struct _mp_map_t {
  393. size_t all_keys_are_qstrs : 1;
  394. size_t is_fixed : 1; // if set, table is fixed/read-only and can't be modified
  395. size_t is_ordered : 1; // if set, table is an ordered array, not a hash map
  396. size_t used : (8 * sizeof(size_t) - 3);
  397. size_t alloc;
  398. mp_map_elem_t *table;
  399. } mp_map_t;
  400. // mp_set_lookup requires these constants to have the values they do
  401. typedef enum _mp_map_lookup_kind_t {
  402. 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
  406. } mp_map_lookup_kind_t;
  407. static inline bool mp_map_slot_is_filled(const mp_map_t *map, size_t pos) {
  408. assert(pos < map->alloc);
  409. return (map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL;
  410. }
  411. void mp_map_init(mp_map_t *map, size_t n);
  412. void mp_map_init_fixed_table(mp_map_t *map, size_t n, const mp_obj_t *table);
  413. mp_map_t *mp_map_new(size_t n);
  414. void mp_map_deinit(mp_map_t *map);
  415. void mp_map_free(mp_map_t *map);
  416. mp_map_elem_t *mp_map_lookup(mp_map_t *map, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
  417. void mp_map_clear(mp_map_t *map);
  418. void mp_map_dump(mp_map_t *map);
  419. // Underlying set implementation (not set object)
  420. typedef struct _mp_set_t {
  421. size_t alloc;
  422. size_t used;
  423. mp_obj_t *table;
  424. } mp_set_t;
  425. static inline bool mp_set_slot_is_filled(const mp_set_t *set, size_t pos) {
  426. return (set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL;
  427. }
  428. void mp_set_init(mp_set_t *set, size_t n);
  429. mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
  430. mp_obj_t mp_set_remove_first(mp_set_t *set);
  431. void mp_set_clear(mp_set_t *set);
  432. // Type definitions for methods
  433. typedef mp_obj_t (*mp_fun_0_t)(void);
  434. typedef mp_obj_t (*mp_fun_1_t)(mp_obj_t);
  435. typedef mp_obj_t (*mp_fun_2_t)(mp_obj_t, mp_obj_t);
  436. typedef mp_obj_t (*mp_fun_3_t)(mp_obj_t, mp_obj_t, mp_obj_t);
  437. typedef mp_obj_t (*mp_fun_var_t)(size_t n, const mp_obj_t *);
  438. // mp_fun_kw_t takes mp_map_t* (and not const mp_map_t*) to ease passing
  439. // this arg to mp_map_lookup().
  440. typedef mp_obj_t (*mp_fun_kw_t)(size_t n, const mp_obj_t *, mp_map_t *);
  441. // Flags for type behaviour (mp_obj_type_t.flags)
  442. // If MP_TYPE_FLAG_EQ_NOT_REFLEXIVE is clear then __eq__ is reflexive (A==A returns True).
  443. // If MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE is clear then the type can't be equal to an
  444. // instance of any different class that also clears this flag. If this flag is set
  445. // then the type may check for equality against a different type.
  446. // If MP_TYPE_FLAG_EQ_HAS_NEQ_TEST is clear then the type only implements the __eq__
  447. // operator and not the __ne__ operator. If it's set then __ne__ may be implemented.
  448. // If MP_TYPE_FLAG_BINDS_SELF is set then the type as a method binds self as the first arg.
  449. // If MP_TYPE_FLAG_BUILTIN_FUN is set then the type is a built-in function type.
  450. // MP_TYPE_FLAG_ITER_IS_GETITER is a no-op flag that means the default behaviour for the
  451. // iter slot and it's the getiter function.
  452. // If MP_TYPE_FLAG_ITER_IS_ITERNEXT is set then the "iter" slot is the iternext
  453. // function and getiter will be automatically implemented as "return self".
  454. // If MP_TYPE_FLAG_ITER_IS_CUSTOM is set then the "iter" slot is a pointer to a
  455. // mp_getiter_iternext_custom_t struct instance (with both .getiter and .iternext set).
  456. // If MP_TYPE_FLAG_ITER_IS_STREAM is set then the type implicitly gets a "return self"
  457. // getiter, and mp_stream_unbuffered_iter for iternext.
  458. // If MP_TYPE_FLAG_INSTANCE_TYPE is set then this is an instance type (i.e. defined in Python).
  459. #define MP_TYPE_FLAG_NONE (0x0000)
  460. #define MP_TYPE_FLAG_IS_SUBCLASSED (0x0001)
  461. #define MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS (0x0002)
  462. #define MP_TYPE_FLAG_EQ_NOT_REFLEXIVE (0x0004)
  463. #define MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE (0x0008)
  464. #define MP_TYPE_FLAG_EQ_HAS_NEQ_TEST (0x0010)
  465. #define MP_TYPE_FLAG_BINDS_SELF (0x0020)
  466. #define MP_TYPE_FLAG_BUILTIN_FUN (0x0040)
  467. #define MP_TYPE_FLAG_ITER_IS_GETITER (0x0000)
  468. #define MP_TYPE_FLAG_ITER_IS_ITERNEXT (0x0080)
  469. #define MP_TYPE_FLAG_ITER_IS_CUSTOM (0x0100)
  470. #define MP_TYPE_FLAG_ITER_IS_STREAM (MP_TYPE_FLAG_ITER_IS_ITERNEXT | MP_TYPE_FLAG_ITER_IS_CUSTOM)
  471. #define MP_TYPE_FLAG_INSTANCE_TYPE (0x0200)
  472. typedef enum {
  473. PRINT_STR = 0,
  474. PRINT_REPR = 1,
  475. PRINT_EXC = 2, // Special format for printing exception in unhandled exception message
  476. PRINT_JSON = 3,
  477. PRINT_RAW = 4, // Special format for printing bytes as an undercorated string
  478. PRINT_EXC_SUBCLASS = 0x80, // Internal flag for printing exception subclasses
  479. } mp_print_kind_t;
  480. typedef struct _mp_obj_iter_buf_t {
  481. mp_obj_base_t base;
  482. mp_obj_t buf[3];
  483. } mp_obj_iter_buf_t;
  484. // The number of slots that an mp_obj_iter_buf_t needs on the Python value stack.
  485. // It's rounded up in case mp_obj_base_t is smaller than mp_obj_t (eg for OBJ_REPR_D).
  486. #define MP_OBJ_ITER_BUF_NSLOTS ((sizeof(mp_obj_iter_buf_t) + sizeof(mp_obj_t) - 1) / sizeof(mp_obj_t))
  487. typedef void (*mp_print_fun_t)(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind);
  488. 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);
  489. 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);
  490. typedef mp_obj_t (*mp_unary_op_fun_t)(mp_unary_op_t op, mp_obj_t);
  491. typedef mp_obj_t (*mp_binary_op_fun_t)(mp_binary_op_t op, mp_obj_t, mp_obj_t);
  492. typedef void (*mp_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
  493. typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
  494. typedef mp_obj_t (*mp_getiter_fun_t)(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf);
  495. typedef mp_fun_1_t mp_iternext_fun_t;
  496. // For MP_TYPE_FLAG_ITER_IS_CUSTOM, the "getiter" slot points to an instance of this type.
  497. typedef struct _mp_getiter_iternext_custom_t {
  498. mp_getiter_fun_t getiter;
  499. mp_iternext_fun_t iternext;
  500. } mp_getiter_iternext_custom_t;
  501. // Buffer protocol
  502. typedef struct _mp_buffer_info_t {
  503. void *buf; // can be NULL if len == 0
  504. size_t len; // in bytes
  505. int typecode; // as per binary.h
  506. } mp_buffer_info_t;
  507. #define MP_BUFFER_READ (1)
  508. #define MP_BUFFER_WRITE (2)
  509. #define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
  510. #define MP_BUFFER_RAISE_IF_UNSUPPORTED (4)
  511. typedef mp_int_t (*mp_buffer_fun_t)(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
  512. bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags);
  513. static inline void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
  514. mp_get_buffer(obj, bufinfo, flags | MP_BUFFER_RAISE_IF_UNSUPPORTED);
  515. }
  516. // This struct will be updated to become a variable sized struct. In order to
  517. // use this as a member, or allocate dynamically, use the mp_obj_empty_type_t
  518. // or mp_obj_full_type_t structs below (which must be kept in sync).
  519. struct _mp_obj_type_t {
  520. // A type is an object so must start with this entry, which points to mp_type_type.
  521. mp_obj_base_t base;
  522. // Flags associated with this type.
  523. uint16_t flags;
  524. // The name of this type, a qstr.
  525. uint16_t name;
  526. // Slots: For the rest of the fields, the slot index points to the
  527. // relevant function in the variable-length "slots" field. Ideally these
  528. // would be only 4 bits, but the extra overhead of accessing them adds
  529. // more code, and we also need to be able to take the address of them for
  530. // mp_obj_class_lookup.
  531. // Corresponds to __new__ and __init__ special methods, to make an instance of the type.
  532. uint8_t slot_index_make_new;
  533. // Corresponds to __repr__ and __str__ special methods.
  534. uint8_t slot_index_print;
  535. // Corresponds to __call__ special method, ie T(...).
  536. uint8_t slot_index_call;
  537. // Implements unary and binary operations.
  538. // Can return MP_OBJ_NULL if the operation is not supported.
  539. uint8_t slot_index_unary_op;
  540. uint8_t slot_index_binary_op;
  541. // Implements load, store and delete attribute.
  542. //
  543. // dest[0] = MP_OBJ_NULL means load
  544. // return: for fail, do nothing
  545. // for fail but continue lookup in locals_dict, dest[1] = MP_OBJ_SENTINEL
  546. // for attr, dest[0] = value
  547. // for method, dest[0] = method, dest[1] = self
  548. //
  549. // dest[0,1] = {MP_OBJ_SENTINEL, MP_OBJ_NULL} means delete
  550. // dest[0,1] = {MP_OBJ_SENTINEL, object} means store
  551. // return: for fail, do nothing
  552. // for success set dest[0] = MP_OBJ_NULL
  553. uint8_t slot_index_attr;
  554. // Implements load, store and delete subscripting:
  555. // - value = MP_OBJ_SENTINEL means load
  556. // - value = MP_OBJ_NULL means delete
  557. // - all other values mean store the value
  558. // Can return MP_OBJ_NULL if operation not supported.
  559. uint8_t slot_index_subscr;
  560. // This slot's behaviour depends on the MP_TYPE_FLAG_ITER_IS_* flags above.
  561. // - If MP_TYPE_FLAG_ITER_IS_GETITER flag is set, then this corresponds to the __iter__
  562. // special method (of type mp_getiter_fun_t). Can use the given mp_obj_iter_buf_t
  563. // to store the iterator object, otherwise can return a pointer to an object on the heap.
  564. // - If MP_TYPE_FLAG_ITER_IS_ITERNEXT is set, then this corresponds to __next__ special method.
  565. // May return MP_OBJ_STOP_ITERATION as an optimisation instead of raising StopIteration()
  566. // with no args. The type will implicitly implement getiter as "return self".
  567. // - If MP_TYPE_FLAG_ITER_IS_CUSTOM is set, then this slot must point to an
  568. // mp_getiter_iternext_custom_t instance with both the getiter and iternext fields set.
  569. // - If MP_TYPE_FLAG_ITER_IS_STREAM is set, this this slot should be unset.
  570. uint8_t slot_index_iter;
  571. // Implements the buffer protocol if supported by this type.
  572. uint8_t slot_index_buffer;
  573. // One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
  574. uint8_t slot_index_protocol;
  575. // A pointer to the parents of this type:
  576. // - 0 parents: pointer is NULL (object is implicitly the single parent)
  577. // - 1 parent: a pointer to the type of that parent
  578. // - 2 or more parents: pointer to a tuple object containing the parent types
  579. uint8_t slot_index_parent;
  580. // A dict mapping qstrs to objects local methods/constants/etc.
  581. uint8_t slot_index_locals_dict;
  582. const void *slots[];
  583. };
  584. // Non-variable sized versions of mp_obj_type_t to be used as a member
  585. // in other structs or for dynamic allocation. The fields are exactly
  586. // as in mp_obj_type_t, but with a fixed size for the flexible array
  587. // members.
  588. typedef struct _mp_obj_empty_type_t {
  589. mp_obj_base_t base;
  590. uint16_t flags;
  591. uint16_t name;
  592. uint8_t slot_index_make_new;
  593. uint8_t slot_index_print;
  594. uint8_t slot_index_call;
  595. uint8_t slot_index_unary_op;
  596. uint8_t slot_index_binary_op;
  597. uint8_t slot_index_attr;
  598. uint8_t slot_index_subscr;
  599. uint8_t slot_index_iter;
  600. uint8_t slot_index_buffer;
  601. uint8_t slot_index_protocol;
  602. uint8_t slot_index_parent;
  603. uint8_t slot_index_locals_dict;
  604. // No slots member.
  605. } mp_obj_empty_type_t;
  606. typedef struct _mp_obj_full_type_t {
  607. mp_obj_base_t base;
  608. uint16_t flags;
  609. uint16_t name;
  610. uint8_t slot_index_make_new;
  611. uint8_t slot_index_print;
  612. uint8_t slot_index_call;
  613. uint8_t slot_index_unary_op;
  614. uint8_t slot_index_binary_op;
  615. uint8_t slot_index_attr;
  616. uint8_t slot_index_subscr;
  617. uint8_t slot_index_iter;
  618. uint8_t slot_index_buffer;
  619. uint8_t slot_index_protocol;
  620. uint8_t slot_index_parent;
  621. uint8_t slot_index_locals_dict;
  622. // Explicitly add 12 slots.
  623. const void *slots[11];
  624. } mp_obj_full_type_t;
  625. #define _MP_OBJ_TYPE_SLOT_TYPE_make_new (mp_make_new_fun_t)
  626. #define _MP_OBJ_TYPE_SLOT_TYPE_print (mp_print_fun_t)
  627. #define _MP_OBJ_TYPE_SLOT_TYPE_call (mp_call_fun_t)
  628. #define _MP_OBJ_TYPE_SLOT_TYPE_unary_op (mp_unary_op_fun_t)
  629. #define _MP_OBJ_TYPE_SLOT_TYPE_binary_op (mp_binary_op_fun_t)
  630. #define _MP_OBJ_TYPE_SLOT_TYPE_attr (mp_attr_fun_t)
  631. #define _MP_OBJ_TYPE_SLOT_TYPE_subscr (mp_subscr_fun_t)
  632. #define _MP_OBJ_TYPE_SLOT_TYPE_iter (const void *)
  633. #define _MP_OBJ_TYPE_SLOT_TYPE_buffer (mp_buffer_fun_t)
  634. #define _MP_OBJ_TYPE_SLOT_TYPE_protocol (const void *)
  635. #define _MP_OBJ_TYPE_SLOT_TYPE_parent (const void *)
  636. #define _MP_OBJ_TYPE_SLOT_TYPE_locals_dict (struct _mp_obj_dict_t *)
  637. // Implementation of MP_DEFINE_CONST_OBJ_TYPE for each number of arguments.
  638. // Do not use these directly, instead use MP_DEFINE_CONST_OBJ_TYPE.
  639. // Generated with:
  640. // for i in range(13):
  641. // print(f"#define MP_DEFINE_CONST_OBJ_TYPE_NARGS_{i}(_struct_type, _typename, _name, _flags{''.join(f', f{j+1}, v{j+1}' for j in range(i))}) const _struct_type _typename = {{ .base = {{ &mp_type_type }}, .flags = _flags, .name = _name{''.join(f', .slot_index_##f{j+1} = {j+1}' for j in range(i))}{', .slots = { ' + ''.join(f'v{j+1}, ' for j in range(i)) + '}' if i else '' } }}")
  642. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_0(_struct_type, _typename, _name, _flags) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name }
  643. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_1(_struct_type, _typename, _name, _flags, f1, v1) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slots = { v1, } }
  644. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_2(_struct_type, _typename, _name, _flags, f1, v1, f2, v2) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slots = { v1, v2, } }
  645. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_3(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slots = { v1, v2, v3, } }
  646. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_4(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slots = { v1, v2, v3, v4, } }
  647. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_5(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slots = { v1, v2, v3, v4, v5, } }
  648. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_6(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slots = { v1, v2, v3, v4, v5, v6, } }
  649. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_7(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slots = { v1, v2, v3, v4, v5, v6, v7, } }
  650. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_8(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, } }
  651. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_9(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, } }
  652. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_10(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, } }
  653. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_11(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, } }
  654. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_12(_struct_type, _typename, _name, _flags, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const _struct_type _typename = { .base = { &mp_type_type }, .flags = _flags, .name = _name, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slot_index_##f12 = 12, .slots = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, } }
  655. // Because the mp_obj_type_t instances are in (zero-initialised) ROM, we take
  656. // slot_index_foo=0 to mean that the slot is unset. This also simplifies checking
  657. // if the slot is set. That means that we need to store index+1 in slot_index_foo
  658. // though and then access it as slots[slot_index_foo - 1]. This is an implementation
  659. // detail, the user of these macros doesn't need to be aware of it, and when using
  660. // MP_OBJ_TYPE_OFFSETOF_SLOT you should use zero-based indexing.
  661. #define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->slot_index_##f)
  662. #define MP_OBJ_TYPE_GET_SLOT(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(t)->slots[(t)->slot_index_##f - 1])
  663. #define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(MP_OBJ_TYPE_HAS_SLOT(t, f) ? MP_OBJ_TYPE_GET_SLOT(t, f) : NULL))
  664. #define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->slot_index_##f = (n) + 1, (t)->slots[(n)] = (void *)v)
  665. #define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, slot_index_##f))
  666. #define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(uint8_t *)((char *)(t) + (offset)) != 0)
  667. // Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked
  668. #define MP_DEFINE_CONST_OBJ_TYPE_EXPAND(x) x
  669. // This macro evaluates to MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N, where N is the value
  670. // of the 29th argument (29 is 13*2 + 3).
  671. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, N, ...) MP_DEFINE_CONST_OBJ_TYPE_NARGS_##N
  672. // This macros is used to define a object type in ROM.
  673. // Invoke as MP_DEFINE_CONST_OBJ_TYPE(_typename, _name, _flags, _make_new [, slot, func]*)
  674. // It uses the number of arguments to select which MP_DEFINE_CONST_OBJ_TYPE_*
  675. // macro to use based on the number of arguments. It works by shifting the
  676. // numeric values 12, 11, ... 0 by the number of arguments, such that the
  677. // 29th argument ends up being the number to use. The _INV values are
  678. // placeholders because the slot arguments come in pairs.
  679. #define MP_DEFINE_CONST_OBJ_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_EXPAND(MP_DEFINE_CONST_OBJ_TYPE_NARGS(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0)(mp_obj_type_t, __VA_ARGS__))
  680. // Constant types, globally accessible
  681. extern const mp_obj_type_t mp_type_type;
  682. extern const mp_obj_type_t mp_type_object;
  683. extern const mp_obj_type_t mp_type_NoneType;
  684. extern const mp_obj_type_t mp_type_bool;
  685. extern const mp_obj_type_t mp_type_int;
  686. extern const mp_obj_type_t mp_type_str;
  687. extern const mp_obj_type_t mp_type_bytes;
  688. extern const mp_obj_type_t mp_type_bytearray;
  689. extern const mp_obj_type_t mp_type_memoryview;
  690. extern const mp_obj_type_t mp_type_float;
  691. extern const mp_obj_type_t mp_type_complex;
  692. extern const mp_obj_type_t mp_type_tuple;
  693. extern const mp_obj_type_t mp_type_list;
  694. extern const mp_obj_type_t mp_type_map; // map (the python builtin, not the dict implementation detail)
  695. extern const mp_obj_type_t mp_type_enumerate;
  696. extern const mp_obj_type_t mp_type_filter;
  697. extern const mp_obj_type_t mp_type_deque;
  698. extern const mp_obj_type_t mp_type_dict;
  699. extern const mp_obj_type_t mp_type_ordereddict;
  700. extern const mp_obj_type_t mp_type_range;
  701. extern const mp_obj_type_t mp_type_set;
  702. extern const mp_obj_type_t mp_type_frozenset;
  703. extern const mp_obj_type_t mp_type_slice;
  704. extern const mp_obj_type_t mp_type_zip;
  705. extern const mp_obj_type_t mp_type_array;
  706. extern const mp_obj_type_t mp_type_super;
  707. extern const mp_obj_type_t mp_type_gen_wrap;
  708. extern const mp_obj_type_t mp_type_native_gen_wrap;
  709. extern const mp_obj_type_t mp_type_gen_instance;
  710. extern const mp_obj_type_t mp_type_fun_builtin_0;
  711. extern const mp_obj_type_t mp_type_fun_builtin_1;
  712. extern const mp_obj_type_t mp_type_fun_builtin_2;
  713. extern const mp_obj_type_t mp_type_fun_builtin_3;
  714. extern const mp_obj_type_t mp_type_fun_builtin_var;
  715. extern const mp_obj_type_t mp_type_fun_bc;
  716. extern const mp_obj_type_t mp_type_fun_native;
  717. extern const mp_obj_type_t mp_type_fun_viper;
  718. extern const mp_obj_type_t mp_type_fun_asm;
  719. extern const mp_obj_type_t mp_type_module;
  720. extern const mp_obj_type_t mp_type_staticmethod;
  721. extern const mp_obj_type_t mp_type_classmethod;
  722. extern const mp_obj_type_t mp_type_bound_meth;
  723. extern const mp_obj_type_t mp_type_property;
  724. extern const mp_obj_type_t mp_type_stringio;
  725. extern const mp_obj_type_t mp_type_bytesio;
  726. extern const mp_obj_type_t mp_type_reversed;
  727. extern const mp_obj_type_t mp_type_polymorph_iter;
  728. #if MICROPY_ENABLE_FINALISER
  729. extern const mp_obj_type_t mp_type_polymorph_iter_with_finaliser;
  730. #endif
  731. // Exceptions
  732. extern const mp_obj_type_t mp_type_BaseException;
  733. extern const mp_obj_type_t mp_type_ArithmeticError;
  734. extern const mp_obj_type_t mp_type_AssertionError;
  735. extern const mp_obj_type_t mp_type_AttributeError;
  736. extern const mp_obj_type_t mp_type_EOFError;
  737. extern const mp_obj_type_t mp_type_Exception;
  738. extern const mp_obj_type_t mp_type_GeneratorExit;
  739. extern const mp_obj_type_t mp_type_ImportError;
  740. extern const mp_obj_type_t mp_type_IndentationError;
  741. extern const mp_obj_type_t mp_type_IndexError;
  742. extern const mp_obj_type_t mp_type_KeyboardInterrupt;
  743. extern const mp_obj_type_t mp_type_KeyError;
  744. extern const mp_obj_type_t mp_type_LookupError;
  745. extern const mp_obj_type_t mp_type_MemoryError;
  746. extern const mp_obj_type_t mp_type_NameError;
  747. extern const mp_obj_type_t mp_type_NotImplementedError;
  748. extern const mp_obj_type_t mp_type_OSError;
  749. extern const mp_obj_type_t mp_type_OverflowError;
  750. extern const mp_obj_type_t mp_type_RuntimeError;
  751. extern const mp_obj_type_t mp_type_StopAsyncIteration;
  752. extern const mp_obj_type_t mp_type_StopIteration;
  753. extern const mp_obj_type_t mp_type_SyntaxError;
  754. extern const mp_obj_type_t mp_type_SystemExit;
  755. extern const mp_obj_type_t mp_type_TypeError;
  756. extern const mp_obj_type_t mp_type_UnicodeError;
  757. extern const mp_obj_type_t mp_type_ValueError;
  758. extern const mp_obj_type_t mp_type_ViperTypeError;
  759. extern const mp_obj_type_t mp_type_ZeroDivisionError;
  760. // Constant objects, globally accessible: None, False, True
  761. // These should always be accessed via the below macros.
  762. #if MICROPY_OBJ_IMMEDIATE_OBJS
  763. // None is even while False/True are odd so their types can be distinguished with 1 bit.
  764. #define mp_const_none MP_OBJ_NEW_IMMEDIATE_OBJ(0)
  765. #define mp_const_false MP_OBJ_NEW_IMMEDIATE_OBJ(1)
  766. #define mp_const_true MP_OBJ_NEW_IMMEDIATE_OBJ(3)
  767. #else
  768. #define mp_const_none (MP_OBJ_FROM_PTR(&mp_const_none_obj))
  769. #define mp_const_false (MP_OBJ_FROM_PTR(&mp_const_false_obj))
  770. #define mp_const_true (MP_OBJ_FROM_PTR(&mp_const_true_obj))
  771. extern const struct _mp_obj_none_t mp_const_none_obj;
  772. extern const struct _mp_obj_bool_t mp_const_false_obj;
  773. extern const struct _mp_obj_bool_t mp_const_true_obj;
  774. #endif
  775. // Constant objects, globally accessible: b'', (), {}, Ellipsis, NotImplemented, GeneratorExit()
  776. // The below macros are for convenience only.
  777. #define mp_const_empty_bytes (MP_OBJ_FROM_PTR(&mp_const_empty_bytes_obj))
  778. #define mp_const_empty_tuple (MP_OBJ_FROM_PTR(&mp_const_empty_tuple_obj))
  779. #define mp_const_notimplemented (MP_OBJ_FROM_PTR(&mp_const_notimplemented_obj))
  780. extern const struct _mp_obj_str_t mp_const_empty_bytes_obj;
  781. extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
  782. extern const struct _mp_obj_dict_t mp_const_empty_dict_obj;
  783. extern const struct _mp_obj_singleton_t mp_const_ellipsis_obj;
  784. extern const struct _mp_obj_singleton_t mp_const_notimplemented_obj;
  785. extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
  786. // Fixed empty map. Useful when calling keyword-receiving functions
  787. // without any keywords from C, etc.
  788. #define mp_const_empty_map (mp_const_empty_dict_obj.map)
  789. // General API for objects
  790. // Helper versions of m_new_obj when you need to immediately set base.type.
  791. // Implementing this as a call rather than inline saves 8 bytes per usage.
  792. #define mp_obj_malloc(struct_type, obj_type) ((struct_type *)mp_obj_malloc_helper(sizeof(struct_type), obj_type))
  793. #define mp_obj_malloc_var(struct_type, var_field, var_type, var_num, obj_type) ((struct_type *)mp_obj_malloc_helper(offsetof(struct_type, var_field) + sizeof(var_type) * (var_num), obj_type))
  794. void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type);
  795. // Object allocation macros for allocating objects that have a finaliser.
  796. #if MICROPY_ENABLE_FINALISER
  797. #define mp_obj_malloc_with_finaliser(struct_type, obj_type) ((struct_type *)mp_obj_malloc_with_finaliser_helper(sizeof(struct_type), obj_type))
  798. #define mp_obj_malloc_var_with_finaliser(struct_type, var_type, var_num, obj_type) ((struct_type *)mp_obj_malloc_with_finaliser_helper(sizeof(struct_type) + sizeof(var_type) * (var_num), obj_type))
  799. void *mp_obj_malloc_with_finaliser_helper(size_t num_bytes, const mp_obj_type_t *type);
  800. #else
  801. #define mp_obj_malloc_with_finaliser(struct_type, obj_type) mp_obj_malloc(struct_type, obj_type)
  802. #define mp_obj_malloc_var_with_finaliser(struct_type, var_type, var_num, obj_type) mp_obj_malloc_var(struct_type, var_type, var_num, obj_type)
  803. #endif
  804. // These macros are derived from more primitive ones and are used to
  805. // check for more specific object types.
  806. // Note: these are kept as macros because inline functions sometimes use much
  807. // more code space than the equivalent macros, depending on the compiler.
  808. // don't use mp_obj_is_exact_type directly; use mp_obj_is_type which provides additional safety checks.
  809. // use the former only if you need to bypass these checks (because you've already checked everything else)
  810. #define mp_obj_is_exact_type(o, t) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type == (t)))
  811. // Type checks are split to a separate, constant result macro. This is so it doesn't hinder the compilers's
  812. // optimizations (other tricks like using ({ expr; exper; }) or (exp, expr, expr) in mp_obj_is_type() result
  813. // in missed optimizations)
  814. #define mp_type_assert_not_bool_int_str_nonetype(t) ( \
  815. MP_STATIC_ASSERT_NONCONSTEXPR((t) != &mp_type_bool), assert((t) != &mp_type_bool), \
  816. MP_STATIC_ASSERT_NONCONSTEXPR((t) != &mp_type_int), assert((t) != &mp_type_int), \
  817. MP_STATIC_ASSERT_NONCONSTEXPR((t) != &mp_type_str), assert((t) != &mp_type_str), \
  818. MP_STATIC_ASSERT_NONCONSTEXPR((t) != &mp_type_NoneType), assert((t) != &mp_type_NoneType), \
  819. 1)
  820. #define mp_obj_is_type(o, t) (mp_type_assert_not_bool_int_str_nonetype(t) && mp_obj_is_exact_type(o, t))
  821. #if MICROPY_OBJ_IMMEDIATE_OBJS
  822. // bool's are immediates, not real objects, so test for the 2 possible values.
  823. #define mp_obj_is_bool(o) ((o) == mp_const_false || (o) == mp_const_true)
  824. #else
  825. #define mp_obj_is_bool(o) mp_obj_is_exact_type(o, &mp_type_bool)
  826. #endif
  827. #define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_exact_type(o, &mp_type_int))
  828. #define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_exact_type(o, &mp_type_str))
  829. #define mp_obj_is_str_or_bytes(o) (mp_obj_is_qstr(o) || (mp_obj_is_obj(o) && MP_OBJ_TYPE_GET_SLOT_OR_NULL(((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type, binary_op) == mp_obj_str_binary_op))
  830. bool mp_obj_is_dict_or_ordereddict(mp_obj_t o);
  831. #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))
  832. mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
  833. static inline mp_obj_t mp_obj_new_bool(mp_int_t x) {
  834. return x ? mp_const_true : mp_const_false;
  835. }
  836. mp_obj_t mp_obj_new_cell(mp_obj_t obj);
  837. mp_obj_t mp_obj_new_int(mp_int_t value);
  838. mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
  839. mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base);
  840. 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)
  841. 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)
  842. mp_obj_t mp_obj_new_str(const char *data, size_t len); // will check utf-8 (raises UnicodeError)
  843. mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len); // input data must be valid utf-8
  844. mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr); // will check utf-8 (raises UnicodeError)
  845. #if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK
  846. mp_obj_t mp_obj_new_str_from_utf8_vstr(vstr_t *vstr); // input data must be valid utf-8
  847. #else
  848. #define mp_obj_new_str_from_utf8_vstr mp_obj_new_str_from_vstr
  849. #endif
  850. mp_obj_t mp_obj_new_bytes_from_vstr(vstr_t *vstr);
  851. mp_obj_t mp_obj_new_bytes(const byte *data, size_t len);
  852. mp_obj_t mp_obj_new_bytearray(size_t n, const void *items);
  853. mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items);
  854. #if MICROPY_PY_BUILTINS_FLOAT
  855. mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
  856. mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
  857. #endif
  858. mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
  859. mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args);
  860. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
  861. #define mp_obj_new_exception_msg(exc_type, msg) mp_obj_new_exception(exc_type)
  862. #define mp_obj_new_exception_msg_varg(exc_type, ...) mp_obj_new_exception(exc_type)
  863. #else
  864. mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
  865. mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
  866. #endif
  867. #ifdef va_start
  868. mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, va_list arg); // same fmt restrictions as above
  869. #endif
  870. mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
  871. mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed, const mp_obj_t *closed);
  872. mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
  873. mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);
  874. mp_obj_t mp_obj_new_dict(size_t n_args);
  875. mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items);
  876. mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
  877. mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
  878. mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf);
  879. mp_obj_t mp_obj_new_module(qstr module_name);
  880. mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items);
  881. const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
  882. const char *mp_obj_get_type_str(mp_const_obj_t o_in);
  883. bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
  884. mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type);
  885. void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
  886. void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
  887. void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc);
  888. bool mp_obj_is_true(mp_obj_t arg);
  889. bool mp_obj_is_callable(mp_obj_t o_in);
  890. mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2);
  891. bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
  892. // returns true if o is bool, small int or long int
  893. static inline bool mp_obj_is_integer(mp_const_obj_t o) {
  894. return mp_obj_is_int(o) || mp_obj_is_bool(o);
  895. }
  896. mp_int_t mp_obj_get_int(mp_const_obj_t arg);
  897. mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
  898. bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
  899. #if MICROPY_PY_BUILTINS_FLOAT
  900. mp_float_t mp_obj_get_float(mp_obj_t self_in);
  901. bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value);
  902. void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  903. bool mp_obj_get_complex_maybe(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  904. #endif
  905. void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items); // *items may point inside a GC block
  906. 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
  907. size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice);
  908. mp_obj_t mp_obj_id(mp_obj_t o_in);
  909. mp_obj_t mp_obj_len(mp_obj_t o_in);
  910. mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL
  911. mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
  912. // cell
  913. typedef struct _mp_obj_cell_t {
  914. mp_obj_base_t base;
  915. mp_obj_t obj;
  916. } mp_obj_cell_t;
  917. static inline mp_obj_t mp_obj_cell_get(mp_obj_t self_in) {
  918. mp_obj_cell_t *self = (mp_obj_cell_t *)MP_OBJ_TO_PTR(self_in);
  919. return self->obj;
  920. }
  921. static inline void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) {
  922. mp_obj_cell_t *self = (mp_obj_cell_t *)MP_OBJ_TO_PTR(self_in);
  923. self->obj = obj;
  924. }
  925. // int
  926. // For long int, returns value truncated to mp_int_t
  927. mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
  928. // Will raise exception if value doesn't fit into mp_int_t
  929. mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
  930. // Will raise exception if value is negative or doesn't fit into mp_uint_t
  931. mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in);
  932. // exception
  933. bool mp_obj_is_native_exception_instance(mp_obj_t self_in);
  934. bool mp_obj_is_exception_type(mp_obj_t self_in);
  935. bool mp_obj_is_exception_instance(mp_obj_t self_in);
  936. bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
  937. void mp_obj_exception_clear_traceback(mp_obj_t self_in);
  938. void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
  939. void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
  940. mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
  941. 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);
  942. mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
  943. void mp_init_emergency_exception_buf(void);
  944. static inline mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
  945. assert(MP_OBJ_TYPE_GET_SLOT_OR_NULL(exc_type, make_new) == mp_obj_exception_make_new);
  946. return mp_obj_exception_make_new(exc_type, 1, 0, &arg);
  947. }
  948. // str
  949. bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
  950. qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
  951. const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
  952. const char *mp_obj_str_get_data(mp_obj_t self_in, size_t *len);
  953. mp_obj_t mp_obj_str_intern(mp_obj_t str);
  954. mp_obj_t mp_obj_str_intern_checked(mp_obj_t obj);
  955. void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, size_t str_len, bool is_bytes);
  956. #if MICROPY_PY_BUILTINS_FLOAT
  957. // float
  958. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
  959. static inline float mp_obj_get_float_to_f(mp_obj_t o) {
  960. return mp_obj_get_float(o);
  961. }
  962. static inline double mp_obj_get_float_to_d(mp_obj_t o) {
  963. return (double)mp_obj_get_float(o);
  964. }
  965. static inline mp_obj_t mp_obj_new_float_from_f(float o) {
  966. return mp_obj_new_float(o);
  967. }
  968. static inline mp_obj_t mp_obj_new_float_from_d(double o) {
  969. return mp_obj_new_float((mp_float_t)o);
  970. }
  971. #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
  972. static inline float mp_obj_get_float_to_f(mp_obj_t o) {
  973. return (float)mp_obj_get_float(o);
  974. }
  975. static inline double mp_obj_get_float_to_d(mp_obj_t o) {
  976. return mp_obj_get_float(o);
  977. }
  978. static inline mp_obj_t mp_obj_new_float_from_f(float o) {
  979. return mp_obj_new_float((mp_float_t)o);
  980. }
  981. static inline mp_obj_t mp_obj_new_float_from_d(double o) {
  982. return mp_obj_new_float(o);
  983. }
  984. #endif
  985. #if MICROPY_FLOAT_HIGH_QUALITY_HASH
  986. mp_int_t mp_float_hash(mp_float_t val);
  987. #else
  988. static inline mp_int_t mp_float_hash(mp_float_t val) {
  989. return (mp_int_t)val;
  990. }
  991. #endif
  992. 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
  993. // complex
  994. void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  995. 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
  996. #else
  997. #define mp_obj_is_float(o) (false)
  998. #endif
  999. // tuple
  1000. void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
  1001. void mp_obj_tuple_del(mp_obj_t self_in);
  1002. mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
  1003. // list
  1004. mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
  1005. mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value);
  1006. void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
  1007. void mp_obj_list_set_len(mp_obj_t self_in, size_t len);
  1008. void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
  1009. mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
  1010. // dict
  1011. typedef struct _mp_obj_dict_t {
  1012. mp_obj_base_t base;
  1013. mp_map_t map;
  1014. } mp_obj_dict_t;
  1015. mp_obj_t mp_obj_dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
  1016. void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args);
  1017. size_t mp_obj_dict_len(mp_obj_t self_in);
  1018. mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
  1019. mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
  1020. mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
  1021. mp_obj_t mp_obj_dict_copy(mp_obj_t self_in);
  1022. static inline mp_map_t *mp_obj_dict_get_map(mp_obj_t dict) {
  1023. return &((mp_obj_dict_t *)MP_OBJ_TO_PTR(dict))->map;
  1024. }
  1025. // set
  1026. void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
  1027. // slice indexes resolved to particular sequence
  1028. typedef struct {
  1029. mp_int_t start;
  1030. mp_int_t stop;
  1031. mp_int_t step;
  1032. } mp_bound_slice_t;
  1033. // slice
  1034. typedef struct _mp_obj_slice_t {
  1035. mp_obj_base_t base;
  1036. mp_obj_t start;
  1037. mp_obj_t stop;
  1038. mp_obj_t step;
  1039. } mp_obj_slice_t;
  1040. void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result);
  1041. // functions
  1042. typedef struct _mp_obj_fun_builtin_fixed_t {
  1043. mp_obj_base_t base;
  1044. union {
  1045. mp_fun_0_t _0;
  1046. mp_fun_1_t _1;
  1047. mp_fun_2_t _2;
  1048. mp_fun_3_t _3;
  1049. } fun;
  1050. } mp_obj_fun_builtin_fixed_t;
  1051. typedef struct _mp_obj_fun_builtin_var_t {
  1052. mp_obj_base_t base;
  1053. uint32_t sig; // see MP_OBJ_FUN_MAKE_SIG
  1054. union {
  1055. mp_fun_var_t var;
  1056. mp_fun_kw_t kw;
  1057. } fun;
  1058. } mp_obj_fun_builtin_var_t;
  1059. qstr mp_obj_fun_get_name(mp_const_obj_t fun);
  1060. mp_obj_t mp_identity(mp_obj_t self);
  1061. MP_DECLARE_CONST_FUN_OBJ_1(mp_identity_obj);
  1062. // module
  1063. typedef struct _mp_obj_module_t {
  1064. mp_obj_base_t base;
  1065. mp_obj_dict_t *globals;
  1066. } mp_obj_module_t;
  1067. static inline mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t module) {
  1068. return ((mp_obj_module_t *)MP_OBJ_TO_PTR(module))->globals;
  1069. }
  1070. // staticmethod and classmethod types; defined here so we can make const versions
  1071. // this structure is used for instances of both staticmethod and classmethod
  1072. typedef struct _mp_obj_static_class_method_t {
  1073. mp_obj_base_t base;
  1074. mp_obj_t fun;
  1075. } mp_obj_static_class_method_t;
  1076. typedef struct _mp_rom_obj_static_class_method_t {
  1077. mp_obj_base_t base;
  1078. mp_rom_obj_t fun;
  1079. } mp_rom_obj_static_class_method_t;
  1080. // property
  1081. const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
  1082. // sequence helpers
  1083. void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest);
  1084. #if MICROPY_PY_BUILTINS_SLICE
  1085. bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes);
  1086. #endif
  1087. #define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
  1088. #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)); }
  1089. bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2);
  1090. 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);
  1091. mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args);
  1092. mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value);
  1093. mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
  1094. // Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
  1095. #define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte *)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
  1096. // Note: dest and slice regions may overlap
  1097. #define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_sz) \
  1098. memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \
  1099. memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), (dest_len - end) * (item_sz));
  1100. // Note: dest and slice regions may overlap
  1101. #define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_sz) \
  1102. memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \
  1103. memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));
  1104. // Provide translation for legacy API
  1105. #define MP_OBJ_IS_SMALL_INT mp_obj_is_small_int
  1106. #define MP_OBJ_IS_QSTR mp_obj_is_qstr
  1107. #define MP_OBJ_IS_OBJ mp_obj_is_obj
  1108. #define MP_OBJ_IS_INT mp_obj_is_int
  1109. #define MP_OBJ_IS_TYPE mp_obj_is_type
  1110. #define MP_OBJ_IS_STR mp_obj_is_str
  1111. #define MP_OBJ_IS_STR_OR_BYTES mp_obj_is_str_or_bytes
  1112. #define MP_OBJ_IS_FUN mp_obj_is_fun
  1113. #define MP_MAP_SLOT_IS_FILLED mp_map_slot_is_filled
  1114. #define MP_SET_SLOT_IS_FILLED mp_set_slot_is_filled
  1115. #endif // MICROPY_INCLUDED_PY_OBJ_H