blob: 87ece625ab5d3ff472ec62cd885ff5bbdebf113d (
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
#include <stddef.h>
#include "./std/unistd.h"
#include "./gc.h"
#include "./closures.h"
#include "./effects.h"
struct ShedEffectHandler {
struct ShedGcObjectHeader gc_object_header;
ShedEffectTag effect_tag;
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;
}
ShedEffectHandler* shed__runtime__effects_push_handler(
ShedEffectTag effect_tag,
ShedEffectMethodIndex method_count
) {
ShedEffectHandler* handler = shed__runtime__gc_alloc(
sizeof(ShedEffectHandler) + sizeof(ShedClosurePointer) * method_count,
offsetof(ShedEffectHandler, methods),
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];
}
handler = handler->parent;
}
// TODO: more informative error
return NULL;
}
|