dynruntime.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2019 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_DYNRUNTIME_H
  27. #define MICROPY_INCLUDED_PY_DYNRUNTIME_H
  28. // This header file contains definitions to dynamically implement the static
  29. // MicroPython runtime API defined in py/obj.h and py/runtime.h.
  30. #include "py/nativeglue.h"
  31. #include "py/objfun.h"
  32. #include "py/objstr.h"
  33. #include "py/objtype.h"
  34. #if !MICROPY_ENABLE_DYNRUNTIME
  35. #error "dynruntime.h included in non-dynamic-module build."
  36. #endif
  37. #undef MP_ROM_QSTR
  38. #undef MP_OBJ_QSTR_VALUE
  39. #undef MP_OBJ_NEW_QSTR
  40. #undef mp_const_none
  41. #undef mp_const_false
  42. #undef mp_const_true
  43. #undef mp_const_empty_bytes
  44. #undef mp_const_empty_tuple
  45. #undef nlr_raise
  46. /******************************************************************************/
  47. // Memory allocation
  48. #define m_malloc(n) (m_malloc_dyn((n)))
  49. #define m_free(ptr) (m_free_dyn((ptr)))
  50. #define m_realloc(ptr, new_num_bytes) (m_realloc_dyn((ptr), (new_num_bytes)))
  51. static inline void *m_malloc_dyn(size_t n) {
  52. // TODO won't raise on OOM
  53. return mp_fun_table.realloc_(NULL, n, false);
  54. }
  55. static inline void m_free_dyn(void *ptr) {
  56. mp_fun_table.realloc_(ptr, 0, false);
  57. }
  58. static inline void *m_realloc_dyn(void *ptr, size_t new_num_bytes) {
  59. // TODO won't raise on OOM
  60. return mp_fun_table.realloc_(ptr, new_num_bytes, true);
  61. }
  62. /******************************************************************************/
  63. // Printing
  64. #define mp_plat_print (*mp_fun_table.plat_print)
  65. #define mp_printf(p, ...) (mp_fun_table.printf_((p), __VA_ARGS__))
  66. #define mp_vprintf(p, fmt, args) (mp_fun_table.vprintf_((p), (fmt), (args)))
  67. /******************************************************************************/
  68. // Types and objects
  69. #define MP_OBJ_NEW_QSTR(x) (mp_fun_table.native_to_obj(x, MP_NATIVE_TYPE_QSTR))
  70. #define mp_type_type (*mp_fun_table.type_type)
  71. #define mp_type_NoneType (*mp_obj_get_type(mp_const_none))
  72. #define mp_type_bool (*mp_obj_get_type(mp_const_false))
  73. #define mp_type_int (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_int)))
  74. #define mp_type_str (*mp_fun_table.type_str)
  75. #define mp_type_bytes (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_bytes)))
  76. #define mp_type_tuple (*((mp_obj_base_t *)mp_const_empty_tuple)->type)
  77. #define mp_type_list (*mp_fun_table.type_list)
  78. #define mp_type_EOFError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_EOFError)))
  79. #define mp_type_IndexError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_IndexError)))
  80. #define mp_type_KeyError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_KeyError)))
  81. #define mp_type_NotImplementedError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_NotImplementedError)))
  82. #define mp_type_RuntimeError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_RuntimeError)))
  83. #define mp_type_TypeError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_TypeError)))
  84. #define mp_type_ValueError (*(mp_obj_type_t *)(mp_load_global(MP_QSTR_ValueError)))
  85. #define mp_stream_read_obj (*mp_fun_table.stream_read_obj)
  86. #define mp_stream_readinto_obj (*mp_fun_table.stream_readinto_obj)
  87. #define mp_stream_unbuffered_readline_obj (*mp_fun_table.stream_unbuffered_readline_obj)
  88. #define mp_stream_write_obj (*mp_fun_table.stream_write_obj)
  89. #define mp_const_none ((mp_obj_t)mp_fun_table.const_none)
  90. #define mp_const_false ((mp_obj_t)mp_fun_table.const_false)
  91. #define mp_const_true ((mp_obj_t)mp_fun_table.const_true)
  92. #define mp_const_empty_bytes (MP_OBJ_TYPE_GET_SLOT(&mp_type_bytes, make_new)(NULL, 0, 0, NULL))
  93. #define mp_const_empty_tuple (mp_fun_table.new_tuple(0, NULL))
  94. #define mp_obj_new_bool(b) ((b) ? (mp_obj_t)mp_fun_table.const_true : (mp_obj_t)mp_fun_table.const_false)
  95. #define mp_obj_new_int(i) (mp_fun_table.native_to_obj(i, MP_NATIVE_TYPE_INT))
  96. #define mp_obj_new_int_from_uint(i) (mp_fun_table.native_to_obj(i, MP_NATIVE_TYPE_UINT))
  97. #define mp_obj_new_str(data, len) (mp_fun_table.obj_new_str((data), (len)))
  98. #define mp_obj_new_str_of_type(t, d, l) (mp_obj_new_str_of_type_dyn((t), (d), (l)))
  99. #define mp_obj_new_bytes(data, len) (mp_fun_table.obj_new_bytes((data), (len)))
  100. #define mp_obj_new_bytearray_by_ref(n, i) (mp_fun_table.obj_new_bytearray_by_ref((n), (i)))
  101. #define mp_obj_new_tuple(n, items) (mp_fun_table.new_tuple((n), (items)))
  102. #define mp_obj_new_list(n, items) (mp_fun_table.new_list((n), (items)))
  103. #define mp_obj_new_dict(n) (mp_fun_table.new_dict((n)))
  104. #define mp_obj_get_type(o) (mp_fun_table.obj_get_type((o)))
  105. #define mp_obj_cast_to_native_base(o, t) (mp_obj_cast_to_native_base_dyn((o), (t)))
  106. #define mp_obj_get_int(o) (mp_fun_table.native_from_obj(o, MP_NATIVE_TYPE_INT))
  107. #define mp_obj_get_int_truncated(o) (mp_fun_table.native_from_obj(o, MP_NATIVE_TYPE_UINT))
  108. #define mp_obj_str_get_str(s) (mp_obj_str_get_data_dyn((s), NULL))
  109. #define mp_obj_str_get_data(o, len) (mp_obj_str_get_data_dyn((o), (len)))
  110. #define mp_get_buffer(o, bufinfo, fl) (mp_fun_table.get_buffer((o), (bufinfo), (fl)))
  111. #define mp_get_buffer_raise(o, bufinfo, fl) (mp_fun_table.get_buffer((o), (bufinfo), (fl) | MP_BUFFER_RAISE_IF_UNSUPPORTED))
  112. #define mp_get_stream_raise(s, flags) (mp_fun_table.get_stream_raise((s), (flags)))
  113. #define mp_obj_is_true(o) (mp_fun_table.native_from_obj(o, MP_NATIVE_TYPE_BOOL))
  114. #define mp_obj_len(o) (mp_obj_len_dyn(o))
  115. #define mp_obj_subscr(base, index, val) (mp_fun_table.obj_subscr((base), (index), (val)))
  116. #define mp_obj_get_array(o, len, items) (mp_obj_get_array_dyn((o), (len), (items)))
  117. #define mp_obj_list_append(list, item) (mp_fun_table.list_append((list), (item)))
  118. #define mp_obj_dict_store(dict, key, val) (mp_fun_table.dict_store((dict), (key), (val)))
  119. #define mp_obj_malloc_helper(n, t) (mp_obj_malloc_helper_dyn(n, t))
  120. static inline mp_obj_t mp_obj_new_str_of_type_dyn(const mp_obj_type_t *type, const byte *data, size_t len) {
  121. if (type == &mp_type_str) {
  122. return mp_obj_new_str((const char *)data, len);
  123. } else {
  124. return mp_obj_new_bytes(data, len);
  125. }
  126. }
  127. static inline mp_obj_t mp_obj_cast_to_native_base_dyn(mp_obj_t self_in, mp_const_obj_t native_type) {
  128. const mp_obj_type_t *self_type = mp_obj_get_type(self_in);
  129. if (MP_OBJ_FROM_PTR(self_type) == native_type) {
  130. return self_in;
  131. } else if (MP_OBJ_TYPE_GET_SLOT_OR_NULL(self_type, parent) != native_type) {
  132. // The self_in object is not a direct descendant of native_type, so fail the cast.
  133. // This is a very simple version of mp_obj_is_subclass_fast that could be improved.
  134. return MP_OBJ_NULL;
  135. } else {
  136. mp_obj_instance_t *self = (mp_obj_instance_t *)MP_OBJ_TO_PTR(self_in);
  137. return self->subobj[0];
  138. }
  139. }
  140. static inline void *mp_obj_str_get_data_dyn(mp_obj_t o, size_t *l) {
  141. mp_buffer_info_t bufinfo;
  142. mp_get_buffer_raise(o, &bufinfo, MP_BUFFER_READ);
  143. if (l != NULL) {
  144. *l = bufinfo.len;
  145. }
  146. return bufinfo.buf;
  147. }
  148. static inline mp_obj_t mp_obj_len_dyn(mp_obj_t o) {
  149. // If bytes implemented MP_UNARY_OP_LEN could use: mp_unary_op(MP_UNARY_OP_LEN, o)
  150. return mp_fun_table.call_function_n_kw(mp_fun_table.load_name(MP_QSTR_len), 1, &o);
  151. }
  152. static inline void *mp_obj_malloc_helper_dyn(size_t num_bytes, const mp_obj_type_t *type) {
  153. mp_obj_base_t *base = (mp_obj_base_t *)m_malloc(num_bytes);
  154. base->type = type;
  155. return base;
  156. }
  157. /******************************************************************************/
  158. // General runtime functions
  159. #define mp_load_name(qst) (mp_fun_table.load_name((qst)))
  160. #define mp_load_global(qst) (mp_fun_table.load_global((qst)))
  161. #define mp_load_attr(base, attr) (mp_fun_table.load_attr((base), (attr)))
  162. #define mp_load_method(base, attr, dest) (mp_fun_table.load_method((base), (attr), (dest)))
  163. #define mp_load_super_method(attr, dest) (mp_fun_table.load_super_method((attr), (dest)))
  164. #define mp_store_name(qst, obj) (mp_fun_table.store_name((qst), (obj)))
  165. #define mp_store_global(qst, obj) (mp_fun_table.store_global((qst), (obj)))
  166. #define mp_store_attr(base, attr, val) (mp_fun_table.store_attr((base), (attr), (val)))
  167. #define mp_unary_op(op, obj) (mp_fun_table.unary_op((op), (obj)))
  168. #define mp_binary_op(op, lhs, rhs) (mp_fun_table.binary_op((op), (lhs), (rhs)))
  169. #define mp_make_function_from_raw_code(rc, context, def_args) \
  170. (mp_fun_table.make_function_from_raw_code((rc), (context), (def_args)))
  171. #define mp_call_function_n_kw(fun, n_args, n_kw, args) \
  172. (mp_fun_table.call_function_n_kw((fun), (n_args) | ((n_kw) << 8), args))
  173. #define mp_arg_check_num(n_args, n_kw, n_args_min, n_args_max, takes_kw) \
  174. (mp_fun_table.arg_check_num_sig((n_args), (n_kw), MP_OBJ_FUN_MAKE_SIG((n_args_min), (n_args_max), (takes_kw))))
  175. #define MP_DYNRUNTIME_INIT_ENTRY \
  176. mp_obj_t old_globals = mp_fun_table.swap_globals(self->context->module.globals); \
  177. mp_raw_code_t rc; \
  178. rc.kind = MP_CODE_NATIVE_VIPER; \
  179. rc.scope_flags = 0; \
  180. (void)rc;
  181. #define MP_DYNRUNTIME_INIT_EXIT \
  182. mp_fun_table.swap_globals(old_globals); \
  183. return mp_const_none;
  184. #define MP_DYNRUNTIME_MAKE_FUNCTION(f) \
  185. (mp_make_function_from_raw_code((rc.fun_data = (f), &rc), self->context, NULL))
  186. #define mp_import_name(name, fromlist, level) \
  187. (mp_fun_table.import_name((name), (fromlist), (level)))
  188. #define mp_import_from(module, name) \
  189. (mp_fun_table.import_from((module), (name)))
  190. #define mp_import_all(module) \
  191. (mp_fun_table.import_all((module))
  192. /******************************************************************************/
  193. // Exceptions
  194. #define mp_obj_new_exception(o) ((mp_obj_t)(o)) // Assumes returned object will be raised, will create instance then
  195. #define mp_obj_new_exception_arg1(e_type, arg) (mp_obj_new_exception_arg1_dyn((e_type), (arg)))
  196. #define nlr_raise(o) (mp_raise_dyn(o))
  197. #define mp_raise_type_arg(type, arg) (mp_raise_dyn(mp_obj_new_exception_arg1_dyn((type), (arg))))
  198. #define mp_raise_msg(type, msg) (mp_fun_table.raise_msg((type), (msg)))
  199. #define mp_raise_OSError(er) (mp_raise_OSError_dyn(er))
  200. #define mp_raise_NotImplementedError(msg) (mp_raise_msg(&mp_type_NotImplementedError, (msg)))
  201. #define mp_raise_TypeError(msg) (mp_raise_msg(&mp_type_TypeError, (msg)))
  202. #define mp_raise_ValueError(msg) (mp_raise_msg(&mp_type_ValueError, (msg)))
  203. static inline mp_obj_t mp_obj_new_exception_arg1_dyn(const mp_obj_type_t *exc_type, mp_obj_t arg) {
  204. mp_obj_t args[1] = { arg };
  205. return mp_call_function_n_kw(MP_OBJ_FROM_PTR(exc_type), 1, 0, &args[0]);
  206. }
  207. static NORETURN inline void mp_raise_dyn(mp_obj_t o) {
  208. mp_fun_table.raise(o);
  209. for (;;) {
  210. }
  211. }
  212. static inline void mp_raise_OSError_dyn(int er) {
  213. mp_obj_t args[1] = { MP_OBJ_NEW_SMALL_INT(er) };
  214. nlr_raise(mp_call_function_n_kw(mp_load_global(MP_QSTR_OSError), 1, 0, &args[0]));
  215. }
  216. /******************************************************************************/
  217. // Floating point
  218. #define mp_obj_new_float_from_f(f) (mp_fun_table.obj_new_float_from_f((f)))
  219. #define mp_obj_new_float_from_d(d) (mp_fun_table.obj_new_float_from_d((d)))
  220. #define mp_obj_get_float_to_f(o) (mp_fun_table.obj_get_float_to_f((o)))
  221. #define mp_obj_get_float_to_d(o) (mp_fun_table.obj_get_float_to_d((o)))
  222. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
  223. #define mp_obj_new_float(f) (mp_obj_new_float_from_f((f)))
  224. #define mp_obj_get_float(o) (mp_obj_get_float_to_f((o)))
  225. #elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
  226. #define mp_obj_new_float(f) (mp_obj_new_float_from_d((f)))
  227. #define mp_obj_get_float(o) (mp_obj_get_float_to_d((o)))
  228. #endif
  229. /******************************************************************************/
  230. // Inline function definitions.
  231. // *items may point inside a GC block
  232. static inline void mp_obj_get_array_dyn(mp_obj_t o, size_t *len, mp_obj_t **items) {
  233. const mp_obj_type_t *type = mp_obj_get_type(o);
  234. if (type == &mp_type_tuple) {
  235. mp_obj_tuple_t *t = MP_OBJ_TO_PTR(o);
  236. *len = t->len;
  237. *items = &t->items[0];
  238. } else if (type == &mp_type_list) {
  239. mp_obj_list_t *l = MP_OBJ_TO_PTR(o);
  240. *len = l->len;
  241. *items = l->items;
  242. } else {
  243. mp_raise_TypeError("expected tuple/list");
  244. }
  245. }
  246. #endif // MICROPY_INCLUDED_PY_DYNRUNTIME_H