aboutsummaryrefslogtreecommitdiff
path: root/runtime/src
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
parent303f428e78cf44f10032e55a69d5d5cd00f5d715 (diff)
Implement basic effect handling in the runtime
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/effects.c53
-rw-r--r--runtime/src/shed.h14
2 files changed, 63 insertions, 4 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;
+}
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 <unistd.h>
#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