From b99b938b2f33e4d12d63629f3d80e987c31d1892 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 19 Nov 2023 15:47:38 +0000 Subject: Implement basic effect handling in the runtime --- runtime/src/effects.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ runtime/src/shed.h | 14 ++++++++++---- 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 runtime/src/effects.c (limited to 'runtime/src') 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; +} diff --git a/runtime/src/shed.h b/runtime/src/shed.h index 8beab85d..3a9d3ff9 100644 --- a/runtime/src/shed.h +++ b/runtime/src/shed.h @@ -6,16 +6,19 @@ #ifdef __WASM__ typedef uint32_t size_t; typedef int32_t ssize_t; +#define NULL 0 #else #include #endif #ifdef __WASM__ -typedef uint32_t ShedObject; -typedef uint32_t ShedStringLength; +typedef uint32_t PatioInt; +typedef PatioInt ShedObject; +typedef PatioInt ShedStringLength; #else -typedef uint64_t ShedObject; -typedef uint64_t ShedStringLength; +typedef uint64_t PatioInt; +typedef PatioInt ShedObject; +typedef PatioInt ShedStringLength; #endif extern ShedObject const shed_unit; @@ -40,4 +43,7 @@ typedef ShedObject* ShedEnv; void* shed__runtime__alloc(size_t size); +typedef PatioInt ShedEffectTag; +typedef PatioInt ShedEffectMethodIndex; + #endif -- cgit v1.2.3