mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 17:02:45 +00:00 
			
		
		
		
	LibJS: Disallow 'return' outside of a function
This commit is contained in:
		
							parent
							
								
									5feb7e8d28
								
							
						
					
					
						commit
						9a82c22a85
					
				
					 2 changed files with 6 additions and 0 deletions
				
			
		|  | @ -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()); | ||||
|  |  | |||
|  | @ -158,6 +158,7 @@ private: | |||
|         bool m_strict_mode { false }; | ||||
|         bool m_allow_super_property_lookup { false }; | ||||
|         bool m_allow_super_constructor_call { false }; | ||||
|         bool m_in_function_context { false }; | ||||
| 
 | ||||
|         explicit ParserState(Lexer); | ||||
|     }; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Matthew Olsson
						Matthew Olsson