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

+20
View File
@@ -14,6 +14,7 @@
#include "cat/error.h"
struct CAT_BYTECODE_Function;
struct CAT_VM_State;
/* CAT_MODULE_Module == Cat_Module.
* One compilable unit: a name plus its top-level function prototype. */
@@ -36,4 +37,23 @@ int cat_module_run(CAT_MODULE_Module *mod, CAT_VALUE_Value **out,
/* Free a module. */
void cat_module_free(CAT_MODULE_Module *mod);
/* ---- the runtime `module` global -----------------------------------
*
* `module` is a global table reflecting the CURRENT module: its name,
* and (when loaded from a file) its path and directory. Importing
* another file (dofile/use) swaps `module` for the duration of that
* file and restores the caller's afterwards, so `module` always names
* the unit that is executing right now. */
/* Build a `module` table for a unit (name and optional path). */
CAT_VALUE_Value *cat_module_table(const char *name, const char *path);
/* Make `module` reflect a new unit (name/path); the previous value is
* pushed and restored by cat_module_leave(). */
void cat_module_enter(struct CAT_VM_State *vm, const char *name,
const char *path);
/* Restore the `module` in effect before the matching enter. */
void cat_module_leave(struct CAT_VM_State *vm);
#endif /* CAT_MODULE_H */
+3
View File
@@ -57,6 +57,9 @@ typedef struct CAT_VM_State {
/* native functions installed into this VM */
CAT_VM_Native natives[CAT_VM_MAXNATIVE];
int nnatives;
/* saved `module` globals for nested imports (dofile/use) */
CAT_VALUE_Value *module_stack[64];
int nmodule;
} CAT_VM_State;
/* Create a VM with an empty globals table. */
+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;
}
+18
View File
@@ -0,0 +1,18 @@
-- tests/module.cat -- the `module` global reflects the current unit.
--
-- `module` is a global table: `name` is the basename of the running
-- file, `path` its path and `dir` its directory. Importing another
-- file (use/dofile) swaps `module` for the duration of that file and
-- restores the caller's afterwards.
print("name:", module.name)
print("dir:", module.dir)
-- a user may mutate it like any table
module.extra = "tagged"
print("extra:", module.extra)
use("tests/module_lib.cat")
print("back:", module.name)
print("extra kept:", module.extra)
+4
View File
@@ -0,0 +1,4 @@
-- tests/module_lib.cat -- a helper imported by tests/module.cat.
-- While this file runs, `module` reflects THIS unit.
print("lib name:", module.name)
print("lib dir:", module.dir)
+11
View File
@@ -174,6 +174,17 @@ check "repr-list" "list: [a, b, _repr_]" "`echo "$out" | sed -
out=`printf 'x = 50\nx\nx + 5\n' | $BIN | tr '>' '\n' | grep -oE '[0-9]+'`
check "repl-echo" "50" "`echo "$out" | sed -n 1p`"
check "repl-state" "55" "`echo "$out" | sed -n 2p`"
# ---- the `module` global ---------------------------------------------
out=`$BIN tests/module.cat`
check "module-name" "name: module.cat" "`echo "$out" | sed -n 1p`"
check "module-dir" "dir: tests" "`echo "$out" | sed -n 2p`"
check "module-mut" "extra: tagged" "`echo "$out" | sed -n 3p`"
check "module-imp" "lib name: module_lib.cat" "`echo "$out" | sed -n 4p`"
check "module-impdir" "lib dir: tests" "`echo "$out" | sed -n 5p`"
check "module-back" "back: module.cat" "`echo "$out" | sed -n 6p`"
check "module-kept" "extra kept: tagged" "`echo "$out" | sed -n 7p`"
# ---- bytecode injection (fc_fn) --------------------------------------
out=`$BIN tests/bc.cat`