Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,7 @@
}

case 'loop': {
// eslint-disable-next-line no-constant-condition
while (true) {
while (!this.stop) {

Check warning on line 550 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always truthy
const v = await this._run(node.statements, scope.createChildScope(), callStack);
if (v.type === 'break') {
if (v.label != null && v.label !== node.label) {
Expand All @@ -573,7 +572,7 @@
return times;
}
assertNumber(times);
for (let i = 0; i < times.value; i++) {
for (let i = 0; i < times.value && !this.stop; i++) {

Check warning on line 575 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always truthy
const v = await this._evalClause(node.for, scope, callStack);
if (v.type === 'break') {
if (v.label != null && v.label !== node.label) {
Expand All @@ -599,7 +598,7 @@
}
assertNumber(from);
assertNumber(to);
for (let i = from.value; i < from.value + to.value; i++) {
for (let i = from.value; i < from.value + to.value && !this.stop; i++) {

Check warning on line 601 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always truthy
const v = await this._eval(node.for, scope.createChildScope(new Map([
[node.var!, {
isMutable: false,
Expand Down Expand Up @@ -630,6 +629,7 @@
}
assertArray(items);
for (const item of items.value) {
if (this.stop) break;

Check warning on line 632 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always falsy
const eachScope = scope.createChildScope();
this.define(eachScope, node.var, item, false);
const v = await this._eval(node.for, eachScope, callStack);
Expand Down Expand Up @@ -1080,8 +1080,7 @@
}

case 'loop': {
// eslint-disable-next-line no-constant-condition
while (true) {
while (!this.stop) {

Check warning on line 1083 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always truthy
const v = this._runSync(node.statements, scope.createChildScope(), callStack);
if (v.type === 'break') {
if (v.label != null && v.label !== node.label) {
Expand All @@ -1106,7 +1105,7 @@
return times;
}
assertNumber(times);
for (let i = 0; i < times.value; i++) {
for (let i = 0; i < times.value && !this.stop; i++) {

Check warning on line 1108 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always truthy
const v = this._evalClauseSync(node.for, scope, callStack);
if (v.type === 'break') {
if (v.label != null && v.label !== node.label) {
Expand All @@ -1132,7 +1131,7 @@
}
assertNumber(from);
assertNumber(to);
for (let i = from.value; i < from.value + to.value; i++) {
for (let i = from.value; i < from.value + to.value && !this.stop; i++) {

Check warning on line 1134 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always truthy
const v = this._evalSync(node.for, scope.createChildScope(new Map([
[node.var!, {
isMutable: false,
Expand Down Expand Up @@ -1163,6 +1162,7 @@
}
assertArray(items);
for (const item of items.value) {
if (this.stop) break;

Check warning on line 1165 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unnecessary conditional, value is always falsy
const eachScope = scope.createChildScope();
this.define(eachScope, node.var, item, false);
const v = this._evalSync(node.for, eachScope, callStack);
Expand Down Expand Up @@ -1631,7 +1631,7 @@
public pause(): void {
if (this.pausing) return;
let resolve: () => void;
const promise = new Promise<void>(r => { resolve = () => r(); });

Check warning on line 1634 in src/interpreter/index.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
this.pausing = { promise, resolve: resolve! };
for (const handler of this.pauseHandlers) {
handler();
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter/lib/std.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export const std: Record<string, Value> = {
assertFunction(callback);
if (immediate) {
assertBoolean(immediate);
if (immediate.value) opts.call(callback, []);
if (immediate.value) opts.topCall(callback, []);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM なのですが、一応補足としてコメントを残しておきます。

call から topCall とした場合、コールスタックが途切れますが、待たずに実行を予約する意図 (JS の setImmediate 相当) ならば問題なさそうです。

https://github.com/FineArchs/aiscript/blob/0fe4cb6c087e760cdc697c59f71cafd7a8f32e79/src/interpreter/index.ts#L342

}

let id: ReturnType<typeof setInterval>;
Expand Down
29 changes: 29 additions & 0 deletions test/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,35 @@ describe('error handler', () => {
});
});

describe('abort', () => {
const repetitions = [
['loop', 'loop { abort() }'],
['while', 'while true { abort() }'],
['do-while', 'do { abort() } while true'],
['for times', 'for Math:Infinity { abort() }'],
['for range', 'for let i, Math:Infinity { abort() }'],
['each', 'each let item, [1, 2, 3] { abort() }'],
];

test.each(repetitions)('stops %s during async evaluation', async (_name, source) => {
let interpreter: Interpreter;
interpreter = new Interpreter({
abort: FN_NATIVE(() => interpreter.abort()),
});

await interpreter.exec(Parser.parse(source));
});

test.each(repetitions)('stops %s during sync evaluation', (_name, source) => {
let interpreter: Interpreter;
interpreter = new Interpreter({
abort: FN_NATIVE(() => interpreter.abort()),
});

interpreter.execSync(Parser.parse(source));
});
});

describe('error location', () => {
const exeAndGetErrPos = (src: string): Promise<Ast.Pos|undefined> => new Promise((ok, ng) => {
const aiscript = new Interpreter({
Expand Down
1 change: 1 addition & 0 deletions unreleased/fix-abort-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- fix: abortOnError時に空の無限ループが発生する
1 change: 1 addition & 0 deletions unreleased/fix-async-interval-immidiate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- fix: Async:intervalの即時実行時のエラーが補足されない不具合を修正
Loading