LCOV - code coverage report
Current view: top level - py - runtime.h (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-343-g7b050b366.info Lines: 25 27 92.6 %
Date: 2024-04-25 17:00:48 Functions: 1 1 100.0 %
Branches: 11 13 84.6 %

           Branch data     Line data    Source code
       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                 :            : #include "py/stackctrl.h"
      32                 :            : 
      33                 :            : // For use with mp_call_function_1_from_nlr_jump_callback.
      34                 :            : #define MP_DEFINE_NLR_JUMP_CALLBACK_FUNCTION_1(ctx, f, a) \
      35                 :            :     nlr_jump_callback_node_call_function_1_t ctx = { \
      36                 :            :         .func = (void (*)(void *))(f), \
      37                 :            :         .arg = (a), \
      38                 :            :     }
      39                 :            : 
      40                 :            : typedef enum {
      41                 :            :     MP_VM_RETURN_NORMAL,
      42                 :            :     MP_VM_RETURN_YIELD,
      43                 :            :     MP_VM_RETURN_EXCEPTION,
      44                 :            : } mp_vm_return_kind_t;
      45                 :            : 
      46                 :            : typedef enum {
      47                 :            :     MP_ARG_BOOL      = 0x001,
      48                 :            :     MP_ARG_INT       = 0x002,
      49                 :            :     MP_ARG_OBJ       = 0x003,
      50                 :            :     MP_ARG_KIND_MASK = 0x0ff,
      51                 :            :     MP_ARG_REQUIRED  = 0x100,
      52                 :            :     MP_ARG_KW_ONLY   = 0x200,
      53                 :            : } mp_arg_flag_t;
      54                 :            : 
      55                 :            : typedef union _mp_arg_val_t {
      56                 :            :     bool u_bool;
      57                 :            :     mp_int_t u_int;
      58                 :            :     mp_obj_t u_obj;
      59                 :            :     mp_rom_obj_t u_rom_obj;
      60                 :            : } mp_arg_val_t;
      61                 :            : 
      62                 :            : typedef struct _mp_arg_t {
      63                 :            :     uint16_t qst;
      64                 :            :     uint16_t flags;
      65                 :            :     mp_arg_val_t defval;
      66                 :            : } mp_arg_t;
      67                 :            : 
      68                 :            : struct _mp_sched_node_t;
      69                 :            : 
      70                 :            : typedef void (*mp_sched_callback_t)(struct _mp_sched_node_t *);
      71                 :            : 
      72                 :            : typedef struct _mp_sched_node_t {
      73                 :            :     mp_sched_callback_t callback;
      74                 :            :     struct _mp_sched_node_t *next;
      75                 :            : } mp_sched_node_t;
      76                 :            : 
      77                 :            : // For use with mp_globals_locals_set_from_nlr_jump_callback.
      78                 :            : typedef struct _nlr_jump_callback_node_globals_locals_t {
      79                 :            :     nlr_jump_callback_node_t callback;
      80                 :            :     mp_obj_dict_t *globals;
      81                 :            :     mp_obj_dict_t *locals;
      82                 :            : } nlr_jump_callback_node_globals_locals_t;
      83                 :            : 
      84                 :            : // For use with mp_call_function_1_from_nlr_jump_callback.
      85                 :            : typedef struct _nlr_jump_callback_node_call_function_1_t {
      86                 :            :     nlr_jump_callback_node_t callback;
      87                 :            :     void (*func)(void *);
      88                 :            :     void *arg;
      89                 :            : } nlr_jump_callback_node_call_function_1_t;
      90                 :            : 
      91                 :            : // Tables mapping operator enums to qstrs, defined in objtype.c
      92                 :            : extern const byte mp_unary_op_method_name[];
      93                 :            : extern const byte mp_binary_op_method_name[];
      94                 :            : 
      95                 :            : void mp_init(void);
      96                 :            : void mp_deinit(void);
      97                 :            : 
      98                 :            : void mp_sched_exception(mp_obj_t exc);
      99                 :            : void mp_sched_keyboard_interrupt(void);
     100                 :            : #if MICROPY_ENABLE_VM_ABORT
     101                 :            : void mp_sched_vm_abort(void);
     102                 :            : #endif
     103                 :            : void mp_handle_pending(bool raise_exc);
     104                 :            : 
     105                 :            : #if MICROPY_ENABLE_SCHEDULER
     106                 :            : void mp_sched_lock(void);
     107                 :            : void mp_sched_unlock(void);
     108                 :            : #define mp_sched_num_pending() (MP_STATE_VM(sched_len))
     109                 :            : bool mp_sched_schedule(mp_obj_t function, mp_obj_t arg);
     110                 :            : bool mp_sched_schedule_node(mp_sched_node_t *node, mp_sched_callback_t callback);
     111                 :            : #endif
     112                 :            : 
     113                 :            : // Handles any pending MicroPython events without waiting for an interrupt or event.
     114                 :            : void mp_event_handle_nowait(void);
     115                 :            : 
     116                 :            : // Handles any pending MicroPython events and then suspends execution until the
     117                 :            : // next interrupt or event.
     118                 :            : //
     119                 :            : // Note: on "tickless" ports this can suspend execution for a long time,
     120                 :            : // don't call unless you know an interrupt is coming to continue execution.
     121                 :            : // On "ticked" ports it may return early due to the tick interrupt.
     122                 :            : void mp_event_wait_indefinite(void);
     123                 :            : 
     124                 :            : // Handle any pending MicroPython events and then suspends execution until the
     125                 :            : // next interrupt or event, or until timeout_ms milliseconds have elapsed.
     126                 :            : //
     127                 :            : // On "ticked" ports it may return early due to the tick interrupt.
     128                 :            : void mp_event_wait_ms(mp_uint_t timeout_ms);
     129                 :            : 
     130                 :            : // extra printing method specifically for mp_obj_t's which are integral type
     131                 :            : 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);
     132                 :            : 
     133                 :            : void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig);
     134                 :    1564718 : 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) {
     135                 :    1564718 :     mp_arg_check_num_sig(n_args, n_kw, MP_OBJ_FUN_MAKE_SIG(n_args_min, n_args_max, takes_kw));
     136                 :            : }
     137                 :            : 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);
     138                 :            : 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);
     139                 :            : NORETURN void mp_arg_error_terse_mismatch(void);
     140                 :            : NORETURN void mp_arg_error_unimpl_kw(void);
     141                 :            : 
     142                 :    1755682 : static inline mp_obj_dict_t *mp_locals_get(void) {
     143                 :    1755682 :     return MP_STATE_THREAD(dict_locals);
     144                 :            : }
     145                 :      13318 : static inline void mp_locals_set(mp_obj_dict_t *d) {
     146         [ +  + ]:      12369 :     MP_STATE_THREAD(dict_locals) = d;
     147                 :            : }
     148                 :   23274649 : static inline mp_obj_dict_t *mp_globals_get(void) {
     149         [ +  + ]:   23986862 :     return MP_STATE_THREAD(dict_globals);
     150                 :            : }
     151                 :   16582550 : static inline void mp_globals_set(mp_obj_dict_t *d) {
     152   [ +  +  +  +  :    8999977 :     MP_STATE_THREAD(dict_globals) = d;
                      + ]
     153                 :            : }
     154                 :            : 
     155                 :            : void mp_globals_locals_set_from_nlr_jump_callback(void *ctx_in);
     156                 :            : void mp_call_function_1_from_nlr_jump_callback(void *ctx_in);
     157                 :            : 
     158                 :            : #if MICROPY_PY_THREAD
     159                 :        593 : static inline void mp_thread_init_state(mp_state_thread_t *ts, size_t stack_size, mp_obj_dict_t *locals, mp_obj_dict_t *globals) {
     160                 :        593 :     mp_thread_set_state(ts);
     161                 :            : 
     162                 :        593 :     mp_stack_set_top(ts + 1); // need to include ts in root-pointer scan
     163                 :        592 :     mp_stack_set_limit(stack_size);
     164                 :            : 
     165                 :            :     // GC starts off unlocked
     166                 :        593 :     ts->gc_lock_depth = 0;
     167                 :            : 
     168                 :            :     // There are no pending jump callbacks or exceptions yet
     169                 :        593 :     ts->nlr_jump_callback_top = NULL;
     170                 :        593 :     ts->mp_pending_exception = MP_OBJ_NULL;
     171                 :            : 
     172                 :            :     // If locals/globals are not given, inherit from main thread
     173         [ -  + ]:        593 :     if (locals == NULL) {
     174                 :          0 :         locals = mp_state_ctx.thread.dict_locals;
     175                 :            :     }
     176         [ -  + ]:        593 :     if (globals == NULL) {
     177                 :          0 :         globals = mp_state_ctx.thread.dict_globals;
     178                 :            :     }
     179                 :        593 :     mp_locals_set(locals);
     180                 :        593 :     mp_globals_set(globals);
     181                 :        593 : }
     182                 :            : #endif
     183                 :            : 
     184                 :            : mp_obj_t mp_load_name(qstr qst);
     185                 :            : mp_obj_t mp_load_global(qstr qst);
     186                 :            : mp_obj_t mp_load_build_class(void);
     187                 :            : void mp_store_name(qstr qst, mp_obj_t obj);
     188                 :            : void mp_store_global(qstr qst, mp_obj_t obj);
     189                 :            : void mp_delete_name(qstr qst);
     190                 :            : void mp_delete_global(qstr qst);
     191                 :            : 
     192                 :            : mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg);
     193                 :            : mp_obj_t mp_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs);
     194                 :            : 
     195                 :            : mp_obj_t mp_call_function_0(mp_obj_t fun);
     196                 :            : mp_obj_t mp_call_function_1(mp_obj_t fun, mp_obj_t arg);
     197                 :            : mp_obj_t mp_call_function_2(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
     198                 :            : 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);
     199                 :            : mp_obj_t mp_call_method_n_kw(size_t n_args, size_t n_kw, const mp_obj_t *args);
     200                 :            : mp_obj_t mp_call_method_n_kw_var(bool have_self, size_t n_args_n_kw, const mp_obj_t *args);
     201                 :            : 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);
     202                 :            : // Call function and catch/dump exception - for Python callbacks from C code
     203                 :            : // (return MP_OBJ_NULL in case of exception).
     204                 :            : mp_obj_t mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg);
     205                 :            : mp_obj_t mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2);
     206                 :            : 
     207                 :            : typedef struct _mp_call_args_t {
     208                 :            :     mp_obj_t fun;
     209                 :            :     size_t n_args, n_kw, n_alloc;
     210                 :            :     mp_obj_t *args;
     211                 :            : } mp_call_args_t;
     212                 :            : 
     213                 :            : #if MICROPY_STACKLESS
     214                 :            : // Takes arguments which are the most general mix of Python arg types, and
     215                 :            : // prepares argument array suitable for passing to ->call() method of a
     216                 :            : // function object (and mp_call_function_n_kw()).
     217                 :            : // (Only needed in stackless mode.)
     218                 :            : 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);
     219                 :            : #endif
     220                 :            : 
     221                 :            : void mp_unpack_sequence(mp_obj_t seq, size_t num, mp_obj_t *items);
     222                 :            : void mp_unpack_ex(mp_obj_t seq, size_t num, mp_obj_t *items);
     223                 :            : mp_obj_t mp_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value);
     224                 :            : mp_obj_t mp_load_attr(mp_obj_t base, qstr attr);
     225                 :            : void mp_convert_member_lookup(mp_obj_t obj, const mp_obj_type_t *type, mp_obj_t member, mp_obj_t *dest);
     226                 :            : void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest);
     227                 :            : void mp_load_method_maybe(mp_obj_t base, qstr attr, mp_obj_t *dest);
     228                 :            : void mp_load_method_protected(mp_obj_t obj, qstr attr, mp_obj_t *dest, bool catch_all_exc);
     229                 :            : void mp_load_super_method(qstr attr, mp_obj_t *dest);
     230                 :            : void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t val);
     231                 :            : 
     232                 :            : mp_obj_t mp_getiter(mp_obj_t o, mp_obj_iter_buf_t *iter_buf);
     233                 :            : mp_obj_t mp_iternext_allow_raise(mp_obj_t o); // may return MP_OBJ_STOP_ITERATION instead of raising StopIteration()
     234                 :            : mp_obj_t mp_iternext(mp_obj_t o); // will always return MP_OBJ_STOP_ITERATION instead of raising StopIteration(...)
     235                 :            : 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);
     236                 :            : 
     237                 :        533 : static inline mp_obj_t mp_make_stop_iteration(mp_obj_t o) {
     238                 :        533 :     MP_STATE_THREAD(stop_iteration_arg) = o;
     239                 :        533 :     return MP_OBJ_STOP_ITERATION;
     240                 :            : }
     241                 :            : 
     242                 :            : mp_obj_t mp_make_raise_obj(mp_obj_t o);
     243                 :            : 
     244                 :            : mp_obj_t mp_import_name(qstr name, mp_obj_t fromlist, mp_obj_t level);
     245                 :            : mp_obj_t mp_import_from(mp_obj_t module, qstr name);
     246                 :            : void mp_import_all(mp_obj_t module);
     247                 :            : 
     248                 :            : #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
     249                 :            : NORETURN void mp_raise_type(const mp_obj_type_t *exc_type);
     250                 :            : NORETURN void mp_raise_ValueError_no_msg(void);
     251                 :            : NORETURN void mp_raise_TypeError_no_msg(void);
     252                 :            : NORETURN void mp_raise_NotImplementedError_no_msg(void);
     253                 :            : #define mp_raise_msg(exc_type, msg) mp_raise_type(exc_type)
     254                 :            : #define mp_raise_msg_varg(exc_type, ...) mp_raise_type(exc_type)
     255                 :            : #define mp_raise_ValueError(msg) mp_raise_ValueError_no_msg()
     256                 :            : #define mp_raise_TypeError(msg) mp_raise_TypeError_no_msg()
     257                 :            : #define mp_raise_NotImplementedError(msg) mp_raise_NotImplementedError_no_msg()
     258                 :            : #else
     259                 :            : #define mp_raise_type(exc_type) mp_raise_msg(exc_type, NULL)
     260                 :            : NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
     261                 :            : NORETURN void mp_raise_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...);
     262                 :            : NORETURN void mp_raise_ValueError(mp_rom_error_text_t msg);
     263                 :            : NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg);
     264                 :            : NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg);
     265                 :            : #endif
     266                 :            : 
     267                 :            : NORETURN void mp_raise_type_arg(const mp_obj_type_t *exc_type, mp_obj_t arg);
     268                 :            : NORETURN void mp_raise_StopIteration(mp_obj_t arg);
     269                 :            : NORETURN void mp_raise_TypeError_int_conversion(mp_const_obj_t arg);
     270                 :            : NORETURN void mp_raise_OSError(int errno_);
     271                 :            : NORETURN void mp_raise_OSError_with_filename(int errno_, const char *filename);
     272                 :            : NORETURN void mp_raise_recursion_depth(void);
     273                 :            : 
     274                 :            : #if MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG
     275                 :            : #undef mp_check_self
     276                 :            : #define mp_check_self(pred)
     277                 :            : #else
     278                 :            : // A port may define to raise TypeError for example
     279                 :            : #ifndef mp_check_self
     280                 :            : #define mp_check_self(pred) assert(pred)
     281                 :            : #endif
     282                 :            : #endif
     283                 :            : 
     284                 :            : // helper functions for native/viper code
     285                 :            : int mp_native_type_from_qstr(qstr qst);
     286                 :            : mp_uint_t mp_native_from_obj(mp_obj_t obj, mp_uint_t type);
     287                 :            : mp_obj_t mp_native_to_obj(mp_uint_t val, mp_uint_t type);
     288                 :            : 
     289                 :            : #if MICROPY_PY_SYS_PATH
     290                 :            : #define mp_sys_path (MP_STATE_VM(sys_mutable[MP_SYS_MUTABLE_PATH]))
     291                 :            : #endif
     292                 :            : 
     293                 :            : #if MICROPY_PY_SYS_ARGV
     294                 :            : #define mp_sys_argv (MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_sys_argv_obj)))
     295                 :            : #endif
     296                 :            : 
     297                 :            : #if MICROPY_WARNINGS
     298                 :            : #ifndef mp_warning
     299                 :            : void mp_warning(const char *category, const char *msg, ...);
     300                 :            : #endif
     301                 :            : #else
     302                 :            : #define mp_warning(...)
     303                 :            : #endif
     304                 :            : 
     305                 :            : #endif // MICROPY_INCLUDED_PY_RUNTIME_H

Generated by: LCOV version 1.15-5-g462f71d