Add support for function rest parameter (#2647)

ECMAScript v6, 14.1.

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2019-01-25 09:24:39 +01:00
committed by Akos Kiss
parent a42f239abb
commit 4c1ee94652
14 changed files with 185 additions and 3 deletions
+1
View File
@@ -648,6 +648,7 @@ typedef enum
CBC_CODE_FLAGS_STATIC_FUNCTION = (1u << 7), /**< this function is a static snapshot function */
CBC_CODE_FLAGS_DEBUGGER_IGNORE = (1u << 8), /**< this function should be ignored by debugger */
CBC_CODE_FLAGS_CONSTRUCTOR = (1u << 9), /**< this function is a constructor */
CBC_CODE_FLAGS_REST_PARAMETER = (1u << 10), /**< this function has rest parameter */
} cbc_code_flags;
/**
+11
View File
@@ -1152,6 +1152,17 @@ lexer_next_token (parser_context_t *context_p) /**< context */
return;
}
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
if (length >= 3
&& context_p->source_p[1] == LIT_CHAR_DOT
&& context_p->source_p[2] == LIT_CHAR_DOT)
{
context_p->token.type = LEXER_THREE_DOTS;
length = 3;
break;
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */
context_p->token.type = LEXER_DOT;
length = 1;
break;
+3
View File
@@ -42,6 +42,9 @@ typedef enum
#ifndef CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS
LEXER_TEMPLATE_LITERAL, /**< multi segment template literal */
#endif /* !CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS */
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
LEXER_THREE_DOTS, /**< ... (rest or spread operator) */
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */
/* Unary operators
* IMPORTANT: update CBC_UNARY_OP_TOKEN_TO_OPCODE and
@@ -45,6 +45,9 @@ typedef enum
PARSER_IS_FUNC_EXPRESSION = (1u << 3), /**< a function expression is parsed */
PARSER_IS_PROPERTY_GETTER = (1u << 4), /**< a property getter function is parsed */
PARSER_IS_PROPERTY_SETTER = (1u << 5), /**< a property setter function is parsed */
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
PARSER_FUNCTION_HAS_REST_PARAM = (1u << 6), /**< function has rest parameter */
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */
PARSER_HAS_NON_STRICT_ARG = (1u << 7), /**< the function has arguments which
* are not supported in strict mode */
PARSER_ARGUMENTS_NEEDED = (1u << 8), /**< arguments object must be created */
+7
View File
@@ -865,6 +865,13 @@ parser_scan_until (parser_context_t *context_p, /**< context */
{
while (true)
{
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
if (context_p->token.type == LEXER_THREE_DOTS)
{
lexer_next_token (context_p);
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */
if (context_p->token.type != LEXER_LITERAL
|| context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
{
+10
View File
@@ -1043,6 +1043,16 @@ parser_error_to_string (parser_error_t error) /**< error code */
{
return "Duplicated function argument names are not allowed here.";
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
case PARSER_ERR_FORMAL_PARAM_AFTER_REST_PARAMETER:
{
return "Rest parameter must be last formal parameter.";
}
case PARSER_ERR_REST_PARAMETER_DEFAULT_INITIALIZER:
{
return "Rest parameter may not have a default initializer.";
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
case PARSER_ERR_OBJECT_PROPERTY_REDEFINED:
{
+42
View File
@@ -700,6 +700,14 @@ parser_generate_initializers (parser_context_t *context_p, /**< context */
}
}
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
if (context_p->status_flags & PARSER_FUNCTION_HAS_REST_PARAM)
{
JERRY_ASSERT ((argument_count - 1) == context_p->argument_count);
return dst_p;
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */
JERRY_ASSERT (argument_count == context_p->argument_count);
return dst_p;
} /* parser_generate_initializers */
@@ -1742,6 +1750,14 @@ parser_post_processing (parser_context_t *context_p) /**< context */
compiled_code_p->refs = 1;
compiled_code_p->status_flags = CBC_CODE_FLAGS_FUNCTION;
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
if (context_p->status_flags & PARSER_FUNCTION_HAS_REST_PARAM)
{
JERRY_ASSERT (context_p->argument_count > 0);
context_p->argument_count--;
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */
if (needs_uint16_arguments)
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) compiled_code_p;
@@ -1807,6 +1823,13 @@ parser_post_processing (parser_context_t *context_p) /**< context */
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
if (context_p->status_flags & PARSER_FUNCTION_HAS_REST_PARAM)
{
compiled_code_p->status_flags |= CBC_CODE_FLAGS_REST_PARAMETER;
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */
literal_pool_p = (ecma_value_t *) byte_code_p;
literal_pool_p -= context_p->register_count;
byte_code_p += literal_length;
@@ -2160,6 +2183,18 @@ parser_parse_function_arguments (parser_context_t *context_p, /**< context */
{
uint16_t literal_count = context_p->literal_count;
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
if (context_p->status_flags & PARSER_FUNCTION_HAS_REST_PARAM)
{
parser_raise_error (context_p, PARSER_ERR_FORMAL_PARAM_AFTER_REST_PARAMETER);
}
else if (context_p->token.type == LEXER_THREE_DOTS)
{
lexer_expect_identifier (context_p, LEXER_IDENT_LITERAL);
context_p->status_flags |= PARSER_FUNCTION_HAS_REST_PARAM;
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */
if (context_p->token.type != LEXER_LITERAL
|| context_p->token.lit_location.type != LEXER_IDENT_LITERAL)
{
@@ -2239,6 +2274,13 @@ parser_parse_function_arguments (parser_context_t *context_p, /**< context */
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
if (context_p->token.type == LEXER_ASSIGN)
{
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
if (context_p->status_flags & PARSER_FUNCTION_HAS_REST_PARAM)
{
parser_raise_error (context_p, PARSER_ERR_REST_PARAMETER_DEFAULT_INITIALIZER);
}
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */
parser_branch_t skip_init;
if (duplicated_argument_names)
+4
View File
@@ -122,6 +122,10 @@ typedef enum
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
PARSER_ERR_DUPLICATED_ARGUMENT_NAMES, /**< duplicated argument names */
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER */
#ifndef CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
PARSER_ERR_FORMAL_PARAM_AFTER_REST_PARAMETER, /**< formal parameter after rest parameter */
PARSER_ERR_REST_PARAMETER_DEFAULT_INITIALIZER, /**< rest parameter default initializer */
#endif /* !CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER */
PARSER_ERR_OBJECT_PROPERTY_REDEFINED, /**< property of object literal redefined */
PARSER_ERR_NON_STRICT_ARG_DEFINITION /**< non-strict argument definition */
} parser_error_t;