ladybird/Userland/Libraries/LibJS/Tests/functions/function-evaluation-order.js
Linus Groh 99f9609e45 LibJS: Evaluate function arguments before checking callee type
In the spec, this happens in the EvaluateCall abstract operation
(https://tc39.es/ecma262/#sec-evaluatecall), and the order is defined
as:

    3. Let argList be ? ArgumentListEvaluation of arguments.
    4. If Type(func) is not Object, throw a TypeError exception.
    5. If IsCallable(func) is false, throw a TypeError exception.

In LibJS this is handled by CallExpression::execute(), which had the
callee function check first and would therefore never evaluate the
arguments for a non-function callee.
2021-09-13 17:44:08 +01:00

30 lines
809 B
JavaScript

test("callee is evaluated before arguments", () => {
function foo() {}
const values = [];
[foo][(values.push("callee"), 0)](values.push("args"));
expect(values).toEqual(["callee", "args"]);
});
test("arguments are evaluated in order", () => {
function foo() {}
const values = [];
foo(values.push("arg1"), values.push("arg2"), values.push("arg3"));
expect(values).toEqual(["arg1", "arg2", "arg3"]);
});
test("arguments are evaluated before callee is checked for its type", () => {
const values = [];
expect(() => {
"foo"(values.push("args"));
}).toThrowWithMessage(TypeError, "foo is not a function");
expect(values).toEqual(["args"]);
expect(() => {
"foo"(bar);
}).toThrowWithMessage(ReferenceError, "'bar' is not defined");
});