mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-06 14:03:16 -05:00
Scripts: .js scripts have Promise and run its jobs before the run ends
A script run from a .js file now has Promise as ECMA-262 11th edition section 25.6 defines it: the constructor, then, catch, finally, and Promise.resolve, reject, all, allSettled and race. .jsx scripts and scripts given as source keep the ES5.1 built-ins; a named engine gets Promise at the first .js run that names it and keeps it. - The callbacks run as jobs from one FIFO queue after the script's own code ends and before the run ends, sliced like the script, with no native recursion along a then chain. Each job takes 1 unit of fuel, so an endless chain stops at the fuel limit. - More than 1,048,576 waiting jobs end the run with a Jobs limit error. A run that fails, hits a limit or is stopped drops its waiting jobs and rolls back as before. - A promise still rejected with no handler when the run ends goes to the report's unhandledRejections (first 20) and unhandledRejectionCount, and to the Script Console with its file, line and column. - Until the language has iterators, all, allSettled and race take arrays, objects with a whole-number length and strings, and the statics always build plain Promise objects. Promise.any is not provided. cargo xtask test262 now runs the suite's built-ins/Promise tests that need only the built-ins present: 402 of 538 runs pass. Docs: docs/mcp.md, docs/target-app-parity.md.
This commit is contained in:
1 parent
db9acab4da
commit
c417aff8fb
14 files changed
+1765
-32
No files matched your search
@@ -689,3 +689,59 @@ fn script_error_that_passes_through_a_finally_block_is_at_its_throw() {
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_js_script_has_promise_and_a_jsx_script_does_not() {
|
||||
let base = folder("promise-realm");
|
||||
let main = base.join("main/main.js");
|
||||
write(&main, "vc.log(typeof Promise); Promise.resolve(2).then(function (v) { vc.log(v); }); vc.log(1);");
|
||||
let r = Session::new().execute("script.run", &json!({"path": main.to_string_lossy()})).unwrap();
|
||||
assert_eq!(r["log"], json!(["function", 1, 2]));
|
||||
assert!(r.get("unhandledRejections").is_none(), "{r}");
|
||||
let r = run(&base, "vc.log(typeof Promise);").unwrap();
|
||||
assert_eq!(r["log"], json!(["undefined"]));
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_named_engine_gets_promise_at_its_first_js_run_and_keeps_it() {
|
||||
let base = folder("promise-engine");
|
||||
let jsx = base.join("main/a.jsx");
|
||||
let js = base.join("main/b.js");
|
||||
let mut s = Session::new();
|
||||
let mut go = |path: &Path, text: &str| {
|
||||
write(path, text);
|
||||
s.execute("script.run", &json!({"path": path.to_string_lossy()})).unwrap()["log"].clone()
|
||||
};
|
||||
assert_eq!(go(&jsx, "#targetengine kept\nvar n = 1; vc.log(typeof Promise);"), json!(["undefined"]));
|
||||
assert_eq!(
|
||||
go(&js, "#targetengine kept\nvc.log(n); vc.log(typeof Promise); Promise.resolve(2).then(function (v) { vc.log(v); });"),
|
||||
json!([1, "function", 2])
|
||||
);
|
||||
assert_eq!(go(&jsx, "#targetengine kept\nvc.log(typeof Promise);"), json!(["function"]));
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unhandled_rejections_reach_the_report_and_the_console_with_their_file_and_line() {
|
||||
let base = folder("promise-rejections");
|
||||
write(&base.join("main/lib.jsxinc"), "var x = 1;\nPromise.reject(new RangeError('in lib'));\n");
|
||||
let main = base.join("main/main.js");
|
||||
write(&main, "var a = 1;\nPromise.reject(new Error('lost'));\n#include \"lib.jsxinc\"\nPromise.reject(3).catch(function () {});\n");
|
||||
let mut s = Session::new();
|
||||
let r = s.execute("script.run", &json!({"path": main.to_string_lossy()})).unwrap();
|
||||
let got: Vec<(String, u64, Option<bool>)> = r["unhandledRejections"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|j| (j["message"].as_str().unwrap().to_string(), j["line"].as_u64().unwrap(), j["file"].as_str().map(|f| f.ends_with("lib.jsxinc"))))
|
||||
.collect();
|
||||
assert_eq!(got, vec![("Error: lost".to_string(), 2, None), ("RangeError: in lib".to_string(), 2, Some(true))], "{r}");
|
||||
assert_eq!(r["unhandledRejectionCount"], 2);
|
||||
let c = s.execute("script.console", &json!({})).unwrap();
|
||||
let errors: Vec<&str> = c["lines"].as_array().unwrap().iter().filter(|l| l["kind"] == "error").filter_map(|l| l["text"].as_str()).collect();
|
||||
assert_eq!(errors.len(), 2, "{errors:?}");
|
||||
assert!(errors[0].starts_with("main.js:2:") && errors[0].ends_with(": unhandled promise rejection: Error: lost"), "{errors:?}");
|
||||
assert!(errors[1].contains("lib.jsxinc:2:"), "{errors:?}");
|
||||
let _ = std::fs::remove_dir_all(base);
|
||||
}
|
||||
Reference in new issue
Block a user