summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-10-08 20:37:21 +0100
committerLinus Groh <mail@linusgroh.de>2021-10-09 14:29:20 +0100
commitcf168fac5002a1f66559dcf96719368d167596bc (patch)
treeed32522fdaed63d35cb2ac76b7b22bcde9c24fc4 /Userland/Libraries/LibJS/Tests
parent58c34012dd5fd48e2e3800d6f374ecc82e91da1e (diff)
downloadserenity-cf168fac5002a1f66559dcf96719368d167596bc.zip
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 :^)
Diffstat (limited to 'Userland/Libraries/LibJS/Tests')
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Reflect/Reflect.construct.js4
-rw-r--r--Userland/Libraries/LibJS/Tests/functions/arrow-functions.js5
2 files changed, 6 insertions, 3 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Reflect/Reflect.construct.js b/Userland/Libraries/LibJS/Tests/builtins/Reflect/Reflect.construct.js
index c0313392bb..34c949a908 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/Reflect/Reflect.construct.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/Reflect/Reflect.construct.js
@@ -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`);
});
});
diff --git a/Userland/Libraries/LibJS/Tests/functions/arrow-functions.js b/Userland/Libraries/LibJS/Tests/functions/arrow-functions.js
index 5b25d4a66b..17b7779c57 100644
--- a/Userland/Libraries/LibJS/Tests/functions/arrow-functions.js
+++ b/Userland/Libraries/LibJS/Tests/functions/arrow-functions.js
@@ -146,7 +146,10 @@ test("cannot be constructed", () => {
let foo = () => {};
expect(() => {
new foo();
- }).toThrowWithMessage(TypeError, "foo is not a constructor");
+ }).toThrowWithMessage(
+ TypeError,
+ "[object ECMAScriptFunctionObject] is not a constructor (evaluated from 'foo')"
+ );
});
test("syntax errors", () => {