
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 :^)
174 lines
4 KiB
JavaScript
174 lines
4 KiB
JavaScript
test("no arguments", () => {
|
|
let getNumber = () => {
|
|
return 42;
|
|
};
|
|
expect(getNumber()).toBe(42);
|
|
|
|
getNumber = () => 42;
|
|
expect(getNumber()).toBe(42);
|
|
|
|
getNumber = () => {
|
|
return 99;
|
|
};
|
|
expect(getNumber()).toBe(99);
|
|
getNumber = () => 99;
|
|
expect(getNumber()).toBe(99);
|
|
});
|
|
|
|
test("arguments", () => {
|
|
let add = (a, b) => a + b;
|
|
expect(add(2, 3)).toBe(5);
|
|
|
|
const addBlock = (a, b) => {
|
|
let res = a + b;
|
|
return res;
|
|
};
|
|
expect(addBlock(5, 4)).toBe(9);
|
|
});
|
|
|
|
test("inside an array", () => {
|
|
let chompy = [x => x, 2];
|
|
expect(chompy).toHaveLength(2);
|
|
expect(chompy[0](1)).toBe(1);
|
|
});
|
|
|
|
test("return object literal", () => {
|
|
const makeObject = (a, b) => ({ a, b });
|
|
const obj = makeObject(33, 44);
|
|
expect(typeof obj).toBe("object");
|
|
expect(obj.a).toBe(33);
|
|
expect(obj.b).toBe(44);
|
|
});
|
|
|
|
test("return undefined", () => {
|
|
let returnUndefined = () => {};
|
|
expect(returnUndefined()).toBeUndefined();
|
|
});
|
|
|
|
test("return array literal", () => {
|
|
const makeArray = (a, b) => [a, b];
|
|
const array = makeArray("3", { foo: 4 });
|
|
expect(array[0]).toBe("3");
|
|
expect(array[1].foo).toBe(4);
|
|
});
|
|
|
|
test("return numeric expression", () => {
|
|
let square = x => x * x;
|
|
expect(square(3)).toBe(9);
|
|
|
|
let squareBlock = x => {
|
|
return x * x;
|
|
};
|
|
expect(squareBlock(4)).toBe(16);
|
|
});
|
|
|
|
test("return called arrow function expression", () => {
|
|
const message = (who => "Hello " + who)("friends!");
|
|
expect(message).toBe("Hello friends!");
|
|
|
|
const sum = ((x, y, z) => x + y + z)(1, 2, 3);
|
|
expect(sum).toBe(6);
|
|
|
|
const product = ((x, y, z) => {
|
|
let res = x * y * z;
|
|
return res;
|
|
})(5, 4, 2);
|
|
expect(product).toBe(40);
|
|
|
|
const half = (x => {
|
|
return x / 2;
|
|
})(10);
|
|
expect(half).toBe(5);
|
|
});
|
|
|
|
test("currying", () => {
|
|
let add = a => b => a + b;
|
|
expect(typeof add(1)).toBe("function");
|
|
expect(typeof add(1, 2)).toBe("function");
|
|
expect(add(1)(2)).toBe(3);
|
|
});
|
|
|
|
test("with comma operator", () => {
|
|
let foo, bar;
|
|
(foo = bar), baz => {};
|
|
expect(foo).toBe(undefined);
|
|
expect(bar).toBe(undefined);
|
|
});
|
|
|
|
test("arrow functions in objects", () => {
|
|
function FooBar() {
|
|
this.x = {
|
|
y: () => this,
|
|
z: function () {
|
|
return (() => this)();
|
|
},
|
|
};
|
|
}
|
|
|
|
const foobar = new FooBar();
|
|
expect(foobar.x.y()).toBe(foobar);
|
|
expect(foobar.x.z()).toBe(foobar.x);
|
|
});
|
|
|
|
test("strict mode propagation", () => {
|
|
(() => {
|
|
"use strict";
|
|
expect(isStrictMode()).toBeTrue();
|
|
|
|
(() => {
|
|
expect(isStrictMode()).toBeTrue();
|
|
})();
|
|
})();
|
|
|
|
(() => {
|
|
"use strict";
|
|
expect(isStrictMode()).toBeTrue();
|
|
})();
|
|
|
|
(() => {
|
|
expect(isStrictMode()).toBeFalse();
|
|
|
|
(() => {
|
|
"use strict";
|
|
expect(isStrictMode()).toBeTrue();
|
|
})();
|
|
|
|
expect(isStrictMode()).toBeFalse();
|
|
})();
|
|
});
|
|
|
|
test("no prototype", () => {
|
|
let foo = () => {};
|
|
expect(foo).not.toHaveProperty("prototype");
|
|
});
|
|
|
|
test("cannot be constructed", () => {
|
|
let foo = () => {};
|
|
expect(() => {
|
|
new foo();
|
|
}).toThrowWithMessage(
|
|
TypeError,
|
|
"[object ECMAScriptFunctionObject] is not a constructor (evaluated from 'foo')"
|
|
);
|
|
});
|
|
|
|
test("syntax errors", () => {
|
|
expect("a, => {}").not.toEval();
|
|
expect("(a, => {}").not.toEval();
|
|
expect("(,) => {}").not.toEval();
|
|
expect("(,,) => {}").not.toEval();
|
|
expect("(a,,) => {}").not.toEval();
|
|
expect("(a,,b) => {}").not.toEval();
|
|
expect("(a, ...b, ...c) => {}").not.toEval();
|
|
expect("(a b) => {}").not.toEval();
|
|
expect("(a ...b) => {}").not.toEval();
|
|
expect("(a = 1 = 2) => {}").not.toEval();
|
|
expect("()\n=> {}").not.toEval();
|
|
});
|
|
|
|
test("destructuring parameters", () => {
|
|
expect(`({ a }) => {}`).toEval();
|
|
expect(`([ a ]) => {}`).toEval();
|
|
expect(`{ a } => {}`).not.toEval();
|
|
expect(`[ a ] => {}`).not.toEval();
|
|
});
|