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_sched_exception(mp_obj_t exc);
68 void mp_sched_keyboard_interrupt(void);
69 void mp_handle_pending(bool raise_exc);
70 void mp_handle_pending_tail(mp_uint_t atomic_state);
71 
72 #if MICROPY_ENABLE_SCHEDULER
73 void mp_sched_lock(void);
74 void mp_sched_unlock(void);
75 #define mp_sched_num_pending() (MP_STATE_VM(sched_len))
76 bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg);
77 #endif
78 
79 // extra printing method specifically for mp_obj_t's which are integral type
80 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);
81 
82 void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig);
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 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) {
84     mp_arg_check_num_sig(n_args, n_kw, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, takes_kw));
85 }
86 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);
87 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);
88 NORETURN void mp_arg_error_terse_mismatch(void);
89 NORETURN void mp_arg_error_unimpl_kw(void);
90 
mp_locals_get(void)91 static inline mp_obj_dict_t *mp_locals_get(void) {
92     return MP_STATE_THREAD(dict_locals);
93 }
mp_locals_set(mp_obj_dict_t * d)94 static inline void mp_locals_set(mp_obj_dict_t *d) {
95     MP_STATE_THREAD(dict_locals) = d;
96 }
mp_globals_get(void)97 static inline mp_obj_dict_t *mp_globals_get(void) {
98     return MP_STATE_THREAD(dict_globals);
99 }
mp_globals_set(mp_obj_dict_t * d)100 static inline void mp_globals_set(mp_obj_dict_t *d) {
101     MP_STATE_THREAD(dict_globals) = d;
102 }
103 
104 mp_obj_t mp_load_name(qstr qst);
105 mp_obj_t mp_load_global(qstr qst);
106 mp_obj_t mp_load_build_class(void);
107 void mp_store_name(qstr qst, mp_obj_t obj);
108 void mp_store_global(qstr qst, mp_obj_t obj);
109 void mp_delete_name(qstr qst);
110 void mp_delete_global(qstr qst);
111 
112 mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg);
113 mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs);
114 
115 mp_obj_t mp_call_function_0(mp_obj_t fun);
116 mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg);
117 mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
118 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);
119 mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args);
120 mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args);
121 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);
122 // Call function and catch/dump exception - for Python callbacks from C code
123 // (return MP_OBJ_NULL in case of exception).
124 mp_obj_t mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg);
125 mp_obj_t mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
126 
127 typedef struct _mp_call_args_t {
128     mp_obj_t fun;
129     size_t n_args, n_kw, n_alloc;
130     mp_obj_t *args;
131 } mp_call_args_t;
132 
133 #if MICROPY_STACKLESS
134 // Takes arguments which are the most general mix of Python arg types, and
135 // prepares argument array suitable for passing to ->call() method of a
136 // function object (and mp_call_function_n_kw()).
137 // (Only needed in stackless mode.)
138 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);
139 #endif
140 
141 void mp_unpack_sequence(mp_obj_t seq, size_t num, mp_obj_t *items);
142 void mp_unpack_ex(mp_obj_t seq, size_t num, mp_obj_t *items);
143 mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value);
144 mp_obj_t mp_load_attr(mp_obj_t base, qstr attr);
145 void mp_convert_member_lookup(mp_obj_t obj, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest);
146 void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest);
147 void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest);
148 void mp_load_method_protected(mp_obj_t obj, qstr attr, mp_obj_t *dest, bool catch_all_exc);
149 void mp_load_super_method(qstr attr, mp_obj_t *dest);
150 void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val);
151 
152 mp_obj_t mp_getiter(mp_obj_t o, mp_obj_iter_buf_t *iter_buf);
153 mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration()
154 mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...)
155 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);
156 
mp_make_stop_iteration(mp_obj_t o)157 static inline mp_obj_t mp_make_stop_iteration(mp_obj_t o) {
158     MP_STATE_THREAD(stop_iteration_arg) = o;
159     return MP_OBJ_STOP_ITERATION;
160 }
161 
162 mp_obj_t mp_make_raise_obj(mp_obj_t o);
163 
164 mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
165 mp_obj_t mp_import_from(mp_obj_t module, qstr name);
166 void mp_import_all(mp_obj_t module);
167 
168 #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
169 NORETURN void mp_raise_type(const mp_obj_type_t *exc_type);
170 NORETURN void mp_raise_ValueError_no_msg(void);
171 NORETURN void mp_raise_TypeError_no_msg(void);
172 NORETURN void mp_raise_NotImplementedError_no_msg(void);
173 #define mp_raise_msg(exc_type, msg) mp_raise_type(exc_type)
174 #define mp_raise_msg_varg(exc_type, ...) mp_raise_type(exc_type)
175 #define mp_raise_ValueError(msg) mp_raise_ValueError_no_msg()
176 #define mp_raise_TypeError(msg) mp_raise_TypeError_no_msg()
177 #define mp_raise_NotImplementedError(msg) mp_raise_NotImplementedError_no_msg()
178 #else
179 #define mp_raise_type(exc_type) mp_raise_msg(exc_type, NULL)
180 NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
181 NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...);
182 NORETURN void mp_raise_ValueError(mp_rom_error_text_t msg);
183 NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg);
184 NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg);
185 #endif
186 
187 NORETURN void mp_raise_type_arg(const mp_obj_type_t *exc_type, mp_obj_t arg);
188 NORETURN void mp_raise_StopIteration(mp_obj_t arg);
189 NORETURN void mp_raise_OSError(int errno_);
190 NORETURN void mp_raise_recursion_depth(void);
191 
192 #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
193 #undef mp_check_self
194 #define mp_check_self(pred)
195 #else
196 // A port may define to raise TypeError for example
197 #ifndef mp_check_self
198 #define mp_check_self(pred) assert(pred)
199 #endif
200 #endif
201 
202 // helper functions for native/viper code
203 int mp_native_type_from_qstr(qstr qst);
204 mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type);
205 mp_obj_t mp_native_to_obj(mp_uint_t val, mp_uint_t type);
206 
207 #define mp_sys_path (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_path_obj)))
208 #define mp_sys_argv (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)))
209 
210 #if MICROPY_WARNINGS
211 #ifndef mp_warning
212 void mp_warning(const char *category, const char *msg, ...);
213 #endif
214 #else
215 #define mp_warning(...)
216 #endif
217 
218 #endif // MICROPY_INCLUDED_PY_RUNTIME_H
219