mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-04 21:12:57 -05:00
Merge pull request #10620 from dotCooCoo/fix/async-function-subclass
Scripts: a class can extend AsyncFunction and GeneratorFunction
This commit is contained in:
5 files changed
+50
-8
No files matched your search
@@ -194,6 +194,23 @@ fn async_function_prototype_has_its_to_string_tag() {
|
||||
assert_eq!(log(src), "AsyncFunction false false true [object AsyncFunction]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_class_extends_async_function_and_its_instances_inherit_from_the_class() {
|
||||
// CreateDynamicFunction takes the prototype from `new.target` (ECMA-262 2017 section
|
||||
// 19.2.1.1.1).
|
||||
let src = "var AF = Object.getPrototypeOf(async function () {}).constructor;\n\
|
||||
class X extends AF { tag() { return 'x'; } }\n\
|
||||
var f = new X('a', 'return a + 1');\n\
|
||||
log.push(Object.getPrototypeOf(f) === X.prototype, f instanceof AF, f.tag(), Object.prototype.toString.call(f));\n\
|
||||
f(1).then(function (v) { log.push(v); });";
|
||||
assert_eq!(log(src), "true true x [object AsyncFunction] 2");
|
||||
// The source is parsed before `new.target.prototype` is read.
|
||||
let order = "var AF = Object.getPrototypeOf(async function () {}).constructor;\n\
|
||||
var NT = new Proxy(function () {}, { get: function (t, k) { if (k === 'prototype') log.push('read'); return t[k]; } });\n\
|
||||
try { Reflect.construct(AF, ['(('], NT); } catch (e) { log.push(e instanceof SyntaxError); }";
|
||||
assert_eq!(log(order), "true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_await_stays_within_the_heap_limit() {
|
||||
// The activation, its two reactions, the derived promise and the suspended frame's bytes all
|
||||
|
||||
@@ -172,7 +172,15 @@ impl Interp {
|
||||
let i = &self.intrinsics;
|
||||
Ok([i.object_proto, i.function_proto, i.array_proto, i.string_proto, i.number_proto, i.boolean_proto, i.regexp_proto, i.date_proto]
|
||||
.contains(&p)
|
||||
|| i.error_protos.contains(&p))
|
||||
|| i.error_protos.contains(&p)
|
||||
|| self.dynamic_function_proto(p))
|
||||
}
|
||||
|
||||
/// Is `p` the prototype of the functions that `GeneratorFunction` or `AsyncFunction` makes?
|
||||
/// Both constructors take a subclass's prototype from `new.target` (ECMA-262 2017 sections
|
||||
/// 25.2.1.1 and 25.5.1.1, through CreateDynamicFunction in section 19.2.1.1.1).
|
||||
fn dynamic_function_proto(&self, p: ObjId) -> bool {
|
||||
self.symbols.objects.is_some_and(|o| p == o.generator_function_proto) || self.promises.async_function_proto == Some(p)
|
||||
}
|
||||
|
||||
/// `new.target`'s `prototype` when it is an object, otherwise `fallback` (section 9.1.14).
|
||||
@@ -367,13 +375,13 @@ impl Interp {
|
||||
return Err(self.type_error("the Promise executor is not a function"));
|
||||
}
|
||||
// `Number`, `String` and `Date` convert arguments before reading `prototype`
|
||||
// (sections 20.1.1.1, 21.1.1.1 and 20.4.2). `Function` and `GeneratorFunction`
|
||||
// convert and parse source first (sections 19.2.1.1.1 and 25.2.1.1).
|
||||
// (sections 20.1.1.1, 21.1.1.1 and 20.4.2). `Function`, `GeneratorFunction` and
|
||||
// `AsyncFunction` convert and parse source first (sections 19.2.1.1.1, 25.2.1.1
|
||||
// and 25.5.1.1).
|
||||
let converts_first = {
|
||||
let i = &self.intrinsics;
|
||||
intrinsic.is_some_and(|p| {
|
||||
[i.number_proto, i.string_proto, i.date_proto, i.function_proto].contains(&p)
|
||||
|| self.symbols.objects.is_some_and(|o| p == o.generator_function_proto)
|
||||
[i.number_proto, i.string_proto, i.date_proto, i.function_proto].contains(&p) || self.dynamic_function_proto(p)
|
||||
})
|
||||
};
|
||||
let fallback = intrinsic.unwrap_or(self.intrinsics.object_proto);
|
||||
|
||||
@@ -253,6 +253,23 @@ fn bad_generators_are_syntax_errors() {
|
||||
truth("function* g(a = function* () { yield 2; }, b = (c = function* () { yield 3; }) => c) { yield* b()(); } g().next().value === 3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_class_extends_generator_function_and_function() {
|
||||
// CreateDynamicFunction takes the prototype from `new.target` (ECMA-262 2015 section
|
||||
// 19.2.1.1.1).
|
||||
truth(
|
||||
"var GF = Object.getPrototypeOf(function* () {}).constructor;\n\
|
||||
class G extends GF { tag() { return 'g'; } }\n\
|
||||
var g = new G('yield 7');\n\
|
||||
Object.getPrototypeOf(g) === G.prototype && g instanceof GF && g.tag() === 'g' && g().next().value === 7",
|
||||
);
|
||||
truth(
|
||||
"class F extends Function { tag() { return 'f'; } }\n\
|
||||
var f = new F('a', 'return a * 2');\n\
|
||||
Object.getPrototypeOf(f) === F.prototype && f.tag() === 'f' && f(3) === 6",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn realms_without_es2015_have_no_generators() {
|
||||
let es5 = Options::default();
|
||||
|
||||
+2
-2
@@ -2838,7 +2838,7 @@ inside a method, accessor or class constructor, and `super(…)` inside a derive
|
||||
arrow function within one; elsewhere, through an indirect `eval` and in a `Function` body they are a `SyntaxError`
|
||||
(https://262.ecma-international.org/6.0/#sec-performeval). A
|
||||
class can extend a class, a function, `null` or the built-in constructors `Object`, `Function`, `Array`, `String`,
|
||||
`Number`, `Boolean`, `RegExp`, `Date` and the error types. A class can name another built-in constructor in
|
||||
`Number`, `Boolean`, `RegExp`, `Date`, the error types, `GeneratorFunction` and `AsyncFunction`. A class can name another built-in constructor in
|
||||
`extends`, but `new` on that class throws a `TypeError` when it reaches the built-in constructor, a rule that is
|
||||
VectorCraft's own. Spread,
|
||||
`for`-`of` and array patterns use the iterator protocol. A loop or pattern left early, by `break`, `return`, a
|
||||
@@ -2898,7 +2898,7 @@ its symbol property is nonwritable, nonenumerable and configurable.
|
||||
A failure during conversion or validation throws before the prototype getter runs. `Reflect.construct(Promise, args, newTarget)`
|
||||
rejects a missing or noncallable executor with `TypeError` before reading `newTarget.prototype`.
|
||||
For a callable executor, it reads that prototype before calling the executor; a throwing prototype getter prevents the call.
|
||||
`Function` and `GeneratorFunction` convert and parse their source arguments before reading `newTarget.prototype`.
|
||||
`Function`, `GeneratorFunction` and `AsyncFunction` convert and parse their source arguments before reading `newTarget.prototype`.
|
||||
A conversion or syntax error throws before that prototype getter runs.
|
||||
`Reflect.has` and an empty-handler proxy's
|
||||
membership checks use host collection indexers, including numeric indices and named items.
|
||||
|
||||
@@ -1201,7 +1201,7 @@ Moved from the ROADMAP's "Shipped so far" on 2026-10-10. Update it when a featur
|
||||
- `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,676 of 4,682 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 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 when run as scripts. With `module: true`, they run as modules and accept these ECMAScript 2015 forms. `import` and `export` outside a module are syntax errors (the modules entry below). `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`.
|
||||
The collector retains tagged-template cache entries only while the site's code is reachable.
|
||||
- With `Options::es2015`, `vectorcraft-js` also reads and runs classes (https://262.ecma-international.org/6.0/#sec-class-definitions): declarations, scoped to their block like `let`, and expressions, with `extends`, `static` members, getters and setters, generator methods, and computed method names, Symbols included; class code is strict mode code, methods are not enumerable and cannot be constructed, and calling a class without `new` throws a `TypeError`. A derived constructor's `this` is unusable until `super(…)` returns (a `ReferenceError`), and its own default constructor passes its arguments on. `super.name` and `super[key]` read, call and write through the prototype of the method's home object with `this` as the receiver, in class methods and in methods and accessors written in object literals, which are enumerable, computed names included. A class takes its own name, or the name it is assigned to, and each method takes the name of its key (`get ` or `set ` before an accessor's, a Symbol's description in brackets). `new.target` names the constructor `new` was applied to, in functions and the arrow functions inside them. A class can extend `null`, a function, a class, or the built-in constructors `Object`, `Function`, `Array`, `String`, `Number`, `Boolean`, `RegExp`, `Date` and the error types; a class can name another built-in constructor in `extends`, but `new` on that class throws a `TypeError` when it reaches the built-in constructor, a rule that is VectorCraft's own. Each class and method takes fuel, and deep chains of constructors stop at the frame limit. `new.target` in code given to direct `eval` inside a function, or inside an arrow function within one, reads that function's `new.target`, and outside a function it is a `SyntaxError`; `super` in code given to direct `eval` is not supported yet. `cargo xtask test262` runs the `class`, `super` and `new.target` tests with the other ES2015 forms. Tests: `crates/js/src/es2015_tests.rs`, `crates/js/tests/test262.rs`.
|
||||
- With `Options::es2015`, `vectorcraft-js` also reads and runs classes (https://262.ecma-international.org/6.0/#sec-class-definitions): declarations, scoped to their block like `let`, and expressions, with `extends`, `static` members, getters and setters, generator methods, and computed method names, Symbols included; class code is strict mode code, methods are not enumerable and cannot be constructed, and calling a class without `new` throws a `TypeError`. A derived constructor's `this` is unusable until `super(…)` returns (a `ReferenceError`), and its own default constructor passes its arguments on. `super.name` and `super[key]` read, call and write through the prototype of the method's home object with `this` as the receiver, in class methods and in methods and accessors written in object literals, which are enumerable, computed names included. A class takes its own name, or the name it is assigned to, and each method takes the name of its key (`get ` or `set ` before an accessor's, a Symbol's description in brackets). `new.target` names the constructor `new` was applied to, in functions and the arrow functions inside them. A class can extend `null`, a function, a class, or the built-in constructors `Object`, `Function`, `Array`, `String`, `Number`, `Boolean`, `RegExp`, `Date`, the error types, `GeneratorFunction` and `AsyncFunction` (neither is a global: a script reaches each through the prototype of a generator or async function, and writing an async function needs `Options::es2017`); a class can name another built-in constructor in `extends`, but `new` on that class throws a `TypeError` when it reaches the built-in constructor, a rule that is VectorCraft's own. Each class and method takes fuel, and deep chains of constructors stop at the frame limit. `new.target` in code given to direct `eval` inside a function, or inside an arrow function within one, reads that function's `new.target`, and outside a function it is a `SyntaxError`; `super` in code given to direct `eval` is not supported yet. `cargo xtask test262` runs the `class`, `super` and `new.target` tests with the other ES2015 forms. Tests: `crates/js/src/es2015_tests.rs`, `crates/js/tests/test262.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`.
|
||||
- A realm made with `Options::es2015` also has `Proxy`, with `Proxy.revocable`, and `Reflect` with its thirteen functions (https://262.ecma-international.org/11.0/, sections 9.5, 26.1 and 26.2), with string and Symbol property keys. Every internal method of an object goes through one dispatch, so a proxy's traps run for property reads and writes, `in`, `delete`, `for`-`in`, `Object.keys` and the other `Object` functions, `instanceof`, calls and `new`, and when the proxy is a prototype or a `with` object; `Array.isArray`, `concat` and `JSON.stringify` treat a proxy of an array as an array. Each trap result is checked against the target's invariants (non-configurable and non-extensible targets, and `ownKeys` results with a duplicate key or a value that is neither a string nor a Symbol), and a violation or an operation on a revoked proxy throws a `TypeError`. Traps take fuel and count against the limit on nested calls, the property name each trap is given counts against the heap limit, and so a trap that calls itself or a chain of proxies stops at a limit error, and a proxy given to the app in a command's params throws a `TypeError` naming it without running its traps. `script.run` gives a `.js` file, a `.mjs` file, and a run with `module: true` both globals. A `.jsx` script and a `source` string run as a script have neither in a new engine or in an engine that has not run ECMAScript 2015 code. An engine that `#targetengine` keeps gets both globals at its first ECMAScript 2015 run and keeps them for later `.jsx` runs, which still read ES5.1. A `Proxy` or `Reflect` global that an earlier run in that engine defined stays as it is, and an ECMAScript 2015 run does not replace it. `cargo xtask test262` includes the suite's Proxy and Reflect tests whose features are present. Tests: `crates/js/src/proxy_tests.rs`, `crates/js/tests/test262.rs`, `crates/engine/src/tests/script_run.rs`.
|
||||
- `vectorcraft-js` runs ECMAScript 2015 modules (https://262.ecma-international.org/6.0/#sec-modules): `parse_module` reads `import` and `export` declarations, with their early errors, and `Realm::start_module` and `Realm::run_module` load a module and the modules it imports through the host, link every import before any module runs, and run each module once, after the modules it imports; in a cycle of imports a module does not wait for a module that is still waiting for its own imports, so it can run before that module has initialized its bindings. An import reads the exporting module's binding each time, and a read before that binding is initialized throws a `ReferenceError`; `import * as ns` gives a namespace object that is not extensible and takes no writes. `export * as ns from` (ECMAScript 2020) is read too; `import()` is a syntax error. Parsing takes fuel for each code unit, and the host bounds how many modules a run loads and how deep its imports go. `script.run` runs a `.mjs` file as a module by default; `module: false` runs it as a script. Any script run with `module: true` runs as a module: specifiers are `./` and `../` paths to `.mjs` and `.js` files, read only in the script's folder and the folders below it through the script's file scope; a bare name or a URL is a `SyntaxError` that names it, and module files count against the limits of `#include` files. File › Scripts lists `.mjs` files and Other Script… opens them, on the web too. `cargo xtask test262` runs the suite's module tests under `test/language/module-code`, `import` and `export` whose feature tags it reads: 232 of 263 pass as modules (154 as scripts). Tests: `crates/js/src/module_tests.rs`, `crates/js/tests/test262.rs`, `crates/engine/src/tests/script_modules.rs`.
|
||||
|
||||
Reference in new issue
Block a user