1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//! The `viper` binary, run as a process.
//!
//! # Why these are out here rather than beside it
//!
//! Everything the command line decides *before* it acts is a pure function and is tested in
//! `src/bin/viper.rs`. What is left is the acting: reading a file, printing a completion value or
//! declining to, and choosing an exit status. None of that is reachable from a unit test — it is
//! the process's behaviour, not a function's — and it is exactly what a person trying the engine
//! meets first.
//!
//! This is DR-0021's lesson applied once more. `examples/embed.rs` found four faults in the
//! embedding surface that fourteen unit tests inside the crate could not, because it was outside
//! the crate. These are outside the *binary*, for the same reason.
use std::io::Write;
use std::process::{Command, Stdio};
/// Run the binary with these arguments and answer its stdout, stderr and exit status.
fn viper(arguments: &[&str], stdin: &str) -> (String, String, Option<i32>) {
let mut child = Command::new(env!("CARGO_BIN_EXE_viper"))
.args(arguments)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.expect("the binary was built");
// Dropped straight after writing, which closes the pipe — otherwise a run that reads standard
// input waits for an end that never comes and the test hangs rather than fails.
if let Some(mut pipe) = child.stdin.take() {
pipe.write_all(stdin.as_bytes()).expect("the pipe takes it");
}
let done = child.wait_with_output().expect("it finishes");
(
String::from_utf8_lossy(&done.stdout).replace("\r\n", "\n"),
String::from_utf8_lossy(&done.stderr).replace("\r\n", "\n"),
done.status.code(),
)
}
#[test]
fn a_multi_line_file_runs_as_one_script() {
// The gap this binary was written for. `examples/evaluate` reads one script per *line*, so this
// source came apart into eight fragments and reported eight parse errors — there was no way to
// run an ordinary `.js` file at all.
let directory = std::env::temp_dir().join("viper-cli-test");
std::fs::create_dir_all(&directory).expect("a place to put a file");
let path = directory.join("fib.js");
std::fs::write(
&path,
"function fib(n) {\n return n < 2 ? n : fib(n - 1) + fib(n - 2);\n}\n\
var out = [];\nfor (var i = 0; i < 10; i++) {\n out.push(fib(i));\n}\n\
print(out.join(','));\n",
)
.expect("it writes");
let (out, err, status) = viper(&[path.to_str().expect("a path")], "");
assert_eq!(out, "0,1,1,2,3,5,8,13,21,34\n");
assert_eq!(err, "");
assert_eq!(status, Some(0));
}
#[test]
fn a_file_prints_only_what_it_printed_and_minus_e_prints_the_answer() {
// §14.2.2's completion value is not a return value. A script whose last statement happens to be
// an expression must not spray it at the terminal — while `-e` exists *for* the answer, which
// is the whole difference between the two and the one thing `Show` decides.
let directory = std::env::temp_dir().join("viper-cli-test");
std::fs::create_dir_all(&directory).expect("a place to put a file");
let path = directory.join("quiet.js");
std::fs::write(&path, "var n = 1 + 1;\nn;\n").expect("it writes");
let (out, _, status) = viper(&[path.to_str().expect("a path")], "");
assert_eq!(out, "", "a file's completion value is not output");
assert_eq!(status, Some(0));
let (out, _, status) = viper(&["-e", "1 + 1"], "");
assert_eq!(out, "2\n", "`-e` is asked for the answer");
assert_eq!(status, Some(0));
// …and standard input is a file, not an expression — same rule.
let (out, _, status) = viper(&[], "var n = 1 + 1;\nn;\n");
assert_eq!(out, "");
assert_eq!(status, Some(0));
}
#[test]
fn a_piped_script_runs_whole_rather_than_a_line_at_a_time() {
let (out, err, status) = viper(&[], "function f(a) {\n return a * 3;\n}\nprint(f(14));\n");
assert_eq!(out, "42\n");
assert_eq!(err, "");
assert_eq!(status, Some(0));
// `-` says the same thing out loud, which is the spelling that works from a terminal.
let (out, _, status) = viper(&["-"], "print('read');\n");
assert_eq!(out, "read\n");
assert_eq!(status, Some(0));
}
#[test]
fn the_exit_status_tells_a_shell_which_kind_of_wrong_it_was() {
// Three outcomes a caller scripts against, and they are deliberately different numbers: a
// script that threw is not the same event as a command line nobody could read.
let (_, err, status) = viper(&["-e", "throw new TypeError('nope')"], "");
assert_eq!(status, Some(1));
assert!(err.contains("TypeError: nope"), "{err}");
let (_, err, status) = viper(&["-e", "var x = ;"], "");
assert_eq!(status, Some(1));
assert!(err.contains("SyntaxError"), "{err}");
let (_, err, status) = viper(&["--nope"], "");
assert_eq!(status, Some(2));
assert!(err.contains("unknown option"), "{err}");
let (_, err, status) = viper(&["no-such-file-anywhere.js"], "");
assert_eq!(status, Some(2));
assert!(err.contains("no-such-file-anywhere.js"), "{err}");
}
#[test]
fn a_time_budget_stops_a_loop_the_script_tries_to_catch() {
// DR-0022, from the outside. The budget is not a throw — a script cannot catch it, which is
// what makes it a bound on untrusted code rather than a suggestion — so this `catch` never
// runs and the process still ends.
let (out, err, status) = viper(
&[
"--time-budget",
"150",
"-e",
"try { while (true) {} } catch (e) { 'caught' }",
],
"",
);
assert_eq!(out, "", "nothing was produced, so nothing is printed");
assert!(err.contains("time budget"), "{err}");
assert_eq!(status, Some(1));
// …and a budget the run does not spend changes nothing.
let (out, _, status) = viper(&["--time-budget", "10000", "-e", "1 + 1"], "");
assert_eq!(out, "2\n");
assert_eq!(status, Some(0));
}
#[test]
fn help_and_version_answer_on_stdout_and_succeed() {
// A help text on stderr with a failing status is a small cruelty to anyone piping it to a
// pager, and asking for help is not an error.
let (out, err, status) = viper(&["--help"], "");
assert!(out.contains("usage:"), "{out}");
assert_eq!(err, "");
assert_eq!(status, Some(0));
let (out, _, status) = viper(&["-V"], "");
assert!(out.starts_with("viper "), "{out}");
assert_eq!(status, Some(0));
}
#[test]
fn the_host_binds_print_and_console_and_nothing_else() {
// GOAL.md §3 — the host provides I/O and this host provides almost none. Naming what is absent
// is the point: a `require` or a `process` arriving by accident is exactly the drift toward
// Node compatibility the charter refuses, and this is what would notice.
//
// `console` is deliberately on the other side of that line and was not always. It is a WHATWG
// surface that browsers, workers, Deno and Bun all have, and refusing it broke ordinary
// libraries in code that was never about output. Its *shape* is checked below; this row is
// about the boundary, and the boundary moved once, on purpose.
let (out, _, status) = viper(
&[
"-e",
"[typeof print, typeof console, typeof require, typeof process, typeof globalThis].join()",
],
"",
);
assert_eq!(out, "function,object,undefined,undefined,object\n");
assert_eq!(status, Some(0));
}
#[test]
fn module_code_is_named_as_module_code_rather_than_as_a_puzzling_token() {
// A Script parse of Module code trips on whatever token comes first, and the message is then
// true and useless: `export` reads as "expected an expression". The goal symbol is the actual
// answer, and it is asked by *parsing* rather than by guessing at a file extension.
for source in [
"export const x = 1;",
"import x from './a.js';",
// §16.2.1's top-level `await` is the third thing a Module accepts and a Script does not,
// so it reaches the same sentence without an `import` or `export` anywhere in it.
"const x = await Promise.resolve(1);",
] {
let (_, err, status) = viper(&["-e", source], "");
assert!(
err.contains("this is Module code"),
"{source}: expected the Module signpost, got {err:?}"
);
// The token the Script parse tripped on is kept, because it is what tells apart a Module
// that is fine from one that is *also* malformed.
assert!(err.contains("as a Script it said:"), "{source}: {err:?}");
assert_eq!(status, Some(1), "{source}");
}
// An ordinary syntax error must be untouched: it is not Module code, and saying so would send
// a reader looking for an `import` that is not there.
let (_, err, status) = viper(&["-e", "function ( {"], "");
assert!(!err.contains("Module code"), "{err:?}");
assert!(err.contains("SyntaxError"), "{err:?}");
assert_eq!(status, Some(1));
// And code that runs never reaches the question at all.
let (out, err, status) = viper(&["-e", "print('fine')"], "");
assert!(out.contains("fine"), "{out:?}");
assert!(!err.contains("Module"), "{err:?}");
assert_eq!(status, Some(0));
}
#[test]
fn console_writes_diagnostics_to_standard_error_and_output_to_standard_out() {
// Real libraries reach for `console.warn` to report a deprecation and `console.error` for a
// failure they are handling. Without a `console` they die on a ReferenceError in code that was
// never about output, which is how this got noticed: `ajv` does exactly that.
//
// The split is the part worth testing rather than the existence. A host that sent everything
// to one stream would corrupt `viper x.js > out.json` the first time a library warned.
let (out, err, status) = viper(
&[],
"console.log('to out'); console.warn('to err'); console.error('also err');\n",
);
assert_eq!(out.trim(), "to out");
assert_eq!(
err.trim().lines().collect::<Vec<_>>(),
["to err", "also err"]
);
assert_eq!(status, Some(0));
}
#[test]
fn a_console_method_joins_every_argument_with_a_space() {
// `print` takes one argument and `console.log` takes as many as it is given — which is why they
// are separate functions rather than one under two names. The conversion is `ToString` per
// argument, so an object is `[object Object]` and not JSON.
let (out, _, status) = viper(
&[],
"console.log('a', 1, true, null, undefined, {x: 1}, [1, 2]);\n",
);
assert_eq!(out.trim(), "a 1 true null undefined [object Object] 1,2");
assert_eq!(status, Some(0));
// No arguments is an empty line rather than nothing, which is what a bare `console.log()` means.
let (out, _, _) = viper(&[], "console.log();\n");
assert_eq!(
out,
"
"
);
}
#[test]
fn console_is_an_ordinary_object_the_program_may_take_apart() {
// A namespace the host hands over belongs to the script: it can be destructured, re-bound and
// passed around, which is what a library does when it caches `const warn = console.warn`.
let (out, err, status) = viper(
&[],
"var w = console.warn; w('detached');\n\
print(console.log.name + ',' + typeof console + ',' + Object.keys(console).length);\n",
);
assert_eq!(out.trim(), "console.log,object,6");
assert_eq!(err.trim(), "detached");
assert_eq!(status, Some(0));
// And `print` is still there, because every example and the conformance harness use it.
let (out, _, _) = viper(&[], "print(typeof print);\n");
assert_eq!(out.trim(), "function");
}
#[test]
fn the_heap_budget_can_be_raised_and_lowered_from_the_command_line() {
// DR-0013's default is 64 MiB, which is a policy and not a fact about the machine. A real
// bundle can want hundreds of megabytes — a 1.9 MB build of `mathjs` needs more than 256 —
// and before the flag there was no way for the person running it to say so.
let program = "var a = []; for (var i = 0; i < 200000; i++) a.push({x: i}); print(a.length);\n";
let (out, _, status) = viper(&["--heap-budget", "256"], program);
assert_eq!(out.trim(), "200000");
assert_eq!(status, Some(0));
// Lowered, the same program is refused — as a throw the script could have caught, not as a
// process that stopped existing.
let (_, err, status) = viper(&["--heap-budget", "1"], program);
assert!(err.contains("heap has grown past"), "{err}");
assert_eq!(status, Some(1));
// Mebibytes, not bytes: `1` above is a real limit rather than one byte. If this were bytes the
// first row would refuse too, and the two would be indistinguishable.
let (out, _, status) = viper(&["--heap-budget", "64", "-e", "1 + 1"], "");
assert_eq!(out.trim(), "2");
assert_eq!(status, Some(0));
}
#[test]
fn a_heap_budget_that_makes_no_sense_is_refused_before_anything_runs() {
// Zero is refused rather than read as "no limit": a heap that may hand out nothing refuses the
// first allocation, and the engine would look broken rather than configured.
for argument in ["0", "abc", "-1"] {
let (_, err, status) = viper(&["--heap-budget", argument, "-e", "1"], "");
assert!(
err.contains("--heap-budget wants"),
"for `{argument}`: {err}"
);
assert_eq!(status, Some(2), "for `{argument}`");
}
// …and with nothing after it at all.
let (_, err, status) = viper(&["--heap-budget"], "");
assert!(err.contains("--heap-budget wants"), "{err}");
assert_eq!(status, Some(2));
}
#[test]
fn the_command_line_binds_the_html_standards_two_base64_functions() {
// Not ECMAScript, and here on the same terms as `console`: in the Minimum Common API, every
// host has them, and they are pure arithmetic. `entities` — which `htmlparser2` and `cheerio`
// are built on — decodes its own tables with `atob` while it loads, so a very common dependency
// chain died on a ReferenceError before any of the program ran.
let (out, _, status) = viper(&[], "print(btoa('hello')); print(atob('aGVsbG8='));\n");
assert_eq!(out, "aGVsbG8=\nhello\n");
assert_eq!(status, Some(0));
// Every byte value, which is the only test that covers the alphabet's edges — a table with one
// character out of place round-trips almost everything and fails on a handful.
let (out, _, _) = viper(
&[],
"var all = ''; for (var b = 0; b < 256; b++) { all += String.fromCharCode(b); }\
print(atob(btoa(all)) === all);\n",
);
assert_eq!(out.trim(), "true");
// The three lengths a group can end on, each with its own amount of padding.
let (out, _, _) = viper(
&[],
"print(btoa('a') + ' ' + btoa('ab') + ' ' + btoa('abc'));\n",
);
assert_eq!(out.trim(), "YQ== YWI= YWJj");
}
#[test]
fn forgiving_base64_forgives_exactly_three_things_and_nothing_else() {
// "Forgiving" is the standard's own word and it names three allowances rather than a general
// leniency. Whitespace anywhere is the first…
let (out, _, _) = viper(&[], "print(atob(' a G V s b G 8 = '));\n");
assert_eq!(out.trim(), "hello");
// …and it is *ASCII* whitespace, the HTML infra set: no U+000B and no U+00A0, so a string that
// looks blank to a reader is still refused.
let (_, err, status) = viper(&[], "atob('aGVs\\u00a0bG8=');\n");
assert!(err.contains("InvalidCharacterError"), "{err}");
assert_eq!(status, Some(1));
// The second is the padding, which is optional only when the length is *already* a multiple of
// four — so `'YQ='` stays four-and-a-remainder and is refused rather than read as `'YQ'`.
let (out, _, _) = viper(&[], "print(atob('aGVsbG8'));\n");
assert_eq!(out.trim(), "hello");
let (_, err, _) = viper(&[], "atob('YQ=');\n");
assert!(err.contains("InvalidCharacterError"), "{err}");
// The third is that a length one past a multiple of four encodes no number of bytes at all.
let (_, err, _) = viper(&[], "atob('a');\n");
assert!(err.contains("no number of bytes"), "{err}");
// And nothing else is forgiven: a character outside the alphabet is refused wherever it is.
for bad in ["'!!!!'", "'a=b='", "'ab-d'"] {
let (_, err, status) = viper(&[], &format!("atob({bad});\n"));
assert!(err.contains("InvalidCharacterError"), "for {bad}: {err}");
assert_eq!(status, Some(1), "for {bad}");
}
}
#[test]
fn btoa_encodes_bytes_and_refuses_a_string_that_is_not_made_of_them() {
// `btoa` encodes *bytes* and a String holds code units, so there is no encoding to choose and
// the standard refuses rather than choosing one. U+00FF goes; U+0100 does not, and the boundary
// between them is the whole rule.
let (out, _, status) = viper(&[], "print(btoa(String.fromCharCode(255, 254)));\n");
assert_eq!(out.trim(), "//4=");
assert_eq!(status, Some(0));
let (_, err, status) = viper(&[], "btoa(String.fromCharCode(256));\n");
assert!(err.contains("code point above U+00FF"), "{err}");
assert_eq!(status, Some(1));
// The error is a TypeError carrying the standard's name rather than a DOMException, which this
// host has no way to make. Recorded as a test so the difference is a decision and not a
// surprise — a caller matching on the message finds `InvalidCharacterError` either way.
let (out, _, _) = viper(
&[],
"try { btoa('\\u0100') } catch (e) { print(e.name + '|' + (e.message.indexOf('InvalidCharacterError') === 0)); }\n",
);
assert_eq!(out.trim(), "TypeError|true");
}