Numworks Epsilon  1.4.1
Graphing Calculator Operating System
runtime.h
Go to the documentation of this file.
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 
31 typedef enum {
36 
37 typedef enum {
38  MP_ARG_BOOL = 0x001,
39  MP_ARG_INT = 0x002,
40  MP_ARG_OBJ = 0x003,
42  MP_ARG_REQUIRED = 0x100,
43  MP_ARG_KW_ONLY = 0x200,
45 
46 typedef union _mp_arg_val_t {
47  bool u_bool;
51 } mp_arg_val_t;
52 
53 typedef struct _mp_arg_t {
57 } mp_arg_t;
58 
59 // Tables mapping operator enums to qstrs, defined in objtype.c
60 extern const byte mp_unary_op_method_name[];
61 extern const byte mp_binary_op_method_name[];
62 
63 void mp_init(void);
64 void mp_deinit(void);
65 
66 void mp_handle_pending(void);
67 void mp_handle_pending_tail(mp_uint_t atomic_state);
68 
69 #if MICROPY_ENABLE_SCHEDULER
70 void mp_sched_lock(void);
71 void mp_sched_unlock(void);
72 static inline unsigned int mp_sched_num_pending(void) { return MP_STATE_VM(sched_sp); }
73 bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg);
74 #endif
75 
76 // extra printing method specifically for mp_obj_t's which are integral type
77 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);
78 
79 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);
80 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);
81 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);
84 
85 static inline mp_obj_dict_t *mp_locals_get(void) { return MP_STATE_THREAD(dict_locals); }
86 static inline void mp_locals_set(mp_obj_dict_t *d) { MP_STATE_THREAD(dict_locals) = d; }
87 static inline mp_obj_dict_t *mp_globals_get(void) { return MP_STATE_THREAD(dict_globals); }
88 static inline void mp_globals_set(mp_obj_dict_t *d) { MP_STATE_THREAD(dict_globals) = d; }
89 
93 void mp_store_name(qstr qst, mp_obj_t obj);
94 void mp_store_global(qstr qst, mp_obj_t obj);
95 void mp_delete_name(qstr qst);
96 void mp_delete_global(qstr qst);
97 
100 
104 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);
105 mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args);
106 mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args);
107 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);
108 // Call function and catch/dump exception - for Python callbacks from C code
111 
112 typedef struct _mp_call_args_t {
114  size_t n_args, n_kw, n_alloc;
117 
118 #if MICROPY_STACKLESS
119 // Takes arguments which are the most general mix of Python arg types, and
120 // prepares argument array suitable for passing to ->call() method of a
121 // function object (and mp_call_function_n_kw()).
122 // (Only needed in stackless mode.)
123 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);
124 #endif
125 
126 void mp_unpack_sequence(mp_obj_t seq, size_t num, mp_obj_t *items);
127 void mp_unpack_ex(mp_obj_t seq, size_t num, mp_obj_t *items);
130 void mp_convert_member_lookup(mp_obj_t obj, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest);
131 void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest);
132 void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest);
134 void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val);
135 
137 mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration()
138 mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...)
139 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);
140 
142 
144 mp_obj_t mp_import_from(mp_obj_t module, qstr name);
145 void mp_import_all(mp_obj_t module);
146 
147 NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg);
148 //NORETURN void nlr_raise_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...);
149 NORETURN void mp_raise_ValueError(const char *msg);
150 NORETURN void mp_raise_TypeError(const char *msg);
151 NORETURN void mp_raise_NotImplementedError(const char *msg);
152 NORETURN void mp_raise_OSError(int errno_);
154 
155 #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
156 #undef mp_check_self
157 #define mp_check_self(pred)
158 #else
159 // A port may define to raise TypeError for example
160 #ifndef mp_check_self
161 #define mp_check_self(pred) assert(pred)
162 #endif
163 #endif
164 
165 // helper functions for native/viper code
168 mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, size_t n_args_kw, const mp_obj_t *args);
169 void mp_native_raise(mp_obj_t o);
170 
171 #define mp_sys_path (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_path_obj)))
172 #define mp_sys_argv (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)))
173 
174 #if MICROPY_WARNINGS
175 void mp_warning(const char *msg, ...);
176 #else
177 #define mp_warning(...)
178 #endif
179 
180 #endif // MICROPY_INCLUDED_PY_RUNTIME_H
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)
Definition: argcheck.c:32
Charge level()
Definition: battery.cpp:20
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)
Definition: runtime.c:1221
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)
Definition: argcheck.c:74
intptr_t mp_int_t
Definition: mpconfigport.h:73
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args)
Definition: runtime.c:639
union _mp_arg_val_t mp_arg_val_t
struct _mp_arg_t mp_arg_t
void mp_store_global(qstr qst, mp_obj_t obj)
Definition: runtime.c:205
NORETURN void mp_arg_error_unimpl_kw(void)
void mp_deinit(void)
Definition: runtime.c:130
void mp_import_all(mp_obj_t module)
Definition: runtime.c:1380
STATIC const uint8_t attr[]
Definition: unicode.c:51
mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level)
Definition: runtime.c:1321
void mp_handle_pending(void)
Definition: scheduler.c:109
unsigned short uint16_t
Definition: stdint.h:5
NORETURN void mp_raise_OSError(int errno_)
Definition: runtime.c:1464
#define MP_STATE_THREAD(x)
Definition: mpstate.h:248
#define MP_STATE_VM(x)
Definition: mpstate.h:241
mp_unary_op_t
Definition: runtime0.h:45
mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs)
Definition: runtime.c:277
mp_obj_t u_obj
Definition: runtime.h:49
NORETURN void mp_raise_TypeError(const char *msg)
Definition: runtime.c:1460
const byte mp_binary_op_method_name[]
Definition: objtype.c:416
mp_obj_t mp_make_raise_obj(mp_obj_t o)
Definition: runtime.c:1304
void mp_native_raise(mp_obj_t o)
struct _mp_call_args_t mp_call_args_t
NORETURN void mp_exc_recursion_depth(void)
void mp_convert_member_lookup(mp_obj_t obj, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest)
Definition: runtime.c:992
void mp_init(void)
Definition: runtime.c:58
mp_vm_return_kind_t
Definition: runtime.h:31
mp_obj_t mp_iternext_allow_raise(mp_obj_t o)
Definition: runtime.c:1162
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)
Definition: mpprint.c:204
uint16_t qst
Definition: runtime.h:54
NORETURN void mp_raise_ValueError(const char *msg)
Definition: runtime.c:1456
mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg)
Definition: runtime.c:216
uint16_t flags
Definition: runtime.h:55
mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type)
void mp_delete_name(qstr qst)
Definition: runtime.c:199
const byte mp_unary_op_method_name[]
Definition: objtype.c:337
mp_obj_t mp_getiter(mp_obj_t o, mp_obj_iter_buf_t *iter_buf)
Definition: runtime.c:1120
size_t qstr
Definition: qstr.h:48
mp_const_obj_t mp_rom_obj_t
Definition: obj.h:239
void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val)
Definition: runtime.c:1100
mp_binary_op_t
Definition: runtime0.h:67
mp_obj_t mp_import_from(mp_obj_t module, qstr name)
Definition: runtime.c:1336
mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, size_t n_args_kw, const mp_obj_t *args)
mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2)
Definition: runtime.c:607
args
Definition: i18n.py:175
#define NORETURN
Definition: mpconfig.h:1268
void mp_store_name(qstr qst, mp_obj_t obj)
Definition: runtime.c:194
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)
Definition: objboundmeth.c:50
void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest)
Definition: runtime.c:1076
mp_obj_t fun
Definition: runtime.h:113
Definition: obj.h:356
unsigned char byte
Definition: misc.h:37
void mp_unpack_sequence(mp_obj_t seq, size_t num, mp_obj_t *items)
Definition: runtime.c:827
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)
Definition: argcheck.c:130
void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest)
Definition: runtime.c:1040
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg)
Definition: runtime.c:1448
mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg)
Definition: runtime.c:603
mp_obj_t mp_load_attr(mp_obj_t base, qstr attr)
Definition: runtime.c:930
void mp_unpack_ex(mp_obj_t seq, size_t num, mp_obj_t *items)
Definition: runtime.c:874
mp_obj_t mp_call_function_0(mp_obj_t fun)
Definition: runtime.c:599
#define mp_warning(...)
Definition: runtime.h:177
STATIC 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)
Definition: runtime.c:649
mp_arg_val_t defval
Definition: runtime.h:56
size_t n_kw
Definition: runtime.h:114
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)
Definition: runtime.c:615
void mp_delete_global(qstr qst)
Definition: runtime.c:210
mp_obj_t mp_load_name(qstr qst)
Definition: runtime.c:140
mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type)
mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args)
Definition: runtime.c:816
void mp_handle_pending_tail(mp_uint_t atomic_state)
uint64_t mp_obj_t
Definition: obj.h:39
mp_rom_obj_t u_rom_obj
Definition: runtime.h:50
mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value)
mp_obj_t mp_iternext(mp_obj_t o)
Definition: runtime.c:1186
void mp_load_super_method(qstr attr, mp_obj_t *dest)
Definition: objtype.c:1133
size_t n_args
Definition: runtime.h:114
NORETURN void mp_raise_NotImplementedError(const char *msg)
Definition: runtime.c:1468
NORETURN void mp_arg_error_terse_mismatch(void)
Definition: argcheck.c:136
void mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg)
Definition: runtime_utils.c:30
mp_obj_t mp_load_global(qstr qst)
Definition: runtime.c:153
size_t n_alloc
Definition: runtime.h:114
mp_arg_flag_t
Definition: runtime.h:37
bool u_bool
Definition: runtime.h:47
mp_int_t u_int
Definition: runtime.h:48
mp_obj_t mp_load_build_class(void)
Definition: runtime.c:180
void mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2)
Definition: runtime_utils.c:40
mp_obj_t * args
Definition: runtime.h:115