use js_compile::{compile_script, disassemble};
use js_parse::parse_script;
use quickjs_conformance::locate_disassembler;
use std::path::PathBuf;
fn oracle() -> Option<PathBuf> {
locate_disassembler()
}
macro_rules! require_oracle {
() => {
match oracle() {
Some(path) => path,
None => {
eprintln!("skipping: no disassembling qjsc found (run scripts/setup-oracle.sh)");
return;
}
}
};
}
struct Scratch(PathBuf);
impl Scratch {
fn new(tag: &str) -> Scratch {
static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let sequence = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let mut path = std::env::temp_dir();
path.push(format!(
"quickjs-rs-compile-{tag}-{}-{sequence}",
std::process::id()
));
std::fs::create_dir_all(&path).expect("scratch dir");
Scratch(path)
}
fn file(&self, name: &str, contents: &str) -> PathBuf {
let path = self.0.join(name);
std::fs::write(&path, contents).expect("write case");
path
}
}
impl Drop for Scratch {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn reference_disassembly(oracle: &PathBuf, scratch: &Scratch, source: &str) -> Option<String> {
let file = scratch.file("case.js", source);
let output = std::process::Command::new(oracle)
.arg("-c")
.arg("-o")
.arg(scratch.0.join("out.c"))
.arg(&file)
.output()
.expect("run reference compiler");
if !output.status.success() {
return None;
}
Some(String::from_utf8_lossy(&output.stdout).into_owned())
}
fn normalise(text: &str, path: &str) -> String {
text.replace(path, "<file>")
}
fn ours(source: &str, path: &str) -> Result<String, String> {
let program = parse_script(source).map_err(|e| e.to_string())?;
let mut function = compile_script(&program).map_err(|e| e.to_string())?;
set_filename(&mut function, "<file>");
let _ = path;
Ok(disassemble(&function))
}
fn set_filename(function: &mut js_compile::Function, name: &str) {
function.filename = Some(name.to_string());
for constant in &mut function.cpool {
if let js_compile::bytecode::Constant::Function(inner) = constant {
set_filename(inner, name);
}
}
}
fn compare(cases: &[&str]) -> (Vec<String>, usize) {
let Some(oracle) = oracle() else {
return (Vec::new(), 0);
};
let scratch = Scratch::new("diff");
let mut failures = Vec::new();
let mut compared = 0;
for source in cases {
let Some(reference) = reference_disassembly(&oracle, &scratch, source) else {
let path = scratch.0.join("case.js");
let path = path.to_str().expect("utf-8 path");
if ours(source, path).is_ok() {
failures.push(format!(
" {source:?}\n the reference rejects this, but we compile it"
));
}
continue;
};
let path = scratch.0.join("case.js");
let path = path.to_str().expect("utf-8 path");
let reference = normalise(&reference, path);
compared += 1;
match ours(source, path) {
Err(err) => failures.push(format!(" {source:?}\n we failed: {err}")),
Ok(actual) => {
if actual != reference {
failures.push(format!(" {source:?}\n{}", diff(&reference, &actual)));
}
}
}
}
(failures, compared)
}
fn diff(expected: &str, actual: &str) -> String {
let expected_lines: Vec<&str> = expected.lines().collect();
let actual_lines: Vec<&str> = actual.lines().collect();
let mut out = String::from(" reference | ours\n");
let mut shown = 0;
for index in 0..expected_lines.len().max(actual_lines.len()) {
let e = expected_lines.get(index).copied().unwrap_or("<missing>");
let a = actual_lines.get(index).copied().unwrap_or("<missing>");
if e == a {
if shown > 0 && index + 1 < expected_lines.len().max(actual_lines.len()) {
out.push_str(&format!(" {e:<38} | {a}\n"));
shown += 1;
}
continue;
}
out.push_str(&format!(" > {e:<38} | {a}\n"));
shown += 1;
if shown > 12 {
out.push_str(" …\n");
break;
}
}
out
}
#[test]
fn literals_match_the_reference() {
require_oracle!();
let cases = [
"1;",
"0;",
"-1;",
"7;",
"8;",
"100;",
"-128;",
"127;",
"1000;",
"1000000;",
"1.5;",
"0.1;",
"true;",
"false;",
"null;",
"\"s\";",
"\"\";",
"\"two words\";",
"undefined;",
];
assert_cases("literals_match_the_reference", &cases);
}
#[test]
fn operators_match_the_reference() {
require_oracle!();
let cases = [
"a + b;",
"a - b;",
"a * b;",
"a / b;",
"a % b;",
"a ** b;",
"a << b;",
"a >> b;",
"a >>> b;",
"a & b;",
"a | b;",
"a ^ b;",
"a < b;",
"a <= b;",
"a > b;",
"a >= b;",
"a == b;",
"a != b;",
"a === b;",
"a !== b;",
"a in b;",
"a instanceof b;",
"-a;",
"+a;",
"!a;",
"~a;",
"typeof a;",
"void a;",
"a && b;",
"a || b;",
"a ? b : c;",
"a, b;",
"(a, b, c);",
];
assert_cases("operators_match_the_reference", &cases);
}
#[test]
fn variables_match_the_reference() {
require_oracle!();
let cases = [
"var x;",
"var x = 1;",
"var x = 1, y = 2;",
"let x = 1;",
"const x = 1;",
"x = 1;",
"x += 1;",
"x -= 1;",
"x *= 2;",
"x++;",
"x--;",
"++x;",
"--x;",
"var x = 1; x;",
"function f() { var x = 1; return x; }",
"function f(a) { return a; }",
"function f(a, b) { return a + b; }",
"function f() { return 1; } f();",
];
assert_cases("variables_match_the_reference", &cases);
}
#[test]
fn parameter_scopes_match_the_reference() {
require_oracle!();
let cases = [
"function f(a = 1){ return a }",
"function f(a, b = 2){ return a + b }",
"function f(a = 1, b){ return a + b }",
"function f(a = 1, ...r){ return r }",
"function f(a = a){ return a }",
"function f(x = y, y){ return x }",
"function f(a = 1){ return () => a }",
"function f(a = 1){ let q; return q }",
"function f(a = 1){ return arguments }",
"function f(a = 1, b = 2){ b = 3 }",
"function f(a = 1){ { let a = 2; } }",
"function f(...a){ return a }",
"function f(a, ...b){ return b }",
"function f({a}, b = 1){ return a + b }",
"function f(a = 1, {b}){ return a + b }",
"function f({a}, {b}) { return a + b }",
"(a = 1) => a",
"(a, b = 2) => a + b",
"function f(a = 1){ function g(){} return g }",
];
assert_cases("parameter_scopes_match_the_reference", &cases);
}
#[test]
fn unsupported_constructs_fail_loudly() {
let source = "function f(o, k){ var {[k]: a, ...r} = o; }";
let program = parse_script(source).expect("parses");
let error = compile_script(&program).expect_err(&format!("{source:?} should not compile yet"));
assert!(!error.message.is_empty(), "{source:?} needs a message");
}
#[test]
fn for_await_compiles_like_the_reference() {
let oracle = match oracle() {
Some(path) => path,
None => {
eprintln!("skipping: no disassembling qjsc found (run scripts/setup-oracle.sh)");
return;
}
};
let scratch = Scratch::new("for-await");
for source in [
"async function f(xs){ for await (const x of xs) ; }",
"async function f(xs){ for await (x of xs) { g(x); } }",
"async function f(xs){ for await (const x of xs) { break } }",
"async function f(xs){ for await (const x of xs) { return x } }",
] {
let program = parse_script(source).expect("parses");
let ours = disassemble(&compile_script(&program).expect("compiles"));
let reference = oracle_disassembly(&oracle, &scratch, source);
assert_eq!(
ours.trim_end(),
reference.trim_end(),
"{source:?} differs from the reference:\nours:\n{ours}\nreference:\n{reference}"
);
}
}
fn oracle_disassembly(oracle: &PathBuf, scratch: &Scratch, source: &str) -> String {
let path = scratch.file("case.js", source);
let output = std::process::Command::new(oracle)
.arg("-c")
.arg("-o")
.arg(scratch.0.join("out.c"))
.arg(&path)
.output()
.expect("run the oracle");
let text = String::from_utf8_lossy(&output.stdout).to_string();
text.lines()
.map(|line| match line.find(": ") {
Some(index) if line.starts_with(path.to_str().unwrap_or("")) => {
line[index + 2..].to_string()
}
_ => line.to_string(),
})
.collect::<Vec<_>>()
.join("\n")
}
#[test]
fn statements_match_the_reference() {
require_oracle!();
let cases = [
"if (a) b;",
"if (a) b; else c;",
"if (a) { b; } else { c; }",
"while (a) b;",
"return_ = 1;",
"a.b;",
"a[b];",
"a.b = 1;",
"a[0] = 1;",
"f();",
"f(1);",
"f(1, 2);",
"f(1, 2, 3);",
];
assert_cases("statements_match_the_reference", &cases);
}
#[test]
fn tdz_and_scopes_match_the_reference() {
require_oracle!();
let cases = [
"function f(){ let x = 1; return x; }",
"function f(){ let x; return x; }",
"function f(){ let x; x = 1; return x; }",
"function f(){ let x = 0; x = 1; return x; }",
"function f(){ let x = 0; x += 1; }",
"function f(){ let x = 0; x++; }",
"function f(){ const c = 1; return c; }",
"function f(){ return x; let x = 1; }",
"function f(){ g(); let x = 1; }",
"function f(){ { let x = 1; } return 0; }",
"function f(){ { let x = 1; return x; } }",
"function f(){ let a = 1; { let b = 2; return b; } }",
"function f(){ let a; { let b = a; } }",
"function f(){ { let a; let b; } }",
"function f(){ let a; let b; }",
"function f(){ let a; let b; let c; }",
"function f(){ { let a; } { let b; } }",
"function f(){ let a; { let b; { let c; } } }",
"function f(){ { { let c; } let b; } let a; }",
"function f(){ { let a = 1; } let b = 2; }",
"function f(){ let b = 2; { let a = 1; } }",
"function f(){ return v; var v = 1; }",
"function f(){ var v = 1; { let w = 2; } return v; }",
"function f(){ let x = 1; return function(){ return x; }; }",
"function f(){ let x = 0; return function(){ x += 1; }; }",
"function f(){ let x = 1; return function(){ x = 2; return x; }; }",
"function f(a){ return function(){ return a; }; }",
"function f(){ var v = 1; return function(){ return v; }; }",
"function f(){ function g(){} return g; }",
"function f(){ a; function g(){} }",
"g(); function f(){ return 1; }",
"function f(){ function g(){} function h(){} return h; }",
"function f(){ { let x = 1; g = function(){ return x; }; } }",
"function f(){ let a = 1; { let b = 2; g = function(){ return a + b; }; } }",
"function f(){ g(); }",
"var g; function f(){ g(); }",
"function f(){ function h(){ k(); } }",
"function f(){ g(); h(); }",
"function f(){ return 1; let x = 2; }",
"function f(){ return 1; var x = 2; }",
"function f(){ return; g(); }",
"function f(){ throw 1; g(); }",
"function f(){ const c = 1; return c; }",
"function f(){ const c = 1; c; return c; }",
"function f(){ let a = 1; var b = 2; }",
"function f(){ var b = 2; let a = 1; }",
"function f(){ { let a = 1; } var b = 2; }",
"function f(){ var b = 2; { let a = 1; } }",
"function f(){ let a = 1; function g(){} }",
"function f(){ let a; { let b; { let c; } } }",
"function f(){ { { let c; } let b; } let a; }",
"function f(){ let a; { let b; } let c; }",
];
assert_cases("tdz_and_scopes_match_the_reference", &cases);
}
#[test]
fn named_functions_and_captures_match_the_reference() {
require_oracle!();
let cases = [
"var f = function(){};",
"var f = function named(){};",
"g = function(){};",
"function k(){ var f = function(){}; }",
"function k(p){ return function(){ return p; }; }",
"function k(p){ var q = p; return function(){ return q + p; }; }",
"function k(){ var a = 1; var b = 2; return function(){ return a + b; }; }",
"function k(){ return function(){ return function(){ return 1; }; }; }",
"function k(){ var a = 1; return function(){ return function(){ return a; }; }; }",
"function k(){ { let a = 1; } return 0; }",
"function k(){ for (let i = 0; i < 1; i++); }",
"function k(){ for (;;) { let a = 1; } }",
"function k(){ { } }",
"function k(){ {} {} }",
"function k(){}",
"function k(a, b, c, d){ return d; }",
"function k(a, b, c, d, e){ return e; }",
];
assert_cases("named_functions_and_captures_match_the_reference", &cases);
}
#[test]
fn loops_and_labels_match_the_reference() {
require_oracle!();
let cases = [
"function f(){ for(;;){ break; } }",
"function f(){ for(;;){ continue; } }",
"function f(){ do { break; } while(1); }",
"function f(){ for(;;){ a(); break; b(); } c(); }",
"function f(){ do { a(); break; } while(b); c(); }",
"function f(c){ for(;;){ a(); if(c) break; b(); } c2(); }",
"function f(c){ while(c){ a(); if(c2) break; } c3(); }",
"function f(c){ for(let i=0;i<1;i++){ a(); if(c) continue; b(); } }",
"function f(c){ do { a(); if(c) continue; } while(c2); }",
"function f(c){ while(c){ a(); if(c2) continue; b(); } c3(); }",
"function f(c){ l: for(;;){ if(c) break l; a(); } c2(); }",
"function f(c){ l: for(;;){ if(c) continue l; a(); } c2(); }",
"function f(c){ outer: for(;;){ for(;;){ break outer; } } }",
"function f(c){ outer: for(;;){ for(;;){ continue outer; } } }",
"function f(c){ l: { a(); if(c) break l; b(); } }",
"function f(c){ l: { a(); if(c) break l; b(); } c2(); }",
"function f(c){ a: b: for(;;){ break a; } }",
"function f(c){ a: b: for(;;){ break b; } }",
"function f(c){ a: b: for(;;){ continue a; } }",
"function f(c){ for(;;){ for(;;){ break; } a(); } }",
"function f(c){ for(;;){ for(;;){ continue; } a(); } }",
];
assert_cases("loops_and_labels_match_the_reference", &cases);
}
#[test]
#[ignore = "needs fold_constants and the resolve_labels jump rewrites"]
fn constant_conditions_and_dead_jumps_match_the_reference() {
require_oracle!();
let cases = [
"function f(){ while(1){ break; } }",
"function f(){ while(1){ a(); break; b(); } c(); }",
"function f(){ while(0){ a(); } c(); }",
"function f(){ if(1) a(); }",
"function f(){ if(0) a(); else b(); }",
"function f(){ for(;1;){ break; } }",
"function f(a,c){ return a.b(c); }",
"function f(a,c){ return g(c); }",
"function f(a,c){ (0, a.b)(c); }",
"function f(a,c){ (0, g)(c); }",
"function f(c){ while(c){ for(;;){ break; } } }",
"function f(){ for(;;){ for(;;){ break; } } }",
"function f(){ for(;;){ for(;;){ break; } a(); } }",
"function f(c){ o: for(;;){ i: while(1){ break o; } } }",
"function f(c){ o: for(;;){ i: while(1){ break i; a(); } } }",
"function f(c){ o: for(;;){ i: while(1){ continue i; } } }",
"function f(c){ o: for(;;){ i: while(c2){ break o; } } }",
"function f(c){ o: for(;;){ i: while(c2){ break i; a(); } } }",
"function f(c){ o: for(;;){ i: while(c2){ continue i; } } }",
"function f(a){ return a.length; }",
"function f(){ return [1,2].length; }",
];
assert_cases(
"constant_conditions_and_dead_jumps_match_the_reference",
&cases,
);
}
#[test]
fn a_jump_target_after_a_store_stays_put() {
require_oracle!();
let cases = [
"function f(c){ var p=\"\"; function g(){ p=\"x\"; if(c){ p=\"a\"; } return p; } return g; }",
"function f(c){ var p=\"\"; function g(){ p=\"x\"; if(c){ p=\"a\"; } else { p=\"b\"; } return p; } return g; }",
"function f(n){ var p=\"\"; function g(){ switch(n){ case 1: p=\"a\"; break; default: p=\"b\"; } return p; } return g; }",
"function f(n){ var p=\"\"; function g(){ p=\"s\"; while(n-- > 0){ p+=\"w\"; } return p; } return g; }",
"function f(c){ var p; p=\"x\"; if(c){ p=\"a\"; } return p; }",
"function f(c){ var p; p=\"x\"; if(c){ p=\"a\"; } else { p=\"b\"; } return p; }",
];
assert_cases("a_jump_target_after_a_store_stays_put", &cases);
}
#[test]
fn assignments_match_the_reference() {
require_oracle!();
let cases = [
"function f(v){ var a; a = v; }",
"function f(a,b){ var x = (a = b); }",
"function f(v){ g = v; }",
"function f(v){ a = b = v; }",
"function f(a,b){ a += b; }",
"function f(a,b){ var x = (a -= b); }",
"function f(a,b){ a *= b; }",
"function f(a,b){ a /= b; }",
"function f(a,b){ a %= b; }",
"function f(a,b){ a **= b; }",
"function f(a,b){ a <<= b; }",
"function f(a,b){ a >>= b; }",
"function f(a,b){ a >>>= b; }",
"function f(a,b){ a &= b; }",
"function f(a,b){ a |= b; }",
"function f(a,b){ a ^= b; }",
"function f(a,b){ a ||= b; }",
"function f(a,b){ a &&= b; }",
"function f(a,b){ a ??= b; }",
"function f(a,b){ var x = (a ||= b); }",
"function f(b){ var x = (g ||= b); }",
"function f(o,p,v){ o.p = v; }",
"function f(o,v){ var x = (o.a = v); }",
"function f(o,k,v){ o[k] = v; }",
"function f(o,k,v){ var x = (o[k] = v); }",
"function f(o,b){ o.a += b; }",
"function f(o,b){ var x = (o.a += b); }",
"function f(o,k,b){ o[k] += b; }",
"function f(o,k,b){ var x = (o[k] += b); }",
"function f(o,b){ o.a -= b; c(); }",
"function f(o,b){ o.a ||= b; c(); }",
"function f(o,b){ o.a &&= b; c(); }",
"function f(o,b){ o.a ??= b; c(); }",
"function f(o,k,b){ o[k] ||= b; c(); }",
"function f(o,k,b){ o[k] ??= b; c(); }",
"function f(o,k,b){ var x = (o[k] ||= b); }",
];
assert_cases("assignments_match_the_reference", &cases);
}
#[test]
fn calls_pass_their_receiver() {
require_oracle!();
let cases = [
"function f(a,c){ a.b(c); }",
"function f(a){ a.b(); }",
"function f(a,c){ a.b(c,d); }",
"function f(a,c){ (a.b)(c); }",
"function f(a,c){ ((a.b))(c); }",
"function f(a,c){ a.b.c(d); }",
"function f(a,c){ a.b(c)(d); }",
"function f(a,c){ a.b()(c); }",
"function f(a,c){ a[k](c); }",
"function f(a,c){ a[\"b\"](c); }",
"function f(a,c){ a.b[k](c); }",
"function f(a,c){ g(c); }",
"function f(a,c){ a(c); }",
"function f(a,c){ (g)(c); }",
"function f(a,c){ a?.b(c); }",
"function f(a,c){ a?.[k](c); }",
];
assert_cases("calls_pass_their_receiver", &cases);
}
#[test]
fn nullish_and_optional_chains_match_the_reference() {
require_oracle!();
let cases = [
"function f(a,b){ return a ?? b; }",
"function f(a,b,c){ return a ?? b ?? c; }",
"function f(a,b){ return a || b; }",
"function f(a,b,c){ return a || b || c; }",
"function f(a,b){ return (a || b) && c; }",
"function f(a,b){ return a ?? b || c; }",
"function f(a,b){ if (a ?? b) c(); }",
"function f(a){ return a?.b; }",
"function f(a){ return a?.b.c; }",
"function f(a){ return a.b?.c; }",
"function f(a){ return (a?.b).c; }",
"function f(a){ return a?.[k]; }",
"function f(a){ return a?.[k]?.l; }",
"function f(a){ return a?.b?.[k]; }",
"function f(a){ return a?.(); }",
"function f(a){ return a?.b(); }",
"function f(a){ return a?.[k](); }",
"function f(a){ return a?.(); }",
"function f(a){ var x = a?.b; }",
"function f(a){ return a?.b + 1; }",
"function f(a){ if (a?.b) c(); }",
];
assert_cases("nullish_and_optional_chains_match_the_reference", &cases);
}
#[test]
fn switch_matches_the_reference() {
require_oracle!();
let cases = [
"function f(x){ switch(x){ } }",
"function f(x){ switch(x){ case 1: a(); } }",
"function f(x){ switch(x){ case 1: } }",
"function f(x){ switch(x){ case 1: a(); case 2: b(); } }",
"function f(x){ switch(x){ case 1: a(); break; } }",
"function f(x){ switch(x){ case 1: a(); break; case 2: b(); break; } }",
"function f(x){ switch(x){ default: b(); } }",
"function f(x){ switch(x){ case 1: a(); break; default: b(); } }",
"function f(x){ switch(x){ default: b(); case 2: c(); } }",
"function f(x){ switch(x){ case 1: case 2: a(); } }",
"function f(x){ switch(x){ case 1: case 2: case 3: a(); break; } }",
"function f(x){ switch(x){ case 1: case 2: a(); case 3: b(); } }",
"function f(x){ switch(x){ case 1: a(); default: b(); case 2: c(); } }",
"function f(x){ switch(x){ case 1: default: b(); } }",
"function f(x){ switch(x){ case 1: case 2: } }",
"function f(x){ switch(x){ case 1: case 2: case 3: } }",
"function f(x){ switch(x.y){ case 1: return 1; } }",
"function f(x){ switch(x){ case 1: return; default: b(); } }",
];
assert_cases("switch_matches_the_reference", &cases);
}
#[test]
fn try_catch_finally_matches_the_reference() {
require_oracle!();
let cases = [
"function f(){ try { a(); } catch(e) { b(e); } }",
"function f(){ try { a(); } catch { b(); } }",
"function f(){ try { a(); } catch(e) { } }",
"function f(){ try { a(); } finally { c(); } }",
"function f(){ try { a(); } finally { c(); } d(); }",
"function f(){ try { a(); } catch(e) { b(e); } finally { c(); } }",
"function f(){ try { a(); } catch(e) { b(e); } finally { c(); } d(); }",
"function f(){ try { a(); throw x; } catch(e) { b(e); } }",
"function f(){ try { a(); } catch(e) { throw e; } }",
"function f(){ try { let x = 1; a(x); } catch(e) { let y = 2; b(e, y); } }",
"function f(){ try { a(); } catch(e) { b(); } finally { let z = 3; c(z); } }",
"function f(){ try { try { a(); } catch(e) { b(e); } } catch(e) { c(e); } }",
"function f(){ try { a(); } catch(e) { try { b(); } finally { c(); } } }",
"function f(){ try { return 1; } catch(e) { b(); } }",
"function f(){ try { return 1; } finally { c(); } }",
"function f(){ try { return 1; } catch(e) { b(); } finally { c(); } }",
"function f(){ try { return; } finally { c(); } }",
"function f(){ try { a(); return 1; } finally { c(); } }",
"function f(){ try { try { return 1; } finally { c(); } } finally { d(); } }",
"function f(y){ try { switch(y){ case 1: return 2; } } finally { c(); } }",
"function f(){ while(a){ try { break; } finally { c(); } } }",
"function f(){ while(a){ try { continue; } finally { c(); } } }",
"function f(){ while(a){ try { if(b) break; d(); } finally { c(); } } e(); }",
"function f(){ o: while(a){ try { try { break o; } finally { c(); } } finally { d(); } } e(); }",
"function f(){ try { a(); } finally { return 1; } }",
];
assert_cases("try_catch_finally_matches_the_reference", &cases);
}
#[test]
fn for_in_of_matches_the_reference() {
require_oracle!();
let cases = [
"function f(o){ for (var k in o) a(k); }",
"function f(o){ for (var v of o) a(v); }",
"function f(o){ for (let k in o) a(k); }",
"function f(o){ for (const v of o) a(v); }",
"function f(o){ for (var k in o) { break; } a(); }",
"function f(o){ for (var v of o) { break; } a(); }",
"function f(o){ for (var k in o) { a(); break; } b(); }",
"function f(o){ for (var v of o) { if (v) break; } a(); }",
"function f(o){ for (var k in o) { if (k) continue; a(); } b(); }",
"function f(o){ for (var v of o) { if (v) continue; a(); } b(); }",
"function f(x){ o: while(c){ for (var k in x){ break o; } } a(); }",
"function f(x){ o: while(c){ for (var v of x){ break o; } } a(); }",
"function f(x){ o: while(c){ for (var k in x){ continue o; } } a(); }",
"function f(x){ o: while(c){ for (var v of x){ continue o; } } a(); }",
"function f(o){ for (var k in o) return k; }",
"function f(o){ for (var v of o) return v; }",
"function f(o){ for (var v of o) { if (v) return 1; } }",
"function f(o){ for (var k in o) { if (k) return; } }",
"function f(o){ for (var k in o) for (var v of o) a(k, v); }",
"function f(o){ try { for (var v of o) a(v); } finally { b(); } }",
"function f(o){ for (const [x] of o) a(x); }",
"function f(o){ for (var [x, y] in o) a(x, y); }",
"function f(o, t){ for (t[0] in o) a(t); }",
"function f(o, t){ for (t.k of o) a(t); }",
];
assert_cases("for_in_of_matches_the_reference", &cases);
}
const CLASS_CASES: &[&str] = &[
"class C {}",
"class C extends B {}",
"class C { constructor() {} }",
"class C extends B { constructor() { super() } }",
"class C { m() { return 1 } }",
"class C { static m() { return 1 } }",
"class C { get x() { return 1 } set x(v) {} }",
"class C { static get x() { return 1 } }",
"class C { *g() { yield 1 } }",
"class C { async m() {} }",
"class C { ['m']() {} }",
"class C { [k]() {} }",
"class C { ['x'] = 1 }",
"class C { [k] = 1 }",
"class C { x }",
"class C { x = 1 }",
"class C { static x = 1 }",
"class C { x = 1; y = 2 }",
"class C { static x = 1; static y = 2 }",
"class C { x = () => this }",
"(class {})",
"(class Named {})",
"var a = class {};",
"var a = class Named {};",
"class C { m() { return this } }",
"class C { m() { return super.toString() } }",
"function f() { class C { m() { return f } } }",
"class C { static { this.x = 1 } }",
"class C { static a = 1; static { this.b = 2 } }",
"class C { #x }",
"class C { #x = 1 }",
"class C { static #x = 1 }",
"class C { static #m() {} }",
"class C { #m() {} static x = 1 }",
"class C { #m() {} static { this.y = 1 } }",
"class C extends B { #m() {} }",
"class C { get #a() { return 1 } #b = 1 }",
"class C { #x = 1; constructor() { this.#x = 2 } }",
];
#[test]
#[ignore = "four known divergences, none about private members; see the doc comment"]
fn classes_match_the_reference() {
require_oracle!();
assert_class_cases("classes_match_the_reference", CLASS_CASES);
}
#[test]
fn private_member_shapes_match_the_reference() {
require_oracle!();
let cases = [
"class C { #m() {} }",
"class C { get #a() { return 1 } }",
"class C { set #a(v) {} }",
"class C { get #a() { return 1 } set #a(v) {} }",
"class C { #m() {} #n() {} }",
"class C { #m() {} #x = 1 }",
"class C { #x = 1; #m() {} }",
"class C { #m() {} get #a() { return 1 } set #a(v) {} }",
"class C { #m() { return 1 } #g = 2; get #a() { return 3 } set #a(v) {} }",
"class C { #m() {} ['a'] = 1 }",
"class C { #m() {} constructor() {} }",
"class C { #m() {}; m() {} }",
];
assert_class_cases("private_member_shapes_match_the_reference", &cases);
}
#[test]
fn generators_match_the_reference() {
require_oracle!();
let cases = [
"function* g(){}",
"function* g(){ yield 1 }",
"function* g(){ return 1 }",
"function* g(){ yield 1; return 2 }",
"function* g(){ var a = yield 1; return a }",
"async function* g(){}",
"async function* g(){ yield 1 }",
"async function* g(){ return 1 }",
"async function* g(){ yield 1; return 2 }",
"function* g(){ yield 1 }",
"function* g(){ yield* a }",
"function* g(){ yield* a; return 1 }",
"async function* g(){ yield* a }",
"async function* g(){ yield* a; return 1 }",
"async function* g(){ yield* a; yield* b }",
"async function* g(){ try { yield* a } finally { } }",
"async function* g(){ for await (const x of a) { } }",
"async function* g(){ for await (const x of a) { break } }",
"function* g(){ for (const x of a) { } }",
];
assert_cases("generators_match_the_reference", &cases);
}
#[test]
fn literals_build_in_place() {
require_oracle!();
let cases = [
"function f(){ return {}; }",
"function f(){ return {a:1}; }",
"function f(){ return {a:1, a:2}; }",
"function f(a){ return {a}; }",
"function f(){ return {a:1, b:2, c:3}; }",
"function f(p){ return {__proto__:p}; }",
"function f(){ return {\"__proto__\":x}; }",
"function f(){ return {__proto__}; }",
"function f(){ return {m(){return 1}}; }",
"function f(){ return {get a(){return 1}}; }",
"function f(){ return {set a(v){}}; }",
"function f(){ return {a:function(){}}; }",
"function f(){ return {a:function b(){}}; }",
"function f(k){ return {[k]:1}; }",
"function f(){ return {[\"a\"]:1}; }",
"function f(k){ return {[k](){}}; }",
"function f(k){ return {get [k](){return 1}}; }",
"function f(k){ return {[k]:function(){}}; }",
"function f(b){ return {...b}; }",
"function f(){ return {a:1, ...b, c:2}; }",
"function f(){ return {...a, ...b}; }",
"function f(x){ return {2:x}; }",
"function f(x){ return {\"x\":1, 2:3}; }",
"function f(x){ return {2147483647:x}; }",
"function f(x){ return {2147483648:x}; }",
"function f(x){ return {\"007\":x}; }",
"function f(x){ return {\"1e2\":x}; }",
"function f(x){ return {1e2:x}; }",
"function f(x){ return {0x10:x}; }",
"function f(x){ return {1.5:x}; }",
"function f(x){ return {4294967296:x}; }",
"function f(x){ return {0:x}; }",
"function f(x){ return {\"10\":x}; }",
"function f(){ return []; }",
"function f(){ return [1,2,3]; }",
"function f(b){ return [1,...b]; }",
"function f(){ return [1,,3]; }",
"function f(){ return [,,]; }",
"function f(){ return [1,]; }",
"function f(){ return [...a, ...b]; }",
"function f(){ return {a:[1,{b:2}]}; }",
"function f(){ return [{a:1},{b:2}]; }",
"function f(){ return {a:{b:{c:1}}}; }",
"function f(){ return [[[1]]]; }",
"function f(){ return [,,1]; }",
"function f(){ return [,,,...a]; }",
"function f(){ return [1,,...a]; }",
"function f(){ return [...a,,1]; }",
"function f(){ return [1,...,,]; }",
"function f(){ var a = [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]; }",
"function f(){ var a = [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]; }",
"function f(o){ return o[{a:1}]; }",
"function f(){ for (var k in [1,2]) a(k); }",
"function f(){ return {a:1}.a; }",
"function f(){ g({a:1}, [2]); }",
"function f(){ return typeof {}; }",
];
assert_cases("literals_build_in_place", &cases);
}
fn compare_by_function(cases: &[&str]) -> (Vec<String>, usize) {
let Some(oracle) = oracle() else {
return (Vec::new(), 0);
};
let scratch = Scratch::new("functions");
let mut failures = Vec::new();
let mut compared = 0;
for source in cases {
let path = scratch.0.join("case.js");
let path = path.to_str().expect("utf-8 path").to_string();
let Some(reference) = reference_disassembly(&oracle, &scratch, source) else {
if ours(source, &path).is_ok() {
failures.push(format!(
" {source:?}\n the reference rejects this, but we compile it"
));
}
continue;
};
let reference = normalise_function_constants(&normalise(&reference, &path));
compared += 1;
let actual = match ours(source, &path) {
Err(err) => {
failures.push(format!(" {source:?}\n we failed: {err}"));
continue;
}
Ok(actual) => actual,
};
let mut expected = functions_by_header(&reference);
let mut seen = 0;
for (header, body) in functions_by_header(&actual) {
seen += 1;
match expected.remove(&header) {
Some(expected_body) if expected_body == body => {}
Some(expected_body) => failures.push(format!(
" {source:?}\n function {header} differs:\n{}",
diff(&expected_body, &body)
)),
None => failures.push(format!(
" {source:?}\n we compiled a function the reference did not: {header}"
)),
}
}
let _ = seen;
for header in expected.keys() {
failures.push(format!(
" {source:?}\n the reference compiled a function we did not: {header}"
));
}
}
(failures, compared)
}
fn normalise_function_constants(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for line in text.lines() {
let trimmed = line.trim_start();
match trimmed.split_once(": [bytecode") {
Some((head, tail))
if head.starts_with("push_const8 ") || head.starts_with("push_const ") =>
{
let indent = &line[..line.len() - trimmed.len()];
let number = head.split_whitespace().nth(1).unwrap_or("0");
let short = head.starts_with("push_const8");
let opcode = if short { "fclosure8" } else { "fclosure" };
out.push_str(indent);
out.push_str(&format!("{opcode} {number}: [bytecode{tail}"));
}
_ => out.push_str(line),
}
out.push('\n');
}
out
}
fn functions_by_header(disassembly: &str) -> std::collections::BTreeMap<String, String> {
let mut out = std::collections::BTreeMap::new();
let mut header = String::new();
let mut body = String::new();
for line in disassembly.lines() {
if line.contains(": function: ") && !line.starts_with(' ') {
if !header.is_empty() {
out.insert(std::mem::take(&mut header), std::mem::take(&mut body));
}
header = line.to_string();
continue;
}
if header.is_empty() {
continue;
}
body.push_str(line);
body.push('\n');
}
if !header.is_empty() {
out.insert(header, body);
}
out
}
fn assert_class_cases(name: &str, cases: &[&str]) {
let (failures, compared) = compare_by_function(cases);
report(name, cases, failures, compared);
}
fn assert_cases(name: &str, cases: &[&str]) {
let (failures, compared) = compare(cases);
report(name, cases, failures, compared);
}
fn report(name: &str, cases: &[&str], failures: Vec<String>, compared: usize) {
if let Some(dir) = std::env::var_os("QUICKJS_CASE_COUNTS") {
use std::io::Write;
static COUNTS_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _guard = COUNTS_LOCK
.lock()
.unwrap_or_else(|poison| poison.into_inner());
let line = format!("{name}\t{}\t{compared}\n", cases.len());
let mut file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(std::path::Path::new(&dir).join("disasm-cases.txt"))
.expect("open the case-count file");
file.write_all(line.as_bytes())
.expect("write the case count");
}
assert!(
failures.is_empty(),
"{} of {} cases differ from the reference:\n{}",
failures.len(),
cases.len(),
failures.join("\n")
);
}