`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).
94 lines
3.3 KiB
C
94 lines
3.3 KiB
C
/*
|
|
* cat/vm.h -- the stack VM that executes catscript bytecode.
|
|
*
|
|
* Naming rule: symbols here are cat_vm_* / CAT_VM_*.
|
|
*/
|
|
#ifndef CAT_VM_H
|
|
#define CAT_VM_H
|
|
|
|
#include "cat/value.h"
|
|
#include "cat/error.h"
|
|
#include "cat/bytecode.h"
|
|
|
|
/* Forward declarations so the native-callback signature below refers to
|
|
* the real CAT_VM_State type, not a parameter-list-scoped struct. */
|
|
typedef struct CAT_VM_State CAT_VM_State;
|
|
typedef struct CAT_TYPES_FUNCTION_Closure CAT_TYPES_FUNCTION_Closure;
|
|
|
|
/* One registered native function. Natives live in the VM (not in a
|
|
* process-global list) so distinct VMs never share mutable state and can
|
|
* run concurrently on different threads with no global lock (no GIL). */
|
|
typedef struct CAT_VM_Native {
|
|
char name[64];
|
|
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int, CAT_VALUE_Value **);
|
|
} CAT_VM_Native;
|
|
|
|
#define CAT_VM_MAXNATIVE 128
|
|
|
|
/* An active `try` handler: where to jump and the stack height to
|
|
* restore when an error is caught. */
|
|
typedef struct CAT_VM_Handler {
|
|
int target; /* instruction index of the handler */
|
|
int sp; /* stack height to restore */
|
|
} CAT_VM_Handler;
|
|
|
|
#define CAT_VM_MAXHANDLER 64
|
|
|
|
/* CAT_VM_Frame -- one function activation. A frame holds everything
|
|
* needed to suspend and resume execution (ip, operand stack, locals and
|
|
* active handlers), so the same interpreter drives both ordinary calls
|
|
* and generators that yield in the middle. */
|
|
typedef struct CAT_VM_Frame {
|
|
CAT_BYTECODE_Function *fn;
|
|
CAT_TYPES_FUNCTION_Closure *cl;
|
|
CAT_VALUE_Value **stack; /* operand stack (owned by the caller) */
|
|
CAT_VALUE_Value *locals[256];
|
|
int sp;
|
|
int ip;
|
|
CAT_VM_Handler handlers[CAT_VM_MAXHANDLER];
|
|
int nhandlers;
|
|
} CAT_VM_Frame;
|
|
|
|
/* CAT_VM_State -- globals + a call stack. Fully self-contained: two VMs
|
|
* share no mutable state, so they may execute on separate threads. */
|
|
typedef struct CAT_VM_State {
|
|
/* global table (name -> value) */ CAT_VALUE_Value *globals;
|
|
CAT_ERROR_Error err;
|
|
/* 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. */
|
|
CAT_VM_State *cat_vm_new(void);
|
|
|
|
/* Run a top-level function; `out` receives the return value. */
|
|
int cat_vm_run(CAT_VM_State *vm, CAT_BYTECODE_Function *fn,
|
|
CAT_VALUE_Value **out);
|
|
|
|
/* Register a native function (name, C callback) into globals. */
|
|
int cat_vm_register_native(CAT_VM_State *vm, const char *name,
|
|
int (*cb)(CAT_VM_State *, CAT_VALUE_Value **, int,
|
|
CAT_VALUE_Value **));
|
|
|
|
/* The VM currently executing on THIS thread (thread-local), used by
|
|
* closures/natives invoked through the object system. */
|
|
CAT_VM_State *cat_vm_current(void);
|
|
|
|
/* Run (or resume) a frame. Returns 0 when the frame returns (*out set),
|
|
* 1 when it yields (*out set, frame left resumable), or -1 on error
|
|
* (vm->err set). The frame owns neither its operand stack nor its
|
|
* handlers' lifetime; the caller keeps the stack alive across resumes. */
|
|
int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out);
|
|
|
|
/* Convenience: get/set a global. */
|
|
CAT_VALUE_Value *cat_vm_get_global(CAT_VM_State *vm, const char *name);
|
|
int cat_vm_set_global(CAT_VM_State *vm, const char *name, CAT_VALUE_Value *v);
|
|
|
|
void cat_vm_free(CAT_VM_State *vm);
|
|
|
|
#endif /* CAT_VM_H */
|