-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathblock.c
More file actions
104 lines (94 loc) · 3.23 KB
/
block.c
File metadata and controls
104 lines (94 loc) · 3.23 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "object.h"
#include "vm.h"
#include "runtime.h"
#include "table.h"
#include "memory.h"
#include "compiler.h"
ObjClass *lxBlockClass = NULL;
ObjNative *nativeBlockInit = NULL;
typedef struct LxBlock {
// types of callable objects that can be coerced into blocks with &
// are classes, closures, native functions (any callable)
Obj *callable;
} LxBlock;
static void markInternalBlock(Obj *internalObj) {
ASSERT(internalObj->type == OBJ_T_INTERNAL);
ObjInternal *internal = (ObjInternal*)internalObj;
LxBlock *blk = internal->data;
grayObject((Obj*)blk->callable);
}
static inline LxBlock *blockGetHidden(Value block) {
return (LxBlock*) AS_INSTANCE(block)->internal->data;
}
Obj *blockCallable(Value block) {
LxBlock *blk = blockGetHidden(block);
return blk->callable;
}
Obj *blockCallableBlock(Value block) {
LxBlock *blk = blockGetHidden(block);
Obj *callable = blk->callable;
if (callable->type == OBJ_T_CLOSURE) {
return (Obj*)(((ObjClosure*)callable)->function);
} else {
return callable;
}
}
ObjInstance *getBlockArg(CallFrame *frame) {
if (frame->callInfo) {
return frame->callInfo->blockInstance;
} else {
return NULL;
}
}
static Value lxBlockInit(int argCount, Value *args) {
CHECK_ARITY("Block#init", 2, 2, argCount);
Value self = args[0];
Value callableVal = args[1];
ObjInstance *selfObj = AS_INSTANCE(self);
ObjInternal *internalObj = newInternalObject(false, NULL, 0, markInternalBlock, NULL, NEWOBJ_FLAG_NONE);
hideFromGC(TO_OBJ(internalObj));
LxBlock *blk = ALLOCATE(LxBlock, 1);
blk->callable = AS_OBJ(callableVal);
internalObj->data = blk;
internalObj->dataSz = sizeof(LxBlock);
selfObj->internal = internalObj;
unhideFromGC(TO_OBJ(internalObj));
return self;
}
static Value lxBlockYield(int argCount, Value *args) {
CHECK_ARITY("Block#yield", 1, -1, argCount);
Value self = *args;
LxBlock *blk = blockGetHidden(self);
Value callable = OBJ_VAL(blk->callable);
push(callable);
for (int i = 1; i < argCount; i++) {
push(args[i]);
}
volatile int status = 0;
Obj *block = blockCallableBlock(self);
volatile LxThread *th = THREAD();
volatile BlockStackEntry *bentry = NULL;
SETUP_BLOCK(block, bentry, status, th->errInfo)
if (status == TAG_NONE) {
} else if (status == TAG_RAISE) {
pop();
ObjInstance *errInst = AS_INSTANCE(th->lastErrorThrown);
ASSERT(errInst);
if (errInst->klass == lxBreakBlockErrClass) {
return NIL_VAL;
} else if (errInst->klass == lxContinueBlockErrClass) { // continue
return getProp(th->lastErrorThrown, INTERN("ret"));
} else if (errInst->klass == lxReturnBlockErrClass) {
return getProp(th->lastErrorThrown, INTERN("ret"));
} else {
throwError(th->lastErrorThrown);
}
}
callCallable(callable, argCount-1, false, NULL);
UNREACHABLE("block didn't longjmp?"); // blocks should always longjmp out
}
void Init_BlockClass() {
lxBlockClass = addGlobalClass("Block", lxObjClass);
nativeBlockInit = addNativeMethod(lxBlockClass, "init", lxBlockInit);
addNativeMethod(lxBlockClass, "yield", lxBlockYield);
}