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_RUNTIME_H |
27 | #define MICROPY_INCLUDED_PY_RUNTIME_H |
28 | |
29 | #include "py/mpstate.h" |
30 | #include "py/pystack.h" |
31 | |
32 | typedef enum { |
33 | MP_VM_RETURN_NORMAL, |
34 | MP_VM_RETURN_YIELD, |
35 | MP_VM_RETURN_EXCEPTION, |
36 | } mp_vm_return_kind_t; |
37 | |
38 | typedef enum { |
39 | MP_ARG_BOOL = 0x001, |
40 | MP_ARG_INT = 0x002, |
41 | MP_ARG_OBJ = 0x003, |
42 | MP_ARG_KIND_MASK = 0x0ff, |
43 | MP_ARG_REQUIRED = 0x100, |
44 | MP_ARG_KW_ONLY = 0x200, |
45 | } mp_arg_flag_t; |
46 | |
47 | typedef union _mp_arg_val_t { |
48 | bool u_bool; |
49 | mp_int_t u_int; |
50 | mp_obj_t u_obj; |
51 | mp_rom_obj_t u_rom_obj; |
52 | } mp_arg_val_t; |
53 | |
54 | typedef struct _mp_arg_t { |
55 | uint16_t qst; |
56 | uint16_t flags; |
57 | mp_arg_val_t defval; |
58 | } mp_arg_t; |
59 | |
60 | // Tables mapping operator enums to qstrs, defined in objtype.c |
61 | extern const byte mp_unary_op_method_name[]; |
62 | extern const byte mp_binary_op_method_name[]; |
63 | |
64 | void mp_init(void); |
65 | void mp_deinit(void); |
66 | |
67 | void mp_keyboard_interrupt(void); |
68 | void mp_handle_pending(bool raise_exc); |
69 | void mp_handle_pending_tail(mp_uint_t atomic_state); |
70 | |
71 | #if MICROPY_ENABLE_SCHEDULER |
72 | void mp_sched_lock(void); |
73 | void mp_sched_unlock(void); |
74 | #define mp_sched_num_pending() (MP_STATE_VM(sched_len)) |
75 | bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg); |
76 | #endif |
77 | |
78 | // extra printing method specifically for mp_obj_t's which are integral type |
79 | int mp_print_mp_int(const mp_print_t *print, mp_obj_t x, int base, int base_char, int flags, char fill, int width, int prec); |
80 | |
81 | void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig); |
82 | static inline void mp_arg_check_num(size_t n_args, size_t n_kw, size_t n_args_min, size_t n_args_max, bool takes_kw) { |
83 | mp_arg_check_num_sig(n_args, n_kw, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, takes_kw)); |
84 | } |
85 | void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals); |
86 | void mp_arg_parse_all_kw_array(size_t n_pos, size_t n_kw, const mp_obj_t *args, size_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals); |
87 | NORETURN void mp_arg_error_terse_mismatch(void); |
88 | NORETURN void mp_arg_error_unimpl_kw(void); |
89 | |
90 | static inline mp_obj_dict_t *mp_locals_get(void) { |
91 | return MP_STATE_THREAD(dict_locals); |
92 | } |
93 | static inline void mp_locals_set(mp_obj_dict_t *d) { |
94 | MP_STATE_THREAD(dict_locals) = d; |
95 | } |
96 | static inline mp_obj_dict_t *mp_globals_get(void) { |
97 | return MP_STATE_THREAD(dict_globals); |
98 | } |
99 | static inline void mp_globals_set(mp_obj_dict_t *d) { |
100 | MP_STATE_THREAD(dict_globals) = d; |
101 | } |
102 | |
103 | mp_obj_t mp_load_name(qstr qst); |
104 | mp_obj_t mp_load_global(qstr qst); |
105 | mp_obj_t mp_load_build_class(void); |
106 | void mp_store_name(qstr qst, mp_obj_t obj); |
107 | void mp_store_global(qstr qst, mp_obj_t obj); |
108 | void mp_delete_name(qstr qst); |
109 | void mp_delete_global(qstr qst); |
110 | |
111 | mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg); |
112 | mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs); |
113 | |
114 | mp_obj_t mp_call_function_0(mp_obj_t fun); |
115 | mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg); |
116 | mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2); |
117 | mp_obj_t mp_call_function_n_kw(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *args); |
118 | mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args); |
119 | mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args); |
120 | mp_obj_t mp_call_method_self_n_kw(mp_obj_t meth, mp_obj_t self, size_t n_args, size_t n_kw, const mp_obj_t *args); |
121 | // Call function and catch/dump exception - for Python callbacks from C code |
122 | // (return MP_OBJ_NULL in case of exception). |
123 | mp_obj_t mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg); |
124 | mp_obj_t mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2); |
125 | |
126 | typedef struct _mp_call_args_t { |
127 | mp_obj_t fun; |
128 | size_t n_args, n_kw, n_alloc; |
129 | mp_obj_t *args; |
130 | } mp_call_args_t; |
131 | |
132 | #if MICROPY_STACKLESS |
133 | // Takes arguments which are the most general mix of Python arg types, and |
134 | // prepares argument array suitable for passing to ->call() method of a |
135 | // function object (and mp_call_function_n_kw()). |
136 | // (Only needed in stackless mode.) |
137 | void mp_call_prepare_args_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args, mp_call_args_t *out_args); |
138 | #endif |
139 | |
140 | void mp_unpack_sequence(mp_obj_t seq, size_t num, mp_obj_t *items); |
141 | void mp_unpack_ex(mp_obj_t seq, size_t num, mp_obj_t *items); |
142 | mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value); |
143 | mp_obj_t mp_load_attr(mp_obj_t base, qstr attr); |
144 | void mp_convert_member_lookup(mp_obj_t obj, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest); |
145 | void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest); |
146 | void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest); |
147 | void mp_load_method_protected(mp_obj_t obj, qstr attr, mp_obj_t *dest, bool catch_all_exc); |
148 | void mp_load_super_method(qstr attr, mp_obj_t *dest); |
149 | void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val); |
150 | |
151 | mp_obj_t mp_getiter(mp_obj_t o, mp_obj_iter_buf_t *iter_buf); |
152 | mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration() |
153 | mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...) |
154 | mp_vm_return_kind_t mp_resume(mp_obj_t self_in, mp_obj_t send_value, mp_obj_t throw_value, mp_obj_t *ret_val); |
155 | |
156 | mp_obj_t mp_make_raise_obj(mp_obj_t o); |
157 | |
158 | mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level); |
159 | mp_obj_t mp_import_from(mp_obj_t module, qstr name); |
160 | void mp_import_all(mp_obj_t module); |
161 | |
162 | #define mp_raise_type(exc_type) mp_raise_msg(exc_type, NULL) |
163 | NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg); |
164 | NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...); |
165 | NORETURN void mp_raise_ValueError(mp_rom_error_text_t msg); |
166 | NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg); |
167 | NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg); |
168 | NORETURN void mp_raise_OSError(int errno_); |
169 | NORETURN void mp_raise_recursion_depth(void); |
170 | |
171 | #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG |
172 | #undef mp_check_self |
173 | #define mp_check_self(pred) |
174 | #else |
175 | // A port may define to raise TypeError for example |
176 | #ifndef mp_check_self |
177 | #define mp_check_self(pred) assert(pred) |
178 | #endif |
179 | #endif |
180 | |
181 | // helper functions for native/viper code |
182 | int mp_native_type_from_qstr(qstr qst); |
183 | mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type); |
184 | mp_obj_t mp_native_to_obj(mp_uint_t val, mp_uint_t type); |
185 | |
186 | #define mp_sys_path (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_path_obj))) |
187 | #define mp_sys_argv (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_argv_obj))) |
188 | |
189 | #if MICROPY_WARNINGS |
190 | #ifndef mp_warning |
191 | void mp_warning(const char *category, const char *msg, ...); |
192 | #endif |
193 | #else |
194 | #define mp_warning(...) |
195 | #endif |
196 | |
197 | #endif // MICROPY_INCLUDED_PY_RUNTIME_H |
198 | |