summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Parser.cpp
diff options
context:
space:
mode:
authorMatthew Olsson <matthewcolsson@gmail.com>2020-10-07 11:33:48 -0700
committerAndreas Kling <kling@serenityos.org>2020-10-08 10:03:21 +0200
commit9a82c22a85f9a9fc21eee69d7bd57a9a02bea43c (patch)
treec57633e1d719494851e8ab8798b72ba62b34735a /Libraries/LibJS/Parser.cpp
parent5feb7e8d2883e8a1400bfb5d0911bcb9cae5ae63 (diff)
downloadserenity-9a82c22a85f9a9fc21eee69d7bd57a9a02bea43c.zip
LibJS: Disallow 'return' outside of a function
Diffstat (limited to 'Libraries/LibJS/Parser.cpp')
-rw-r--r--Libraries/LibJS/Parser.cpp5
1 files changed, 5 insertions, 0 deletions
diff --git a/Libraries/LibJS/Parser.cpp b/Libraries/LibJS/Parser.cpp
index 6e1980b9d4..8a10aeadae 100644
--- a/Libraries/LibJS/Parser.cpp
+++ b/Libraries/LibJS/Parser.cpp
@@ -396,6 +396,7 @@ RefPtr<FunctionExpression> Parser::try_parse_arrow_function_expression(bool expe
bool is_strict = false;
auto function_body_result = [&]() -> RefPtr<BlockStatement> {
+ TemporaryChange change(m_parser_state.m_in_function_context, true);
if (match(TokenType::CurlyOpen)) {
// Parse a function body with statements
return parse_block_statement(is_strict);
@@ -1202,6 +1203,9 @@ NonnullRefPtr<NewExpression> Parser::parse_new_expression()
NonnullRefPtr<ReturnStatement> Parser::parse_return_statement()
{
+ if (!m_parser_state.m_in_function_context)
+ syntax_error("'return' not allowed outside of a function");
+
consume(TokenType::Return);
// Automatic semicolon insertion: terminate statement when return is followed by newline
@@ -1315,6 +1319,7 @@ NonnullRefPtr<FunctionNodeType> Parser::parse_function_node(bool check_for_funct
function_length = parameters.size();
bool is_strict = false;
+ TemporaryChange change(m_parser_state.m_in_function_context, true);
auto body = parse_block_statement(is_strict);
body->add_variables(m_parser_state.m_var_scopes.last());
body->add_functions(m_parser_state.m_function_scopes.last());