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
3 changes: 3 additions & 0 deletions src/runtime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ export async function runScript(source, opts = {}) {
}
const out = opts.out || ((s) => console.log(s));
const max = Number.isFinite(opts.max) ? opts.max : DEFAULT_MAX;
if (!Number.isInteger(max) || max < 1) {
throw new Error(`moshscript: max must be a positive integer, got ${JSON.stringify(opts.max)}`);
}
const control = makeControl(max, out);

const ctx = {
Expand Down
13 changes: 13 additions & 0 deletions test/runtime.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ test("DEFAULT_MAX bounds an unbounded while when no max is passed", async () =>
assert.equal(calls.length, DEFAULT_MAX);
});

test("runScript rejects invalid max values", async () => {
const { registry } = recorder();

await assert.rejects(
() => runScript(`while (alive) {}`, { commands: registry, max: 0 }),
/max must be a positive integer/,
);
await assert.rejects(
() => runScript(`while (alive) {}`, { commands: registry, max: 1.5 }),
/max must be a positive integer/,
);
});

test("a rejecting fire-and-forget verb does not kill a script that keeps running", async () => {
const calls = [];
const registry = createRegistry([
Expand Down
Loading