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.
This commit is contained in:
Minus committed 2026-11-17 22:43:29 +00:00
1 parent ee5861a25c
commit a73600e221
6 files changed
+73 -29

No files matched your search

+6
View File
@@ -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); 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); 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); void cat_vm_free(CAT_VM_State *vm);
#endif /* CAT_VM_H */ #endif /* CAT_VM_H */
+20 -11
View File
@@ -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 *cat_module_table(const char *name, const char *path)
{ {
CAT_VALUE_Value *t = cat_types_table_new(); CAT_VALUE_Value *t = cat_types_table_new();
CAT_VALUE_Value *nv;
CAT_ERROR_Error e; CAT_ERROR_Error e;
char dir[512]; char dir[512];
cat_error_clear(&e); cat_error_clear(&e);
cat_types_table_set(t, "name", nv = cat_types_ascii_text_new(name ? name : "main");
cat_types_ascii_text_new(name ? name : "main"), cat_types_table_set(t, "name", nv, &e);
&e); cat_value_release(nv);
/* `path`/`dir` reflect where this unit came from, when known. */ /* `path`/`dir` reflect where this unit came from, when known. */
if (path != NULL && path[0] != '\0') { if (path != NULL && path[0] != '\0') {
cat_error_clear(&e); cat_error_clear(&e);
cat_types_table_set(t, "path", nv = cat_types_ascii_text_new(path);
cat_types_ascii_text_new(path), &e); cat_types_table_set(t, "path", nv, &e);
cat_value_release(nv);
module_dir(path, dir, sizeof(dir)); module_dir(path, dir, sizeof(dir));
cat_error_clear(&e); cat_error_clear(&e);
cat_types_table_set(t, "dir", nv = cat_types_ascii_text_new(dir);
cat_types_ascii_text_new(dir), &e); cat_types_table_set(t, "dir", nv, &e);
cat_value_release(nv);
} }
return t; return t;
} }
@@ -114,15 +117,21 @@ void cat_module_enter(CAT_VM_State *vm, const char *name, const char *path)
return; return;
prev = cat_vm_get_global(vm, "module"); prev = cat_vm_get_global(vm, "module");
if (vm->nmodule < (int)(sizeof(vm->module_stack) / 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; 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) void cat_module_leave(CAT_VM_State *vm)
{ {
CAT_VALUE_Value *prev;
if (vm == NULL || vm->nmodule <= 0) if (vm == NULL || vm->nmodule <= 0)
return; return;
cat_vm_set_global(vm, "module", prev = vm->module_stack[--vm->nmodule];
vm->module_stack[--vm->nmodule]); cat_vm_set_global(vm, "module", prev);
cat_value_release(prev); /* drop the stack's reference */
} }
+8 -8
View File
@@ -665,27 +665,27 @@ int cat_stdlib_stdlib_install(CAT_VM_State *vm)
int i; int i;
for (i = 0; std[i].name != NULL; 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, cat_types_native_new_doc(std[i].name, std[i].cb,
std[i].doc)); std[i].doc));
cat_vm_register_native(vm, "__class_new", lf_class_new); cat_vm_register_native(vm, "__class_new", lf_class_new);
/* the file family: AbstractFile (abstract base), File, StringIO, BytesIO */ /* 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_types_native_new("AbstractFile", lf_abstractfile));
cat_vm_set_global(vm, "File", cat_types_file_class_table()); cat_vm_set_global_owned(vm, "File", cat_types_file_class_table());
cat_vm_set_global(vm, "StringIO", cat_vm_set_global_owned(vm, "StringIO",
cat_types_native_new("StringIO", lf_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)); cat_types_native_new("BytesIO", lf_bytesio));
/* the `ffi` library: call native code (ctypes/CDLL-like) */ /* 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) */ /* the empty object: the value for "no value" (catscript has no nil) */
cat_vm_set_global(vm, "empty", cat_types_empty()); cat_vm_set_global(vm, "empty", cat_types_empty());
/* `memory(n)` -- an owned, bounds-checked byte buffer */ /* `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, cat_types_native_new_doc("memory", lf_memory,
"memory(n) -- buffer de n bytes com limites verificados.")); "memory(n) -- buffer de n bytes com limites verificados."));
/* `module` reflects the unit currently executing (name/path/dir) */ /* `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; return 0;
} }
+7 -5
View File
@@ -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 *cat_types_ffi_module(void)
{ {
CAT_VALUE_Value *m = cat_types_table_new(); CAT_VALUE_Value *m = cat_types_table_new();
CAT_VALUE_Value *nv;
CAT_ERROR_Error e; CAT_ERROR_Error e;
cat_error_clear(&e); cat_error_clear(&e);
cat_types_table_set(m, "load", nv = cat_types_native_new("load", ffi_load_native);
cat_types_native_new("load", ffi_load_native), &e); cat_types_table_set(m, "load", nv, &e);
cat_types_table_set(m, "pointer", cat_value_release(nv);
cat_types_native_new("pointer", ffi_pointer_native), nv = cat_types_native_new("pointer", ffi_pointer_native);
&e); cat_types_table_set(m, "pointer", nv, &e);
cat_value_release(nv);
return m; return m;
} }
+3 -1
View File
@@ -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 *cat_types_file_class_table(void)
{ {
CAT_VALUE_Value *t = cat_types_table_new(); 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_Error e;
cat_error_clear(&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; return t;
} }
+29 -4
View File
@@ -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); 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 cat_vm_register_native(CAT_VM_State *vm, const char *name,
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int,
CAT_VALUE_Value **)) 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 /* A native function is a first-class value. Registering installs a
* `native` object into globals, so no per-VM name table or global * `native` object into globals, so no per-VM name table or global
* registry is needed and the VM stays re-entrant. */ * 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; 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) void cat_vm_free(CAT_VM_State *vm)
{ {
int i;
if (vm == NULL) if (vm == NULL)
return; return;
if (vm->globals != NULL && vm->globals->vtable && /* Release any module still saved on the stack (e.g. the initial
vm->globals->vtable->destroy) * `module` if the run never balanced enter/leave). */
vm->globals->vtable->destroy(vm->globals); 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); free(vm);
} }