Implement ES2015 class feature (part I.) (#2404)

This patch is the first milestone of the implementation of this new language element.

Currently supported:
 - Class statement
 - Class expression
 - Static methods

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2018-07-12 18:20:08 +02:00
committed by yichoi
parent 62cdb3965f
commit 43aae199ce
18 changed files with 779 additions and 15 deletions
+7
View File
@@ -551,6 +551,12 @@
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, \
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_GETTER, CBC_HAS_LITERAL_ARG | CBC_HAS_LITERAL_ARG2, 0, \
VM_OC_SET_GETTER | VM_OC_GET_LITERAL_LITERAL) \
\
/* Binary compound assignment opcodes with pushing the result. */ \
CBC_EXT_BINARY_LVALUE_OPERATION (CBC_EXT_ASSIGN_ADD, \
@@ -667,6 +673,7 @@ typedef enum
CBC_CODE_FLAGS_ARROW_FUNCTION = (1u << 7), /**< this function is an arrow function */
CBC_CODE_FLAGS_STATIC_FUNCTION = (1u << 8), /**< this function is a static snapshot function */
CBC_CODE_FLAGS_DEBUGGER_IGNORE = (1u << 9), /**< this function should be ignored by debugger */
CBC_CODE_FLAGS_CONSTRUCTOR = (1u << 10), /**< this function is a constructor */
} cbc_code_flags;
#define CBC_OPCODE(arg1, arg2, arg3, arg4) arg1,
+62
View File
@@ -277,6 +277,23 @@ lexer_skip_spaces (parser_context_t *context_p) /**< context */
}
} /* lexer_skip_spaces */
#ifndef CONFIG_DISABLE_ES2015_CLASS
/**
* Skip all the continuous empty statements.
*/
void
lexer_skip_empty_statements (parser_context_t *context_p) /**< context */
{
lexer_skip_spaces (context_p);
while (*context_p->source_p == LIT_CHAR_SEMICOLON)
{
lexer_next_token (context_p);
lexer_skip_spaces (context_p);
}
} /* lexer_skip_empty_statements */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
/**
* Keyword data.
*/
@@ -1308,6 +1325,25 @@ lexer_check_colon (parser_context_t *context_p) /**< context */
&& context_p->source_p[0] == (uint8_t) LIT_CHAR_COLON);
} /* lexer_check_colon */
#ifndef CONFIG_DISABLE_ES2015_CLASS
/**
* Checks whether the next token is a left parenthesis.
*
* @return true - if the next token is a left parenthesis
* false - otherwise
*/
bool
lexer_check_left_paren (parser_context_t *context_p) /**< context */
{
lexer_skip_spaces (context_p);
context_p->token.flags = (uint8_t) (context_p->token.flags | LEXER_NO_SKIP_SPACES);
return (context_p->source_p < context_p->source_end_p
&& context_p->source_p[0] == (uint8_t) LIT_CHAR_LEFT_PAREN);
} /* lexer_check_left_paren */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
/**
@@ -2231,6 +2267,10 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
{
lexer_skip_spaces (context_p);
#ifndef CONFIG_DISABLE_ES2015_CLASS
bool is_static_method = (context_p->token.type == LEXER_KEYW_STATIC);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
context_p->token.line = context_p->line;
context_p->token.column = context_p->column;
@@ -2262,6 +2302,17 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
}
}
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
if ((context_p->status_flags & PARSER_IS_CLASS)
&& !must_be_identifier
&& !is_static_method
&& context_p->token.lit_location.length == 6
&& lexer_compare_raw_identifier_to_current (context_p, "static", 6))
{
context_p->token.type = LEXER_KEYW_STATIC;
return;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
create_literal_object = true;
}
@@ -2297,6 +2348,17 @@ lexer_expect_object_literal_id (parser_context_t *context_p, /**< context */
}
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
if ((context_p->status_flags & PARSER_IS_CLASS)
&& !must_be_identifier
&& !is_static_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)
{
lexer_construct_literal_object (context_p,
+1
View File
@@ -149,6 +149,7 @@ typedef enum
LEXER_PROPERTY_SETTER, /**< property setter function */
LEXER_COMMA_SEP_LIST, /**< comma separated bracketed expression list */
LEXER_SCAN_SWITCH, /**< special value for switch pre-scan */
LEXER_CLASS_CONSTRUCTOR, /**< special value for class constructor method */
/* Future reserved words: these keywords
* must form a group after all other keywords. */
+252
View File
@@ -21,6 +21,10 @@
#ifndef JERRY_DISABLE_JS_PARSER
#if !defined (CONFIG_DISABLE_ES2015_CLASS) && (defined (JERRY_DEBUGGER) || defined (JERRY_ENABLE_LINE_INFO))
#include "jcontext.h"
#endif /* !CONFIG_DISABLE_ES2015_CLASS && (JERRY_DEBUGGER || JERRY_ENABLE_LINE_INFO) */
/** \addtogroup parser Parser
* @{
*
@@ -353,6 +357,247 @@ parser_append_object_literal_item (parser_context_t *context_p, /**< context */
}
} /* parser_append_object_literal_item */
#ifndef CONFIG_DISABLE_ES2015_CLASS
/**
* Parse class as an object literal.
*/
static void
parser_parse_class_literal (parser_context_t *context_p, /**< context */
lexer_literal_t *constructor_literal_p) /**< constructor literal */
{
JERRY_ASSERT (context_p->token.type == LEXER_LEFT_BRACE);
JERRY_ASSERT (constructor_literal_p->type == LEXER_UNUSED_LITERAL);
parser_emit_cbc (context_p, CBC_CREATE_OBJECT);
bool is_static = false;
while (true)
{
if (!is_static)
{
lexer_skip_empty_statements (context_p);
}
lexer_expect_object_literal_id (context_p, false);
if (context_p->token.type == LEXER_RIGHT_BRACE)
{
break;
}
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;
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;
}
lexer_expect_object_literal_id (context_p, true);
literal_index = context_p->lit_object.index;
if (!is_static && lexer_compare_raw_identifier_to_current (context_p, "constructor", 11))
{
parser_raise_error (context_p, PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR);
}
parser_flush_cbc (context_p);
function_literal_index = lexer_construct_function_object (context_p, status_flags);
parser_emit_cbc_literal (context_p,
CBC_PUSH_LITERAL,
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;
is_static = false;
}
else if (!is_static && context_p->token.type == LEXER_CLASS_CONSTRUCTOR)
{
if (constructor_literal_p->type == LEXER_FUNCTION_LITERAL)
{
/* 14.5.1 */
parser_raise_error (context_p, PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTOR);
}
parser_flush_cbc (context_p);
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;
}
else if (!is_static && context_p->token.type == LEXER_KEYW_STATIC)
{
is_static = true;
}
else
{
if (is_static && lexer_compare_raw_identifier_to_current (context_p, "prototype", 9))
{
parser_raise_error (context_p, PARSER_ERR_CLASS_STATIC_PROPERTY_NAME_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;
}
}
}
if (constructor_literal_p->type == LEXER_UNUSED_LITERAL)
{
parser_flush_cbc (context_p);
constructor_literal_p->u.bytecode_p = parser_create_class_implicit_constructor (context_p);
constructor_literal_p->type = LEXER_FUNCTION_LITERAL;
}
JERRY_ASSERT (constructor_literal_p->type == LEXER_FUNCTION_LITERAL);
} /* parser_parse_class_literal */
/**
* Description of "prototype" literal string.
*/
static const lexer_lit_location_t lexer_prototype_literal =
{
(const uint8_t *) "prototype", 9, LEXER_STRING_LITERAL, false
};
/**
* Parse class statement or expression.
*/
void
parser_parse_class (parser_context_t *context_p, /**< context */
bool is_statement) /**< true - if class is parsed as a statement
* false - otherwise (as an expression) */
{
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_CLASS);
uint16_t class_ident_index = PARSER_MAXIMUM_NUMBER_OF_LITERALS;
if (is_statement)
{
/* Class statement must contain an identifier. */
lexer_expect_identifier (context_p, LEXER_IDENT_LITERAL);
JERRY_ASSERT (context_p->token.type == LEXER_LITERAL
&& context_p->token.lit_location.type == LEXER_IDENT_LITERAL);
class_ident_index = context_p->lit_object.index;
context_p->lit_object.literal_p->status_flags |= LEXER_FLAG_VAR;
lexer_next_token (context_p);
}
else
{
lexer_next_token (context_p);
/* Class expression may contain an identifier. */
if (context_p->token.type == LEXER_LITERAL && context_p->token.lit_location.type == LEXER_IDENT_LITERAL)
{
/* NOTE: If 'Function.name' will be supported, the current literal object must be set to 'name' property. */
lexer_next_token (context_p);
}
}
/* Currently heritage is not supported so the next token must be left brace. */
if (context_p->token.type != LEXER_LEFT_BRACE)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_BRACE_EXPECTED);
}
/* Create an empty literal for class constructor. */
if (context_p->literal_count >= PARSER_MAXIMUM_NUMBER_OF_LITERALS)
{
parser_raise_error (context_p, PARSER_ERR_LITERAL_LIMIT_REACHED);
}
lexer_literal_t *constructor_literal_p = (lexer_literal_t *) parser_list_append (context_p, &context_p->literal_pool);
constructor_literal_p->type = LEXER_UNUSED_LITERAL;
constructor_literal_p->status_flags = 0;
parser_emit_cbc_literal (context_p, CBC_PUSH_LITERAL, context_p->literal_count);
context_p->literal_count++;
bool is_strict = context_p->status_flags & PARSER_IS_STRICT;
/* 14.5. A ClassBody is always strict code. */
context_p->status_flags |= PARSER_IS_STRICT | PARSER_IS_CLASS;
/* ClassDeclaration is parsed. Continue with class body. */
parser_parse_class_literal (context_p, constructor_literal_p);
#ifdef JERRY_DEBUGGER
if (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
{
jerry_debugger_send_string (JERRY_DEBUGGER_FUNCTION_NAME,
JERRY_DEBUGGER_NO_SUBTYPE,
constructor_literal_p->u.char_p,
constructor_literal_p->prop.length);
}
#endif /* JERRY_DEBUGGER */
JERRY_ASSERT (context_p->token.type == LEXER_RIGHT_BRACE);
lexer_construct_literal_object (context_p,
(lexer_lit_location_t *) &lexer_prototype_literal,
lexer_prototype_literal.type);
parser_emit_cbc_literal (context_p, CBC_SET_PROPERTY, context_p->lit_object.index);
if (is_statement)
{
parser_emit_cbc_literal (context_p, CBC_ASSIGN_SET_IDENT, class_ident_index);
}
parser_flush_cbc (context_p);
context_p->status_flags &= (uint32_t) ~PARSER_IS_CLASS;
if (!is_strict)
{
/* Restore flag */
context_p->status_flags &= (uint32_t) ~PARSER_IS_STRICT;
}
lexer_next_token (context_p);
} /* parser_parse_class */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
/**
* Parse object literal.
*/
@@ -857,6 +1102,13 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
PARSER_IS_FUNCTION | PARSER_IS_FUNC_EXPRESSION | PARSER_IS_CLOSURE);
break;
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
case LEXER_KEYW_CLASS:
{
parser_parse_class (context_p, false);
return;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
case LEXER_LEFT_BRACE:
{
parser_parse_object_literal (context_p);
+14
View File
@@ -67,6 +67,10 @@ typedef enum
PARSER_IS_ARROW_FUNCTION = (1u << 18), /**< an arrow function is parsed */
PARSER_ARROW_PARSE_ARGS = (1u << 19), /**< parse the argument list of an arrow function */
#endif /* !CONFIG_DISABLE_ES2015_ARROW_FUNCTION */
#ifndef CONFIG_DISABLE_ES2015_CLASS
PARSER_IS_CLASS = (1u << 20), /**< statements parsed inside class body */
PARSER_CLASS_CONSTRUCTOR = (1u << 21), /**< a class constructor is parsed */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
} parser_general_flags_t;
/**
@@ -426,6 +430,10 @@ void parser_set_continues_to_current_position (parser_context_t *context_p, pars
void lexer_next_token (parser_context_t *context_p);
bool lexer_check_colon (parser_context_t *context_p);
#ifndef CONFIG_DISABLE_ES2015_CLASS
bool lexer_check_left_paren (parser_context_t *context_p);
void lexer_skip_empty_statements (parser_context_t *context_p);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
lexer_token_type_t lexer_check_arrow (parser_context_t *context_p);
#endif /* !CONFIG_DISABLE_ES2015_ARROW_FUNCTION */
@@ -472,6 +480,9 @@ void parser_scan_until (parser_context_t *context_p, lexer_range_t *range_p, lex
*/
void parser_parse_statements (parser_context_t *context_p);
#ifndef CONFIG_DISABLE_ES2015_CLASS
void parser_parse_class (parser_context_t *context_p, bool is_statement);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
void parser_free_jumps (parser_stack_iterator_t iterator);
/**
@@ -485,6 +496,9 @@ ecma_compiled_code_t *parser_parse_function (parser_context_t *context_p, uint32
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
ecma_compiled_code_t *parser_parse_arrow_function (parser_context_t *context_p, uint32_t status_flags);
#endif /* !CONFIG_DISABLE_ES2015_ARROW_FUNCTION */
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_compiled_code_t *parser_create_class_implicit_constructor (parser_context_t *context_p);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
/* Error management. */
+93 -2
View File
@@ -41,6 +41,10 @@ typedef enum
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
SCAN_MODE_ARROW_FUNCTION, /**< arrow function might follows */
#endif /* !CONFIG_DISABLE_ES2015_ARROW_FUNCTION */
#ifndef CONFIG_DISABLE_ES2015_CLASS
SCAN_MODE_CLASS_DECLARATION, /**< scanning class declaration */
SCAN_MODE_CLASS_METHOD, /**< scanning class method */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
SCAN_MODE_POST_PRIMARY_EXPRESSION, /**< scanning post primary expression */
SCAN_MODE_PRIMARY_EXPRESSION_END, /**< scanning primary expression end */
SCAN_MODE_STATEMENT, /**< scanning statement */
@@ -55,9 +59,9 @@ typedef enum
{
SCAN_STACK_HEAD, /**< head */
SCAN_STACK_PAREN_EXPRESSION, /**< parent expression group */
SCAN_STACK_PAREN_STATEMENT, /**< parent stetement group */
SCAN_STACK_PAREN_STATEMENT, /**< parent statement group */
SCAN_STACK_COLON_EXPRESSION, /**< colon expression group */
SCAN_STACK_COLON_STATEMENT, /**< colon statement group*/
SCAN_STACK_COLON_STATEMENT, /**< colon statement group */
SCAN_STACK_SQUARE_BRACKETED_EXPRESSION, /**< square bracketed expression group */
SCAN_STACK_OBJECT_LITERAL, /**< object literal group */
SCAN_STACK_BLOCK_STATEMENT, /**< block statement group */
@@ -66,6 +70,9 @@ typedef enum
#ifndef CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS
SCAN_STACK_TEMPLATE_STRING, /**< template string */
#endif /* !CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS */
#ifndef CONFIG_DISABLE_ES2015_CLASS
SCAN_STACK_CLASS, /**< class language element */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
} scan_stack_modes_t;
/**
@@ -99,6 +106,14 @@ parser_scan_primary_expression (parser_context_t *context_p, /**< context */
*mode = SCAN_MODE_FUNCTION_ARGUMENTS;
break;
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
case LEXER_KEYW_CLASS:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_EXPRESSION);
*mode = SCAN_MODE_CLASS_DECLARATION;
break;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
case LEXER_LEFT_PAREN:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_PAREN_EXPRESSION);
@@ -360,6 +375,9 @@ parser_scan_primary_expression_end (parser_context_t *context_p, /**< context */
/* Check whether we can enter to statement mode. */
if (stack_top != SCAN_STACK_BLOCK_STATEMENT
&& stack_top != SCAN_STACK_BLOCK_EXPRESSION
#ifndef CONFIG_DISABLE_ES2015_CLASS
&& stack_top != SCAN_STACK_CLASS
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
&& !(stack_top == SCAN_STACK_HEAD && end_type == LEXER_SCAN_SWITCH))
{
parser_raise_error (context_p, PARSER_ERR_INVALID_EXPRESSION);
@@ -482,6 +500,9 @@ parser_scan_statement (parser_context_t *context_p, /**< context */
{
if (stack_top == SCAN_STACK_BLOCK_STATEMENT
|| stack_top == SCAN_STACK_BLOCK_EXPRESSION
#ifndef CONFIG_DISABLE_ES2015_CLASS
|| stack_top == SCAN_STACK_CLASS
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
|| stack_top == SCAN_STACK_BLOCK_PROPERTY)
{
parser_stack_pop_uint8 (context_p);
@@ -490,6 +511,12 @@ parser_scan_statement (parser_context_t *context_p, /**< context */
{
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
if (stack_top == SCAN_STACK_CLASS)
{
*mode = SCAN_MODE_CLASS_METHOD;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
else if (stack_top == SCAN_STACK_BLOCK_PROPERTY)
{
*mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
@@ -516,6 +543,14 @@ parser_scan_statement (parser_context_t *context_p, /**< context */
*mode = SCAN_MODE_FUNCTION_ARGUMENTS;
return false;
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
case LEXER_KEYW_CLASS:
{
parser_stack_push_uint8 (context_p, SCAN_STACK_BLOCK_STATEMENT);
*mode = SCAN_MODE_CLASS_DECLARATION;
return false;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
default:
{
break;
@@ -620,6 +655,55 @@ parser_scan_until (parser_context_t *context_p, /**< context */
}
break;
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
case SCAN_MODE_CLASS_DECLARATION:
{
if (context_p->token.type == LEXER_LITERAL && context_p->token.lit_location.type == LEXER_IDENT_LITERAL)
{
lexer_next_token (context_p);
}
/* Currently heritage is not supported so the next token must be left brace. */
if (context_p->token.type != LEXER_LEFT_BRACE)
{
parser_raise_error (context_p, PARSER_ERR_LEFT_BRACE_EXPECTED);
}
mode = SCAN_MODE_CLASS_METHOD;
break;
}
case SCAN_MODE_CLASS_METHOD:
{
if (type == LEXER_SEMICOLON)
{
break;
}
if (type == LEXER_RIGHT_BRACE
&& (stack_top == SCAN_STACK_BLOCK_STATEMENT
|| stack_top == SCAN_STACK_BLOCK_EXPRESSION))
{
mode = (stack_top == SCAN_STACK_BLOCK_EXPRESSION) ? SCAN_MODE_PRIMARY_EXPRESSION_END : SCAN_MODE_STATEMENT;
parser_stack_pop_uint8 (context_p);
break;
}
if (lexer_compare_raw_identifier_to_current (context_p, "static", 6))
{
lexer_next_token (context_p);
}
if (lexer_compare_raw_identifier_to_current (context_p, "get", 3)
|| lexer_compare_raw_identifier_to_current (context_p, "set", 3))
{
lexer_next_token (context_p);
}
parser_stack_push_uint8 (context_p, SCAN_STACK_CLASS);
mode = SCAN_MODE_FUNCTION_ARGUMENTS;
continue;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
case SCAN_MODE_ARROW_FUNCTION:
{
@@ -678,9 +762,16 @@ parser_scan_until (parser_context_t *context_p, /**< context */
}
case SCAN_MODE_FUNCTION_ARGUMENTS:
{
#ifndef CONFIG_DISABLE_ES2015_CLASS
JERRY_ASSERT (stack_top == SCAN_STACK_BLOCK_STATEMENT
|| stack_top == SCAN_STACK_BLOCK_EXPRESSION
|| stack_top == SCAN_STACK_CLASS
|| stack_top == SCAN_STACK_BLOCK_PROPERTY);
#else
JERRY_ASSERT (stack_top == SCAN_STACK_BLOCK_STATEMENT
|| stack_top == SCAN_STACK_BLOCK_EXPRESSION
|| stack_top == SCAN_STACK_BLOCK_PROPERTY);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
if (context_p->token.type == LEXER_LITERAL
&& (context_p->token.lit_location.type == LEXER_IDENT_LITERAL
+8
View File
@@ -1837,6 +1837,14 @@ parser_parse_statements (parser_context_t *context_p) /**< context */
break;
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
case LEXER_KEYW_CLASS:
{
parser_parse_class (context_p, true);
continue;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
case LEXER_KEYW_FUNCTION:
{
parser_parse_function_statement (context_p);
+14
View File
@@ -920,6 +920,20 @@ parser_error_to_string (parser_error_t error) /**< error code */
return "Expected '}' token.";
}
#endif /* !CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS */
#ifndef CONFIG_DISABLE_ES2015_CLASS
case PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTOR:
{
return "Multiple constructors are not allowed.";
}
case PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR:
{
return "Class constructor may not be an accessor.";
}
case PARSER_ERR_CLASS_STATIC_PROPERTY_NAME_PROTOTYPE:
{
return "Classes may not have a static property called 'prototype'.";
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
case PARSER_ERR_COLON_EXPECTED:
{
return "Expected ':' token.";
+52
View File
@@ -1763,6 +1763,13 @@ parser_post_processing (parser_context_t *context_p) /**< context */
}
#endif /* !CONFIG_DISABLE_ES2015_ARROW_FUNCTION */
#ifndef CONFIG_DISABLE_ES2015_CLASS
if (context_p->status_flags & PARSER_CLASS_CONSTRUCTOR)
{
compiled_code_p->status_flags |= CBC_CODE_FLAGS_CONSTRUCTOR;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
literal_pool_p = (ecma_value_t *) byte_code_p;
literal_pool_p -= context_p->register_count;
byte_code_p += literal_length;
@@ -2479,6 +2486,38 @@ parser_restore_context (parser_context_t *context_p, /**< context */
#endif /* !JERRY_NDEBUG */
} /* parser_restore_context */
#ifndef CONFIG_DISABLE_ES2015_CLASS
/**
* Parse default constructor code
*
* @return compiled code
*/
ecma_compiled_code_t *
parser_create_class_implicit_constructor (parser_context_t *context_p) /**< context */
{
parser_saved_context_t saved_context;
parser_save_context (context_p, &saved_context);
#ifdef JERRY_DEBUGGER
if ((JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED)
&& jerry_debugger_send_parse_function (context_p->token.line, context_p->token.column))
{
/* This option has a high memory and performance costs,
* but it is necessary for executing eval operations by the debugger. */
context_p->status_flags |= PARSER_LEXICAL_ENV_NEEDED | PARSER_NO_REG_STORE;
}
#endif /* JERRY_DEBUGGER */
context_p->status_flags |= PARSER_CLASS_CONSTRUCTOR;
ecma_compiled_code_t *compiled_code_p = parser_post_processing (context_p);
parser_restore_context (context_p, &saved_context);
return compiled_code_p;
} /* parser_create_class_implicit_constructor */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
/**
* Parse function code
*
@@ -2498,7 +2537,14 @@ parser_parse_function (parser_context_t *context_p, /**< context */
#ifdef PARSER_DUMP_BYTE_CODE
if (context_p->is_show_opcodes)
{
#ifndef CONFIG_DISABLE_ES2015_CLASS
bool is_constructor = context_p->status_flags & PARSER_CLASS_CONSTRUCTOR;
JERRY_DEBUG_MSG (is_constructor ? "\n--- Class constructor parsing start ---\n\n"
: "\n--- Function parsing start ---\n\n");
#else
JERRY_DEBUG_MSG ("\n--- Function parsing start ---\n\n");
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
}
#endif /* PARSER_DUMP_BYTE_CODE */
@@ -2596,7 +2642,13 @@ parser_parse_function (parser_context_t *context_p, /**< context */
#ifdef PARSER_DUMP_BYTE_CODE
if (context_p->is_show_opcodes)
{
#ifndef CONFIG_DISABLE_ES2015_CLASS
bool is_constructor = context_p->status_flags & PARSER_CLASS_CONSTRUCTOR;
JERRY_DEBUG_MSG (is_constructor ? "\n--- Class constructor parsing end ---\n\n"
: "\n--- Function parsing end ---\n\n");
#else
JERRY_DEBUG_MSG ("\n--- Function parsing end ---\n\n");
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
}
#endif /* PARSER_DUMP_BYTE_CODE */
+5
View File
@@ -87,6 +87,11 @@ typedef enum
#ifndef CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS
PARSER_ERR_RIGHT_BRACE_EXPECTED, /**< right brace expected */
#endif /* !CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS */
#ifndef CONFIG_DISABLE_ES2015_CLASS
PARSER_ERR_MULTIPLE_CLASS_CONSTRUCTOR, /**< multiple class constructor */
PARSER_ERR_CLASS_CONSTRUCTOR_AS_ACCESSOR, /**< class constructor cannot be an accessor */
PARSER_ERR_CLASS_STATIC_PROPERTY_NAME_PROTOTYPE, /**< static method name 'prototype' is not allowed */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
PARSER_ERR_COLON_EXPECTED, /**< colon expected */
PARSER_ERR_COLON_FOR_CONDITIONAL_EXPECTED, /**< colon expected for conditional expression */
PARSER_ERR_SEMICOLON_EXPECTED, /**< semicolon expected */