summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-07-05 16:22:10 +0200
committerJo-Philipp Wich <jo@mein.io>2021-07-11 15:49:14 +0200
commitd5bc22338f6ed30a1d59b160d7118701f28c0d05 (patch)
tree58cbc205f4c7803480457d24465fad37d62d8566
parentef0baf1d8f2ae5fc4ec48bd99dcc7424605356e6 (diff)
vm: add uc_vm_invoke() helper
The uc_vm_invoke() function simplifies calling a named ucode function with arbitrary arguments. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r--vm.c32
-rw-r--r--vm.h1
2 files changed, 33 insertions, 0 deletions
diff --git a/vm.c b/vm.c
index 24b2472..b05fbb7 100644
--- a/vm.c
+++ b/vm.c
@@ -2379,3 +2379,35 @@ uc_vm_scope_set(uc_vm *vm, uc_value_t *ctx)
ucv_put(vm->globals);
vm->globals = ctx;
}
+
+uc_value_t *
+uc_vm_invoke(uc_vm *vm, const char *fname, size_t nargs, ...)
+{
+ uc_exception_type_t ex;
+ uc_value_t *fno, *arg;
+ va_list ap;
+ size_t i;
+
+ fno = ucv_property_get(vm->globals, fname);
+
+ if (!ucv_is_callable(fno))
+ return NULL;
+
+ uc_vm_stack_push(vm, ucv_get(fno));
+
+ va_start(ap, nargs);
+
+ for (i = 0; i < nargs; i++) {
+ arg = va_arg(ap, uc_value_t *);
+ uc_vm_stack_push(vm, ucv_get(arg));
+ }
+
+ va_end(ap);
+
+ ex = uc_vm_call(vm, false, nargs);
+
+ if (ex)
+ return NULL;
+
+ return uc_vm_stack_pop(vm);
+}
diff --git a/vm.h b/vm.h
index 2dae69d..5065ec6 100644
--- a/vm.h
+++ b/vm.h
@@ -125,5 +125,6 @@ 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_value_t *modules);
+uc_value_t *uc_vm_invoke(uc_vm *vm, const char *fname, size_t nargs, ...);
#endif /* __VM_H_ */