Skip to content

Commit e25e35b

Browse files
committed
stdを分離
1 parent 0178f1b commit e25e35b

3 files changed

Lines changed: 12 additions & 12 deletions

File tree

src/interpreter/index.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { AiScriptError, NonAiScriptError, AiScriptNamespaceError, AiScriptIndexO
77
import * as Ast from '../node.js';
88
import { nodeToJs } from '../utils/node-to-js.js';
99
import { Scope } from './scope.js';
10-
import { std } from './lib/std.js';
1110
import { RETURN, unWrapRet, BREAK, CONTINUE, assertValue, isControl, type Control, unWrapLabeledBreak } from './control.js';
1211
import { assertNumber, assertString, assertFunction, assertBoolean, assertObject, assertArray, eq, isObject, isArray, expectAny, reprValue, isFunction } from './util.js';
1312
import { NULL, FN_NATIVE, BOOL, NUM, STR, ARR, OBJ, FN, ERROR } from './value.js';
@@ -36,12 +35,11 @@ export class Interpreter {
3635
private abortHandlers: (() => void)[] = [];
3736
private pauseHandlers: (() => void)[] = [];
3837
private unpauseHandlers: (() => void)[] = [];
39-
private vars: Record<string, Variable> = {};
4038
private irqRate: number;
4139
private irqSleep: () => Promise<void>;
4240

4341
constructor(
44-
consts: Record<string, Value>,
42+
globals: Record<string, Value>,
4543
private opts: {
4644
in?(q: string): Promise<string>;
4745
out?(value: Value): void;
@@ -67,13 +65,12 @@ export class Interpreter {
6765
}),
6866
};
6967

70-
this.vars = Object.fromEntries(Object.entries({
71-
...consts,
72-
...std,
73-
...io,
74-
}).map(([k, v]) => [k, Variable.const(v)]));
75-
76-
this.scope = new Scope([new Map(Object.entries(this.vars))]);
68+
this.scope = new Scope([
69+
new Map(
70+
Object.entries({ ...globals, ...io })
71+
.map(([k, v]) => [k, Variable.const(v)]),
72+
),
73+
]);
7774
this.scope.opts.log = (type, params): void => {
7875
switch (type) {
7976
case 'add': this.log('var:add', params); break;

test/interpreter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as assert from 'assert';
22
import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest';
33
import { Parser, Interpreter, values, errors, utils, Ast } from '../src';
44
import { FALSE, NUM, OBJ, STR, TRUE, Value } from '../src/interpreter/value';
5+
import { std } from '../src/interpreter/lib/std';
56

67
let { FN_NATIVE } = values;
78
let { AiScriptRuntimeError, AiScriptIndexOutOfRangeError, AiScriptHostsideError } = errors;
@@ -261,6 +262,7 @@ describe('pause', () => {
261262
let count = 0;
262263

263264
const interpreter = new Interpreter({
265+
...std,
264266
count: values.FN_NATIVE(() => { count++; }),
265267
}, {});
266268

test/testutils.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { expect as globalExpect } from 'vitest';
22
import { Parser, Interpreter } from '../src';
33
import { Value } from '../src/interpreter/value';
4+
import { std } from '../src/interpreter/lib/std';
45

56
export async function exe(script: string): Promise<Value | undefined> {
67
const parser = new Parser();
78
let result = undefined;
8-
const interpreter = new Interpreter({}, {
9+
const interpreter = new Interpreter(std, {
910
out(value) {
1011
if (!result) result = value;
1112
else if (!Array.isArray(result)) result = [result, value];
@@ -23,7 +24,7 @@ export async function exe(script: string): Promise<Value | undefined> {
2324

2425
export function exeSync(script: string): Value | undefined {
2526
const parser = new Parser();
26-
const interpreter = new Interpreter({}, {
27+
const interpreter = new Interpreter(std, {
2728
out(value) {
2829
},
2930
log(type, {val}) {

0 commit comments

Comments
 (0)