1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +00:00

LibJS: Add missing steps and spec comments to PerformEval

While adding spec comments to PerformEval, I noticed we were missing
multiple steps.

Namely, these were:
- Checking if the host will allow us to compile the string
  (allowing LibWeb to perform CSP for eval)
- The parser's initial state depending on the environment around us
  on direct eval:
   - Allowing new.target via eval in functions
   - Allowing super calls and super properties via eval in classes
   - Disallowing the use of the arguments object in class field
     initializers at eval's parse time
- Setting ScriptOrModule of eval's execution context

The spec allows us to apply the additional parsing steps in any order.
The method I have gone with is passing in a struct to the parser's
constructor, which overrides the parser's initial state to (dis)allow
the things stated above from the get-go.
This commit is contained in:
Luke Wilde 2022-04-10 00:55:45 +01:00 committed by Linus Groh
parent f4f850aaf2
commit 34f902fb52
8 changed files with 185 additions and 39 deletions

View file

@ -398,10 +398,16 @@ Parser::ParserState::ParserState(Lexer l, Program::Type program_type)
current_token = lexer.next();
}
Parser::Parser(Lexer lexer, Program::Type program_type)
Parser::Parser(Lexer lexer, Program::Type program_type, Optional<EvalInitialState> initial_state_for_eval)
: m_state(move(lexer), program_type)
, m_program_type(program_type)
{
if (initial_state_for_eval.has_value()) {
m_state.in_eval_function_context = initial_state_for_eval->in_eval_function_context;
m_state.allow_super_property_lookup = initial_state_for_eval->allow_super_property_lookup;
m_state.allow_super_constructor_call = initial_state_for_eval->allow_super_constructor_call;
m_state.in_class_field_initializer = initial_state_for_eval->in_class_field_initializer;
}
}
Associativity Parser::operator_associativity(TokenType type) const
@ -1448,7 +1454,7 @@ Parser::PrimaryExpressionParseResult Parser::parse_primary_expression()
auto new_start = position();
auto new_target_result = try_parse_new_target_expression();
if (!new_target_result.is_null()) {
if (!m_state.in_function_context && !m_state.in_class_static_init_block)
if (!m_state.in_function_context && !m_state.in_eval_function_context && !m_state.in_class_static_init_block)
syntax_error("'new.target' not allowed outside of a function", new_start);
return { new_target_result.release_nonnull() };
}