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> {
let Some(oracle) = oracle() else {
return Vec::new();
};
let scratch = Scratch::new("diff");
let mut failures = Vec::new();
for source in cases {
let Some(reference) = reference_disassembly(&oracle, &scratch, source) else {
continue;
};
let path = scratch.0.join("case.js");
let path = path.to_str().expect("utf-8 path");
let reference = normalise(&reference, path);
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
}
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(&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(&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(&cases);
}
#[test]
fn unsupported_constructs_fail_loudly() {
for source in [
"function f(){ try { a(); } catch ([e]) { } }",
"for (const [x] of xs) ;",
"async function f(xs){ for await (const x of xs) ; }",
"class A { }",
"function* g() { }",
"async function f() { }",
"let [a] = x;",
"with (a) b;",
] {
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 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(&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(&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(&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(&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(&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(&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(&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(&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(&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(&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(); } }",
];
assert_cases(&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(&cases);
}
fn assert_cases(cases: &[&str]) {
let failures = compare(cases);
assert!(
failures.is_empty(),
"{} of {} cases differ from the reference:\n{}",
failures.len(),
cases.len(),
failures.join("\n")
);
}