blob: 6288e88367f0f8aeb10771eaa1a1535a353d65de [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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 */
Alexander Steffen299bc622017-06-29 23:14:58 +020026#ifndef MICROPY_INCLUDED_PY_RUNTIME_H
27#define MICROPY_INCLUDED_PY_RUNTIME_H
Damien George9ddbe292014-12-29 01:02:19 +000028
Damien Georgeb4b10fd2015-01-01 23:30:53 +000029#include "py/mpstate.h"
Damien George02d830c2017-11-26 23:28:40 +110030#include "py/pystack.h"
Damien George04b91472014-05-03 23:27:38 +010031
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +030032typedef enum {
33 MP_VM_RETURN_NORMAL,
34 MP_VM_RETURN_YIELD,
35 MP_VM_RETURN_EXCEPTION,
36} mp_vm_return_kind_t;
37
Damien Georgea3f94e02014-04-20 00:13:22 +010038typedef enum {
Damien Georgedbc81df2014-04-26 11:19:17 +010039 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 Georgea3f94e02014-04-20 00:13:22 +010046
Damien Georgedbc81df2014-04-26 11:19:17 +010047typedef union _mp_arg_val_t {
Damien Georgea3f94e02014-04-20 00:13:22 +010048 bool u_bool;
Damien George40f3c022014-07-03 13:25:24 +010049 mp_int_t u_int;
Damien Georgea3f94e02014-04-20 00:13:22 +010050 mp_obj_t u_obj;
Damien Georgecbf76742015-11-27 13:38:15 +000051 mp_rom_obj_t u_rom_obj;
Damien Georgedbc81df2014-04-26 11:19:17 +010052} mp_arg_val_t;
Damien Georgea3f94e02014-04-20 00:13:22 +010053
Damien Georgedbc81df2014-04-26 11:19:17 +010054typedef struct _mp_arg_t {
Damien Georgee97df972016-09-23 12:13:51 +100055 uint16_t qst;
56 uint16_t flags;
Damien Georgedbc81df2014-04-26 11:19:17 +010057 mp_arg_val_t defval;
58} mp_arg_t;
Damien Georgea3f94e02014-04-20 00:13:22 +010059
Damien George6c82cfc2017-10-04 11:31:05 +110060// Tables mapping operator enums to qstrs, defined in objtype.c
Paul Sokolovsky9956fd02017-10-21 11:06:32 +030061extern const byte mp_unary_op_method_name[];
62extern const byte mp_binary_op_method_name[];
Damien Georgea5efcd42015-01-27 18:02:25 +000063
Damien Georged17926d2014-03-30 13:35:08 +010064void mp_init(void);
65void mp_deinit(void);
Damien George2326d522014-03-27 23:26:35 +000066
Damien George6e74d242017-02-16 18:05:06 +110067void mp_handle_pending(void);
68void mp_handle_pending_tail(mp_uint_t atomic_state);
69
70#if MICROPY_ENABLE_SCHEDULER
71void mp_sched_lock(void);
72void mp_sched_unlock(void);
73static inline unsigned int mp_sched_num_pending(void) { return MP_STATE_VM(sched_sp); }
74bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg);
75#endif
76
Damien George7f9d1d62015-04-09 23:56:15 +010077// extra printing method specifically for mp_obj_t's which are integral type
78int 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);
79
Damien Georgee7cd1692016-03-14 22:35:48 +000080void 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);
81void 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);
82void 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);
Damien George7f233842015-01-01 15:33:50 +000083NORETURN void mp_arg_error_terse_mismatch(void);
Damien Georgec53b4082014-05-06 16:52:35 +000084NORETURN void mp_arg_error_unimpl_kw(void);
Dave Hylands51dabac2014-02-17 17:57:13 -080085
Damien George64a4f112017-03-24 18:43:28 +110086static inline mp_obj_dict_t *mp_locals_get(void) { return MP_STATE_THREAD(dict_locals); }
87static inline void mp_locals_set(mp_obj_dict_t *d) { MP_STATE_THREAD(dict_locals) = d; }
88static inline mp_obj_dict_t *mp_globals_get(void) { return MP_STATE_THREAD(dict_globals); }
89static inline void mp_globals_set(mp_obj_dict_t *d) { MP_STATE_THREAD(dict_globals) = d; }
Damien429d7192013-10-04 19:53:11 +010090
Damien George50912e72015-01-20 11:55:10 +000091mp_obj_t mp_load_name(qstr qst);
92mp_obj_t mp_load_global(qstr qst);
Damien Georged17926d2014-03-30 13:35:08 +010093mp_obj_t mp_load_build_class(void);
Damien George50912e72015-01-20 11:55:10 +000094void mp_store_name(qstr qst, mp_obj_t obj);
95void mp_store_global(qstr qst, mp_obj_t obj);
96void mp_delete_name(qstr qst);
97void mp_delete_global(qstr qst);
Damien George66028ab2014-01-03 14:03:48 +000098
Damien George58321dd2017-08-29 13:04:01 +100099mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg);
100mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs);
Damien Georged17926d2014-03-30 13:35:08 +0100101
Damien Georged17926d2014-03-30 13:35:08 +0100102mp_obj_t mp_call_function_0(mp_obj_t fun);
103mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg);
104mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
Damien George4e3bac22017-02-16 15:32:34 +1100105mp_obj_t mp_call_function_n_kw(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *args);
106mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args);
107mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args);
Paul Sokolovsky037e6912016-11-22 01:33:55 +0300108mp_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);
Paul Sokolovsky6d103b62016-04-25 19:28:12 +0300109// Call function and catch/dump exception - for Python callbacks from C code
Paul Sokolovsky62b96142017-12-05 00:38:41 +0200110// (return MP_OBJ_NULL in case of exception).
111mp_obj_t mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg);
112mp_obj_t mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
Damien Georged17926d2014-03-30 13:35:08 +0100113
Damien George12a5e172015-04-01 23:31:30 +0100114typedef struct _mp_call_args_t {
Paul Sokolovskye6c6fe32015-03-28 01:14:45 +0200115 mp_obj_t fun;
Damien George4e3bac22017-02-16 15:32:34 +1100116 size_t n_args, n_kw, n_alloc;
Paul Sokolovskye6c6fe32015-03-28 01:14:45 +0200117 mp_obj_t *args;
Damien George12a5e172015-04-01 23:31:30 +0100118} mp_call_args_t;
Paul Sokolovskye6c6fe32015-03-28 01:14:45 +0200119
Damien George12a5e172015-04-01 23:31:30 +0100120#if MICROPY_STACKLESS
Paul Sokolovskye6c6fe32015-03-28 01:14:45 +0200121// Takes arguments which are the most general mix of Python arg types, and
122// prepares argument array suitable for passing to ->call() method of a
123// function object (and mp_call_function_n_kw()).
Damien George12a5e172015-04-01 23:31:30 +0100124// (Only needed in stackless mode.)
Damien George4e3bac22017-02-16 15:32:34 +1100125void 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);
Damien George12a5e172015-04-01 23:31:30 +0100126#endif
Paul Sokolovskye6c6fe32015-03-28 01:14:45 +0200127
Damien George4e3bac22017-02-16 15:32:34 +1100128void mp_unpack_sequence(mp_obj_t seq, size_t num, mp_obj_t *items);
129void mp_unpack_ex(mp_obj_t seq, size_t num, mp_obj_t *items);
Damien Georged17926d2014-03-30 13:35:08 +0100130mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value);
131mp_obj_t mp_load_attr(mp_obj_t base, qstr attr);
Damien George55b74d12015-03-21 14:21:54 +0000132void mp_convert_member_lookup(mp_obj_t obj, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest);
Damien Georged17926d2014-03-30 13:35:08 +0100133void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest);
Damien Georgee44d26a2014-03-31 22:57:56 +0100134void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest);
Damien Georgedd11af22017-04-19 09:45:59 +1000135void mp_load_super_method(qstr attr, mp_obj_t *dest);
Damien Georged17926d2014-03-30 13:35:08 +0100136void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val);
Damien Georged17926d2014-03-30 13:35:08 +0100137
Damien Georgeae8d8672016-01-09 23:14:54 +0000138mp_obj_t mp_getiter(mp_obj_t o, mp_obj_iter_buf_t *iter_buf);
Damien Georgeea8d06c2014-04-17 23:19:36 +0100139mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration()
140mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...)
Paul Sokolovskyf39d3b92014-03-30 23:14:55 +0300141mp_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 Georged17926d2014-03-30 13:35:08 +0100142
143mp_obj_t mp_make_raise_obj(mp_obj_t o);
144
Damien Georged17926d2014-03-30 13:35:08 +0100145mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
146mp_obj_t mp_import_from(mp_obj_t module, qstr name);
147void mp_import_all(mp_obj_t module);
Paul Sokolovsky5500cde2014-04-13 06:43:18 +0300148
Paul Sokolovsky9e1b61d2016-08-12 21:26:12 +0300149NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, const char *msg);
150//NORETURN void nlr_raise_msg_varg(const mp_obj_type_t *exc_type, const char *fmt, ...);
151NORETURN void mp_raise_ValueError(const char *msg);
152NORETURN void mp_raise_TypeError(const char *msg);
Javier Candeira35a1fea2017-08-09 14:40:45 +1000153NORETURN void mp_raise_NotImplementedError(const char *msg);
Damien George3a0a7712016-10-07 13:31:59 +1100154NORETURN void mp_raise_OSError(int errno_);
Damien George5b8998d2017-11-20 17:29:58 +1100155NORETURN void mp_raise_recursion_depth(void);
Paul Sokolovsky7e4a2b02014-06-07 23:22:41 +0300156
Paul Sokolovsky8c50f932016-08-12 21:58:56 +0300157#if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
158#undef mp_check_self
159#define mp_check_self(pred)
160#else
161// A port may define to raise TypeError for example
162#ifndef mp_check_self
163#define mp_check_self(pred) assert(pred)
164#endif
165#endif
166
Damien George86de21b2014-08-16 22:06:11 +0100167// helper functions for native/viper code
Damien Georgee6c0dff2014-08-15 23:47:59 +0100168mp_uint_t mp_convert_obj_to_native(mp_obj_t obj, mp_uint_t type);
169mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type);
Damien George4e3bac22017-02-16 15:32:34 +1100170mp_obj_t mp_native_call_function_n_kw(mp_obj_t fun_in, size_t n_args_kw, const mp_obj_t *args);
Damien Georgeb6e6b522015-01-21 17:00:01 +0000171void mp_native_raise(mp_obj_t o);
Damien Georgee6c0dff2014-08-15 23:47:59 +0100172
Damien George999cedb2015-11-27 17:01:44 +0000173#define mp_sys_path (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_path_obj)))
174#define mp_sys_argv (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)))
Damien George9ddbe292014-12-29 01:02:19 +0000175
Paul Sokolovsky8a8c1fc2015-01-01 09:29:28 +0200176#if MICROPY_WARNINGS
177void mp_warning(const char *msg, ...);
178#else
Damien George89f657f2017-09-13 20:33:55 +1000179#define mp_warning(...)
Paul Sokolovsky8a8c1fc2015-01-01 09:29:28 +0200180#endif
181
Alexander Steffen299bc622017-06-29 23:14:58 +0200182#endif // MICROPY_INCLUDED_PY_RUNTIME_H