From a73600e221e80dee1cc07b8d58620b6d2c71a9f7 Mon Sep 17 00:00:00 2001 From: Minus Date: Tue, 17 Nov 2026 22:43:29 +0000 Subject: [PATCH] refcount: globals, modules and stdlib own their values Extend the ownership discipline to the VM's global table and the non-container value producers: - cat_vm_set_global_owned: takes ownership of a freshly-created value (used for the native-registered globals and the `module` table). - cat_vm_free releases the saved `module` stack entries and the globals table (which cascades: each stored value drops its reference). - module enter/leave retains the saved `module` while it is off-global. - stdlib/ffi/file build their tables with owned values and drop the transient reference after a store. Suite stays 130 green. --- include/cat/vm.h | 6 ++++++ src/cat/module.c | 31 ++++++++++++++++++++----------- src/cat/stdlib/stdlib.c | 16 ++++++++-------- src/cat/types/ffi.c | 12 +++++++----- src/cat/types/file.c | 4 +++- src/cat/vm.c | 33 +++++++++++++++++++++++++++++---- 6 files changed, 73 insertions(+), 29 deletions(-) diff --git a/include/cat/vm.h b/include/cat/vm.h index 79e5ce4..23637f9 100644 --- a/include/cat/vm.h +++ b/include/cat/vm.h @@ -88,6 +88,12 @@ int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out); CAT_VALUE_Value *cat_vm_get_global(CAT_VM_State *vm, const char *name); int cat_vm_set_global(CAT_VM_State *vm, const char *name, CAT_VALUE_Value *v); +/* Like cat_vm_set_global, but the caller hands over its reference: the + * globals table takes ownership and the caller's ref is dropped. Use + * with an inline-created value. */ +int cat_vm_set_global_owned(CAT_VM_State *vm, const char *name, + CAT_VALUE_Value *v); + void cat_vm_free(CAT_VM_State *vm); #endif /* CAT_VM_H */ diff --git a/src/cat/module.c b/src/cat/module.c index 8dbb8b6..a61c226 100644 --- a/src/cat/module.c +++ b/src/cat/module.c @@ -86,22 +86,25 @@ static void module_dir(const char *path, char *buf, size_t cap) CAT_VALUE_Value *cat_module_table(const char *name, const char *path) { CAT_VALUE_Value *t = cat_types_table_new(); + CAT_VALUE_Value *nv; CAT_ERROR_Error e; char dir[512]; cat_error_clear(&e); - cat_types_table_set(t, "name", - cat_types_ascii_text_new(name ? name : "main"), - &e); + nv = cat_types_ascii_text_new(name ? name : "main"); + cat_types_table_set(t, "name", nv, &e); + cat_value_release(nv); /* `path`/`dir` reflect where this unit came from, when known. */ if (path != NULL && path[0] != '\0') { cat_error_clear(&e); - cat_types_table_set(t, "path", - cat_types_ascii_text_new(path), &e); + nv = cat_types_ascii_text_new(path); + cat_types_table_set(t, "path", nv, &e); + cat_value_release(nv); module_dir(path, dir, sizeof(dir)); cat_error_clear(&e); - cat_types_table_set(t, "dir", - cat_types_ascii_text_new(dir), &e); + nv = cat_types_ascii_text_new(dir); + cat_types_table_set(t, "dir", nv, &e); + cat_value_release(nv); } return t; } @@ -114,15 +117,21 @@ void cat_module_enter(CAT_VM_State *vm, const char *name, const char *path) return; prev = cat_vm_get_global(vm, "module"); if (vm->nmodule < (int)(sizeof(vm->module_stack) / - sizeof(vm->module_stack[0]))) + sizeof(vm->module_stack[0]))) { + /* Keep the saved module alive while it is off the global. */ + cat_value_retain(prev); vm->module_stack[vm->nmodule++] = prev; - cat_vm_set_global(vm, "module", cat_module_table(name, path)); + } + cat_vm_set_global_owned(vm, "module", cat_module_table(name, path)); } void cat_module_leave(CAT_VM_State *vm) { + CAT_VALUE_Value *prev; + if (vm == NULL || vm->nmodule <= 0) return; - cat_vm_set_global(vm, "module", - vm->module_stack[--vm->nmodule]); + prev = vm->module_stack[--vm->nmodule]; + cat_vm_set_global(vm, "module", prev); + cat_value_release(prev); /* drop the stack's reference */ } diff --git a/src/cat/stdlib/stdlib.c b/src/cat/stdlib/stdlib.c index c022116..ee4a0fc 100644 --- a/src/cat/stdlib/stdlib.c +++ b/src/cat/stdlib/stdlib.c @@ -665,27 +665,27 @@ int cat_stdlib_stdlib_install(CAT_VM_State *vm) int i; for (i = 0; std[i].name != NULL; i++) - cat_vm_set_global(vm, std[i].name, + cat_vm_set_global_owned(vm, std[i].name, cat_types_native_new_doc(std[i].name, std[i].cb, std[i].doc)); cat_vm_register_native(vm, "__class_new", lf_class_new); /* the file family: AbstractFile (abstract base), File, StringIO, BytesIO */ - cat_vm_set_global(vm, "AbstractFile", + cat_vm_set_global_owned(vm, "AbstractFile", cat_types_native_new("AbstractFile", lf_abstractfile)); - cat_vm_set_global(vm, "File", cat_types_file_class_table()); - cat_vm_set_global(vm, "StringIO", + cat_vm_set_global_owned(vm, "File", cat_types_file_class_table()); + cat_vm_set_global_owned(vm, "StringIO", cat_types_native_new("StringIO", lf_stringio)); - cat_vm_set_global(vm, "BytesIO", + cat_vm_set_global_owned(vm, "BytesIO", cat_types_native_new("BytesIO", lf_bytesio)); /* the `ffi` library: call native code (ctypes/CDLL-like) */ - cat_vm_set_global(vm, "ffi", cat_types_ffi_module()); + cat_vm_set_global_owned(vm, "ffi", cat_types_ffi_module()); /* the empty object: the value for "no value" (catscript has no nil) */ cat_vm_set_global(vm, "empty", cat_types_empty()); /* `memory(n)` -- an owned, bounds-checked byte buffer */ - cat_vm_set_global(vm, "memory", + cat_vm_set_global_owned(vm, "memory", cat_types_native_new_doc("memory", lf_memory, "memory(n) -- buffer de n bytes com limites verificados.")); /* `module` reflects the unit currently executing (name/path/dir) */ - cat_vm_set_global(vm, "module", cat_module_table("main", NULL)); + cat_vm_set_global_owned(vm, "module", cat_module_table("main", NULL)); return 0; } diff --git a/src/cat/types/ffi.c b/src/cat/types/ffi.c index 5165429..3611962 100644 --- a/src/cat/types/ffi.c +++ b/src/cat/types/ffi.c @@ -444,13 +444,15 @@ static int ffi_pointer_native(CAT_VM_State *vm, CAT_VALUE_Value **args, CAT_VALUE_Value *cat_types_ffi_module(void) { CAT_VALUE_Value *m = cat_types_table_new(); + CAT_VALUE_Value *nv; CAT_ERROR_Error e; cat_error_clear(&e); - cat_types_table_set(m, "load", - cat_types_native_new("load", ffi_load_native), &e); - cat_types_table_set(m, "pointer", - cat_types_native_new("pointer", ffi_pointer_native), - &e); + nv = cat_types_native_new("load", ffi_load_native); + cat_types_table_set(m, "load", nv, &e); + cat_value_release(nv); + nv = cat_types_native_new("pointer", ffi_pointer_native); + cat_types_table_set(m, "pointer", nv, &e); + cat_value_release(nv); return m; } diff --git a/src/cat/types/file.c b/src/cat/types/file.c index b26fe9f..873fb41 100644 --- a/src/cat/types/file.c +++ b/src/cat/types/file.c @@ -383,9 +383,11 @@ static int cm_open(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs, CAT_VALUE_Value *cat_types_file_class_table(void) { CAT_VALUE_Value *t = cat_types_table_new(); + CAT_VALUE_Value *nv = cat_types_native_new("open", cm_open); CAT_ERROR_Error e; cat_error_clear(&e); - cat_types_table_set(t, "open", cat_types_native_new("open", cm_open), &e); + cat_types_table_set(t, "open", nv, &e); + cat_value_release(nv); return t; } diff --git a/src/cat/vm.c b/src/cat/vm.c index 64212fe..6ce8ba4 100644 --- a/src/cat/vm.c +++ b/src/cat/vm.c @@ -70,6 +70,19 @@ int cat_vm_set_global(CAT_VM_State *vm, const char *name, CAT_VALUE_Value *v) return cat_types_table_set(vm->globals, name, v, &e); } +/* Set a global from a freshly-created value, transferring ownership to + * the globals table (the caller's reference is dropped). Use this when + * the value is created inline: `cat_vm_set_global_owned(vm, "x", + * cat_types_native_new(...))`. */ +int cat_vm_set_global_owned(CAT_VM_State *vm, const char *name, + CAT_VALUE_Value *v) +{ + int rc = cat_vm_set_global(vm, name, v); + + cat_value_release(v); + return rc; +} + int cat_vm_register_native(CAT_VM_State *vm, const char *name, int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, CAT_VALUE_Value **)) @@ -77,7 +90,7 @@ int cat_vm_register_native(CAT_VM_State *vm, const char *name, /* A native function is a first-class value. Registering installs a * `native` object into globals, so no per-VM name table or global * registry is needed and the VM stays re-entrant. */ - cat_vm_set_global(vm, name, cat_types_native_new(name, cb)); + cat_vm_set_global_owned(vm, name, cat_types_native_new(name, cb)); return 0; } @@ -595,10 +608,22 @@ int cat_vm_run(CAT_VM_State *vm, CAT_BYTECODE_Function *fn, CAT_VALUE_Value **ou void cat_vm_free(CAT_VM_State *vm) { + int i; + if (vm == NULL) return; - if (vm->globals != NULL && vm->globals->vtable && - vm->globals->vtable->destroy) - vm->globals->vtable->destroy(vm->globals); + /* Release any module still saved on the stack (e.g. the initial + * `module` if the run never balanced enter/leave). */ + while (vm->nmodule > 0) { + CAT_VALUE_Value *m = vm->module_stack[--vm->nmodule]; + cat_value_release(m); + } + /* The globals table owns one reference per value it holds; releasing + * it cascades: each stored value's reference drops and the last + * holder frees it. */ + if (vm->globals != NULL) + cat_value_release(vm->globals); + for (i = 0; i < vm->nnatives; i++) + (void)vm->natives[i]; free(vm); }