aboutsummaryrefslogtreecommitdiff
path: root/runtime/src/effects.c
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-11-19 15:47:38 +0000
committerMichael Williamson <mike@zwobble.org>2023-11-19 15:49:28 +0000
commitb99b938b2f33e4d12d63629f3d80e987c31d1892 (patch)
tree5057bba7084240792bfa23520ea18d66564b7cdd /runtime/src/effects.c
parent303f428e78cf44f10032e55a69d5d5cd00f5d715 (diff)
Implement basic effect handling in the runtime
Diffstat (limited to 'runtime/src/effects.c')
-rw-r--r--runtime/src/effects.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/runtime/src/effects.c b/runtime/src/effects.c
new file mode 100644
index 00000000..d456fbe7
--- /dev/null
+++ b/runtime/src/effects.c
@@ -0,0 +1,53 @@
+#include "./shed.h"
+
+struct ShedEffectHandler {
+ ShedEffectTag effect_tag;
+ struct ShedEffectHandler* parent;
+ ShedClosurePointer methods[];
+};
+
+typedef struct ShedEffectHandler ShedEffectHandler;
+
+struct ShedEffectHandler* shed__runtime__current_effect_handler = NULL;
+
+ShedEffectHandler* shed__runtime__effects_push_handler(
+ ShedEffectTag effect_tag,
+ ShedEffectMethodIndex method_count
+) {
+ ShedEffectHandler* handler = shed__runtime__alloc(
+ sizeof(ShedEffectHandler) + sizeof(ShedClosurePointer) * method_count
+ );
+ handler->effect_tag = effect_tag;
+ handler->parent = shed__runtime__current_effect_handler;
+
+ shed__runtime__current_effect_handler = handler;
+
+ return handler;
+}
+
+void shed__runtime__effects_set_handler_method(
+ ShedEffectHandler* effect_handler,
+ ShedEffectMethodIndex method_index,
+ ShedClosurePointer handler_method
+) {
+ effect_handler->methods[method_index] = handler_method;
+}
+
+void shed__runtime__effects_pop_handler() {
+ shed__runtime__current_effect_handler = shed__runtime__current_effect_handler->parent;
+}
+
+ShedClosurePointer shed__runtime__effects_find_handler_method(
+ ShedEffectTag effect_tag,
+ ShedEffectMethodIndex method_index
+) {
+ ShedEffectHandler* handler = shed__runtime__current_effect_handler;
+ while (handler != NULL) {
+ if (handler->effect_tag == effect_tag) {
+ return handler->methods[method_index];
+ }
+ }
+
+ // TODO: more informative error
+ return NULL;
+}