mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
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:
parent
aeddb1cd88
commit
83fc0aa04f
@ -39,6 +39,7 @@
|
||||
# define CONFIG_DISABLE_ES2015_ARROW_FUNCTION
|
||||
# define CONFIG_DISABLE_ES2015_BUILTIN
|
||||
# define CONFIG_DISABLE_ES2015_CLASS
|
||||
# define CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
|
||||
# define CONFIG_DISABLE_ES2015_MAP_BUILTIN
|
||||
# define CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
|
||||
# define CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
@ -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.";
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -32,6 +32,9 @@ Alternatively, if you want to use a custom profile at
|
||||
# Turn off every ES2015 feature EXCEPT the arrow functions
|
||||
CONFIG_DISABLE_ES2015_BUILTIN
|
||||
CONFIG_DISABLE_ES2015_CLASS
|
||||
CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
|
||||
CONFIG_DISABLE_ES2015_MAP_BUILTIN
|
||||
CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
|
||||
CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
|
||||
CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS
|
||||
CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
|
||||
@ -87,6 +90,8 @@ In JerryScript all of the features are enabled by default, so an empty profile f
|
||||
Disable the built-in updates of the 5.1 standard. There are some differences in those built-ins which available in both [5.1](http://www.ecma-international.org/ecma-262/5.1/) and [2015](http://www.ecma-international.org/ecma-262/6.0/) versions of the standard. JerryScript uses the latest definition by default.
|
||||
* `CONFIG_DISABLE_ES2015_CLASS`:
|
||||
Disable the [class](https://www.ecma-international.org/ecma-262/6.0/#sec-class-definitions) language element.
|
||||
* `CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER`:
|
||||
Disable the [default value](http://www.ecma-international.org/ecma-262/6.0/#sec-function-definitions) for formal parameters.
|
||||
* `CONFIG_DISABLE_ES2015_MAP_BUILTIN`:
|
||||
Disable the [Map](http://www.ecma-international.org/ecma-262/6.0/#sec-keyed-collection) built-ins.
|
||||
* `CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER`:
|
||||
@ -98,4 +103,6 @@ In JerryScript all of the features are enabled by default, so an empty profile f
|
||||
* `CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN`:
|
||||
Disable the [ArrayBuffer](http://www.ecma-international.org/ecma-262/6.0/#sec-arraybuffer-objects) and [TypedArray](http://www.ecma-international.org/ecma-262/6.0/#sec-typedarray-objects) built-ins.
|
||||
* `CONFIG_DISABLE_ES2015`: Disable all of the implemented [ECMAScript2015 features](http://www.ecma-international.org/ecma-262/6.0/).
|
||||
(equivalent to `CONFIG_DISABLE_ES2015_ARROW_FUNCTION`, `CONFIG_DISABLE_ES2015_BUILTIN`, `CONFIG_DISABLE_ES2015_CLASS`, `CONFIG_DISABLE_ES2015_PROMISE_BUILTIN`, `CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS`, and `CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN`).
|
||||
(equivalent to `CONFIG_DISABLE_ES2015_ARROW_FUNCTION`, `CONFIG_DISABLE_ES2015_BUILTIN`, `CONFIG_DISABLE_ES2015_CLASS`,
|
||||
`CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER`, `CONFIG_DISABLE_ES2015_MAP_BUILTIN`, `CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER`,
|
||||
`CONFIG_DISABLE_ES2015_PROMISE_BUILTIN`, `CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS`, and `CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN`).
|
||||
|
||||
81
tests/jerry/es2015/function-param-init.js
Normal file
81
tests/jerry/es2015/function-param-init.js
Normal file
@ -0,0 +1,81 @@
|
||||
// Copyright JS Foundation and other contributors, http://js.foundation
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
var local = 0;
|
||||
|
||||
switch(0) { /* This switch forces a pre-scanner run. */
|
||||
default:
|
||||
|
||||
function f(a = (5, local = 6),
|
||||
b = ((5 + function(a = 6) { return a }() * 3)),
|
||||
c,
|
||||
d = true ? 1 : 2)
|
||||
{
|
||||
return "" + a + ", " + b + ", " + c + ", " + d;
|
||||
}
|
||||
|
||||
assert(f() === "6, 23, undefined, 1");
|
||||
assert(local === 6);
|
||||
|
||||
var obj = {
|
||||
f: function(a = [10,,20],
|
||||
b,
|
||||
c = Math.cos(0),
|
||||
d)
|
||||
{
|
||||
return "" + a + ", " + b + ", " + c + ", " + d;
|
||||
}
|
||||
};
|
||||
|
||||
assert(obj.f() === "10,,20, undefined, 1, undefined");
|
||||
|
||||
function g(a, b = (local = 7)) { }
|
||||
|
||||
local = 0;
|
||||
g();
|
||||
assert(local === 7);
|
||||
|
||||
local = 0;
|
||||
g(0);
|
||||
assert(local === 7);
|
||||
|
||||
local = 0;
|
||||
g(0, undefined);
|
||||
assert(local === 7);
|
||||
|
||||
local = 0;
|
||||
g(0, null);
|
||||
assert(local === 0);
|
||||
|
||||
local = 0;
|
||||
g(0, false);
|
||||
assert(local === 0);
|
||||
break;
|
||||
}
|
||||
|
||||
function CheckSyntaxError(str)
|
||||
{
|
||||
try {
|
||||
eval(str);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e instanceof SyntaxError);
|
||||
}
|
||||
}
|
||||
|
||||
CheckSyntaxError('function x(a += 5) {}');
|
||||
CheckSyntaxError('function x(a =, b) {}');
|
||||
CheckSyntaxError('function x(a = (b) {}');
|
||||
CheckSyntaxError('function x(a, a = 5) {}');
|
||||
CheckSyntaxError('function x(a = 5, a) {}');
|
||||
Loading…
x
Reference in New Issue
Block a user