diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-02-06 00:12:48 +0100 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-02-07 00:04:35 +0100 |
commit | 3a49192f3a1e8a5d348cdbfccd0a16d74ba61e3d (patch) | |
tree | 6d8a4b620b362ab4f707678a5b626a85e1531c0b /types.c | |
parent | 5bd764a35aeaf50b54957bfa94ba94198514baf0 (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 'types.c')
-rw-r--r-- | types.c | 90 |
1 files changed, 22 insertions, 68 deletions
@@ -51,11 +51,11 @@ ucv_typename(uc_value_t *uv) case UC_ARRAY: return "array"; case UC_OBJECT: return "object"; case UC_REGEXP: return "regexp"; - case UC_FUNCTION: return "function"; case UC_CFUNCTION: return "cfunction"; case UC_CLOSURE: return "closure"; case UC_UPVALUE: return "upvalue"; case UC_RESOURCE: return "resource"; + case UC_PROGRAM: return "program"; } return "unknown"; @@ -167,7 +167,7 @@ ucv_gc_mark(uc_value_t *uv) for (i = 0; i < function->nupvals; i++) ucv_gc_mark(&closure->upvals[i]->header); - ucv_gc_mark(&function->header); + ucv_gc_mark(&function->program->header); break; @@ -196,6 +196,7 @@ ucv_free(uc_value_t *uv, bool retain) uc_resource_t *resource; uc_function_t *function; uc_closure_t *closure; + uc_program_t *program; uc_upvalref_t *upval; uc_regexp_t *regexp; uc_object_t *object; @@ -237,19 +238,6 @@ ucv_free(uc_value_t *uv, bool retain) regfree(®exp->regexp); break; - 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); - break; - case UC_CLOSURE: closure = (uc_closure_t *)uv; function = closure->function; @@ -258,7 +246,7 @@ ucv_free(uc_value_t *uv, bool retain) for (i = 0; i < function->nupvals; i++) ucv_put_value(&closure->upvals[i]->header, retain); - ucv_put_value(&function->header, retain); + ucv_put_value(&function->program->header, retain); break; case UC_RESOURCE: @@ -274,6 +262,16 @@ ucv_free(uc_value_t *uv, bool retain) upval = (uc_upvalref_t *)uv; ucv_put_value(upval->value, retain); break; + + case UC_PROGRAM: + program = (uc_program_t *)uv; + + uc_program_function_foreach_safe(program, func) + uc_program_function_free(func); + + uc_vallist_free(&program->constants); + uc_source_put(program->source); + break; } if (!ref || !retain) { @@ -950,48 +948,6 @@ ucv_object_length(uc_value_t *uv) uc_value_t * -ucv_function_new(const char *name, size_t srcpos, uc_program_t *program) -{ - size_t namelen = 0; - uc_function_t *fn; - - if (name) - namelen = strlen(name); - - fn = xalloc(sizeof(*fn) + namelen + 1); - fn->header.type = UC_FUNCTION; - fn->header.refcount = 1; - - if (name) - strcpy(fn->name, name); - - fn->nargs = 0; - fn->nupvals = 0; - fn->srcpos = srcpos; - fn->program = program; - fn->vararg = false; - - uc_chunk_init(&fn->chunk); - - return &fn->header; -} - -size_t -ucv_function_srcpos(uc_value_t *uv, size_t off) -{ - uc_function_t *fn = (uc_function_t *)uv; - size_t pos; - - if (ucv_type(uv) != UC_FUNCTION) - return 0; - - pos = uc_chunk_debug_get_srcpos(&fn->chunk, off); - - return pos ? fn->srcpos + pos : 0; -} - - -uc_value_t * ucv_cfunction_new(const char *name, uc_cfn_ptr_t fptr) { uc_cfunction_t *cfn; @@ -1028,7 +984,7 @@ 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); + uc_program_get(function->program); return &closure->header; } @@ -1370,9 +1326,9 @@ ucv_to_json(uc_value_t *uv) case UC_CLOSURE: case UC_CFUNCTION: - case UC_FUNCTION: case UC_RESOURCE: case UC_UPVALUE: + case UC_PROGRAM: case UC_NULL: return NULL; } @@ -1691,14 +1647,6 @@ ucv_to_stringbuf_formatted(uc_vm_t *vm, uc_stringbuf_t *pb, uc_value_t *uv, size break; - case UC_FUNCTION: - ucv_stringbuf_printf(pb, "%s<function %p>%s", - json ? "\"" : "", - uv, - json ? "\"" : ""); - - break; - case UC_RESOURCE: resource = (uc_resource_t *)uv; restype = resource->type; @@ -1718,6 +1666,12 @@ ucv_to_stringbuf_formatted(uc_vm_t *vm, uc_stringbuf_t *pb, uc_value_t *uv, size json ? "\"" : ""); break; + + case UC_PROGRAM: + ucv_stringbuf_printf(pb, "%s<program %p>%s", + json ? "\"" : "", + uv, + json ? "\"" : ""); } ucv_clear_mark(uv); |