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:
1 parent
ee5861a25c
commit
a73600e221
6 files changed
+73
-29
No files matched your search
+20
-11
@@ -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 */
|
||||
}
|
||||
Reference in new issue
Block a user