Skip to content

Commit c9089e7

Browse files
committed
py/runtime: Add helpers to call a general function on nlr jump callback.
Signed-off-by: Damien George <[email protected]>
1 parent dc99840 commit c9089e7

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

py/runtime.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ void mp_globals_locals_set_from_nlr_jump_callback(void *ctx_in) {
197197
mp_locals_set(ctx->locals);
198198
}
199199

200+
void mp_call_function_1_from_nlr_jump_callback(void *ctx_in) {
201+
nlr_jump_callback_node_call_function_1_t *ctx = ctx_in;
202+
ctx->func(ctx->arg);
203+
}
204+
200205
mp_obj_t MICROPY_WRAP_MP_LOAD_NAME(mp_load_name)(qstr qst) {
201206
// logic: search locals, globals, builtins
202207
DEBUG_OP_printf("load name %s\n", qstr_str(qst));

py/runtime.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@
2929
#include "py/mpstate.h"
3030
#include "py/pystack.h"
3131

32+
// For use with mp_call_function_1_from_nlr_jump_callback.
33+
#define MP_DEFINE_NLR_JUMP_CALLBACK_FUNCTION_1(ctx, f, a) \
34+
nlr_jump_callback_node_call_function_1_t ctx = { \
35+
.func = (void (*)(void *))(f), \
36+
.arg = (a), \
37+
}
38+
3239
typedef enum {
3340
MP_VM_RETURN_NORMAL,
3441
MP_VM_RETURN_YIELD,
@@ -73,6 +80,13 @@ typedef struct _nlr_jump_callback_node_globals_locals_t {
7380
mp_obj_dict_t *locals;
7481
} nlr_jump_callback_node_globals_locals_t;
7582

83+
// For use with mp_call_function_1_from_nlr_jump_callback.
84+
typedef struct _nlr_jump_callback_node_call_function_1_t {
85+
nlr_jump_callback_node_t callback;
86+
void (*func)(void *);
87+
void *arg;
88+
} nlr_jump_callback_node_call_function_1_t;
89+
7690
// Tables mapping operator enums to qstrs, defined in objtype.c
7791
extern const byte mp_unary_op_method_name[];
7892
extern const byte mp_binary_op_method_name[];
@@ -121,6 +135,7 @@ static inline void mp_globals_set(mp_obj_dict_t *d) {
121135
}
122136

123137
void mp_globals_locals_set_from_nlr_jump_callback(void *ctx_in);
138+
void mp_call_function_1_from_nlr_jump_callback(void *ctx_in);
124139

125140
mp_obj_t mp_load_name(qstr qst);
126141
mp_obj_t mp_load_global(qstr qst);

0 commit comments

Comments
 (0)