Add support for function argument initializers. (#2571)

EcmaScript 2015 14.1.

Note: arrow functions with default arguments are not supported yet.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2018-10-30 10:48:35 +01:00
committed by László Langó
parent aeddb1cd88
commit 83fc0aa04f
8 changed files with 164 additions and 18 deletions
+28
View File
@@ -74,6 +74,9 @@ typedef enum
SCAN_STACK_CLASS, /**< class language element */
SCAN_STACK_CLASS_EXTENDS, /**< class extends expression */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
SCAN_STACK_FUNCTION_PARAMETERS, /**< function parameter initializer */
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
} scan_stack_modes_t;
/**
@@ -420,6 +423,22 @@ parser_scan_primary_expression_end (parser_context_t *context_p, /**< context */
}
#endif /* !CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
if (context_p->token.type == LEXER_RIGHT_PAREN && stack_top == SCAN_STACK_FUNCTION_PARAMETERS)
{
lexer_next_token (context_p);
parser_stack_pop_uint8 (context_p);
if (context_p->token.type != LEXER_LEFT_BRACE)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_BRACE_EXPECTED);
}
*mode = SCAN_MODE_STATEMENT;
return false;
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
/* Check whether we can enter to statement mode. */
if (stack_top != SCAN_STACK_BLOCK_STATEMENT
&& stack_top != SCAN_STACK_BLOCK_EXPRESSION
@@ -861,6 +880,15 @@ parser_scan_until (parser_context_t *context_p, /**< context */
}
}
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
if (context_p->token.type == LEXER_ASSIGN)
{
parser_stack_push_uint8 (context_p, SCAN_STACK_FUNCTION_PARAMETERS);
mode = SCAN_MODE_PRIMARY_EXPRESSION;
break;
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
if (context_p->token.type != LEXER_RIGHT_PAREN)
{
parser_raise_error (context_p, PARSER_ERR_RIGHT_PAREN_EXPECTED);
+1 -17
View File
@@ -324,8 +324,6 @@ parser_parse_var_statement (parser_context_t *context_p) /**< context */
context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_VAR;
parser_emit_cbc_literal_from_token (context_p, CBC_PUSH_LITERAL);
lexer_next_token (context_p);
if (context_p->token.type == LEXER_ASSIGN)
@@ -334,19 +332,11 @@ parser_parse_var_statement (parser_context_t *context_p) /**< context */
if ((JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
&& ident_line_counter != context_p->last_breakpoint_line)
{
JERRY_ASSERT (context_p->last_cbc_opcode == CBC_PUSH_LITERAL);
cbc_argument_t last_cbc = context_p->last_cbc;
context_p->last_cbc_opcode = PARSER_CBC_UNAVAILABLE;
parser_emit_cbc (context_p, CBC_BREAKPOINT_DISABLED);
parser_flush_cbc (context_p);
parser_append_breakpoint_info (context_p, JERRY_DEBUGGER_BREAKPOINT_LIST, ident_line_counter);
context_p->last_cbc_opcode = CBC_PUSH_LITERAL;
context_p->last_cbc = last_cbc;
context_p->last_breakpoint_line = ident_line_counter;
}
#endif /* JERRY_DEBUGGER */
@@ -358,16 +348,10 @@ parser_parse_var_statement (parser_context_t *context_p) /**< context */
}
#endif /* JERRY_ENABLE_LINE_INFO */
parser_emit_cbc_literal_from_token (context_p, CBC_PUSH_LITERAL);
parser_parse_expression (context_p,
PARSE_EXPR_STATEMENT | PARSE_EXPR_NO_COMMA | PARSE_EXPR_HAS_LITERAL);
}
else
{
JERRY_ASSERT (context_p->last_cbc_opcode == CBC_PUSH_LITERAL
&& context_p->last_cbc.literal_type == LEXER_IDENT_LITERAL);
/* We don't need to assign anything to this variable. */
context_p->last_cbc_opcode = PARSER_CBC_UNAVAILABLE;
}
if (context_p->token.type != LEXER_COMMA)
{
+6
View File
@@ -1038,6 +1038,12 @@ parser_error_to_string (parser_error_t error) /**< error code */
{
return "Duplicated label.";
}
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
case PARSER_ERR_DUPLICATED_ARGUMENT_NAMES:
{
return "Duplicated function argument names are not allowed here.";
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
case PARSER_ERR_OBJECT_PROPERTY_REDEFINED:
{
return "Property of object literal redefined.";
+36
View File
@@ -2177,6 +2177,11 @@ static void
parser_parse_function_arguments (parser_context_t *context_p, /**< context */
lexer_token_type_t end_type) /**< expected end type */
{
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
bool duplicated_argument_names = false;
bool initializer_found = false;
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
if (context_p->token.type == end_type)
{
return;
@@ -2218,6 +2223,14 @@ parser_parse_function_arguments (parser_context_t *context_p, /**< context */
{
lexer_literal_t *literal_p;
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
if (initializer_found)
{
parser_raise_error (context_p, PARSER_ERR_DUPLICATED_ARGUMENT_NAMES);
}
duplicated_argument_names = true;
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
if (context_p->literal_count >= PARSER_MAXIMUM_NUMBER_OF_LITERALS)
{
parser_raise_error (context_p, PARSER_ERR_LITERAL_LIMIT_REACHED);
@@ -2254,6 +2267,29 @@ parser_parse_function_arguments (parser_context_t *context_p, /**< context */
lexer_next_token (context_p);
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
if (context_p->token.type == LEXER_ASSIGN)
{
parser_branch_t skip_init;
if (duplicated_argument_names)
{
parser_raise_error (context_p, PARSER_ERR_DUPLICATED_ARGUMENT_NAMES);
}
initializer_found = true;
/* LEXER_ASSIGN does not overwrite lit_object. */
parser_emit_cbc (context_p, CBC_PUSH_UNDEFINED);
parser_emit_cbc_literal (context_p, CBC_STRICT_EQUAL_RIGHT_LITERAL, context_p->lit_object.index);
parser_emit_cbc_forward_branch (context_p, CBC_BRANCH_IF_FALSE_FORWARD, &skip_init);
parser_emit_cbc_literal_from_token (context_p, CBC_PUSH_LITERAL);
parser_parse_expression (context_p, PARSE_EXPR_STATEMENT | PARSE_EXPR_NO_COMMA | PARSE_EXPR_HAS_LITERAL);
parser_set_branch_to_current_position (context_p, &skip_init);
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
if (context_p->token.type != LEXER_COMMA)
{
break;
+3
View File
@@ -119,6 +119,9 @@ typedef enum
PARSER_ERR_INVALID_RETURN, /**< return must be inside a function */
PARSER_ERR_INVALID_RIGHT_SQUARE, /**< right square must terminate a block */
PARSER_ERR_DUPLICATED_LABEL, /**< duplicated label */
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
PARSER_ERR_DUPLICATED_ARGUMENT_NAMES, /**< duplicated argument names */
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
PARSER_ERR_OBJECT_PROPERTY_REDEFINED, /**< property of object literal redefined */
PARSER_ERR_NON_STRICT_ARG_DEFINITION /**< non-strict argument definition */
} parser_error_t;