diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-07-05 20:22:39 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-07-11 15:49:14 +0200 |
commit | 2f77657ae97f84edcd665c4cfe00ef91b9cde1bc (patch) | |
tree | bc786e60d2a743310f037a8bff595c9252011887 | |
parent | 111645aa965717adb40604e53d0bab07e25c1b27 (diff) |
vm: extend API to allow returning result value from VM execution
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | main.c | 22 | ||||
-rw-r--r-- | tests/cram/test_basic.t | 6 | ||||
-rw-r--r-- | vm.c | 16 | ||||
-rw-r--r-- | vm.h | 2 |
4 files changed, 30 insertions, 16 deletions
@@ -78,7 +78,7 @@ parse(uc_parse_config *config, uc_source *src, uc_value_t *env, uc_value_t *modules, int argc, char **argv) { - uc_value_t *globals = NULL, *arr, *name, *mod; + uc_value_t *globals = NULL, *res = NULL, *arr, *name, *mod; uc_function_t *entry; uc_vm vm = { 0 }; int i, rc = 0; @@ -92,7 +92,7 @@ parse(uc_parse_config *config, uc_source *src, if (!entry) { fprintf(stderr, "%s", err); free(err); - rc = 2; + rc = -1; goto out; } @@ -125,15 +125,25 @@ parse(uc_parse_config *config, uc_source *src, register_variable(globals, ucv_string_get(name), mod); } - rc = uc_vm_execute(&vm, entry); + rc = uc_vm_execute(&vm, entry, &res); - if (rc) { - rc = 1; - goto out; + switch (rc) { + case STATUS_OK: + rc = 0; + break; + + case ERROR_COMPILE: + rc = -1; + break; + + case ERROR_RUNTIME: + rc = -2; + break; } out: uc_vm_free(&vm); + ucv_put(res); return rc; } diff --git a/tests/cram/test_basic.t b/tests/cram/test_basic.t index aab5b16..d866c49 100644 --- a/tests/cram/test_basic.t +++ b/tests/cram/test_basic.t @@ -38,15 +38,13 @@ check that ucode provides proper error messages: $ ucode -m foo -s ' ' Runtime error: No module named 'foo' could be found - At start of program - [1] + [254] $ touch moo; ucode -m foo -i moo Runtime error: No module named 'foo' could be found - At start of program - [1] + [254] check that ucode can load fs module: @@ -2024,7 +2024,7 @@ uc_vm_output_exception(uc_vm *vm) } static uc_vm_status_t -uc_vm_execute_chunk(uc_vm *vm) +uc_vm_execute_chunk(uc_vm *vm, uc_value_t **retvalp) { uc_callframe *frame = uc_vm_current_frame(vm); uc_chunk *chunk = uc_vm_frame_chunk(frame); @@ -2227,7 +2227,10 @@ uc_vm_execute_chunk(uc_vm *vm) retval = uc_vm_callframe_pop(vm); if (vm->callframes.count == 0) { - ucv_put(retval); + if (retvalp) + *retvalp = retval; + else + ucv_put(retval); return STATUS_OK; } @@ -2280,7 +2283,7 @@ uc_vm_execute_chunk(uc_vm *vm) } uc_vm_status_t -uc_vm_execute(uc_vm *vm, uc_function_t *fn) +uc_vm_execute(uc_vm *vm, uc_function_t *fn, uc_value_t **retval) { uc_closure_t *closure = (uc_closure_t *)ucv_closure_new(vm, fn, false); uc_callframe *frame; @@ -2308,7 +2311,10 @@ uc_vm_execute(uc_vm *vm, uc_function_t *fn) //uc_vm_stack_push(vm, closure->header.jso); uc_vm_stack_push(vm, NULL); - return uc_vm_execute_chunk(vm); + if (retval) + *retval = NULL; + + return uc_vm_execute_chunk(vm, retval); } uc_exception_type_t @@ -2319,7 +2325,7 @@ uc_vm_call(uc_vm *vm, bool mcall, size_t nargs) if (uc_vm_call_function(vm, ctx, fno, mcall, nargs & 0xffff)) { if (ucv_type(fno) != UC_CFUNCTION) - uc_vm_execute_chunk(vm); + uc_vm_execute_chunk(vm, NULL); } return vm->exception.type; @@ -124,7 +124,7 @@ uc_exception_type_t uc_vm_call(uc_vm *vm, bool mcall, size_t nargs); void __attribute__((format(printf, 3, 0))) uc_vm_raise_exception(uc_vm *vm, uc_exception_type_t type, const char *fmt, ...); -uc_vm_status_t uc_vm_execute(uc_vm *vm, uc_function_t *fn); +uc_vm_status_t uc_vm_execute(uc_vm *vm, uc_function_t *fn, uc_value_t **retval); uc_value_t *uc_vm_invoke(uc_vm *vm, const char *fname, size_t nargs, ...); #endif /* __VM_H_ */ |