|
| 1 | +# JavaScript Implementation: Core Runtime |
| 2 | + |
| 3 | +:::tip |
| 4 | +For basic usage, please see the [Guide: "Embedding into Your Application"](/en/guides/implementation). |
| 5 | +::: |
| 6 | + |
| 7 | +## Parser |
| 8 | + |
| 9 | +### `Parser.parse(script: string)` |
| 10 | + |
| 11 | +Converts a string of AiScript code into an Abstract Syntax Tree (AST). |
| 12 | + |
| 13 | +```ts |
| 14 | +import { Parser } from '@syuilo/aiscript'; |
| 15 | + |
| 16 | +const program = "<: \"Hello, World!\""; |
| 17 | + |
| 18 | +let parser: Parser; |
| 19 | + |
| 20 | +async function run() { |
| 21 | + parser = new Parser(); |
| 22 | + const ast = parser.parse(program); |
| 23 | +} |
| 24 | + |
| 25 | +run(); |
| 26 | +``` |
| 27 | + |
| 28 | +### `Parser.addPlugin(type: PluginType, plugin: ParserPlugin)` |
| 29 | + |
| 30 | +Adds a plugin to the current instance that receives the AST to perform validation (`validate`) or modification (`transform`). |
| 31 | + |
| 32 | +----- |
| 33 | + |
| 34 | +## Interpreter |
| 35 | + |
| 36 | +AiScript Values must be converted to JavaScript primitive types as needed. |
| 37 | + |
| 38 | +### Constructor |
| 39 | + |
| 40 | +`Interpreter(consts: Record<string, Value>, opts: ...)` |
| 41 | + |
| 42 | +#### `consts` |
| 43 | + |
| 44 | +Specifies properties to be injected at runtime. Provide an object where the keys are property names and the values are the AiScript values to inject. |
| 45 | + |
| 46 | +#### `opts` |
| 47 | + |
| 48 | +Specifies execution options. All are optional. |
| 49 | + |
| 50 | +- `in(q: string): Promise<string>`: This function is called to accept input. `q` is the value to display in the prompt, and the return value is a Promise that resolves with the string of user input. |
| 51 | +- `out(value: Value): void`: This function is called to produce output. `value` is the AiScript Value to be output. |
| 52 | +- `err(e: AiScriptError): void`: This function is called when a problem occurs during AiScript execution. `e` is an `AiScriptError` that extends JavaScript's `Error` class. |
| 53 | +- `log(type: string, params: LogObject): void`: This function is called when there is a log from the runtime. |
| 54 | +- `maxStep: number`: To limit the execution depth, enter the maximum number of steps here. |
| 55 | +- `abortOnError: boolean`: Whether to stop all other executions handled by the interpreter (including future ones) when an error occurs during execution. |
| 56 | +- `irqRate: number`: Allows you to specify the Interrupt Request (IRQ) rate. |
| 57 | +- `irqSleep() => Promise<void>`: Allows you to define the IRQ wait. |
| 58 | + |
| 59 | +### `Interpreter.exec(script?: Ast.Node[])` |
| 60 | + |
| 61 | +Asynchronously executes the AiScript AST specified in `script`. **This is the recommended method for normal use.** |
| 62 | + |
| 63 | +### `Interpreter.execSync(script?: Ast.Node[])` |
| 64 | + |
| 65 | +Synchronously executes the AiScript AST specified in `script`. Use with caution. |
| 66 | + |
| 67 | +:::danger |
| 68 | +- When running synchronously, it is **strongly recommended** to adjust limits such as the maximum number of steps. Unlike the asynchronous `exec`, `execSync` can have a severe impact on the host JavaScript environment if a malicious script (e.g., an infinite loop) is executed with poor configuration. |
| 69 | +- When running synchronously, functions injected via `consts` that return a Promise (in other words, async functions) cannot be executed and will result in an error. |
| 70 | +::: |
| 71 | + |
| 72 | +### `Interpreter.execFn(fn: VFn, args: Value[])` |
| 73 | + |
| 74 | +Asynchronously executes the AiScript function passed in `fn`. The `args` array contains the arguments for the function. To execute synchronously, use `Interpreter.execFnSync`. |
| 75 | + |
| 76 | +### `Interpreter.execFnSimple(fn: VFn, args: Value[])` |
| 77 | + |
| 78 | +Asynchronously executes the AiScript function passed in `fn`. Unlike `execFn`, which calls the interpreter's callback (`opts.err`) if an error occurs within the function, `execFnSimple` will always throw an `Error`. |
| 79 | + |
| 80 | +### `Interpreter.collectMetadata(script?: Ast.Node[])` |
| 81 | + |
| 82 | +Retrieves the content of the [metadata syntax](../syntax.md#metadata-syntax) as a JavaScript value. |
0 commit comments