Put stdlib calls onto the stack as well as normal function calls

This commit is contained in:
Konstantin Nazarov 2024-09-15 02:33:47 +01:00
parent e9b9ef6d61
commit 6126c7b8eb
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22

View file

@ -114,11 +114,10 @@ Result<void> VM::vm_call_stdlib(Opcode& oc, StdlibFunction& fun) {
uint64_t reg_end = (uint64_t)oc.arg2().arg;
auto params = TRY(_stack.slice(reg_start + 1, reg_end));
_stack = TRY(_stack.set(reg_start + 1, TRY(params.copy_value())));
_stack = TRY(_stack.settop(reg_start + 2));
_stack = TRY(_stack.call(TRY(fun.copy_value()), reg_start, reg_start + 2));
auto res = TRY(call_stdlib_function(fun.fun_id(), params));
_stack = TRY(setreg(oc.arg1().arg, res));
_stack = TRY(_stack.incpc());
return Result<void>();
}
@ -278,7 +277,24 @@ Result<void> VM::step_bytecode() {
return Result<void>();
}
Result<void> VM::step_native() { return ERROR(NotImplemented); }
Result<void> VM::step_native() {
// foo
auto fun = TRY(_stack.fun());
if (!fun.is<StdlibFunction>()) return ERROR(TypeMismatch);
auto fun_id = fun.to<StdlibFunction>()->fun_id();
auto params = TRY(_stack.get(0));
if (!params.is<Array>()) return ERROR(TypeMismatch);
auto res = TRY(call_stdlib_function(fun_id, *params.to<Array>()));
auto top = TRY(_stack.size());
_stack = TRY(_stack.set(top, res));
_stack = TRY(_stack.ret(top));
_stack = TRY(_stack.incpc());
return Result<void>();
}
Result<void> VM::step() {
auto fun = TRY(_stack.fun());