diff --git a/etc/aiscript.api.md b/etc/aiscript.api.md index bebd9ef7..2ef9823e 100644 --- a/etc/aiscript.api.md +++ b/etc/aiscript.api.md @@ -416,7 +416,7 @@ type Index = NodeBase & { // @public (undocumented) export class Interpreter { - constructor(consts: Record, opts?: { + constructor(globals: Record, opts?: { in?(q: string): Promise; out?(value: Value): void; err?(e: AiScriptError): void; @@ -713,6 +713,9 @@ export class Scope { // @public (undocumented) type Statement = Definition | Return | Each | For | Loop | Break | Continue | Assign | AddAssign | SubAssign; +// @public (undocumented) +export const std: Record; + // @public (undocumented) const STR: (str: VStr["value"]) => VStr; diff --git a/src/index.ts b/src/index.ts index ee600c18..0bdc3994 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ //export * from './interpreter/index'; //export * as utils from './interpreter/util'; //export * as values from './interpreter/value'; -import { Interpreter } from './interpreter/index.js'; +import { Interpreter, std } from './interpreter/index.js'; import { Scope } from './interpreter/scope.js'; import * as utils from './interpreter/util.js'; import * as values from './interpreter/value.js'; @@ -12,7 +12,7 @@ import * as errors from './error.js'; import * as Ast from './node.js'; import { AISCRIPT_VERSION } from './constants.js'; import type { ParserPlugin, PluginType } from './parser/index.js'; -export { Interpreter }; +export { Interpreter, std }; export { Scope }; export { utils }; export { values }; diff --git a/src/interpreter/index.ts b/src/interpreter/index.ts index 1ea3d16c..d29c7e40 100644 --- a/src/interpreter/index.ts +++ b/src/interpreter/index.ts @@ -7,7 +7,6 @@ import { AiScriptError, NonAiScriptError, AiScriptNamespaceError, AiScriptIndexO import * as Ast from '../node.js'; import { nodeToJs } from '../utils/node-to-js.js'; import { Scope } from './scope.js'; -import { std } from './lib/std.js'; import { RETURN, unWrapRet, BREAK, CONTINUE, assertValue, isControl, type Control, unWrapLabeledBreak } from './control.js'; import { assertNumber, assertString, assertFunction, assertBoolean, assertObject, assertArray, eq, isObject, isArray, expectAny, reprValue, isFunction } from './util.js'; import { NULL, FN_NATIVE, BOOL, NUM, STR, ARR, OBJ, FN, ERROR } from './value.js'; @@ -17,6 +16,8 @@ import { Reference } from './reference.js'; import type { JsValue } from './util.js'; import type { Value, VFn, VUserFn } from './value.js'; +export { std } from './lib/std.js'; + export type LogObject = { scope?: string; var?: string; @@ -36,12 +37,11 @@ export class Interpreter { private abortHandlers: (() => void)[] = []; private pauseHandlers: (() => void)[] = []; private unpauseHandlers: (() => void)[] = []; - private vars: Record = {}; private irqRate: number; private irqSleep: () => Promise; constructor( - consts: Record, + globals: Record, private opts: { in?(q: string): Promise; out?(value: Value): void; @@ -67,13 +67,12 @@ export class Interpreter { }), }; - this.vars = Object.fromEntries(Object.entries({ - ...consts, - ...std, - ...io, - }).map(([k, v]) => [k, Variable.const(v)])); - - this.scope = new Scope([new Map(Object.entries(this.vars))]); + this.scope = new Scope([ + new Map( + Object.entries({ ...globals, ...io }) + .map(([k, v]) => [k, Variable.const(v)]), + ), + ]); this.scope.opts.log = (type, params): void => { switch (type) { case 'add': this.log('var:add', params); break; diff --git a/test/interpreter.ts b/test/interpreter.ts index 0b8bc0ea..1b1a98e8 100644 --- a/test/interpreter.ts +++ b/test/interpreter.ts @@ -1,6 +1,6 @@ import * as assert from 'assert'; import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest'; -import { Parser, Interpreter, values, errors, utils, Ast } from '../src'; +import { Parser, Interpreter, std, values, errors, utils, Ast } from '../src'; import { FALSE, NUM, OBJ, STR, TRUE, Value } from '../src/interpreter/value'; let { FN_NATIVE } = values; @@ -261,6 +261,7 @@ describe('pause', () => { let count = 0; const interpreter = new Interpreter({ + ...std, count: values.FN_NATIVE(() => { count++; }), }, {}); diff --git a/test/testutils.ts b/test/testutils.ts index 0b09ac11..feedd52c 100644 --- a/test/testutils.ts +++ b/test/testutils.ts @@ -1,11 +1,11 @@ import { expect as globalExpect } from 'vitest'; -import { Parser, Interpreter } from '../src'; +import { Parser, Interpreter, std } from '../src'; import { Value } from '../src/interpreter/value'; export async function exe(script: string): Promise { const parser = new Parser(); let result = undefined; - const interpreter = new Interpreter({}, { + const interpreter = new Interpreter(std, { out(value) { if (!result) result = value; else if (!Array.isArray(result)) result = [result, value]; @@ -23,7 +23,7 @@ export async function exe(script: string): Promise { export function exeSync(script: string): Value | undefined { const parser = new Parser(); - const interpreter = new Interpreter({}, { + const interpreter = new Interpreter(std, { out(value) { }, log(type, {val}) {