summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Runtime/FunctionConstructor.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-10-29 18:11:35 +0000
committerAndreas Kling <kling@serenityos.org>2020-10-29 22:27:55 +0100
commita10d09fababffb1049b342957d79883705257013 (patch)
tree84947942369c38d2a0736377803c1addd4dec981 /Libraries/LibJS/Runtime/FunctionConstructor.cpp
parentbed270ca47172577953cea05d6627504f0be50e6 (diff)
downloadserenity-a10d09fababffb1049b342957d79883705257013.zip
LibJS: Tweak generated source in 'new Function()' to match ES 2015 spec
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.
Diffstat (limited to 'Libraries/LibJS/Runtime/FunctionConstructor.cpp')
-rw-r--r--Libraries/LibJS/Runtime/FunctionConstructor.cpp2
1 files changed, 1 insertions, 1 deletions
diff --git a/Libraries/LibJS/Runtime/FunctionConstructor.cpp b/Libraries/LibJS/Runtime/FunctionConstructor.cpp
index 7b269269c0..b8664907f2 100644
--- a/Libraries/LibJS/Runtime/FunctionConstructor.cpp
+++ b/Libraries/LibJS/Runtime/FunctionConstructor.cpp
@@ -81,7 +81,7 @@ Value FunctionConstructor::construct(Function&)
if (vm.exception())
return {};
}
- auto source = String::formatted("function anonymous({}) {{ {} }}", parameters_source, body_source);
+ auto source = String::formatted("function anonymous({}\n) {{\n{}\n}}", parameters_source, body_source);
auto parser = Parser(Lexer(source));
auto function_expression = parser.parse_function_node<FunctionExpression>();
if (parser.has_errors()) {