summaryrefslogtreecommitdiffhomepage
path: root/lib.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-02-06 00:12:48 +0100
committerJo-Philipp Wich <jo@mein.io>2022-02-07 00:04:35 +0100
commit3a49192f3a1e8a5d348cdbfccd0a16d74ba61e3d (patch)
tree6d8a4b620b362ab4f707678a5b626a85e1531c0b /lib.c
parent5bd764a35aeaf50b54957bfa94ba94198514baf0 (diff)
treewide: rework function memory model
- Instead of treating individual program functions as managed ucode types, demote uc_function_t values to pointers into a uc_program_t entity - Promote uc_program_t to a managed type - Let uc_closure_t claim references to the owning program of the enclosed uc_function_t - Redefine public APIs uc_compile() and uc_vm_execute() APIs to return and expect an uc_program_t object respectively - Remove vallist indirection for function loading and let the compiler emit the function id directly when producing function construction code Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c17
1 files changed, 8 insertions, 9 deletions
diff --git a/lib.c b/lib.c
index 5ad08e7..aaa3eac 100644
--- a/lib.c
+++ b/lib.c
@@ -38,6 +38,7 @@
#include "ucode/vm.h"
#include "ucode/lib.h"
#include "ucode/source.h"
+#include "ucode/program.h"
static void
format_context_line(uc_stringbuf_t *buf, const char *line, size_t off, bool compact)
@@ -692,7 +693,6 @@ uc_type(uc_vm_t *vm, size_t nargs)
switch (t) {
case UC_CFUNCTION:
- case UC_FUNCTION:
case UC_CLOSURE:
return ucv_string_new("function");
@@ -1519,7 +1519,7 @@ static bool
uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t **res)
{
uc_exception_type_t extype;
- uc_function_t *function;
+ uc_program_t *program;
uc_value_t *prev_scope;
uc_value_t *closure;
uc_source_t *source;
@@ -1538,21 +1538,23 @@ uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t **
return true;
}
- function = uc_compile(vm->config, source, &err);
+ program = uc_compile(vm->config, source, &err);
- if (!function) {
+ uc_source_put(source);
+
+ if (!program) {
uc_vm_raise_exception(vm, EXCEPTION_RUNTIME,
"Unable to compile module '%s':\n%s", path, err);
- uc_source_put(source);
free(err);
return true;
}
- closure = ucv_closure_new(vm, function, false);
+ closure = ucv_closure_new(vm, uc_program_entry(program), false);
uc_vm_stack_push(vm, closure);
+ uc_program_put(program);
if (scope) {
prev_scope = ucv_get(uc_vm_scope_get(vm));
@@ -1567,9 +1569,6 @@ uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t **
if (extype == EXCEPTION_NONE)
*res = uc_vm_stack_pop(vm);
- uc_source_put(source);
- ucv_put(&function->header);
-
return true;
}