Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python 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 | */ |
Damien George | 9ddbe29 | 2014-12-29 01:02:19 +0000 | [diff] [blame] | 26 | #ifndef __MICROPY_INCLUDED_PY_RUNTIME_H__ |
| 27 | #define __MICROPY_INCLUDED_PY_RUNTIME_H__ |
| 28 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 29 | #include "py/mpstate.h" |
Damien George | 9ddbe29 | 2014-12-29 01:02:19 +0000 | [diff] [blame] | 30 | #include "py/obj.h" |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 31 | |
Paul Sokolovsky | f39d3b9 | 2014-03-30 23:14:55 +0300 | [diff] [blame] | 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 | |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 38 | typedef enum { |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 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; |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 46 | |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 47 | typedef union _mp_arg_val_t { |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 48 | bool u_bool; |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 49 | mp_int_t u_int; |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 50 | mp_obj_t u_obj; |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 51 | } mp_arg_val_t; |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 52 | |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 53 | typedef struct _mp_arg_t { |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 54 | qstr qst; |
Damien George | 40f3c02 | 2014-07-03 13:25:24 +0100 | [diff] [blame] | 55 | mp_uint_t flags; |
Damien George | dbc81df | 2014-04-26 11:19:17 +0100 | [diff] [blame] | 56 | mp_arg_val_t defval; |
| 57 | } mp_arg_t; |
Damien George | a3f94e0 | 2014-04-20 00:13:22 +0100 | [diff] [blame] | 58 | |
Damien George | a5efcd4 | 2015-01-27 18:02:25 +0000 | [diff] [blame^] | 59 | // defined in objtype.c |
| 60 | extern const qstr mp_unary_op_method_name[]; |
| 61 | extern const qstr mp_binary_op_method_name[]; |
| 62 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 63 | void mp_init(void); |
| 64 | void mp_deinit(void); |
Damien George | 2326d52 | 2014-03-27 23:26:35 +0000 | [diff] [blame] | 65 | |
Damien George | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 66 | void mp_arg_check_num(mp_uint_t n_args, mp_uint_t n_kw, mp_uint_t n_args_min, mp_uint_t n_args_max, bool takes_kw); |
| 67 | void mp_arg_parse_all(mp_uint_t n_pos, const mp_obj_t *pos, mp_map_t *kws, mp_uint_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals); |
| 68 | void mp_arg_parse_all_kw_array(mp_uint_t n_pos, mp_uint_t n_kw, const mp_obj_t *args, mp_uint_t n_allowed, const mp_arg_t *allowed, mp_arg_val_t *out_vals); |
Damien George | 7f23384 | 2015-01-01 15:33:50 +0000 | [diff] [blame] | 69 | NORETURN void mp_arg_error_terse_mismatch(void); |
Damien George | c53b408 | 2014-05-06 16:52:35 +0000 | [diff] [blame] | 70 | NORETURN void mp_arg_error_unimpl_kw(void); |
Dave Hylands | 51dabac | 2014-02-17 17:57:13 -0800 | [diff] [blame] | 71 | |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 72 | static inline mp_obj_dict_t *mp_locals_get(void) { return MP_STATE_CTX(dict_locals); } |
| 73 | static inline void mp_locals_set(mp_obj_dict_t *d) { MP_STATE_CTX(dict_locals) = d; } |
| 74 | static inline mp_obj_dict_t *mp_globals_get(void) { return MP_STATE_CTX(dict_globals); } |
| 75 | static inline void mp_globals_set(mp_obj_dict_t *d) { MP_STATE_CTX(dict_globals) = d; } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 76 | |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 77 | mp_obj_t mp_load_name(qstr qst); |
| 78 | mp_obj_t mp_load_global(qstr qst); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 79 | mp_obj_t mp_load_build_class(void); |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 80 | void mp_store_name(qstr qst, mp_obj_t obj); |
| 81 | void mp_store_global(qstr qst, mp_obj_t obj); |
| 82 | void mp_delete_name(qstr qst); |
| 83 | void mp_delete_global(qstr qst); |
Damien George | 66028ab | 2014-01-03 14:03:48 +0000 | [diff] [blame] | 84 | |
Damien George | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 85 | mp_obj_t mp_unary_op(mp_uint_t op, mp_obj_t arg); |
| 86 | mp_obj_t mp_binary_op(mp_uint_t op, mp_obj_t lhs, mp_obj_t rhs); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 87 | |
Damien George | 50912e7 | 2015-01-20 11:55:10 +0000 | [diff] [blame] | 88 | mp_obj_t mp_load_const_int(qstr qst); |
| 89 | mp_obj_t mp_load_const_dec(qstr qst); |
| 90 | mp_obj_t mp_load_const_str(qstr qst); |
| 91 | mp_obj_t mp_load_const_bytes(qstr qst); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 92 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 93 | mp_obj_t mp_call_function_0(mp_obj_t fun); |
| 94 | mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg); |
| 95 | mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2); |
Damien George | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 96 | mp_obj_t mp_call_function_n_kw(mp_obj_t fun, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args); |
| 97 | mp_obj_t mp_call_method_n_kw(mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args); |
| 98 | mp_obj_t mp_call_method_n_kw_var(bool have_self, mp_uint_t n_args_n_kw, const mp_obj_t *args); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 99 | |
Damien George | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 100 | void mp_unpack_sequence(mp_obj_t seq, mp_uint_t num, mp_obj_t *items); |
| 101 | void mp_unpack_ex(mp_obj_t seq, mp_uint_t num, mp_obj_t *items); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 102 | mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value); |
| 103 | mp_obj_t mp_load_attr(mp_obj_t base, qstr attr); |
| 104 | void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest); |
Damien George | e44d26a | 2014-03-31 22:57:56 +0100 | [diff] [blame] | 105 | void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 106 | void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 107 | |
| 108 | mp_obj_t mp_getiter(mp_obj_t o); |
Damien George | ea8d06c | 2014-04-17 23:19:36 +0100 | [diff] [blame] | 109 | mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration() |
| 110 | mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...) |
Paul Sokolovsky | f39d3b9 | 2014-03-30 23:14:55 +0300 | [diff] [blame] | 111 | 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); |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 112 | |
| 113 | mp_obj_t mp_make_raise_obj(mp_obj_t o); |
| 114 | |
Damien George | d17926d | 2014-03-30 13:35:08 +0100 | [diff] [blame] | 115 | mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level); |
| 116 | mp_obj_t mp_import_from(mp_obj_t module, qstr name); |
| 117 | void mp_import_all(mp_obj_t module); |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 118 | |
Paul Sokolovsky | 7e4a2b0 | 2014-06-07 23:22:41 +0300 | [diff] [blame] | 119 | // Raise NotImplementedError with given message |
| 120 | NORETURN void mp_not_implemented(const char *msg); |
| 121 | |
Damien George | 86de21b | 2014-08-16 22:06:11 +0100 | [diff] [blame] | 122 | // helper functions for native/viper code |
Damien George | e6c0dff | 2014-08-15 23:47:59 +0100 | [diff] [blame] | 123 | mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type); |
| 124 | mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type); |
Damien George | 4abff75 | 2014-08-30 14:59:21 +0100 | [diff] [blame] | 125 | mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, mp_uint_t n_args_kw, const mp_obj_t *args); |
Damien George | b6e6b52 | 2015-01-21 17:00:01 +0000 | [diff] [blame] | 126 | void mp_native_raise(mp_obj_t o); |
Damien George | e6c0dff | 2014-08-15 23:47:59 +0100 | [diff] [blame] | 127 | |
Paul Sokolovsky | 5500cde | 2014-04-13 06:43:18 +0300 | [diff] [blame] | 128 | extern struct _mp_obj_list_t mp_sys_path_obj; |
| 129 | extern struct _mp_obj_list_t mp_sys_argv_obj; |
| 130 | #define mp_sys_path ((mp_obj_t)&mp_sys_path_obj) |
| 131 | #define mp_sys_argv ((mp_obj_t)&mp_sys_argv_obj) |
Damien George | 9ddbe29 | 2014-12-29 01:02:19 +0000 | [diff] [blame] | 132 | |
Paul Sokolovsky | 8a8c1fc | 2015-01-01 09:29:28 +0200 | [diff] [blame] | 133 | #if MICROPY_WARNINGS |
| 134 | void mp_warning(const char *msg, ...); |
| 135 | #else |
| 136 | #define mp_warning(msg, ...) |
| 137 | #endif |
| 138 | |
Damien George | 9ddbe29 | 2014-12-29 01:02:19 +0000 | [diff] [blame] | 139 | #endif // __MICROPY_INCLUDED_PY_RUNTIME_H__ |