summaryrefslogtreecommitdiff
path: root/vm.c
diff options
context:
space:
mode:
Diffstat (limited to 'vm.c')
-rw-r--r--vm.c32
1 files changed, 18 insertions, 14 deletions
diff --git a/vm.c b/vm.c
index a5ffbc6..34660de 100644
--- a/vm.c
+++ b/vm.c
@@ -184,21 +184,25 @@ int runvm(State *S) {
uint8_t len = RBYTE();
Val callee = PEEKN(len);
- if (!IS_FUNC(callee)) {
- fprintf(stderr,"can only call functions");
- exit(1);
- }
- ObjFunc *func = AS_FUNC(callee);
-
- StackFrame *sf = &th->rstack[th->rsp++];
- sf->ip = th->ip;
- sf->ch = th->ch;
- sf->fp = th->fp;
-
- th->ip = 0;
- th->ch = &func->ch;
- th->fp = th->sp - len;
+ if (IS_FUNC(callee)) {
+ ObjFunc *func = AS_FUNC(callee);
+
+ StackFrame *sf = &th->rstack[th->rsp++];
+ sf->ip = th->ip;
+ sf->ch = th->ch;
+ sf->fp = th->fp;
+
+ th->ip = 0;
+ th->ch = &func->ch;
+ th->fp = th->sp - len;
+ } else if (IS_CFUNC(callee)) {
+ int nargs = len - 1;
+ Val *firstarg = &th->stack[th->sp - nargs];
+ Val res = AS_CFUNC(callee)(S, len - 1, firstarg);
+ th->sp -= len;
+ PUSH(res);
+ }
break;
}