use js_parse::{parse_module, parse_script, parse_suspensible_script};
use quickjs_conformance::{Engine, locate_oracle, locate_qjsc};
use std::path::{Path, PathBuf};
fn probe_for(case_path: &Path) -> String {
format!(
r#"var source_ = std.loadFile({case:?});
try {{
std.evalScript(source_, {{ async: true }});
std.puts("ok\n");
}} catch (e) {{
std.puts(String(e.message).split("\n")[0] + "\n");
}}
"#,
case = case_path.to_str().expect("utf-8 path")
)
}
struct TempDir(PathBuf);
impl TempDir {
fn new(tag: &str) -> TempDir {
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-await-{tag}-{}-{sequence}",
std::process::id()
));
std::fs::create_dir_all(&path).expect("create temp dir");
TempDir(path)
}
fn file(&self, name: &str, contents: &str) -> PathBuf {
let path = self.0.join(name);
std::fs::write(&path, contents).expect("write temp file");
path
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
struct Reference {
script: Option<String>,
module: Option<String>,
suspensible: Option<String>,
}
fn message_of(out: &quickjs_conformance::Output) -> Option<String> {
let text = if out.stderr.is_empty() {
&out.stdout
} else {
&out.stderr
};
let first = text.lines().next().unwrap_or("");
if first == "ok" {
return None;
}
(!first.is_empty()).then(|| first.trim_start_matches("SyntaxError: ").to_string())
}
fn reference(qjsc: &Engine, qjs: &Engine, dir: &TempDir, source: &str) -> Reference {
let script_path = dir.file("case.js", source);
let module_path = dir.file("case.mjs", source);
let out_path = dir.0.join("out.c");
let out = out_path.to_str().expect("utf-8 path");
let script = message_of(
&qjsc
.output(&["-c", "-o", out, script_path.to_str().expect("utf-8")])
.expect("script verdict"),
);
let module = message_of(
&qjsc
.output(&["-c", "-m", "-o", out, module_path.to_str().expect("utf-8")])
.expect("module verdict"),
);
let case = dir.file("susp.js", source);
let probe = dir.file("probe.js", &probe_for(&case));
let suspensible = message_of(
&qjs.output(&[
"--std",
"--no-unhandled-rejection",
probe.to_str().expect("utf-8"),
])
.expect("suspensible verdict"),
);
Reference {
script,
module,
suspensible,
}
}
fn ours(source: &str) -> (Option<String>, Option<String>, Option<String>) {
let script = parse_script(source).err().map(|e| e.message);
let module = parse_module(source).err().map(|e| e.message);
let suspensible = parse_suspensible_script(source).err().map(|e| e.message);
(script, module, suspensible)
}
const CORPUS: &[&str] = &[
"await 1",
"await;",
"await;",
"f(await)",
"[await]",
"x ? await : 1",
"await .x",
"var x = await 1",
"for await (const a of []) ;",
"function f(){ await 1 }",
"() => await 1",
"class A { m(){ await 1 } }",
"async function g(){ await 1 }",
"try { await 1 } catch {}",
"label: await 1",
"if (1) { await 1 }",
"var await = 1",
"function f(await){}",
"function f(){ var await = 1 }",
"async function g(){ var await = 1 }",
"await: 1",
"function f(){ await: 1 }",
"({ await: 1 })",
"x = { await: 1 }",
"x.await",
"function f(){ x.await }",
"await using y = z",
"await",
"x = await",
];
#[test]
fn await_is_an_operator_in_exactly_the_async_contexts() {
let Some(qjsc_path) = locate_qjsc() else {
eprintln!("skipping: no reference qjsc found (run scripts/setup-oracle.sh)");
return;
};
let Some(qjs_path) = locate_oracle() else {
eprintln!("skipping: no reference qjs found (run scripts/setup-oracle.sh)");
return;
};
let qjsc = Engine::new("qjsc", qjsc_path);
let qjs = Engine::new("qjs", qjs_path);
let dir = TempDir::new("contexts");
let mut disagreements = Vec::new();
for source in CORPUS {
let expected = reference(&qjsc, &qjs, &dir, source);
let (script, module, suspensible) = ours(source);
for (context, want, got) in [
("script", &expected.script, &script),
("module", &expected.module, &module),
("suspensible", &expected.suspensible, &suspensible),
] {
if want != got {
disagreements.push(format!(
" source: {source:?} ({context})\n reference: {want:?}\n ours: {got:?}"
));
}
}
}
assert!(
disagreements.is_empty(),
"{} of {} checks disagree:\n{}",
disagreements.len(),
CORPUS.len() * 3,
disagreements.join("\n")
);
}
#[test]
fn the_flag_does_not_make_the_body_a_function() {
for source in ["return 1", "new.target", "super()"] {
let plain = parse_script(source).err().map(|e| e.message);
let suspensible = parse_suspensible_script(source).err().map(|e| e.message);
assert_eq!(
plain, suspensible,
"{source:?} should be rejected identically with and without the async flag"
);
}
}