Implement computed properties for object literals. (#2481)

Also disable ES5.1 property name dumplication checks
when ES2015 object literals are enabled.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2018-08-28 15:28:16 +02:00
committed by GitHub
parent 300e40ba9d
commit bd42403600
19 changed files with 493 additions and 163 deletions
+21 -9
View File
@@ -236,7 +236,7 @@
CBC_BACKWARD_BRANCH (CBC_BRANCH_IF_FALSE_BACKWARD, -1, \
VM_OC_BRANCH_IF_FALSE) \
CBC_OPCODE (CBC_SET_PROPERTY, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_SET_PROPERTY | VM_OC_GET_STACK_LITERAL) \
VM_OC_SET_PROPERTY | VM_OC_NON_STATIC_FLAG | VM_OC_GET_STACK_LITERAL) \
CBC_FORWARD_BRANCH (CBC_JUMP_FORWARD_EXIT_CONTEXT, 0, \
VM_OC_JUMP_AND_EXIT_CONTEXT) \
CBC_OPCODE (CBC_CREATE_ARRAY, CBC_NO_FLAG, 1, \
@@ -318,7 +318,7 @@
CBC_OPCODE (CBC_RETURN_WITH_LITERAL, CBC_HAS_LITERAL_ARG, 0, \
VM_OC_RET | VM_OC_GET_LITERAL) \
CBC_OPCODE (CBC_SET_LITERAL_PROPERTY, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_PROPERTY | VM_OC_GET_LITERAL_LITERAL) \
VM_OC_SET_PROPERTY | VM_OC_NON_STATIC_FLAG | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_BREAKPOINT_ENABLED, CBC_NO_FLAG, 0, \
VM_OC_BREAKPOINT_ENABLED) \
CBC_OPCODE (CBC_BREAKPOINT_DISABLED, CBC_NO_FLAG, 0, \
@@ -522,11 +522,11 @@
CBC_FORWARD_BRANCH (CBC_EXT_FOR_IN_CREATE_CONTEXT, \
-1 + PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION, VM_OC_FOR_IN_CREATE_CONTEXT) \
CBC_OPCODE (CBC_EXT_SET_GETTER, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_GETTER | VM_OC_GET_LITERAL_LITERAL) \
VM_OC_SET_GETTER | VM_OC_NON_STATIC_FLAG | VM_OC_GET_LITERAL_LITERAL) \
CBC_BACKWARD_BRANCH (CBC_EXT_BRANCH_IF_FOR_IN_HAS_NEXT, 0, \
VM_OC_FOR_IN_HAS_NEXT) \
CBC_OPCODE (CBC_EXT_SET_SETTER, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_SETTER | VM_OC_GET_LITERAL_LITERAL) \
VM_OC_SET_SETTER | VM_OC_NON_STATIC_FLAG | VM_OC_GET_LITERAL_LITERAL) \
CBC_FORWARD_BRANCH (CBC_EXT_TRY_CREATE_CONTEXT, PARSER_TRY_CONTEXT_STACK_ALLOCATION, \
VM_OC_TRY) \
CBC_OPCODE (CBC_EXT_THROW_REFERENCE_ERROR, CBC_NO_FLAG, 1, \
@@ -539,8 +539,6 @@
VM_OC_FINALLY) \
\
/* Basic opcodes. */ \
CBC_OPCODE (CBC_EXT_DEBUGGER, CBC_NO_FLAG, 0, \
VM_OC_NONE) \
CBC_OPCODE (CBC_EXT_PUSH_LITERAL_PUSH_NUMBER_0, CBC_HAS_LITERAL_ARG, 2, \
VM_OC_PUSH_LIT_0 | VM_OC_GET_LITERAL) \
CBC_OPCODE (CBC_EXT_PUSH_LITERAL_PUSH_NUMBER_POS_BYTE, CBC_HAS_LITERAL_ARG | CBC_HAS_BYTE_ARG, 2, \
@@ -551,12 +549,26 @@
VM_OC_RESOURCE_NAME) \
CBC_OPCODE (CBC_EXT_LINE, CBC_NO_FLAG, 0, \
VM_OC_LINE) \
CBC_OPCODE (CBC_EXT_SET_STATIC_PROPERTY, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
CBC_OPCODE (CBC_EXT_SET_COMPUTED_PROPERTY, CBC_NO_FLAG, -2, \
VM_OC_SET_COMPUTED_PROPERTY | VM_OC_NON_STATIC_FLAG | VM_OC_GET_STACK_STACK) \
CBC_OPCODE (CBC_EXT_SET_COMPUTED_PROPERTY_LITERAL, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_SET_COMPUTED_PROPERTY | VM_OC_NON_STATIC_FLAG | VM_OC_GET_STACK_LITERAL) \
CBC_OPCODE (CBC_EXT_SET_COMPUTED_GETTER, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_SET_GETTER | VM_OC_NON_STATIC_FLAG | VM_OC_GET_STACK_LITERAL) \
CBC_OPCODE (CBC_EXT_SET_COMPUTED_SETTER, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_SET_SETTER | VM_OC_NON_STATIC_FLAG | VM_OC_GET_STACK_LITERAL) \
CBC_OPCODE (CBC_EXT_SET_STATIC_PROPERTY_LITERAL, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_PROPERTY | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_EXT_SET_STATIC_SETTER, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_SETTER | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_EXT_SET_STATIC_COMPUTED_PROPERTY_LITERAL, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_SET_COMPUTED_PROPERTY | VM_OC_GET_STACK_LITERAL) \
CBC_OPCODE (CBC_EXT_SET_STATIC_GETTER, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_GETTER | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_EXT_SET_STATIC_SETTER, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_SETTER | VM_OC_GET_LITERAL_LITERAL) \
CBC_OPCODE (CBC_EXT_SET_STATIC_COMPUTED_GETTER, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_SET_GETTER | VM_OC_GET_STACK_LITERAL) \
CBC_OPCODE (CBC_EXT_SET_STATIC_COMPUTED_SETTER, CBC_HAS_LITERAL_ARG, -1, \
VM_OC_SET_SETTER | VM_OC_GET_STACK_LITERAL) \
\
/* Binary compound assignment opcodes with pushing the result. */ \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_ADD, \
+25 -9
View File
@@ -2323,6 +2323,22 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
lexer_parse_string (context_p);
create_literal_object = true;
}
#ifndef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
else if (context_p->source_p[0] == LIT_CHAR_LEFT_SQUARE)
{
context_p->source_p += 1;
context_p->column++;
lexer_next_token (context_p);
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
if (context_p->token.type != LEXER_RIGHT_SQUARE)
{
parser_raise_error (context_p, PARSER_ERR_RIGHT_SQUARE_EXPECTED);
}
return;
}
#endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
else if (!(ident_opts & LEXER_OBJ_IDENT_ONLY_IDENTIFIERS) && context_p->source_p[0] == LIT_CHAR_RIGHT_BRACE)
{
context_p->token.type = LEXER_RIGHT_BRACE;
@@ -2349,17 +2365,17 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
}
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
if (is_class_method
&& lexer_compare_raw_identifier_to_current (context_p, "constructor", 11))
{
context_p->token.type = LEXER_CLASS_CONSTRUCTOR;
return;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
if (create_literal_object)
{
#ifndef CONFIG_DISABLE_ES2015_CLASS
if (is_class_method
&& lexer_compare_raw_identifier_to_current (context_p, "constructor", 11))
{
context_p->token.type = LEXER_CLASS_CONSTRUCTOR;
return;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
lexer_construct_literal_object (context_p,
&context_p->token.lit_location,
LEXER_STRING_LITERAL);
+139 -51
View File
@@ -260,6 +260,7 @@ parser_parse_array_literal (parser_context_t *context_p) /**< context */
}
} /* parser_parse_array_literal */
#ifdef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
/**
* Object literal item types.
*/
@@ -356,8 +357,14 @@ parser_append_object_literal_item (parser_context_t *context_p, /**< context */
context_p->stack_top_uint8 = PARSER_OBJECT_PROPERTY_BOTH_ACCESSORS;
}
} /* parser_append_object_literal_item */
#endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
#ifndef CONFIG_DISABLE_ES2015_CLASS
#ifdef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
#error "Class support requires ES2015 object literal support"
#endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
/**
* Parse class as an object literal.
*/
@@ -388,25 +395,22 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */
if (context_p->token.type == LEXER_PROPERTY_GETTER || context_p->token.type == LEXER_PROPERTY_SETTER)
{
uint32_t status_flags;
cbc_ext_opcode_t opcode;
uint16_t literal_index, function_literal_index;
bool is_getter = (context_p->token.type == LEXER_PROPERTY_GETTER);
if (context_p->token.type == LEXER_PROPERTY_GETTER)
{
status_flags = PARSER_IS_FUNCTION | PARSER_IS_CLOSURE | PARSER_IS_PROPERTY_GETTER;
opcode = is_static ? CBC_EXT_SET_STATIC_GETTER : CBC_EXT_SET_GETTER;
}
else
{
status_flags = PARSER_IS_FUNCTION | PARSER_IS_CLOSURE | PARSER_IS_PROPERTY_SETTER;
opcode = is_static ? CBC_EXT_SET_STATIC_SETTER : CBC_EXT_SET_SETTER;
}
uint32_t status_flags = PARSER_IS_FUNCTION | PARSER_IS_CLOSURE;
status_flags |= (is_getter ? PARSER_IS_PROPERTY_GETTER : PARSER_IS_PROPERTY_SETTER);
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_CLASS_METHOD | LEXER_OBJ_IDENT_ONLY_IDENTIFIERS);
literal_index = context_p->lit_object.index;
if (!is_static && lexer_compare_raw_identifier_to_current (context_p, "constructor", 11))
bool is_computed = false;
if (context_p->token.type == LEXER_RIGHT_SQUARE)
{
is_computed = true;
}
else if (!is_static && lexer_compare_raw_identifier_to_current (context_p, "constructor", 11))
{
parser_raise_error (context_p, PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR);
}
@@ -419,12 +423,42 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */
literal_index);
JERRY_ASSERT (context_p->last_cbc_opcode == CBC_PUSH_LITERAL);
context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (opcode);
context_p->last_cbc.value = function_literal_index;
cbc_ext_opcode_t opcode;
if (is_computed)
{
context_p->last_cbc.literal_index = function_literal_index;
if (is_getter)
{
opcode = is_static ? CBC_EXT_SET_STATIC_COMPUTED_GETTER : CBC_EXT_SET_COMPUTED_GETTER;
}
else
{
opcode = is_static ? CBC_EXT_SET_STATIC_COMPUTED_SETTER : CBC_EXT_SET_COMPUTED_SETTER;
}
}
else
{
context_p->last_cbc.value = function_literal_index;
if (is_getter)
{
opcode = is_static ? CBC_EXT_SET_STATIC_GETTER : CBC_EXT_SET_GETTER;
}
else
{
opcode = is_static ? CBC_EXT_SET_STATIC_SETTER : CBC_EXT_SET_SETTER;
}
}
context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (opcode);
is_static = false;
continue;
}
else if (!is_static && context_p->token.type == LEXER_CLASS_CONSTRUCTOR)
if (!is_static && context_p->token.type == LEXER_CLASS_CONSTRUCTOR)
{
if (constructor_literal_p->type == LEXER_FUNCTION_LITERAL)
{
@@ -436,47 +470,50 @@ parser_parse_class_literal (parser_context_t *context_p, /**< context */
uint32_t status_flags = PARSER_IS_FUNCTION | PARSER_IS_CLOSURE | PARSER_CLASS_CONSTRUCTOR;
constructor_literal_p->u.bytecode_p = parser_parse_function (context_p, status_flags);
constructor_literal_p->type = LEXER_FUNCTION_LITERAL;
continue;
}
else if (!is_static && context_p->token.type == LEXER_KEYW_STATIC)
if (!is_static && context_p->token.type == LEXER_KEYW_STATIC)
{
is_static = true;
continue;
}
bool is_computed = false;
if (context_p->token.type == LEXER_RIGHT_SQUARE)
{
is_computed = true;
}
else if (is_static && lexer_compare_raw_identifier_to_current (context_p, "prototype", 9))
{
parser_raise_error (context_p, PARSER_ERR_CLASS_STATIC_PROTOTYPE);
}
parser_flush_cbc (context_p);
uint16_t literal_index = context_p->lit_object.index;
uint32_t status_flags = PARSER_IS_FUNCTION | PARSER_IS_CLOSURE;
uint16_t function_literal_index = lexer_construct_function_object (context_p, status_flags);
parser_emit_cbc_literal (context_p,
CBC_PUSH_LITERAL,
function_literal_index);
JERRY_ASSERT (context_p->last_cbc_opcode == CBC_PUSH_LITERAL);
context_p->last_cbc.value = literal_index;
if (is_static)
{
context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (is_computed ? CBC_EXT_SET_STATIC_COMPUTED_PROPERTY_LITERAL
: CBC_EXT_SET_STATIC_PROPERTY_LITERAL);
is_static = false;
}
else
{
if (is_static && lexer_compare_raw_identifier_to_current (context_p, "prototype", 9))
{
parser_raise_error (context_p, PARSER_ERR_CLASS_STATIC_PROTOTYPE);
}
if (!lexer_check_left_paren (context_p))
{
lexer_next_token (context_p);
parser_raise_error (context_p, PARSER_ERR_LEFT_PAREN_EXPECTED);
}
parser_flush_cbc (context_p);
uint16_t literal_index = context_p->lit_object.index;
uint32_t status_flags = PARSER_IS_FUNCTION | PARSER_IS_FUNC_EXPRESSION | PARSER_IS_CLOSURE;
uint16_t function_literal_index = lexer_construct_function_object (context_p, status_flags);
parser_emit_cbc_literal (context_p,
CBC_PUSH_LITERAL,
function_literal_index);
JERRY_ASSERT (context_p->last_cbc_opcode == CBC_PUSH_LITERAL);
context_p->last_cbc.value = literal_index;
if (is_static)
{
context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (CBC_EXT_SET_STATIC_PROPERTY);
is_static = false;
}
else
{
context_p->last_cbc_opcode = CBC_SET_LITERAL_PROPERTY;
}
context_p->last_cbc_opcode = (is_computed ? PARSER_TO_EXT_OPCODE (CBC_EXT_SET_COMPUTED_PROPERTY_LITERAL)
: CBC_SET_LITERAL_PROPERTY);
}
}
@@ -606,7 +643,9 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */
parser_emit_cbc (context_p, CBC_CREATE_OBJECT);
#ifdef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
parser_stack_push_uint8 (context_p, PARSER_OBJECT_PROPERTY_START);
#endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
while (true)
{
@@ -623,29 +662,52 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */
uint32_t status_flags;
cbc_ext_opcode_t opcode;
uint16_t literal_index, function_literal_index;
#ifdef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
parser_object_literal_item_types_t item_type;
#endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
if (context_p->token.type == LEXER_PROPERTY_GETTER)
{
status_flags = PARSER_IS_FUNCTION | PARSER_IS_CLOSURE | PARSER_IS_PROPERTY_GETTER;
opcode = CBC_EXT_SET_GETTER;
#ifdef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
item_type = PARSER_OBJECT_PROPERTY_GETTER;
#endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
}
else
{
status_flags = PARSER_IS_FUNCTION | PARSER_IS_CLOSURE | PARSER_IS_PROPERTY_SETTER;
opcode = CBC_EXT_SET_SETTER;
#ifdef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
item_type = PARSER_OBJECT_PROPERTY_SETTER;
#endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
}
lexer_expect_object_literal_id (context_p, LEXER_OBJ_IDENT_ONLY_IDENTIFIERS);
/* This assignment is a nop for computed getters/setters. */
literal_index = context_p->lit_object.index;
#ifndef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
if (context_p->token.type == LEXER_RIGHT_SQUARE)
{
opcode = ((opcode == CBC_EXT_SET_GETTER) ? CBC_EXT_SET_COMPUTED_GETTER
: CBC_EXT_SET_COMPUTED_SETTER);
}
#else /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
parser_append_object_literal_item (context_p, literal_index, item_type);
#endif /* !CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
parser_flush_cbc (context_p);
function_literal_index = lexer_construct_function_object (context_p, status_flags);
#ifndef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
if (opcode >= CBC_EXT_SET_COMPUTED_GETTER)
{
literal_index = function_literal_index;
}
#endif /* !CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
parser_emit_cbc_literal (context_p,
CBC_PUSH_LITERAL,
literal_index);
@@ -656,13 +718,37 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */
lexer_next_token (context_p);
}
#ifndef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
else if (context_p->token.type == LEXER_RIGHT_SQUARE)
{
lexer_next_token (context_p);
if (context_p->token.type != LEXER_COLON)
{
parser_raise_error (context_p, PARSER_ERR_COLON_EXPECTED);
}
lexer_next_token (context_p);
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
if (context_p->last_cbc_opcode == CBC_PUSH_LITERAL)
{
context_p->last_cbc_opcode = PARSER_TO_EXT_OPCODE (CBC_EXT_SET_COMPUTED_PROPERTY_LITERAL);
}
else
{
parser_emit_cbc_ext (context_p, CBC_EXT_SET_COMPUTED_PROPERTY);
}
}
#endif /* !CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
else
{
uint16_t literal_index = context_p->lit_object.index;
#ifdef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
parser_append_object_literal_item (context_p,
literal_index,
PARSER_OBJECT_PROPERTY_VALUE);
#endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
lexer_next_token (context_p);
if (context_p->token.type != LEXER_COLON)
@@ -694,12 +780,14 @@ parser_parse_object_literal (parser_context_t *context_p) /**< context */
}
}
#ifdef CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
while (context_p->stack_top_uint8 != PARSER_OBJECT_PROPERTY_START)
{
parser_stack_pop (context_p, NULL, 3);
}
parser_stack_pop_uint8 (context_p);
#endif /* CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER */
} /* parser_parse_object_literal */
/**
+8 -1
View File
@@ -1992,7 +1992,14 @@ parser_parse_statements (parser_context_t *context_p) /**< context */
case LEXER_KEYW_DEBUGGER:
{
parser_emit_cbc_ext (context_p, CBC_EXT_DEBUGGER);
#ifdef JERRY_DEBUGGER
/* This breakpoint location is not reported to the
* debugger, so it is impossible to disable it. */
if (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
{
parser_emit_cbc (context_p, CBC_BREAKPOINT_ENABLED);
}
#endif /* JERRY_DEBUGGER */
lexer_next_token (context_p);
break;
}