`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).
60 lines
2.1 KiB
C
60 lines
2.1 KiB
C
/*
|
|
* cat/module.h -- Cat_Module, a unit of catscript code.
|
|
*
|
|
* Naming rule: symbols declared here are cat_module_* / CAT_MODULE_*.
|
|
* (Path of this header below include/ is `cat/module`, so cat_module_*.)
|
|
*
|
|
* A module owns a name and a compiled entry prototype. Loading a module
|
|
* is how an embedder turns catscript source into something callable.
|
|
*/
|
|
#ifndef CAT_MODULE_H
|
|
#define CAT_MODULE_H
|
|
|
|
#include "cat/value.h"
|
|
#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. */
|
|
typedef struct CAT_MODULE_Module {
|
|
char *name;
|
|
struct CAT_BYTECODE_Function *main;
|
|
} CAT_MODULE_Module;
|
|
|
|
/* Create an empty module with the given name (name is copied). */
|
|
CAT_MODULE_Module *cat_module_new(const char *name);
|
|
|
|
/* Compile catscript source text into this module. */
|
|
int cat_module_compile(CAT_MODULE_Module *mod, const char *source,
|
|
CAT_ERROR_Error *err);
|
|
|
|
/* Execute the module's main prototype; `out` receives its return value. */
|
|
int cat_module_run(CAT_MODULE_Module *mod, CAT_VALUE_Value **out,
|
|
CAT_ERROR_Error *err);
|
|
|
|
/* 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 */
|