aboutsummaryrefslogtreecommitdiff
path: root/runtime/src/shed/effects.c
blob: 39dabe56fd15ae736241b6bf8d698f34e398e7eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stddef.h>
#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;
}