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:
1 parent
a950219f1a
commit
cc893ff677
8 files changed
+151
-4
No files matched your search
@@ -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. */
|
||||
|
||||
Reference in new issue
Block a user