mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:48:14 +00:00
LibJS: Implement [[Call]] and [[Construct]] internal slots properly
This patch implements: - Spec compliant [[Call]] and [[Construct]] internal slots, as virtual FunctionObject::internal_{call,construct}(). These effectively replace the old virtual FunctionObject::{call,construct}(), but with several advantages: - Clear and consistent naming, following the object internal methods - Use of completions - internal_construct() returns an Object, and not Value! This has been a source of confusion for a long time, since in the spec there's always an Object returned but the Value return type in LibJS meant that this could not be fully trusted and something could screw you over. - Arguments are passed explicitly in form of a MarkedValueList, allowing manipulation (BoundFunction). We still put them on the execution context as a lot of code depends on it (VM::arguments()), but not from the Call() / Construct() AOs anymore, which now allows for bypassing them and invoking [[Call]] / [[Construct]] directly. Nothing but Call() / Construct() themselves do that at the moment, but future additions to ECMA262 or already existing web specs might. - Spec compliant, standalone Call() and Construct() AOs: currently the closest we have is VM::{call,construct}(), but those try to cater to all the different function object subclasses at once, resulting in a horrible mess and calling AOs with functions they should never be called with; most prominently PrepareForOrdinaryCall and OrdinaryCallBindThis, which are only for ECMAScriptFunctionObject. As a result this also contains an implicit optimization: we no longer need to create a new function environment for NativeFunctions - which, worth mentioning, is what started this whole crusade in the first place :^)
This commit is contained in:
parent
58c34012dd
commit
cf168fac50
16 changed files with 503 additions and 246 deletions
|
@ -14,7 +14,7 @@ describe("errors", () => {
|
|||
test("arguments list must be an object", () => {
|
||||
[null, undefined, "foo", 123, NaN, Infinity].forEach(value => {
|
||||
expect(() => {
|
||||
Reflect.construct(() => {}, value);
|
||||
Reflect.construct(function () {}, value);
|
||||
}).toThrowWithMessage(TypeError, `${value} is not an object`);
|
||||
});
|
||||
});
|
||||
|
@ -22,7 +22,7 @@ describe("errors", () => {
|
|||
test("new target must be a constructor", () => {
|
||||
[null, undefined, "foo", 123, NaN, Infinity, {}].forEach(value => {
|
||||
expect(() => {
|
||||
Reflect.construct(() => {}, [], value);
|
||||
Reflect.construct(function () {}, [], value);
|
||||
}).toThrowWithMessage(TypeError, `${value} is not a constructor`);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue