Implement throwing const re-assignment error. (#3697)

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2020-04-29 15:50:22 +02:00
committed by GitHub
parent 9e6c44be4f
commit 4b780507e1
14 changed files with 220 additions and 52 deletions
+2
View File
@@ -578,6 +578,8 @@
VM_OC_ERROR) \
CBC_OPCODE (CBC_EXT_THROW_REFERENCE_ERROR, CBC_NO_FLAG, 1, \
VM_OC_THROW_REFERENCE_ERROR) \
CBC_OPCODE (CBC_EXT_THROW_ASSIGN_CONST_ERROR, CBC_NO_FLAG, 0, \
VM_OC_THROW_CONST_ERROR) \
CBC_OPCODE (CBC_EXT_REQUIRE_OBJECT_COERCIBLE, CBC_NO_FLAG, 0, \
VM_OC_REQUIRE_OBJECT_COERCIBLE) \
\
+2 -2
View File
@@ -173,10 +173,10 @@ typedef enum
LEXER_PROPERTY_GETTER, /**< property getter function */
LEXER_PROPERTY_SETTER, /**< property setter function */
LEXER_COMMA_SEP_LIST, /**< comma separated bracketed expression list */
LEXER_SCAN_SWITCH, /**< special value for switch pre-scan */
#if ENABLED (JERRY_ES2015)
LEXER_ASSIGN_CONST, /**< a const binding is reassigned */
LEXER_CLASS_CONSTRUCTOR, /**< special value for class constructor method */
LEXER_INVALID_PATTERN, /**< special value for invalid destructuring pattern */
#if ENABLED (JERRY_ES2015)
LEXER_ARROW_LEFT_PAREN, /**< start of arrow function argument list */
#endif /* ENABLED (JERRY_ES2015) */
+72 -28
View File
@@ -133,6 +133,7 @@ parser_check_invalid_assign (parser_context_t *context_p) /**< context */
} /* parser_check_invalid_assign */
#if ENABLED (JERRY_ES2015)
/**
* Check and throw an error if the "new.target" is invalid as a left-hand side expression.
*/
@@ -153,6 +154,7 @@ parser_check_invalid_new_target (parser_context_t *context_p, /**< parser contex
parser_raise_error (context_p, PARSER_ERR_NEW_TARGET_NOT_ALLOWED);
}
} /* parser_check_invalid_new_target */
#endif /* ENABLED (JERRY_ES2015) */
/**
@@ -220,6 +222,16 @@ parser_emit_unary_lvalue_opcode (parser_context_t *context_p, /**< context */
}
parser_emit_ident_reference (context_p, unary_opcode);
#if ENABLED (JERRY_ES2015)
if (unary_opcode != CBC_DELETE_IDENT_PUSH_RESULT
&& scanner_literal_is_const_reg (context_p, context_p->last_cbc.literal_index))
{
/* The current value must be read, but it cannot be changed. */
context_p->last_cbc_opcode = CBC_PUSH_LITERAL;
parser_emit_cbc_ext (context_p, CBC_EXT_THROW_ASSIGN_CONST_ERROR);
}
#endif /* ENABLED (JERRY_ES2015) */
return;
}
@@ -2228,10 +2240,9 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
*/
static uint8_t
parser_append_binary_single_assignment_token (parser_context_t *context_p, /**< context */
uint8_t assign_ident_opcode) /**< assign ident opcode */
bool is_lexical) /**< assign lexical declaration */
{
JERRY_ASSERT (assign_ident_opcode == CBC_ASSIGN_SET_IDENT
|| assign_ident_opcode == CBC_ASSIGN_LET_CONST);
JERRY_UNUSED (is_lexical);
/* Unlike other tokens, the whole byte code is saved for binary
* assignment, since it has multiple forms depending on the
@@ -2242,8 +2253,6 @@ parser_append_binary_single_assignment_token (parser_context_t *context_p, /**<
if (PARSER_IS_PUSH_LITERALS_WITH_THIS (context_p->last_cbc_opcode)
&& context_p->last_cbc.literal_type == LEXER_IDENT_LITERAL)
{
JERRY_ASSERT (CBC_SAME_ARGS (CBC_PUSH_LITERAL, assign_ident_opcode));
parser_check_invalid_assign (context_p);
uint16_t literal_index;
@@ -2278,8 +2287,24 @@ parser_append_binary_single_assignment_token (parser_context_t *context_p, /**<
}
}
assign_opcode = CBC_ASSIGN_SET_IDENT;
#if ENABLED (JERRY_ES2015)
if (!is_lexical)
{
if (scanner_literal_is_const_reg (context_p, literal_index))
{
parser_stack_push_uint8 (context_p, LEXER_ASSIGN_CONST);
}
}
else if (literal_index < PARSER_REGISTER_START)
{
assign_opcode = CBC_ASSIGN_LET_CONST;
}
#endif /* ENABLED (JERRY_ES2015) */
parser_stack_push_uint16 (context_p, literal_index);
assign_opcode = assign_ident_opcode;
JERRY_ASSERT (CBC_SAME_ARGS (CBC_PUSH_LITERAL, assign_opcode));
}
else if (context_p->last_cbc_opcode == CBC_PUSH_PROP)
{
@@ -2359,7 +2384,7 @@ parser_append_binary_token (parser_context_t *context_p) /**< context */
if (context_p->token.type == LEXER_ASSIGN)
{
parser_append_binary_single_assignment_token (context_p, CBC_ASSIGN_SET_IDENT);
parser_append_binary_single_assignment_token (context_p, false);
return;
}
@@ -2371,6 +2396,13 @@ parser_append_binary_token (parser_context_t *context_p) /**< context */
parser_check_invalid_assign (context_p);
parser_emit_ident_reference (context_p, CBC_PUSH_IDENT_REFERENCE);
#if ENABLED (JERRY_ES2015)
if (scanner_literal_is_const_reg (context_p, context_p->last_cbc.literal_index))
{
parser_stack_push_uint8 (context_p, LEXER_ASSIGN_CONST);
}
#endif /* ENABLED (JERRY_ES2015) */
}
else if (PARSER_IS_PUSH_PROP (context_p->last_cbc_opcode))
{
@@ -2437,20 +2469,39 @@ parser_process_binary_opcodes (parser_context_t *context_p, /**< context */
opcode = (cbc_opcode_t) context_p->stack_top_uint8;
parser_stack_pop_uint8 (context_p);
if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL
&& opcode == CBC_ASSIGN_SET_IDENT)
{
JERRY_ASSERT (CBC_ARGS_EQ (CBC_ASSIGN_LITERAL_SET_IDENT,
CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2));
context_p->last_cbc.value = parser_stack_pop_uint16 (context_p);
context_p->last_cbc_opcode = CBC_ASSIGN_LITERAL_SET_IDENT;
continue;
}
int32_t index = -1;
if (cbc_flags[opcode] & CBC_HAS_LITERAL_ARG)
{
uint16_t index = parser_stack_pop_uint16 (context_p);
parser_emit_cbc_literal (context_p, (uint16_t) opcode, index);
JERRY_ASSERT (opcode == CBC_ASSIGN_SET_IDENT
|| opcode == CBC_ASSIGN_PROP_LITERAL
|| opcode == CBC_ASSIGN_PROP_THIS_LITERAL
|| opcode == CBC_ASSIGN_LET_CONST);
index = parser_stack_pop_uint16 (context_p);
}
#if ENABLED (JERRY_ES2015)
if (JERRY_UNLIKELY (context_p->stack_top_uint8 == LEXER_ASSIGN_CONST))
{
parser_stack_pop_uint8 (context_p);
parser_emit_cbc_ext (context_p, CBC_EXT_THROW_ASSIGN_CONST_ERROR);
}
#endif /* ENABLED (JERRY_ES2015) */
if (index >= 0)
{
if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL
&& opcode == CBC_ASSIGN_SET_IDENT)
{
JERRY_ASSERT (CBC_ARGS_EQ (CBC_ASSIGN_LITERAL_SET_IDENT,
CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2));
context_p->last_cbc.value = (uint16_t) index;
context_p->last_cbc_opcode = CBC_ASSIGN_LITERAL_SET_IDENT;
continue;
}
parser_emit_cbc_literal (context_p, (uint16_t) opcode, (uint16_t) index);
if (opcode == CBC_ASSIGN_PROP_THIS_LITERAL
&& (context_p->stack_depth >= context_p->stack_limit))
@@ -2644,16 +2695,9 @@ parser_pattern_form_assignment (parser_context_t *context_p, /**< context */
{
JERRY_UNUSED (ident_line_counter);
uint8_t assign_opcode = CBC_ASSIGN_SET_IDENT;
if (flags & (PARSER_PATTERN_LEXICAL | PARSER_PATTERN_LOCAL)
&& context_p->lit_object.index < PARSER_REGISTER_START)
{
assign_opcode = CBC_ASSIGN_LET_CONST;
}
parser_stack_push_uint8 (context_p, LEXER_EXPRESSION_START);
assign_opcode = parser_append_binary_single_assignment_token (context_p, assign_opcode);
bool is_lexical = (flags & (PARSER_PATTERN_LEXICAL | PARSER_PATTERN_LOCAL)) != 0;
uint8_t assign_opcode = parser_append_binary_single_assignment_token (context_p, is_lexical);
if (flags & PARSER_PATTERN_ARRAY)
{
@@ -750,6 +750,9 @@ void scanner_create_variables (parser_context_t *context_p, uint32_t option_flag
void scanner_get_location (scanner_location_t *location_p, parser_context_t *context_p);
void scanner_set_location (parser_context_t *context_p, scanner_location_t *location_p);
uint16_t scanner_decode_map_to (parser_scope_stack_t *stack_item_p);
#if ENABLED (JERRY_ES2015)
bool scanner_literal_is_const_reg (parser_context_t *context_p, uint16_t literal_index);
#endif /* ENABLED (JERRY_ES2015) */
void scanner_scan_all (parser_context_t *context_p, const uint8_t *arg_list_p, const uint8_t *arg_list_end_p,
const uint8_t *source_p, const uint8_t *source_end_p);
+12 -14
View File
@@ -558,24 +558,22 @@ parser_parse_var_statement (parser_context_t *context_p) /**< context */
}
#endif /* ENABLED (JERRY_LINE_INFO) */
uint16_t index = context_p->lit_object.index;
lexer_next_token (context_p);
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
cbc_opcode_t opcode = CBC_ASSIGN_SET_IDENT;
#if ENABLED (JERRY_ES2015)
if (declaration_type != LEXER_KEYW_VAR
&& context_p->lit_object.index < PARSER_REGISTER_START)
&& (index < PARSER_REGISTER_START))
{
uint16_t index = context_p->lit_object.index;
opcode = CBC_ASSIGN_LET_CONST;
}
#endif /* ENABLED (JERRY_ES2015) */
lexer_next_token (context_p);
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
parser_emit_cbc_literal (context_p, CBC_ASSIGN_LET_CONST, index);
}
else
{
#endif /* ENABLED (JERRY_ES2015) */
parser_emit_cbc_literal_from_token (context_p, CBC_PUSH_LITERAL);
parser_parse_expression_statement (context_p, PARSE_EXPR_NO_COMMA | PARSE_EXPR_HAS_LITERAL);
#if ENABLED (JERRY_ES2015)
}
#endif /* ENABLED (JERRY_ES2015) */
parser_emit_cbc_literal (context_p, opcode, index);
}
#if ENABLED (JERRY_ES2015)
else if (declaration_type == LEXER_KEYW_LET)
-6
View File
@@ -2473,11 +2473,9 @@ parser_parse_arrow_function (parser_context_t *context_p, /**< context */
JERRY_ASSERT (status_flags & PARSER_IS_ARROW_FUNCTION);
parser_save_context (context_p, &saved_context);
context_p->status_flags |= status_flags;
#if ENABLED (JERRY_ES2015)
context_p->status_flags |= saved_context.status_flags & (PARSER_ALLOW_NEW_TARGET
| PARSER_ALLOW_SUPER
| PARSER_ALLOW_SUPER_CALL);
#endif /* ENABLED (JERRY_ES2015) */
#if ENABLED (JERRY_PARSER_DUMP_BYTE_CODE)
if (context_p->is_show_opcodes)
@@ -2529,14 +2527,12 @@ parser_parse_arrow_function (parser_context_t *context_p, /**< context */
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
#if ENABLED (JERRY_ES2015)
if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION)
{
parser_emit_cbc_ext (context_p, CBC_EXT_RETURN_PROMISE);
}
else
{
#endif /* ENABLED (JERRY_ES2015) */
if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL)
{
context_p->last_cbc_opcode = CBC_RETURN_WITH_LITERAL;
@@ -2545,9 +2541,7 @@ parser_parse_arrow_function (parser_context_t *context_p, /**< context */
{
parser_emit_cbc (context_p, CBC_RETURN);
}
#if ENABLED (JERRY_ES2015)
}
#endif /* ENABLED (JERRY_ES2015) */
parser_flush_cbc (context_p);
}
+34
View File
@@ -2291,6 +2291,40 @@ scanner_decode_map_to (parser_scope_stack_t *stack_item_p) /**< scope stack item
#endif /* ENABLED (JERRY_ES2015) */
} /* scanner_decode_map_to */
#if ENABLED (JERRY_ES2015)
/**
* Checks whether the literal is a const in the current scope.
*
* @return true if the literal is a const
*/
bool
scanner_literal_is_const_reg (parser_context_t *context_p, /**< context */
uint16_t literal_index) /**< literal index */
{
if (literal_index < PARSER_REGISTER_START)
{
/* Re-assignment of non-register const bindings are detected elsewhere. */
return false;
}
parser_scope_stack_t *scope_stack_p = context_p->scope_stack_p + context_p->scope_stack_top;
literal_index = (uint16_t) (literal_index - (PARSER_REGISTER_START - 1));
do
{
/* Registers must be found in the scope stack. */
JERRY_ASSERT (scope_stack_p > context_p->scope_stack_p);
scope_stack_p--;
}
while (literal_index != (scope_stack_p->map_to & PARSER_SCOPE_STACK_REGISTER_MASK));
return (scope_stack_p->map_to & PARSER_SCOPE_STACK_IS_CONST) != 0;
} /* scanner_literal_is_const_reg */
#endif /* ENABLED (JERRY_ES2015) */
/**
* @}
* @}