
ES 5(.1) described parsing of the function body string as: https://www.ecma-international.org/ecma-262/5.1/#sec-15.3.2.1 7. If P is not parsable as a FormalParameterList[opt] then throw a SyntaxError exception. 8. If body is not parsable as FunctionBody then throw a SyntaxError exception. We implemented it as building the source string of a complete function and feeding that to the parser, with the same outcome. ES 2015+ does exactly that, but with newlines at certain positions: https://tc39.es/ecma262/#sec-createdynamicfunction 16. Let bodyString be the string-concatenation of 0x000A (LINE FEED), ? ToString(bodyArg), and 0x000A (LINE FEED). 17. Let prefix be the prefix associated with kind in Table 49. 18. Let sourceString be the string-concatenation of prefix, " anonymous(", P, 0x000A (LINE FEED), ") {", bodyString, and "}". This patch updates the generated source string to match these requirements. This will make certain edge cases work, e.g. 'new Function("-->")', where the user supplied input must be placed on its own line to be valid syntax.
53 lines
2.1 KiB
JavaScript
53 lines
2.1 KiB
JavaScript
describe("correct behavior", () => {
|
|
test("constructor properties", () => {
|
|
expect(Function).toHaveLength(1);
|
|
expect(Function.name).toBe("Function");
|
|
expect(Function.prototype).toHaveLength(0);
|
|
expect(Function.prototype.name).toBe("");
|
|
});
|
|
|
|
test("typeof", () => {
|
|
expect(typeof Function()).toBe("function");
|
|
expect(typeof new Function()).toBe("function");
|
|
});
|
|
|
|
test("basic functionality", () => {
|
|
expect(Function()()).toBeUndefined();
|
|
expect(new Function()()).toBeUndefined();
|
|
expect(Function("return 42")()).toBe(42);
|
|
expect(new Function("return 42")()).toBe(42);
|
|
expect(new Function("foo", "return foo")(42)).toBe(42);
|
|
expect(new Function("foo,bar", "return foo + bar")(1, 2)).toBe(3);
|
|
expect(new Function("foo", "bar", "return foo + bar")(1, 2)).toBe(3);
|
|
expect(new Function("foo", "bar,baz", "return foo + bar + baz")(1, 2, 3)).toBe(6);
|
|
expect(new Function("foo", "bar", "baz", "return foo + bar + baz")(1, 2, 3)).toBe(6);
|
|
expect(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(true)).toBe(
|
|
42
|
|
);
|
|
expect(new Function("foo", "if (foo) { return 42; } else { return 'bar'; }")(false)).toBe(
|
|
"bar"
|
|
);
|
|
expect(new Function("return typeof Function()")()).toBe("function");
|
|
expect(new Function("x", "return function (y) { return x + y };")(1)(2)).toBe(3);
|
|
|
|
expect(new Function("-->")()).toBeUndefined();
|
|
|
|
expect(new Function().name).toBe("anonymous");
|
|
expect(new Function().toString()).toBe("function anonymous() {\n ???\n}");
|
|
});
|
|
});
|
|
|
|
describe("errors", () => {
|
|
test("syntax error", () => {
|
|
expect(() => {
|
|
new Function("[");
|
|
})
|
|
// This might be confusing at first but keep in mind it's actually parsing
|
|
// function anonymous() { [ }
|
|
// This is in line with what other engines are reporting.
|
|
.toThrowWithMessage(
|
|
SyntaxError,
|
|
"Unexpected token CurlyClose. Expected BracketClose (line: 4, column: 1)"
|
|
);
|
|
});
|
|
});
|