From a01ee103f9bc0c6defe9ce8cd85651f19f7127bd Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 11 Feb 2024 21:29:15 +0000 Subject: Create separate lib for patio runtime --- runtime/src/shed/effects.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 runtime/src/shed/effects.c (limited to 'runtime/src/shed/effects.c') diff --git a/runtime/src/shed/effects.c b/runtime/src/shed/effects.c new file mode 100644 index 00000000..39dabe56 --- /dev/null +++ b/runtime/src/shed/effects.c @@ -0,0 +1,51 @@ +#include +#include "./std/unistd.h" +#include "./gc.h" +#include "./closures.h" +#include "./effects.h" + +struct ShedEffectHandler { + struct ShedGcObjectHeader gc_object_header; + struct ShedEffectHandler* parent; + ShedClosurePointer methods[]; +}; + +typedef struct ShedEffectHandler ShedEffectHandler; + +struct ShedEffectHandler* shed__runtime__current_effect_handler = NULL; + +ShedEffectHandler* shed__runtime__effects_get_handler() { + return shed__runtime__current_effect_handler; +} + +void shed__runtime__effects_set_handler(ShedEffectHandler* effect_handler) { + shed__runtime__current_effect_handler = effect_handler; +} + +void shed__runtime__effects_push_handler( + ShedEffectHandler* effect_handler +) { + effect_handler->parent = shed__runtime__current_effect_handler; + + shed__runtime__current_effect_handler = effect_handler; +} + +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->gc_object_header.runtime_type_tag == effect_tag) { + return handler->methods[method_index]; + } + handler = handler->parent; + } + + // TODO: more informative error + return NULL; +} -- cgit v1.2.3