summaryrefslogtreecommitdiff
path: root/py/nlr.h
diff options
context:
space:
mode:
Diffstat (limited to 'py/nlr.h')
-rw-r--r--py/nlr.h25
1 files changed, 24 insertions, 1 deletions
diff --git a/py/nlr.h b/py/nlr.h
index b37370b22..62972dba6 100644
--- a/py/nlr.h
+++ b/py/nlr.h
@@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
- * Copyright (c) 2013, 2014 Damien P. George
+ * Copyright (c) 2013-2023 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@@ -31,6 +31,7 @@
#include <limits.h>
#include <assert.h>
+#include <stdbool.h>
#include "py/mpconfig.h"
@@ -123,6 +124,15 @@ struct _nlr_buf_t {
#endif
};
+typedef void (*nlr_jump_callback_fun_t)(void *ctx);
+
+typedef struct _nlr_jump_callback_node_t nlr_jump_callback_node_t;
+
+struct _nlr_jump_callback_node_t {
+ nlr_jump_callback_node_t *prev;
+ nlr_jump_callback_fun_t fun;
+};
+
// Helper macros to save/restore the pystack state
#if MICROPY_ENABLE_PYSTACK
#define MP_NLR_SAVE_PYSTACK(nlr_buf) (nlr_buf)->pystack = MP_STATE_THREAD(pystack_cur)
@@ -140,6 +150,7 @@ struct _nlr_buf_t {
nlr_jump_fail(val); \
} \
top->ret_val = val; \
+ nlr_call_jump_callbacks(top); \
MP_NLR_RESTORE_PYSTACK(top); \
*_top_ptr = top->prev; \
@@ -187,4 +198,16 @@ NORETURN void nlr_jump_fail(void *val);
#endif
+// Push a callback on to the linked-list of NLR jump callbacks. The `node` pointer must
+// be on the C stack. The `fun` callback will be executed if an NLR jump is taken which
+// unwinds the C stack through this `node`.
+void nlr_push_jump_callback(nlr_jump_callback_node_t *node, nlr_jump_callback_fun_t fun);
+
+// Pop a callback from the linked-list of NLR jump callbacks. The corresponding function
+// will be called if `run_callback` is true.
+void nlr_pop_jump_callback(bool run_callback);
+
+// Pop and call all NLR jump callbacks that were registered after `nlr` buffer was pushed.
+void nlr_call_jump_callbacks(nlr_buf_t *nlr);
+
#endif // MICROPY_INCLUDED_PY_NLR_H