py/runtime: Add helpers to call a general function on nlr jump callback.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2023-09-05 18:12:26 +10:00
parent dc99840b3a
commit c9089e71a1
2 changed files with 20 additions and 0 deletions

View File

@ -197,6 +197,11 @@ void mp_globals_locals_set_from_nlr_jump_callback(void *ctx_in) {
mp_locals_set(ctx->locals);
}
void mp_call_function_1_from_nlr_jump_callback(void *ctx_in) {
nlr_jump_callback_node_call_function_1_t *ctx = ctx_in;
ctx->func(ctx->arg);
}
mp_obj_t MICROPY_WRAP_MP_LOAD_NAME(mp_load_name)(qstr qst) {
// logic: search locals, globals, builtins
DEBUG_OP_printf("load name %s\n", qstr_str(qst));

View File

@ -29,6 +29,13 @@
#include "py/mpstate.h"
#include "py/pystack.h"
// For use with mp_call_function_1_from_nlr_jump_callback.
#define MP_DEFINE_NLR_JUMP_CALLBACK_FUNCTION_1(ctx, f, a) \
nlr_jump_callback_node_call_function_1_t ctx = { \
.func = (void (*)(void *))(f), \
.arg = (a), \
}
typedef enum {
MP_VM_RETURN_NORMAL,
MP_VM_RETURN_YIELD,
@ -73,6 +80,13 @@ typedef struct _nlr_jump_callback_node_globals_locals_t {
mp_obj_dict_t *locals;
} nlr_jump_callback_node_globals_locals_t;
// For use with mp_call_function_1_from_nlr_jump_callback.
typedef struct _nlr_jump_callback_node_call_function_1_t {
nlr_jump_callback_node_t callback;
void (*func)(void *);
void *arg;
} nlr_jump_callback_node_call_function_1_t;
// Tables mapping operator enums to qstrs, defined in objtype.c
extern const byte mp_unary_op_method_name[];
extern const byte mp_binary_op_method_name[];
@ -121,6 +135,7 @@ static inline void mp_globals_set(mp_obj_dict_t *d) {
}
void mp_globals_locals_set_from_nlr_jump_callback(void *ctx_in);
void mp_call_function_1_from_nlr_jump_callback(void *ctx_in);
mp_obj_t mp_load_name(qstr qst);
mp_obj_t mp_load_global(qstr qst);