mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
JavaScript: generator functions, yield and yield* in ES2015 realms
A realm made with Options::es2015 reads function* declarations and expressions, *name() methods in object literals, yield and yield*, as ECMA-262 11th edition sections 14.4 and 25.4 define them. A suspended generator keeps its frame in its object; next, return and throw on %GeneratorPrototype% push it back on the interpreter's frame stack, so a resumption takes fuel and counts against the frame limit, and return runs the finally blocks around the yield. Resuming a running generator throws a TypeError. yield* passes next, throw and return on to its iterator and closes one without throw before throwing a TypeError. %GeneratorFunction% is reached through a generator function's constructor. Outside generators, sloppy code still reads yield as a name, and a realm without the option reads function* as a syntax error. An engine that #targetengine kept from a .jsx run has no generator built-ins, so a generator function called there throws a TypeError. cargo xtask test262 runs the generator folders: 509 of 549 runs pass (94 of 96 before). Docs: docs/mcp.md, docs/target-app-parity.md.
This commit is contained in:
1 parent
f1f0911ddb
commit
30eb12aba3
20 files changed
+1312
-53
No files matched your search
@@ -331,6 +331,33 @@ fn a_js_script_reads_ecmascript_2015_and_a_jsx_script_does_not() {
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_js_script_runs_generators_and_a_jsx_script_does_not_read_them() {
|
||||
let dir = fresh_folder("generators");
|
||||
let write = |name: &str, text: &str| {
|
||||
let p = dir.join(name);
|
||||
std::fs::write(&p, text).unwrap();
|
||||
p.to_string_lossy().into_owned()
|
||||
};
|
||||
let mut s = two_docs();
|
||||
let path = |p: &str| json!({"path": p});
|
||||
let source = "function* ids(n) { for (let i = 0; i < n; i++) yield `id${i}`; }\n\
|
||||
function* all() { yield* ids(2); yield* [vc.document ? 'doc' : 'none']; return 'end'; }\n\
|
||||
const it = all(); let s;\nwhile (!(s = it.next()).done) vc.log(s.value);\nvc.log(s.value);\n";
|
||||
let r = s.execute("script.run", &path(&write("gen.js", source))).unwrap();
|
||||
assert_eq!(r["log"], json!(["id0", "id1", "doc", "end"]), "{r}");
|
||||
let e = s.execute("script.run", &path(&write("gen.jsx", "function* g() {}"))).unwrap_err().to_string();
|
||||
assert!(e.contains("SyntaxError"), "{e}");
|
||||
// `yield` stays a name in a `.jsx` script.
|
||||
let r = s.execute("script.run", &path(&write("name.jsx", "var yield = 4; vc.log(yield);"))).unwrap();
|
||||
assert_eq!(r["log"], json!([4]), "{r}");
|
||||
// An engine a `.jsx` run made has no generator built-ins for a later `.js` run.
|
||||
s.execute("script.run", &path(&write("keep.jsx", "#targetengine genkept\nvar a = 1;"))).unwrap();
|
||||
let e = s.execute("script.run", &path(&write("keep.js", "#targetengine genkept\nfunction* g() {}\ng();"))).unwrap_err().to_string();
|
||||
assert!(e.contains("TypeError"), "{e}");
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
/// A new empty folder for test `name`.
|
||||
fn fresh_folder(name: &str) -> std::path::PathBuf {
|
||||
let dir = std::env::temp_dir().join(format!("vc-script-run-{name}-{}", std::process::id()));
|
||||
|
||||
Reference in new issue
Block a user