obj.h 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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 }}, .name = _name, .flags = _flags{''.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 }, .name = _name, .flags = _flags }
  643. #define MP_DEFINE_CONST_OBJ_TYPE_NARGS_1(_struct_type, _typename, _name, _flags, f1, v1) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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 }, .name = _name, .flags = _flags, .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_module;
  717. extern const mp_obj_type_t mp_type_staticmethod;
  718. extern const mp_obj_type_t mp_type_classmethod;
  719. extern const mp_obj_type_t mp_type_bound_meth;
  720. extern const mp_obj_type_t mp_type_property;
  721. extern const mp_obj_type_t mp_type_stringio;
  722. extern const mp_obj_type_t mp_type_bytesio;
  723. extern const mp_obj_type_t mp_type_reversed;
  724. extern const mp_obj_type_t mp_type_polymorph_iter;
  725. #if MICROPY_ENABLE_FINALISER
  726. extern const mp_obj_type_t mp_type_polymorph_iter_with_finaliser;
  727. #endif
  728. // Exceptions
  729. extern const mp_obj_type_t mp_type_BaseException;
  730. extern const mp_obj_type_t mp_type_ArithmeticError;
  731. extern const mp_obj_type_t mp_type_AssertionError;
  732. extern const mp_obj_type_t mp_type_AttributeError;
  733. extern const mp_obj_type_t mp_type_EOFError;
  734. extern const mp_obj_type_t mp_type_Exception;
  735. extern const mp_obj_type_t mp_type_GeneratorExit;
  736. extern const mp_obj_type_t mp_type_ImportError;
  737. extern const mp_obj_type_t mp_type_IndentationError;
  738. extern const mp_obj_type_t mp_type_IndexError;
  739. extern const mp_obj_type_t mp_type_KeyboardInterrupt;
  740. extern const mp_obj_type_t mp_type_KeyError;
  741. extern const mp_obj_type_t mp_type_LookupError;
  742. extern const mp_obj_type_t mp_type_MemoryError;
  743. extern const mp_obj_type_t mp_type_NameError;
  744. extern const mp_obj_type_t mp_type_NotImplementedError;
  745. extern const mp_obj_type_t mp_type_OSError;
  746. extern const mp_obj_type_t mp_type_OverflowError;
  747. extern const mp_obj_type_t mp_type_RuntimeError;
  748. extern const mp_obj_type_t mp_type_StopAsyncIteration;
  749. extern const mp_obj_type_t mp_type_StopIteration;
  750. extern const mp_obj_type_t mp_type_SyntaxError;
  751. extern const mp_obj_type_t mp_type_SystemExit;
  752. extern const mp_obj_type_t mp_type_TypeError;
  753. extern const mp_obj_type_t mp_type_UnicodeError;
  754. extern const mp_obj_type_t mp_type_ValueError;
  755. extern const mp_obj_type_t mp_type_ViperTypeError;
  756. extern const mp_obj_type_t mp_type_ZeroDivisionError;
  757. // Constant objects, globally accessible: None, False, True
  758. // These should always be accessed via the below macros.
  759. #if MICROPY_OBJ_IMMEDIATE_OBJS
  760. // None is even while False/True are odd so their types can be distinguished with 1 bit.
  761. #define mp_const_none MP_OBJ_NEW_IMMEDIATE_OBJ(0)
  762. #define mp_const_false MP_OBJ_NEW_IMMEDIATE_OBJ(1)
  763. #define mp_const_true MP_OBJ_NEW_IMMEDIATE_OBJ(3)
  764. #else
  765. #define mp_const_none (MP_OBJ_FROM_PTR(&mp_const_none_obj))
  766. #define mp_const_false (MP_OBJ_FROM_PTR(&mp_const_false_obj))
  767. #define mp_const_true (MP_OBJ_FROM_PTR(&mp_const_true_obj))
  768. extern const struct _mp_obj_none_t mp_const_none_obj;
  769. extern const struct _mp_obj_bool_t mp_const_false_obj;
  770. extern const struct _mp_obj_bool_t mp_const_true_obj;
  771. #endif
  772. // Constant objects, globally accessible: b'', (), {}, Ellipsis, NotImplemented, GeneratorExit()
  773. // The below macros are for convenience only.
  774. #define mp_const_empty_bytes (MP_OBJ_FROM_PTR(&mp_const_empty_bytes_obj))
  775. #define mp_const_empty_tuple (MP_OBJ_FROM_PTR(&mp_const_empty_tuple_obj))
  776. #define mp_const_notimplemented (MP_OBJ_FROM_PTR(&mp_const_notimplemented_obj))
  777. extern const struct _mp_obj_str_t mp_const_empty_bytes_obj;
  778. extern const struct _mp_obj_tuple_t mp_const_empty_tuple_obj;
  779. extern const struct _mp_obj_dict_t mp_const_empty_dict_obj;
  780. extern const struct _mp_obj_singleton_t mp_const_ellipsis_obj;
  781. extern const struct _mp_obj_singleton_t mp_const_notimplemented_obj;
  782. extern const struct _mp_obj_exception_t mp_const_GeneratorExit_obj;
  783. // Fixed empty map. Useful when calling keyword-receiving functions
  784. // without any keywords from C, etc.
  785. #define mp_const_empty_map (mp_const_empty_dict_obj.map)
  786. // General API for objects
  787. // Helper versions of m_new_obj when you need to immediately set base.type.
  788. // Implementing this as a call rather than inline saves 8 bytes per usage.
  789. #define mp_obj_malloc(struct_type, obj_type) ((struct_type *)mp_obj_malloc_helper(sizeof(struct_type), obj_type))
  790. #define mp_obj_malloc_var(struct_type, var_type, var_num, obj_type) ((struct_type *)mp_obj_malloc_helper(sizeof(struct_type) + sizeof(var_type) * (var_num), obj_type))
  791. void *mp_obj_malloc_helper(size_t num_bytes, const mp_obj_type_t *type);
  792. // These macros are derived from more primitive ones and are used to
  793. // check for more specific object types.
  794. // Note: these are kept as macros because inline functions sometimes use much
  795. // more code space than the equivalent macros, depending on the compiler.
  796. // don't use mp_obj_is_exact_type directly; use mp_obj_is_type which provides additional safety checks.
  797. // use the former only if you need to bypass these checks (because you've already checked everything else)
  798. #define mp_obj_is_exact_type(o, t) (mp_obj_is_obj(o) && (((mp_obj_base_t *)MP_OBJ_TO_PTR(o))->type == (t)))
  799. // Type checks are split to a separate, constant result macro. This is so it doesn't hinder the compilers's
  800. // optimizations (other tricks like using ({ expr; exper; }) or (exp, expr, expr) in mp_obj_is_type() result
  801. // in missed optimizations)
  802. #define mp_type_assert_not_bool_int_str_nonetype(t) ( \
  803. MP_STATIC_ASSERT_NONCONSTEXPR((t) != &mp_type_bool), assert((t) != &mp_type_bool), \
  804. MP_STATIC_ASSERT_NONCONSTEXPR((t) != &mp_type_int), assert((t) != &mp_type_int), \
  805. MP_STATIC_ASSERT_NONCONSTEXPR((t) != &mp_type_str), assert((t) != &mp_type_str), \
  806. MP_STATIC_ASSERT_NONCONSTEXPR((t) != &mp_type_NoneType), assert((t) != &mp_type_NoneType), \
  807. 1)
  808. #define mp_obj_is_type(o, t) (mp_type_assert_not_bool_int_str_nonetype(t) && mp_obj_is_exact_type(o, t))
  809. #if MICROPY_OBJ_IMMEDIATE_OBJS
  810. // bool's are immediates, not real objects, so test for the 2 possible values.
  811. #define mp_obj_is_bool(o) ((o) == mp_const_false || (o) == mp_const_true)
  812. #else
  813. #define mp_obj_is_bool(o) mp_obj_is_exact_type(o, &mp_type_bool)
  814. #endif
  815. #define mp_obj_is_int(o) (mp_obj_is_small_int(o) || mp_obj_is_exact_type(o, &mp_type_int))
  816. #define mp_obj_is_str(o) (mp_obj_is_qstr(o) || mp_obj_is_exact_type(o, &mp_type_str))
  817. #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))
  818. bool mp_obj_is_dict_or_ordereddict(mp_obj_t o);
  819. #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))
  820. mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict);
  821. static inline mp_obj_t mp_obj_new_bool(mp_int_t x) {
  822. return x ? mp_const_true : mp_const_false;
  823. }
  824. mp_obj_t mp_obj_new_cell(mp_obj_t obj);
  825. mp_obj_t mp_obj_new_int(mp_int_t value);
  826. mp_obj_t mp_obj_new_int_from_uint(mp_uint_t value);
  827. mp_obj_t mp_obj_new_int_from_str_len(const char **str, size_t len, bool neg, unsigned int base);
  828. 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)
  829. 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)
  830. mp_obj_t mp_obj_new_str(const char *data, size_t len); // will check utf-8 (raises UnicodeError)
  831. mp_obj_t mp_obj_new_str_via_qstr(const char *data, size_t len); // input data must be valid utf-8
  832. mp_obj_t mp_obj_new_str_from_vstr(vstr_t *vstr); // will check utf-8 (raises UnicodeError)
  833. #if MICROPY_PY_BUILTINS_STR_UNICODE && MICROPY_PY_BUILTINS_STR_UNICODE_CHECK
  834. mp_obj_t mp_obj_new_str_from_utf8_vstr(vstr_t *vstr); // input data must be valid utf-8
  835. #else
  836. #define mp_obj_new_str_from_utf8_vstr mp_obj_new_str_from_vstr
  837. #endif
  838. mp_obj_t mp_obj_new_bytes_from_vstr(vstr_t *vstr);
  839. mp_obj_t mp_obj_new_bytes(const byte *data, size_t len);
  840. mp_obj_t mp_obj_new_bytearray(size_t n, const void *items);
  841. mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items);
  842. #if MICROPY_PY_BUILTINS_FLOAT
  843. mp_obj_t mp_obj_new_int_from_float(mp_float_t val);
  844. mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
  845. #endif
  846. mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
  847. mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args);
  848. #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
  849. #define mp_obj_new_exception_msg(exc_type, msg) mp_obj_new_exception(exc_type)
  850. #define mp_obj_new_exception_msg_varg(exc_type, ...) mp_obj_new_exception(exc_type)
  851. #else
  852. mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
  853. 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!)
  854. #endif
  855. #ifdef va_start
  856. 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
  857. #endif
  858. mp_obj_t mp_obj_new_gen_wrap(mp_obj_t fun);
  859. mp_obj_t mp_obj_new_closure(mp_obj_t fun, size_t n_closed, const mp_obj_t *closed);
  860. mp_obj_t mp_obj_new_tuple(size_t n, const mp_obj_t *items);
  861. mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items);
  862. mp_obj_t mp_obj_new_dict(size_t n_args);
  863. mp_obj_t mp_obj_new_set(size_t n_args, mp_obj_t *items);
  864. mp_obj_t mp_obj_new_slice(mp_obj_t start, mp_obj_t stop, mp_obj_t step);
  865. mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
  866. mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args, mp_obj_iter_buf_t *iter_buf);
  867. mp_obj_t mp_obj_new_module(qstr module_name);
  868. mp_obj_t mp_obj_new_memoryview(byte typecode, size_t nitems, void *items);
  869. const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
  870. const char *mp_obj_get_type_str(mp_const_obj_t o_in);
  871. bool mp_obj_is_subclass_fast(mp_const_obj_t object, mp_const_obj_t classinfo); // arguments should be type objects
  872. mp_obj_t mp_obj_cast_to_native_base(mp_obj_t self_in, mp_const_obj_t native_type);
  873. void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind);
  874. void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
  875. void mp_obj_print_exception(const mp_print_t *print, mp_obj_t exc);
  876. bool mp_obj_is_true(mp_obj_t arg);
  877. bool mp_obj_is_callable(mp_obj_t o_in);
  878. mp_obj_t mp_obj_equal_not_equal(mp_binary_op_t op, mp_obj_t o1, mp_obj_t o2);
  879. bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
  880. // returns true if o is bool, small int or long int
  881. static inline bool mp_obj_is_integer(mp_const_obj_t o) {
  882. return mp_obj_is_int(o) || mp_obj_is_bool(o);
  883. }
  884. mp_int_t mp_obj_get_int(mp_const_obj_t arg);
  885. mp_int_t mp_obj_get_int_truncated(mp_const_obj_t arg);
  886. bool mp_obj_get_int_maybe(mp_const_obj_t arg, mp_int_t *value);
  887. #if MICROPY_PY_BUILTINS_FLOAT
  888. mp_float_t mp_obj_get_float(mp_obj_t self_in);
  889. bool mp_obj_get_float_maybe(mp_obj_t arg, mp_float_t *value);
  890. void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  891. bool mp_obj_get_complex_maybe(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  892. #endif
  893. void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items); // *items may point inside a GC block
  894. 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
  895. size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool is_slice);
  896. mp_obj_t mp_obj_id(mp_obj_t o_in);
  897. mp_obj_t mp_obj_len(mp_obj_t o_in);
  898. mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL
  899. mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
  900. // cell
  901. typedef struct _mp_obj_cell_t {
  902. mp_obj_base_t base;
  903. mp_obj_t obj;
  904. } mp_obj_cell_t;
  905. static inline mp_obj_t mp_obj_cell_get(mp_obj_t self_in) {
  906. mp_obj_cell_t *self = (mp_obj_cell_t *)MP_OBJ_TO_PTR(self_in);
  907. return self->obj;
  908. }
  909. static inline void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) {
  910. mp_obj_cell_t *self = (mp_obj_cell_t *)MP_OBJ_TO_PTR(self_in);
  911. self->obj = obj;
  912. }
  913. // int
  914. // For long int, returns value truncated to mp_int_t
  915. mp_int_t mp_obj_int_get_truncated(mp_const_obj_t self_in);
  916. // Will raise exception if value doesn't fit into mp_int_t
  917. mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in);
  918. // Will raise exception if value is negative or doesn't fit into mp_uint_t
  919. mp_uint_t mp_obj_int_get_uint_checked(mp_const_obj_t self_in);
  920. // exception
  921. bool mp_obj_is_native_exception_instance(mp_obj_t self_in);
  922. bool mp_obj_is_exception_type(mp_obj_t self_in);
  923. bool mp_obj_is_exception_instance(mp_obj_t self_in);
  924. bool mp_obj_exception_match(mp_obj_t exc, mp_const_obj_t exc_type);
  925. void mp_obj_exception_clear_traceback(mp_obj_t self_in);
  926. void mp_obj_exception_add_traceback(mp_obj_t self_in, qstr file, size_t line, qstr block);
  927. void mp_obj_exception_get_traceback(mp_obj_t self_in, size_t *n, size_t **values);
  928. mp_obj_t mp_obj_exception_get_value(mp_obj_t self_in);
  929. 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);
  930. mp_obj_t mp_alloc_emergency_exception_buf(mp_obj_t size_in);
  931. void mp_init_emergency_exception_buf(void);
  932. static inline mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg) {
  933. assert(MP_OBJ_TYPE_GET_SLOT_OR_NULL(exc_type, make_new) == mp_obj_exception_make_new);
  934. return mp_obj_exception_make_new(exc_type, 1, 0, &arg);
  935. }
  936. // str
  937. bool mp_obj_str_equal(mp_obj_t s1, mp_obj_t s2);
  938. qstr mp_obj_str_get_qstr(mp_obj_t self_in); // use this if you will anyway convert the string to a qstr
  939. const char *mp_obj_str_get_str(mp_obj_t self_in); // use this only if you need the string to be null terminated
  940. const char *mp_obj_str_get_data(mp_obj_t self_in, size_t *len);
  941. mp_obj_t mp_obj_str_intern(mp_obj_t str);
  942. mp_obj_t mp_obj_str_intern_checked(mp_obj_t obj);
  943. void mp_str_print_quoted(const mp_print_t *print, const byte *str_data, size_t str_len, bool is_bytes);
  944. #if MICROPY_PY_BUILTINS_FLOAT
  945. // float
  946. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
  947. static inline float mp_obj_get_float_to_f(mp_obj_t o) {
  948. return mp_obj_get_float(o);
  949. }
  950. static inline double mp_obj_get_float_to_d(mp_obj_t o) {
  951. return (double)mp_obj_get_float(o);
  952. }
  953. static inline mp_obj_t mp_obj_new_float_from_f(float o) {
  954. return mp_obj_new_float(o);
  955. }
  956. static inline mp_obj_t mp_obj_new_float_from_d(double o) {
  957. return mp_obj_new_float((mp_float_t)o);
  958. }
  959. #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
  960. static inline float mp_obj_get_float_to_f(mp_obj_t o) {
  961. return (float)mp_obj_get_float(o);
  962. }
  963. static inline double mp_obj_get_float_to_d(mp_obj_t o) {
  964. return mp_obj_get_float(o);
  965. }
  966. static inline mp_obj_t mp_obj_new_float_from_f(float o) {
  967. return mp_obj_new_float((mp_float_t)o);
  968. }
  969. static inline mp_obj_t mp_obj_new_float_from_d(double o) {
  970. return mp_obj_new_float(o);
  971. }
  972. #endif
  973. #if MICROPY_FLOAT_HIGH_QUALITY_HASH
  974. mp_int_t mp_float_hash(mp_float_t val);
  975. #else
  976. static inline mp_int_t mp_float_hash(mp_float_t val) {
  977. return (mp_int_t)val;
  978. }
  979. #endif
  980. 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
  981. // complex
  982. void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
  983. 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
  984. #else
  985. #define mp_obj_is_float(o) (false)
  986. #endif
  987. // tuple
  988. void mp_obj_tuple_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
  989. void mp_obj_tuple_del(mp_obj_t self_in);
  990. mp_int_t mp_obj_tuple_hash(mp_obj_t self_in);
  991. // list
  992. mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg);
  993. mp_obj_t mp_obj_list_remove(mp_obj_t self_in, mp_obj_t value);
  994. void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items);
  995. void mp_obj_list_set_len(mp_obj_t self_in, size_t len);
  996. void mp_obj_list_store(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
  997. mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs);
  998. // dict
  999. typedef struct _mp_obj_dict_t {
  1000. mp_obj_base_t base;
  1001. mp_map_t map;
  1002. } mp_obj_dict_t;
  1003. 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);
  1004. void mp_obj_dict_init(mp_obj_dict_t *dict, size_t n_args);
  1005. size_t mp_obj_dict_len(mp_obj_t self_in);
  1006. mp_obj_t mp_obj_dict_get(mp_obj_t self_in, mp_obj_t index);
  1007. mp_obj_t mp_obj_dict_store(mp_obj_t self_in, mp_obj_t key, mp_obj_t value);
  1008. mp_obj_t mp_obj_dict_delete(mp_obj_t self_in, mp_obj_t key);
  1009. mp_obj_t mp_obj_dict_copy(mp_obj_t self_in);
  1010. static inline mp_map_t *mp_obj_dict_get_map(mp_obj_t dict) {
  1011. return &((mp_obj_dict_t *)MP_OBJ_TO_PTR(dict))->map;
  1012. }
  1013. // set
  1014. void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
  1015. // slice indexes resolved to particular sequence
  1016. typedef struct {
  1017. mp_int_t start;
  1018. mp_int_t stop;
  1019. mp_int_t step;
  1020. } mp_bound_slice_t;
  1021. // slice
  1022. typedef struct _mp_obj_slice_t {
  1023. mp_obj_base_t base;
  1024. mp_obj_t start;
  1025. mp_obj_t stop;
  1026. mp_obj_t step;
  1027. } mp_obj_slice_t;
  1028. void mp_obj_slice_indices(mp_obj_t self_in, mp_int_t length, mp_bound_slice_t *result);
  1029. // functions
  1030. typedef struct _mp_obj_fun_builtin_fixed_t {
  1031. mp_obj_base_t base;
  1032. union {
  1033. mp_fun_0_t _0;
  1034. mp_fun_1_t _1;
  1035. mp_fun_2_t _2;
  1036. mp_fun_3_t _3;
  1037. } fun;
  1038. } mp_obj_fun_builtin_fixed_t;
  1039. typedef struct _mp_obj_fun_builtin_var_t {
  1040. mp_obj_base_t base;
  1041. uint32_t sig; // see MP_OBJ_FUN_MAKE_SIG
  1042. union {
  1043. mp_fun_var_t var;
  1044. mp_fun_kw_t kw;
  1045. } fun;
  1046. } mp_obj_fun_builtin_var_t;
  1047. qstr mp_obj_fun_get_name(mp_const_obj_t fun);
  1048. mp_obj_t mp_identity(mp_obj_t self);
  1049. MP_DECLARE_CONST_FUN_OBJ_1(mp_identity_obj);
  1050. // module
  1051. typedef struct _mp_obj_module_t {
  1052. mp_obj_base_t base;
  1053. mp_obj_dict_t *globals;
  1054. } mp_obj_module_t;
  1055. static inline mp_obj_dict_t *mp_obj_module_get_globals(mp_obj_t module) {
  1056. return ((mp_obj_module_t *)MP_OBJ_TO_PTR(module))->globals;
  1057. }
  1058. // staticmethod and classmethod types; defined here so we can make const versions
  1059. // this structure is used for instances of both staticmethod and classmethod
  1060. typedef struct _mp_obj_static_class_method_t {
  1061. mp_obj_base_t base;
  1062. mp_obj_t fun;
  1063. } mp_obj_static_class_method_t;
  1064. typedef struct _mp_rom_obj_static_class_method_t {
  1065. mp_obj_base_t base;
  1066. mp_rom_obj_t fun;
  1067. } mp_rom_obj_static_class_method_t;
  1068. // property
  1069. const mp_obj_t *mp_obj_property_get(mp_obj_t self_in);
  1070. // sequence helpers
  1071. void mp_seq_multiply(const void *items, size_t item_sz, size_t len, size_t times, void *dest);
  1072. #if MICROPY_PY_BUILTINS_SLICE
  1073. bool mp_seq_get_fast_slice_indexes(mp_uint_t len, mp_obj_t slice, mp_bound_slice_t *indexes);
  1074. #endif
  1075. #define mp_seq_copy(dest, src, len, item_t) memcpy(dest, src, len * sizeof(item_t))
  1076. #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)); }
  1077. bool mp_seq_cmp_bytes(mp_uint_t op, const byte *data1, size_t len1, const byte *data2, size_t len2);
  1078. 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);
  1079. mp_obj_t mp_seq_index_obj(const mp_obj_t *items, size_t len, size_t n_args, const mp_obj_t *args);
  1080. mp_obj_t mp_seq_count_obj(const mp_obj_t *items, size_t len, mp_obj_t value);
  1081. mp_obj_t mp_seq_extract_slice(size_t len, const mp_obj_t *seq, mp_bound_slice_t *indexes);
  1082. // Helper to clear stale pointers from allocated, but unused memory, to preclude GC problems
  1083. #define mp_seq_clear(start, len, alloc_len, item_sz) memset((byte *)(start) + (len) * (item_sz), 0, ((alloc_len) - (len)) * (item_sz))
  1084. // Note: dest and slice regions may overlap
  1085. #define mp_seq_replace_slice_no_grow(dest, dest_len, beg, end, slice, slice_len, item_sz) \
  1086. memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz)); \
  1087. memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), (dest_len - end) * (item_sz));
  1088. // Note: dest and slice regions may overlap
  1089. #define mp_seq_replace_slice_grow_inplace(dest, dest_len, beg, end, slice, slice_len, len_adj, item_sz) \
  1090. memmove(((char *)dest) + (beg + slice_len) * (item_sz), ((char *)dest) + (end) * (item_sz), ((dest_len) + (len_adj) - ((beg) + (slice_len))) * (item_sz)); \
  1091. memmove(((char *)dest) + (beg) * (item_sz), slice, slice_len * (item_sz));
  1092. // Provide translation for legacy API
  1093. #define MP_OBJ_IS_SMALL_INT mp_obj_is_small_int
  1094. #define MP_OBJ_IS_QSTR mp_obj_is_qstr
  1095. #define MP_OBJ_IS_OBJ mp_obj_is_obj
  1096. #define MP_OBJ_IS_INT mp_obj_is_int
  1097. #define MP_OBJ_IS_TYPE mp_obj_is_type
  1098. #define MP_OBJ_IS_STR mp_obj_is_str
  1099. #define MP_OBJ_IS_STR_OR_BYTES mp_obj_is_str_or_bytes
  1100. #define MP_OBJ_IS_FUN mp_obj_is_fun
  1101. #define MP_MAP_SLOT_IS_FILLED mp_map_slot_is_filled
  1102. #define MP_SET_SLOT_IS_FILLED mp_set_slot_is_filled
  1103. #endif // MICROPY_INCLUDED_PY_OBJ_H