module: a global reflecting the current unit

`module` is now a global table describing the unit that is executing:
`name` (basename of the file), `path` and `dir` (when loaded from a
file).  It is mutable like any table, so a unit can tag itself.

- module.{h,c}: cat_module_table(name, path), plus a small enter/leave
  stack (cat_module_enter/leave) that swaps `module` for the duration
  of an import and restores the caller's afterwards.
- vm.h: the enter/leave stack lives on the VM (self-contained, no
  globals shared between VMs).
- stdlib: installs an initial `module` (name "main"); dofile/use now
  enter the imported unit and leave after it returns.
- main.c: the CLI enters the script's basename/path before running it.
- tests/module.cat + tests/module_lib.cat: name/dir/mutation + the
  swap-and-restore around `use` (105 total, all green).
This commit is contained in:
Minus committed 2026-11-17 18:38:05 +00:00
1 parent a950219f1a
commit cc893ff677
8 files changed
+151 -4

No files matched your search

+68
View File
@@ -9,6 +9,8 @@
#include "cat/vm.h"
#include "cat/stdlib/stdlib.h"
#include "cat/stdlib/build.h"
#include "cat/types/table.h"
#include "cat/types/ascii_text.h"
#include <stdlib.h>
#include <string.h>
@@ -58,3 +60,69 @@ void cat_module_free(CAT_MODULE_Module *mod)
free(mod->name);
free(mod);
}
/* The directory part of a path (everything before the last '/'), or
* "." when there is none. Returned in a static-per-call buffer owned
* by the caller's `buf`. */
static void module_dir(const char *path, char *buf, size_t cap)
{
const char *slash;
if (path == NULL || path[0] == '\0') {
snprintf(buf, cap, ".");
return;
}
slash = strrchr(path, '/');
if (slash == NULL) {
snprintf(buf, cap, ".");
return;
}
if (slash == path)
snprintf(buf, cap, "/");
else
snprintf(buf, cap, "%.*s", (int)(slash - path), path);
}
CAT_VALUE_Value *cat_module_table(const char *name, const char *path)
{
CAT_VALUE_Value *t = cat_types_table_new();
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);
/* `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);
module_dir(path, dir, sizeof(dir));
cat_error_clear(&e);
cat_types_table_set(t, "dir",
cat_types_ascii_text_new(dir), &e);
}
return t;
}
void cat_module_enter(CAT_VM_State *vm, const char *name, const char *path)
{
CAT_VALUE_Value *prev;
if (vm == NULL)
return;
prev = cat_vm_get_global(vm, "module");
if (vm->nmodule < (int)(sizeof(vm->module_stack) /
sizeof(vm->module_stack[0])))
vm->module_stack[vm->nmodule++] = prev;
cat_vm_set_global(vm, "module", cat_module_table(name, path));
}
void cat_module_leave(CAT_VM_State *vm)
{
if (vm == NULL || vm->nmodule <= 0)
return;
cat_vm_set_global(vm, "module",
vm->module_stack[--vm->nmodule]);
}
+14 -1
View File
@@ -10,6 +10,7 @@
#include "cat/stdlib/toml.h"
#include "cat/compiler.h"
#include "cat/bytecode.h"
#include "cat/module.h"
#include "cat/types/empty.h"
#include "cat/types/bool.h"
#include "cat/types/number.h"
@@ -477,14 +478,24 @@ static int lf_dofile(CAT_VM_State *vm, CAT_VALUE_Value **args, int nargs,
fclose(f);
cat_error_clear(&e);
fn = cat_compiler_compile(src, &e);
free(src);
if (fn == NULL) {
fprintf(stderr, "dofile: %s\n", e.message);
free(src);
*out = cat_types_empty();
return 0;
}
/* `module` reflects the imported unit while it runs, then the
* caller's `module` is restored. The name is the basename. */
{
const char *p = cat_types_text_bytes(args[0]);
const char *base = strrchr(p, '/');
base = (base != NULL) ? base + 1 : p;
cat_module_enter(vm, base, p);
}
cat_error_clear(&e);
cat_vm_run(vm, fn, out);
cat_module_leave(vm);
free(src);
/* The compiled chunk is intentionally NOT freed: any `function`
* definitions in it became globals whose closures point into this
* bytecode tree. The chunk lives as long as the VM does. */
@@ -614,5 +625,7 @@ int cat_stdlib_stdlib_install(CAT_VM_State *vm)
cat_vm_set_global(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());
/* `module` reflects the unit currently executing (name/path/dir) */
cat_vm_set_global(vm, "module", cat_module_table("main", NULL));
return 0;
}
+13 -3
View File
@@ -23,6 +23,14 @@
#include <stdlib.h>
#include <string.h>
/* Basename of a path (the part after the last '/'), for `module.name`. */
static const char *main_basename(const char *path)
{
const char *slash = path ? strrchr(path, '/') : NULL;
return (slash != NULL) ? slash + 1 : path;
}
static char *main_read_file(const char *path)
{
FILE *f = fopen(path, "rb");
@@ -47,7 +55,7 @@ static char *main_read_file(const char *path)
return buf;
}
static int main_run_source(const char *source, int dump)
static int main_run_source(const char *source, int dump, const char *path)
{
CAT_ERROR_Error err;
CAT_BYTECODE_Function *fn;
@@ -73,6 +81,8 @@ static int main_run_source(const char *source, int dump)
}
cat_stdlib_stdlib_install(vm);
cat_stdlib_build_install(vm);
/* `module` names the file being run. */
cat_module_enter(vm, main_basename(path), path);
rc = cat_vm_run(vm, fn, &out);
if (rc != 0)
fprintf(stderr, "catscript: %s\n", vm->err.message);
@@ -152,7 +162,7 @@ int main(int argc, char **argv)
fprintf(stderr, "catscript: cannot read %s\n", argv[2]);
return 1;
}
rc = main_run_source(src, 1);
rc = main_run_source(src, 1, argv[2]);
free(src);
return rc;
}
@@ -163,7 +173,7 @@ int main(int argc, char **argv)
fprintf(stderr, "catscript: cannot read %s\n", argv[1]);
return 1;
}
rc = main_run_source(src, 0);
rc = main_run_source(src, 0, argv[1]);
free(src);
return rc;
}