-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: call dictu from native code #755
Changes from 2 commits
3d26296
70d1083
5415b63
9d73540
285e4d0
7f3c839
9b16ab0
b07a823
da8129f
a6f331a
69e10a4
4391dd4
1a6764a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
#include "datatypes/enums.h" | ||
#include "natives.h" | ||
#include "../optionals/optionals.h" | ||
#include "value.h" | ||
|
||
static void resetStack(DictuVM *vm) { | ||
vm->stackTop = vm->stack; | ||
|
@@ -866,7 +867,9 @@ static void copyAnnotations(DictuVM *vm, ObjDict *superAnnotations, ObjDict *kla | |
} | ||
} | ||
|
||
static DictuInterpretResult run(DictuVM *vm) { | ||
|
||
|
||
static DictuInterpretResult runWithBreakFrame(DictuVM *vm, int breakFrame) { | ||
CallFrame *frame = &vm->frames[vm->frameCount - 1]; | ||
register uint8_t* ip = frame->ip; | ||
|
||
|
@@ -877,6 +880,7 @@ static DictuInterpretResult run(DictuVM *vm) { | |
#define READ_CONSTANT() \ | ||
(frame->closure->function->chunk.constants.values[READ_BYTE()]) | ||
|
||
|
||
#define READ_STRING() AS_STRING(READ_CONSTANT()) | ||
|
||
#define UNSUPPORTED_OPERAND_TYPE_ERROR(op) \ | ||
|
@@ -2253,6 +2257,7 @@ static DictuInterpretResult run(DictuVM *vm) { | |
} | ||
|
||
CASE_CODE(RETURN): { | ||
|
||
Value result = pop(vm); | ||
|
||
// Close any upvalues still in scope. | ||
|
@@ -2270,6 +2275,10 @@ static DictuInterpretResult run(DictuVM *vm) { | |
|
||
frame = &vm->frames[vm->frameCount - 1]; | ||
ip = frame->ip; | ||
if (breakFrame != -1 && vm->frameCount == breakFrame) { | ||
return INTERPRET_OK; | ||
} | ||
|
||
DISPATCH(); | ||
} | ||
|
||
|
@@ -2435,6 +2444,9 @@ static DictuInterpretResult run(DictuVM *vm) { | |
|
||
return INTERPRET_RUNTIME_ERROR; | ||
} | ||
static DictuInterpretResult run(DictuVM *vm) { | ||
return runWithBreakFrame(vm, -1); | ||
} | ||
|
||
DictuInterpretResult dictuInterpret(DictuVM *vm, char *moduleName, char *source) { | ||
ObjString *name = copyString(vm, moduleName, strlen(moduleName)); | ||
|
@@ -2457,3 +2469,21 @@ DictuInterpretResult dictuInterpret(DictuVM *vm, char *moduleName, char *source) | |
|
||
return result; | ||
} | ||
Value callFunction(DictuVM* vm, Value function, int argCount, Value* args) { | ||
if(!IS_FUNCTION(function) && !IS_CLOSURE(function)) | ||
return NIL_VAL; | ||
int currentFrameCount = vm->frameCount; | ||
Value* currentStack = vm->stackTop; | ||
CallFrame *frame = &vm->frames[vm->frameCount++]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we need to be careful we actually have enough frames, we may need to reallocate if not |
||
uint8_t code[4] = {OP_CALL, argCount, 0, OP_RETURN}; | ||
frame->ip = code; | ||
push(vm, function); | ||
for(int i = argCount -1; i >= 0; i--) { | ||
push(vm, args[i]); | ||
} | ||
runWithBreakFrame(vm, currentFrameCount+1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we would need to handle a runtime error as at the minute (without testing) I'm assuming this will probably dump |
||
Value v = pop(vm); | ||
vm->stackTop = currentStack; | ||
vm->frameCount--; | ||
return v; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here it would be nice to return an EMPTY val and throw a runtime error (happy you're just testing so may not have put that in yet etc)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also be nice to handle natives here too, that should be a lot easier as we don't need to handle call frames