summaryrefslogtreecommitdiffhomepage
path: root/types.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-01-07 19:42:12 +0100
committerJo-Philipp Wich <jo@mein.io>2022-01-18 10:57:42 +0100
commit6b2e79af9fe6e7d05d31245fc9049540a96d5d31 (patch)
tree20aeea16dc72454610be5bf58e83b5070e1c6da8 /types.c
parent0e5b273c3d25d1dce3b469f3a6865f430554e730 (diff)
types: add initial infrastructure for function serialization
- Introduce a new "program" entity which holds the list of functions created during compilation - Instead of storing pointers to the in-memory function representation in the constant list, store the index of the function within the program's function list - When loading functions from the constant list, retrieve the function by index from the program entity Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'types.c')
-rw-r--r--types.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/types.c b/types.c
index 0122984..65cba22 100644
--- a/types.c
+++ b/types.c
@@ -26,6 +26,7 @@
#include "ucode/types.h"
#include "ucode/util.h"
#include "ucode/vm.h"
+#include "ucode/program.h"
uc_type_t
ucv_type(uc_value_t *uv)
@@ -60,14 +61,14 @@ ucv_typename(uc_value_t *uv)
return "unknown";
}
-static void
+void
ucv_unref(uc_weakref_t *ref)
{
ref->prev->next = ref->next;
ref->next->prev = ref->prev;
}
-static void
+void
ucv_ref(uc_weakref_t *head, uc_weakref_t *item)
{
item->next = head->next;
@@ -238,6 +239,14 @@ ucv_free(uc_value_t *uv, bool retain)
case UC_FUNCTION:
function = (uc_function_t *)uv;
+
+ if (function->program) {
+ ucv_unref(&function->progref);
+
+ if (function->root)
+ uc_program_free(function->program);
+ }
+
uc_chunk_free(&function->chunk);
uc_source_put(function->source);
break;
@@ -942,7 +951,7 @@ ucv_object_length(uc_value_t *uv)
uc_value_t *
-ucv_function_new(const char *name, size_t srcpos, uc_source_t *source)
+ucv_function_new(const char *name, size_t srcpos, uc_source_t *source, uc_program_t *program)
{
size_t namelen = 0;
uc_function_t *fn;
@@ -961,6 +970,7 @@ ucv_function_new(const char *name, size_t srcpos, uc_source_t *source)
fn->nupvals = 0;
fn->srcpos = srcpos;
fn->source = uc_source_get(source);
+ fn->program = program;
fn->vararg = false;
uc_chunk_init(&fn->chunk);
@@ -1020,6 +1030,8 @@ ucv_closure_new(uc_vm_t *vm, uc_function_t *function, bool arrow_fn)
if (vm)
ucv_ref(&vm->values, &closure->ref);
+ ucv_get(&function->header);
+
return &closure->header;
}