Make Vm generic over augmented information
This commit is contained in:
parent
981ddd7a6d
commit
fe5f88c95e
1 changed files with 11 additions and 8 deletions
19
src/vm.ts
19
src/vm.ts
|
@ -11,22 +11,25 @@ import {
|
|||
*/
|
||||
export type ScriptType = "action" | "render";
|
||||
|
||||
export type Proc = (state: Vm, argv: TextPiece[]) => TextPiece;
|
||||
export type Proc<Context> = (
|
||||
state: Vm<Context>,
|
||||
argv: TextPiece[]
|
||||
) => TextPiece;
|
||||
|
||||
/**
|
||||
* State for running a script in.
|
||||
*/
|
||||
export type Vm = {
|
||||
export type Vm<Context = {}> = {
|
||||
/** Mutability status */
|
||||
mode: ScriptType;
|
||||
/** Implementations of commands scripts can run */
|
||||
commands: Record<string, Proc>;
|
||||
commands: Record<string, Proc<Context>>;
|
||||
/** Markup to render / output */
|
||||
output: string;
|
||||
};
|
||||
} & Context;
|
||||
|
||||
function evaluateWord(
|
||||
state: Vm,
|
||||
function evaluateWord<Context>(
|
||||
state: Vm<Context>,
|
||||
word: Word | InterpolatedPiece
|
||||
): TextWord | BareWord | HtmlWord {
|
||||
if ("bare" in word || "text" in word || "html" in word) {
|
||||
|
@ -50,8 +53,8 @@ function evaluateWord(
|
|||
* @param onReturn callback optionally invoked with the return word for each top-level command (not triggered by command substitutions)
|
||||
* @returns the return word of the final command in the script, or empty text if the script is empty.
|
||||
*/
|
||||
export function runNoctl(
|
||||
state: Vm,
|
||||
export function runNoctl<Context>(
|
||||
state: Vm<Context>,
|
||||
script: Script,
|
||||
onReturn?: (word: TextPiece) => void
|
||||
): TextPiece {
|
||||
|
|
Loading…
Reference in a new issue