mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-25 20:01:07 +01:00
JavaScript: spread tests follow the iterator protocol
Since #9138, spread steps the array's iterator, so spreading a long array ends at the fuel limit instead of an early RangeError. Three tests in es2015_tests.rs still expected the by-index RangeError and failed: - an_index_setter_without_a_getter_does_not_delay_the_spread_s_check - a_prototype_getter_behind_an_own_element_does_not_delay_the_spread_s_check - looking_for_index_getters_takes_fuel_for_each_property The setter and prototype-getter tests now check the fuel limit and that the setter or getter did not run. The test of the index-getter scan, which #9138 removed, is deleted. docs/target-app-parity.md had two ES2015 entries after #9138. The by-index entry is removed; the entry that describes iterator spread stays.
This commit is contained in:
1 parent
0d27b5e579
commit
382e40dce1
2 files changed
+15
-24
No files matched your search
@@ -613,26 +613,32 @@ fn an_array_s_spread_is_checked_against_the_elements_it_reads() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_index_setter_without_a_getter_does_not_delay_the_spread_s_check() {
|
||||
// A setter does not run when the elements are read, so the array is still checked when
|
||||
// spread begins, on its own and on its prototype.
|
||||
fn an_index_setter_without_a_getter_does_not_run_when_the_array_is_spread() {
|
||||
// The iterator reads the element and the new array defines its own, so a setter on the array
|
||||
// or on its prototype does not run, and the long array ends at the fuel limit.
|
||||
for setter in ["a", "Array.prototype"] {
|
||||
let mut realm = Realm::with_options(es_options(), Limits { fuel: 10_000, ..Limits::default() });
|
||||
let src = format!("var a = []; a.length = 4294967295; Object.defineProperty({setter}, 0, {{ set: function () {{}} }}); [...a]");
|
||||
let src = format!(
|
||||
"var ran = false; var a = []; a.length = 4294967295; Object.defineProperty({setter}, 0, {{ set: function () {{ ran = true; }} }}); [...a]"
|
||||
);
|
||||
let long = realm.run(&src);
|
||||
assert!(is_thrown(&long, "RangeError"), "{setter}: {long:?}");
|
||||
assert!(matches!(&long, Err(e) if e.kind == RunErrorKind::Limit(LimitKind::Fuel)), "{setter}: {long:?}");
|
||||
realm.set_limits(Limits::default());
|
||||
assert_eq!(realm.run("ran"), Ok(JsValue::Bool(false)), "{setter}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_prototype_getter_behind_an_own_element_does_not_delay_the_spread_s_check() {
|
||||
// The array's own element 0 is read instead of the getter on its prototype, so no getter can
|
||||
// shrink it, and it is checked when spread begins.
|
||||
fn a_prototype_getter_behind_an_own_element_is_not_read_by_the_spread() {
|
||||
// The array's own element 0 is read instead of the getter on its prototype, so the getter does
|
||||
// not shrink it, and the long array ends at the fuel limit.
|
||||
let mut realm = Realm::with_options(es_options(), Limits { fuel: 10_000, ..Limits::default() });
|
||||
let src = "var a = [7]; Object.defineProperty(Array.prototype, 0, { get: function () { a.length = 1; return 0; } }); \
|
||||
a.length = 4294967295; [...a]";
|
||||
let long = realm.run(src);
|
||||
assert!(is_thrown(&long, "RangeError"), "{long:?}");
|
||||
assert!(matches!(&long, Err(e) if e.kind == RunErrorKind::Limit(LimitKind::Fuel)), "{long:?}");
|
||||
realm.set_limits(Limits::default());
|
||||
assert_eq!(realm.run("a.length"), Ok(JsValue::Number(4294967295.0)));
|
||||
// A getter on the prototype with no own element in front of it is read, and shrinks the array.
|
||||
let shrink = "var a = []; a.length = 4294967295; \
|
||||
Object.defineProperty(Array.prototype, 0, { get: function () { a.length = 1; return 7; } }); \
|
||||
@@ -640,20 +646,6 @@ fn a_prototype_getter_behind_an_own_element_does_not_delay_the_spread_s_check()
|
||||
truth(shrink);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn looking_for_index_getters_takes_fuel_for_each_property() {
|
||||
// Each attempt to spread the long array looks at every one of its 10,000 elements, so each
|
||||
// takes at least that much fuel.
|
||||
let fuel = 10_000_000;
|
||||
let mut realm = Realm::with_options(es_options(), Limits { fuel, ..Limits::default() });
|
||||
let setup = "var a = []; for (var i = 0; i < 10000; i++) a[i] = i; a.length = 4294967295; 0";
|
||||
assert_eq!(realm.run(setup), Ok(JsValue::Number(0.0)));
|
||||
let attempt = "var ok = false; try { [...a]; } catch (e) { ok = e instanceof RangeError; } ok";
|
||||
assert_eq!(realm.run(attempt), Ok(JsValue::Bool(true)));
|
||||
let used = fuel - realm.interp().fuel;
|
||||
assert!(used >= 10_000, "{used}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spread_counts_each_index_name_against_the_heap() {
|
||||
// The elements' properties, with their index names and a string's characters, fit in the
|
||||
|
||||
@@ -1088,7 +1088,6 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- `vectorcraft-js` parses JavaScript as ECMAScript 5.1 defines it, with the legacy octal forms, block functions and lenient regular expressions of Annex B, into a syntax tree with source spans. A comma may follow the last argument of a call or `new` expression and the last parameter of a function other than a setter, as ECMAScript 2017 allows (https://tc39.es/ecma262/2017/#sec-left-hand-side-expressions, https://tc39.es/ecma262/2017/#sec-function-definitions); `f(,)` and `f(1,,)` give a `SyntaxError`. It reads the `#target`, `#include` and other preprocessor lines as directives without running them, and a script that breaks the grammar or an early-error rule gives a `SyntaxError` with its line and column. Nesting past the parser's depth limit (`Options::max_depth`, 200 by default) gives a `SyntaxError` instead of exhausting the stack. `cargo xtask test262` fetches the conformance suite at a pinned commit and parses its ES5 tests: 5,411 of 5,418 runs pass (the 7 others use `let` and `const`). This entry covers parsing; the next entry covers running scripts. Tests: `crates/js/src/tests.rs`, `crates/engine/tests/it/js_lexer_vs_scriptcheck.rs`.
|
||||
- `vectorcraft-js` identifiers, written out or as `\uXXXX` and `\u{...}` escapes, take the characters Unicode's `ID_Start` and `ID_Continue` properties allow (https://www.unicode.org/reports/tr31/), plus `$` and, at the start, `_`, as later ECMAScript editions define them (https://tc39.es/ecma262/#sec-names-and-keywords). `Other_ID_Start` characters such as U+2118 and `Other_ID_Continue` characters such as U+00B7 and the zero width non-joiner and joiner are accepted, and no `Pattern_Syntax` or `Pattern_White_Space` character is. Letter categories come from Unicode 17.0, so U+088F starts an identifier and U+1ACF may follow its first character; characters assigned after Unicode 17.0 are not yet identifier characters. Tests: `crates/js/src/tests.rs` (`identifier_characters_are_unicode_17_id_start_and_id_continue`, which checks every code point, `identifiers_take_the_characters_unicode_17_assigned`, `identifiers_take_the_other_id_start_and_continue_characters`, `identifiers_refuse_pattern_syntax_surrogates_and_bad_escapes`, `no_pattern_syntax_or_white_space_character_is_an_identifier_character`).
|
||||
- `vectorcraft-js`'s `Realm` runs ECMAScript 5.1 scripts: the parsed script compiles to verified bytecode that a non-recursive interpreter runs, with closures, `this`, `arguments`, `with`, direct and indirect `eval`, `new Function`, `try`/`catch`/`finally`, labeled `break` and `continue`, and `for`-`in` in insertion order, over a mark-and-sweep heap, with the built-ins the next two entries list. Every run is bounded by fuel, heap bytes, frames, nested host calls and an interrupt flag (smaller defaults in the wasm build), and a run that reaches a limit stops with a limit error that `try` cannot catch. `cargo xtask test262` also runs the suite's ES5 language tests that need only the built-ins present: 4,280 of 4,286 runs pass (4 of the others expect the completion values of later editions, and 2 expect a configurable function `length`). The app does not call it yet. Tests: `crates/js/src/interp_tests.rs`, `crates/js/tests/test262.rs`.
|
||||
- With `Options::es2015`, `vectorcraft-js` also reads and runs these ECMAScript 2015 forms (https://262.ecma-international.org/6.0/): `let` and `const` with block scope and their temporal dead zone (a read or write before the declaration runs throws a `ReferenceError`, and a write to a `const` throws a `TypeError` in any code), a new `let` binding for each iteration of a `for` or `for`-`in` loop, function declarations scoped to their block, arrow functions with the `this` and `arguments` of the code around them and no `prototype`, template literals and tagged templates with one frozen strings object for each site, default and rest parameters with an `arguments` object that does not follow the parameters, and spread in array literals, calls and `new`. Spread reads arrays by index, reading the length again before each element, strings and `String` objects by code point, and objects with a `length` property by index, and throws a `TypeError` for any other value; that rule is VectorCraft's own until iterators are added. Spread and template objects take fuel for each element and count against the heap limit, and nested arrows and templates count against `Options::max_depth`. `script.run` sets the option for a `.js` file, and `eval`, `Function`, `#include` files and `$.evalFile` read code the way the script does; a `.jsx` file and a `source` string stay ES5.1. Destructuring, `for`-`of`, classes, generators and modules are still syntax errors. `cargo xtask test262` reads the suite's tests of these forms without and with the option: 390 of 390 negative parse runs and 533 of 708 other runs pass with it (373 and 27 without it). Of the 175 failing runs, 151 do not parse because the tests also use forms this list leaves out (classes, method definitions, destructuring, `for`-`of`, trailing commas in parameter lists, and a later edition's rule for escapes in tagged templates), and 24 expect function `name` properties, a configurable function `length` and the `caller` and `arguments` restrictions, ECMAScript 2015 rules that `vectorcraft-js` does not implement. Tests: `crates/js/src/es2015_tests.rs`, `crates/js/tests/test262.rs`, `crates/engine/src/tests/script_run.rs`.
|
||||
- With `Options::es2015`, `vectorcraft-js` also reads and runs these ECMAScript 2015 forms (https://262.ecma-international.org/6.0/): `let` and `const` with block scope and their temporal dead zone (a read or write before the declaration runs throws a `ReferenceError`, and a write to a `const` throws a `TypeError` in any code), a new `let` binding for each iteration of a `for` or `for`-`in` loop, function declarations scoped to their block, arrow functions with the `this` and `arguments` of the code around them and no `prototype`, template literals and tagged templates with one frozen strings object for each site, default and rest parameters with an `arguments` object that does not follow the parameters, and spread in array literals, calls and `new`. Spread uses the iterator protocol (see the `for`-`of` line below). Spread and template objects take fuel for each element and count against the heap limit, and nested arrows and templates count against `Options::max_depth`. `script.run` sets the option for a `.js` file, and `eval`, `Function`, `#include` files and `$.evalFile` read code the way the script does; a `.jsx` file and a `source` string stay ES5.1. Classes, generators and modules are still syntax errors. `cargo xtask test262` reads the suite's tests of these forms without and with the option: when these forms were added, 390 of 390 negative parse runs and 565 of 589 other runs passed with it (373 and 27 without it), and the 24 failing runs expected function `name` properties, a configurable function `length` and the `caller` and `arguments` restrictions. The object-literal line below adds function names and configurable lengths; the `caller` and `arguments` restrictions are still not implemented. Tests: `crates/js/src/es2015_tests.rs`, `crates/js/tests/test262.rs`, `crates/engine/src/tests/script_run.rs`.
|
||||
- `Options::es2015` also reads and runs two ECMAScript 2020 forms (https://262.ecma-international.org/11.0/): optional chains (`a?.b`, `a?.[key]`, `f?.(args)` and the accesses and calls after them), which evaluate their base once and give `undefined` without evaluating the rest of the chain when a value before `?.` is `undefined` or `null`, keep the object as `this` for `a?.b()` and `a.b?.()`, make `eval?.(code)` an indirect eval and make `delete a?.b` true when `a` is `undefined` or `null`; and `??`, which keeps any left side other than `undefined` and `null`. `?.` before a digit reads as `?` and a number (`a?.5:1`). Mixing `??` with `&&` or `||` without parentheses, assigning to or updating an optional chain, a tagged template after one and `new a?.b()` are syntax errors. A skipped part of a chain takes no fuel, and a chain of any length compiles without recursion. Using the ECMAScript 2015 option for them, so `.js` files read them and `.jsx` files and `source` strings do not, is VectorCraft's own rule. With the suite's `expressions/optional-chaining` and `expressions/coalesce` tests that need only the forms above, `cargo xtask test262` reads 426 of 426 negative parse runs and 591 of 782 other runs as passing with the option (409 and 27 without it). Of the 191 failing runs, 175 are those the entry above names, and 16 do not parse because the tests also use method definitions, classes, `for`-`of` or `new.target`. Tests: `crates/js/src/es2020_tests.rs`, `crates/js/tests/test262.rs`.
|
||||
- `vectorcraft-js` provides the ECMAScript 5.1 built-ins `Object` with its property-descriptor functions, `create`, `keys`, `seal` and `freeze` (section 15.2), `Function.prototype.bind` (15.3.4.5), `Array` with every ES5 method (15.4), the error types (15.11), `Math` (15.8), `JSON` with revivers, replacers and indentation (15.12), and the global functions with Annex B's `escape` and `unescape` (15.1, B.2). The array methods work on array-like objects. A method visits only the indices that exist when its range is longer than 4,096 indices and more than 8 times the properties on the object and its prototypes, so `new Array(4294967295).join("")` ends at once; a shorter range is visited index by index, holes included, and each index takes fuel. `join` writes a cycle back to an array as an empty string, `JSON.stringify` throws a `TypeError` on a cycle, and deep `JSON` nesting uses no native stack. Every loop takes fuel and every result is checked against the heap limit. `cargo xtask test262` runs the suite's ES5 built-in tests that need only these built-ins: 6,798 of 6,866 runs pass (the 34 failing tests expect rules of later editions). It also fetches the `json2.js` polyfill at a pinned commit and checks that its `stringify` and `parse` agree with the built-in `JSON`. Tests: `crates/js/src/builtins_tests.rs`, `crates/js/tests/test262.rs`, `crates/js/tests/json2.rs`.
|
||||
|
||||
Reference in new issue
Block a user