Add support for new.target (#3469)

Notable changes:
* Extracted the pure JS/builtin and external C method invocations
  into two new methods (`ecma_op_function_call_{simple, external}`).
* Updated parser/scanner to handle "new.target" correctly.
* Added JS test case.

JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
Péter Gál
2020-01-14 13:34:19 +01:00
committed by Robert Fancsik
parent be8ae3aae8
commit 0fd1ed6f27
22 changed files with 841 additions and 124 deletions
+1
View File
@@ -107,6 +107,7 @@ typedef enum
ECMA_PARSE_HAS_STATIC_SUPER = (1u << 5), /**< the current context is a static class method */
ECMA_PARSE_EVAL = (1u << 6), /**< eval is called */
ECMA_PARSE_MODULE = (1u << 7), /**< module is parsed */
ECMA_PARSE_FUNCTION = (1u << 8), /**< a function body is parsed or the code is inside a function */
} ecma_parse_opts_t;
/**
@@ -54,6 +54,10 @@ ecma_init (void)
#if ENABLED (JERRY_ES2015_BUILTIN_PROMISE)
ecma_job_queue_init ();
#endif /* ENABLED (JERRY_ES2015_BUILTIN_PROMISE) */
#if ENABLED (JERRY_ES2015)
JERRY_CONTEXT (current_new_target) = JERRY_CONTEXT_INVALID_NEW_TARGET;
#endif /* ENABLED (JERRY_ES2015) */
} /* ecma_init */
/**
@@ -62,6 +66,10 @@ ecma_init (void)
void
ecma_finalize (void)
{
#if ENABLED (JERRY_ES2015)
JERRY_ASSERT (JERRY_CONTEXT (current_new_target) == JERRY_CONTEXT_INVALID_NEW_TARGET);
#endif /* ENABLED (JERRY_ES2015) */
ecma_finalize_global_lex_env ();
ecma_finalize_builtins ();
ecma_gc_run ();
+6
View File
@@ -99,6 +99,12 @@ ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters b
#if ENABLED (JERRY_ES2015)
ECMA_CLEAR_SUPER_EVAL_PARSER_OPTS ();
/* If an eval is used inside the function the info should be propagated. */
if (JERRY_CONTEXT (current_new_target) != JERRY_CONTEXT_INVALID_NEW_TARGET)
{
parse_opts |= ECMA_PARSE_FUNCTION;
}
#endif /* ENABLED (JERRY_ES2015) */
ecma_value_t parse_status = parser_parse_script (NULL,
+197 -123
View File
@@ -724,6 +724,162 @@ ecma_op_set_class_prototype (ecma_value_t completion_value, /**< completion_valu
} /* ecma_op_set_class_prototype */
#endif /* ENABLED (JERRY_ES2015) */
/**
* Perform a JavaScript function object method call.
*
* The input function object should be a pure JavaScript method or
* a builtin function implemented in C.
*
* In case of JERRY_ES2015 the arguments_list_p contains the information
* wheter if the function was invoked with "new" or not.
*
* @return the result of the function call.
*/
static ecma_value_t
ecma_op_function_call_simple (ecma_object_t *func_obj_p, /**< Function object */
ecma_value_t this_arg_value, /**< 'this' argument's value */
const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< length of arguments list */
{
JERRY_ASSERT (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_FUNCTION);
if (JERRY_UNLIKELY (ecma_get_object_is_builtin (func_obj_p)))
{
JERRY_ASSERT (!ecma_op_function_has_construct_flag (arguments_list_p));
ecma_value_t ret_value = ecma_builtin_dispatch_call (func_obj_p,
this_arg_value,
arguments_list_p,
arguments_list_len);
return ret_value;
}
/* Entering Function Code (ECMA-262 v5, 10.4.3) */
ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) func_obj_p;
ecma_object_t *scope_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
ext_func_p->u.function.scope_cp);
/* 8. */
ecma_value_t this_binding = this_arg_value;
bool free_this_binding = false;
const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p);
uint16_t status_flags = bytecode_data_p->status_flags;
#if ENABLED (JERRY_ES2015)
if (JERRY_UNLIKELY (status_flags & (CBC_CODE_FLAGS_CONSTRUCTOR | CBC_CODE_FLAGS_GENERATOR)))
{
bool is_construct_call = ecma_op_function_has_construct_flag (arguments_list_p);
if (!is_construct_call && (status_flags & CBC_CODE_FLAGS_CONSTRUCTOR))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Class constructor cannot be invoked without 'new'."));
}
if (is_construct_call && (status_flags & CBC_CODE_FLAGS_GENERATOR))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Generator functions cannot be invoked with 'new'."));
}
}
#endif /* ENABLED (JERRY_ES2015) */
/* 1. */
if (!(status_flags & CBC_CODE_FLAGS_STRICT_MODE))
{
if (ecma_is_value_undefined (this_binding)
|| ecma_is_value_null (this_binding))
{
/* 2. */
this_binding = ecma_make_object_value (ecma_builtin_get_global ());
}
else if (!ecma_is_value_object (this_binding))
{
/* 3., 4. */
this_binding = ecma_op_to_object (this_binding);
free_this_binding = true;
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (this_binding));
}
}
arguments_list_p = ecma_op_function_clear_construct_flag (arguments_list_p);
/* 5. */
ecma_object_t *local_env_p;
if (status_flags & CBC_CODE_FLAGS_LEXICAL_ENV_NOT_NEEDED)
{
local_env_p = scope_p;
}
else
{
local_env_p = ecma_create_decl_lex_env (scope_p);
if (bytecode_data_p->status_flags & CBC_CODE_FLAGS_IS_ARGUMENTS_NEEDED)
{
ecma_op_create_arguments_object (func_obj_p,
local_env_p,
arguments_list_p,
arguments_list_len,
bytecode_data_p);
}
#if ENABLED (JERRY_ES2015)
if (JERRY_UNLIKELY (status_flags & CBC_CODE_FLAGS_CONSTRUCTOR))
{
ecma_op_set_class_this_binding (local_env_p, this_binding);
}
#endif /* ENABLED (JERRY_ES2015) */
}
ecma_value_t ret_value = vm_run (bytecode_data_p,
this_binding,
local_env_p,
arguments_list_p,
arguments_list_len);
if (!(status_flags & CBC_CODE_FLAGS_LEXICAL_ENV_NOT_NEEDED))
{
ecma_deref_object (local_env_p);
}
if (JERRY_UNLIKELY (free_this_binding))
{
ecma_free_value (this_binding);
}
return ret_value;
} /* ecma_op_function_call_simple */
/**
* Perform a native C method call which was registered via the API.
*
* @return the result of the function call.
*/
static ecma_value_t
ecma_op_function_call_external (ecma_object_t *func_obj_p, /**< Function object */
ecma_value_t this_arg_value, /**< 'this' argument's value */
const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< length of arguments list */
{
JERRY_ASSERT (!ecma_op_function_has_construct_flag (arguments_list_p));
ecma_extended_object_t *ext_func_obj_p = (ecma_extended_object_t *) func_obj_p;
JERRY_ASSERT (ext_func_obj_p->u.external_handler_cb != NULL);
ecma_value_t ret_value = ext_func_obj_p->u.external_handler_cb (ecma_make_object_value (func_obj_p),
this_arg_value,
arguments_list_p,
arguments_list_len);
if (JERRY_UNLIKELY (ecma_is_value_error_reference (ret_value)))
{
ecma_raise_error_from_error_reference (ret_value);
return ECMA_VALUE_ERROR;
}
#if ENABLED (JERRY_DEBUGGER)
JERRY_DEBUGGER_CLEAR_FLAGS (JERRY_DEBUGGER_VM_EXCEPTION_THROWN);
#endif /* ENABLED (JERRY_DEBUGGER) */
return ret_value;
} /* ecma_op_function_call_external */
/**
* [[Call]] implementation for Function objects,
* created through 13.2 (ECMA_OBJECT_TYPE_FUNCTION)
@@ -755,128 +911,40 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
{
case ECMA_OBJECT_TYPE_FUNCTION:
{
if (JERRY_UNLIKELY (ecma_get_object_is_builtin (func_obj_p)))
#if ENABLED (JERRY_ES2015)
ecma_object_t *old_new_target = JERRY_CONTEXT (current_new_target);
if (JERRY_LIKELY (!ecma_get_object_is_builtin (func_obj_p)))
{
JERRY_ASSERT (!ecma_op_function_has_construct_flag (arguments_list_p));
JERRY_CONTEXT (current_new_target) = NULL;
}
#endif /* ENABLED (JERRY_ES2015) */
ecma_value_t ret_value = ecma_builtin_dispatch_call (func_obj_p,
jerry_value_t result = ecma_op_function_call_simple (func_obj_p,
this_arg_value,
arguments_list_p,
arguments_list_len);
#if ENABLED (JERRY_ES2015)
JERRY_CONTEXT (current_new_target) = old_new_target;
#endif /* ENABLED (JERRY_ES2015) */
return result;
}
case ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION:
{
#if ENABLED (JERRY_ES2015)
ecma_object_t *old_new_target = JERRY_CONTEXT (current_new_target);
JERRY_CONTEXT (current_new_target) = NULL;
#endif /* ENABLED (JERRY_ES2015) */
jerry_value_t result = ecma_op_function_call_external (func_obj_p,
this_arg_value,
arguments_list_p,
arguments_list_len);
return ret_value;
}
/* Entering Function Code (ECMA-262 v5, 10.4.3) */
ecma_extended_object_t *ext_func_p = (ecma_extended_object_t *) func_obj_p;
ecma_object_t *scope_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
ext_func_p->u.function.scope_cp);
/* 8. */
ecma_value_t this_binding = this_arg_value;
bool free_this_binding = false;
const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p);
uint16_t status_flags = bytecode_data_p->status_flags;
#if ENABLED (JERRY_ES2015)
if (JERRY_UNLIKELY (status_flags & (CBC_CODE_FLAGS_CONSTRUCTOR | CBC_CODE_FLAGS_GENERATOR)))
{
if ((status_flags & CBC_CODE_FLAGS_CONSTRUCTOR) && !ecma_op_function_has_construct_flag (arguments_list_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Class constructor cannot be invoked without 'new'."));
}
if ((status_flags & CBC_CODE_FLAGS_GENERATOR) && ecma_op_function_has_construct_flag (arguments_list_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Generator functions cannot be invoked with 'new'."));
}
}
JERRY_CONTEXT (current_new_target) = old_new_target;
#endif /* ENABLED (JERRY_ES2015) */
/* 1. */
if (!(status_flags & CBC_CODE_FLAGS_STRICT_MODE))
{
if (ecma_is_value_undefined (this_binding)
|| ecma_is_value_null (this_binding))
{
/* 2. */
this_binding = ecma_make_object_value (ecma_builtin_get_global ());
}
else if (!ecma_is_value_object (this_binding))
{
/* 3., 4. */
this_binding = ecma_op_to_object (this_binding);
free_this_binding = true;
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (this_binding));
}
}
arguments_list_p = ecma_op_function_clear_construct_flag (arguments_list_p);
/* 5. */
ecma_object_t *local_env_p;
if (status_flags & CBC_CODE_FLAGS_LEXICAL_ENV_NOT_NEEDED)
{
local_env_p = scope_p;
}
else
{
local_env_p = ecma_create_decl_lex_env (scope_p);
if (bytecode_data_p->status_flags & CBC_CODE_FLAGS_IS_ARGUMENTS_NEEDED)
{
ecma_op_create_arguments_object (func_obj_p,
local_env_p,
arguments_list_p,
arguments_list_len,
bytecode_data_p);
}
#if ENABLED (JERRY_ES2015)
if (JERRY_UNLIKELY (status_flags & CBC_CODE_FLAGS_CONSTRUCTOR))
{
ecma_op_set_class_this_binding (local_env_p, this_binding);
}
#endif /* ENABLED (JERRY_ES2015) */
}
ecma_value_t ret_value = vm_run (bytecode_data_p,
this_binding,
local_env_p,
arguments_list_p,
arguments_list_len);
if (!(status_flags & CBC_CODE_FLAGS_LEXICAL_ENV_NOT_NEEDED))
{
ecma_deref_object (local_env_p);
}
if (JERRY_UNLIKELY (free_this_binding))
{
ecma_free_value (this_binding);
}
return ret_value;
}
case ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION:
{
ecma_extended_object_t *ext_func_obj_p = (ecma_extended_object_t *) func_obj_p;
ecma_value_t ret_value = ext_func_obj_p->u.external_handler_cb (ecma_make_object_value (func_obj_p),
this_arg_value,
arguments_list_p,
arguments_list_len);
if (JERRY_UNLIKELY (ecma_is_value_error_reference (ret_value)))
{
ecma_raise_error_from_error_reference (ret_value);
return ECMA_VALUE_ERROR;
}
#if ENABLED (JERRY_DEBUGGER)
JERRY_DEBUGGER_CLEAR_FLAGS (JERRY_DEBUGGER_VM_EXCEPTION_THROWN);
#endif /* ENABLED (JERRY_DEBUGGER) */
return ret_value;
return result;
}
#if ENABLED (JERRY_ES2015)
case ECMA_OBJECT_TYPE_ARROW_FUNCTION:
@@ -1156,6 +1224,15 @@ ecma_op_function_construct (ecma_object_t *func_obj_p, /**< Function object */
this_arg_value = ecma_make_object_value (new_this_obj_p);
}
#if ENABLED (JERRY_ES2015)
ecma_object_t *old_new_target = JERRY_CONTEXT (current_new_target);
if (JERRY_LIKELY (new_this_obj_p != NULL))
{
/* This is not a super call so update the "new.target" */
JERRY_CONTEXT (current_new_target) = func_obj_p;
}
#endif /* ENABLED (JERRY_ES2015) */
/* 8. */
ecma_value_t ret_value;
@@ -1164,11 +1241,7 @@ ecma_op_function_construct (ecma_object_t *func_obj_p, /**< Function object */
case ECMA_OBJECT_TYPE_FUNCTION:
{
arguments_list_p = ecma_op_function_set_construct_flag (arguments_list_p);
ret_value = ecma_op_function_call (func_obj_p,
this_arg_value,
arguments_list_p,
arguments_list_len);
ret_value = ecma_op_function_call_simple (func_obj_p, this_arg_value, arguments_list_p, arguments_list_len);
break;
}
#if ENABLED (JERRY_ES2015)
@@ -1209,15 +1282,16 @@ ecma_op_function_construct (ecma_object_t *func_obj_p, /**< Function object */
break;
}
#endif /* ENABLED (JERRY_ES2015) */
ret_value = ecma_op_function_call_external (func_obj_p, this_arg_value, arguments_list_p, arguments_list_len);
ret_value = ecma_op_function_call (func_obj_p,
this_arg_value,
arguments_list_p,
arguments_list_len);
break;
}
}
#if ENABLED (JERRY_ES2015)
JERRY_CONTEXT (current_new_target) = old_new_target;
#endif /* ENABLED (JERRY_ES2015) */
/* 9. */
if (ECMA_IS_VALUE_ERROR (ret_value) || ecma_is_value_object (ret_value))
{
+14
View File
@@ -220,8 +220,22 @@ struct jerry_context_t
/** hash table for caching the last access of properties */
ecma_lcache_hash_entry_t lcache[ECMA_LCACHE_HASH_ROWS_COUNT][ECMA_LCACHE_HASH_ROW_LENGTH];
#endif /* ENABLED (JERRY_LCACHE) */
#if ENABLED (JERRY_ES2015)
/**
* Allowed values and it's meaning:
* * NULL (0x0): the current "new.target" is undefined, that is the execution is inside a normal method.
* * JERRY_CONTEXT_INVALID_NEW_TARGET (0x1): the current "new.target" is invalid, that is outside of a method.
* * Any other valid function object pointer: the current "new.target" is valid and it is constructor call.
*/
ecma_object_t *current_new_target;
#endif /* ENABLED (JERRY_ES2015) */
};
/**
* Magic constant used to indicate that the current "new.target" is not inside a function.
*/
#define JERRY_CONTEXT_INVALID_NEW_TARGET ((ecma_object_t *) 0x1)
#if ENABLED (JERRY_EXTERNAL_CONTEXT)
+2
View File
@@ -680,6 +680,8 @@
VM_OC_EXT_RETURN | VM_OC_GET_STACK) \
CBC_OPCODE (CBC_EXT_RETURN_PROMISE, CBC_NO_FLAG, -1, \
VM_OC_RETURN_PROMISE | VM_OC_GET_STACK) \
CBC_OPCODE (CBC_EXT_PUSH_NEW_TARGET, CBC_NO_FLAG, 1, \
VM_OC_PUSH_NEW_TARGET | VM_OC_PUT_STACK) \
\
/* Last opcode (not a real opcode). */ \
CBC_OPCODE (CBC_EXT_END, CBC_NO_FLAG, 0, \
+49
View File
@@ -113,6 +113,27 @@ parser_check_invalid_assign (parser_context_t *context_p) /**< context */
}
} /* parser_check_invalid_assign */
#if ENABLED (JERRY_ES2015)
/**
* Check and throw an error if the "new.target" is invalid as a left-hand side expression.
*/
static void
parser_check_invalid_new_target (parser_context_t *context_p, /**< parser context */
cbc_opcode_t opcode) /**< current opcode under parsing */
{
JERRY_ASSERT ((opcode >= CBC_PRE_INCR && opcode <= CBC_POST_DECR)
|| (opcode == CBC_ASSIGN
&& (context_p->token.type == LEXER_ASSIGN
|| LEXER_IS_BINARY_LVALUE_TOKEN (context_p->token.type))));
/* new.target is an invalid left-hand side target */
if (context_p->last_cbc_opcode == PARSER_TO_EXT_OPCODE (CBC_EXT_PUSH_NEW_TARGET))
{
parser_raise_error (context_p, PARSER_ERR_NEW_TARGET_NOT_ALLOWED);
}
} /* parser_check_invalid_new_target */
#endif /* ENABLED (JERRY_ES2015) */
/**
* Emit identifier reference
*/
@@ -203,6 +224,10 @@ parser_emit_unary_lvalue_opcode (parser_context_t *context_p, /**< context */
return;
}
#if ENABLED (JERRY_ES2015)
parser_check_invalid_new_target (context_p, opcode);
#endif /* ENABLED (JERRY_ES2015) */
parser_emit_cbc_ext (context_p, CBC_EXT_THROW_REFERENCE_ERROR);
}
@@ -1380,6 +1405,22 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
{
/* After 'new' unary operators are not allowed. */
new_was_seen = true;
#if ENABLED (JERRY_ES2015)
/* Check if "new.target" is written here. */
if (scanner_try_scan_new_target (context_p))
{
if (!(context_p->status_flags & PARSER_IS_FUNCTION))
{
parser_raise_error (context_p, PARSER_ERR_NEW_TARGET_NOT_ALLOWED);
}
parser_emit_cbc_ext (context_p, CBC_EXT_PUSH_NEW_TARGET);
lexer_next_token (context_p);
/* Found "new.target" return here */
return false;
}
#endif /* ENABLED (JERRY_ES2015) */
}
else if (new_was_seen
|| (*grouping_level_p == PARSE_EXPR_LEFT_HAND_SIDE)
@@ -2166,6 +2207,10 @@ parser_append_binary_single_assignment_token (parser_context_t *context_p, /**<
else
{
/* Invalid LeftHandSide expression. */
#if ENABLED (JERRY_ES2015)
parser_check_invalid_new_target (context_p, CBC_ASSIGN);
#endif /* ENABLED (JERRY_ES2015) */
parser_emit_cbc_ext (context_p, CBC_EXT_THROW_REFERENCE_ERROR);
parser_stack_push_uint8 (context_p, CBC_ASSIGN);
}
@@ -2205,6 +2250,10 @@ parser_append_binary_token (parser_context_t *context_p) /**< context */
else
{
/* Invalid LeftHandSide expression. */
#if ENABLED (JERRY_ES2015)
parser_check_invalid_new_target (context_p, CBC_ASSIGN);
#endif /* ENABLED (JERRY_ES2015) */
parser_emit_cbc_ext (context_p, CBC_EXT_THROW_REFERENCE_ERROR);
parser_emit_cbc (context_p, CBC_PUSH_PROP_REFERENCE);
}
@@ -707,6 +707,7 @@ void scanner_cleanup (parser_context_t *context_p);
bool scanner_is_context_needed (parser_context_t *context_p);
#if ENABLED (JERRY_ES2015)
bool scanner_is_global_context_needed (parser_context_t *context_p);
bool scanner_try_scan_new_target (parser_context_t *context_p);
#endif /* ENABLED (JERRY_ES2015) */
void scanner_create_variables (parser_context_t *context_p, uint32_t option_flags);
+8 -1
View File
@@ -3044,7 +3044,14 @@ parser_parse_statements (parser_context_t *context_p) /**< context */
options |= PARSE_EXPR_HAS_LITERAL;
}
if (context_p->status_flags & PARSER_IS_FUNCTION)
#if ENABLED (JERRY_ES2015)
bool is_eval = (context_p->status_flags & PARSER_IS_EVAL) != 0;
#else /* !ENABLED (JERRY_ES2015) */
/* In case of ES5.1 it does not matter that this is an eval parsing or not. */
bool is_eval = false;
#endif /* ENABLED (JERRY_ES2015) */
if ((context_p->status_flags & PARSER_IS_FUNCTION) && !is_eval)
{
parser_parse_expression_statement (context_p, options);
}
+8
View File
@@ -1181,6 +1181,14 @@ parser_error_to_string (parser_error_t error) /**< error code */
{
return "Rest parameter may not have a default initializer.";
}
case PARSER_ERR_NEW_TARGET_EXPECTED:
{
return "Expected new.target expression.";
}
case PARSER_ERR_NEW_TARGET_NOT_ALLOWED:
{
return "new.target expression is not allowed here.";
}
#endif /* ENABLED (JERRY_ES2015) */
#if ENABLED (JERRY_ES2015_MODULE_SYSTEM)
case PARSER_ERR_FILE_NOT_FOUND:
+4
View File
@@ -1982,6 +1982,10 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
{
context.status_flags |= PARSER_IS_EVAL;
}
if (parse_opts & ECMA_PARSE_FUNCTION)
{
context.status_flags |= PARSER_IS_FUNCTION;
}
#endif /* ENABLED (JERRY_ES2015) */
scanner_scan_all (&context,
+2
View File
@@ -144,6 +144,8 @@ typedef enum
PARSER_ERR_DUPLICATED_ARGUMENT_NAMES, /**< duplicated argument names */
PARSER_ERR_INVALID_DESTRUCTURING_PATTERN, /**< invalid destructuring pattern */
PARSER_ERR_ILLEGAL_PROPERTY_IN_DECLARATION, /**< illegal property in declaration context */
PARSER_ERR_NEW_TARGET_EXPECTED, /**< expected new.target expression */
PARSER_ERR_NEW_TARGET_NOT_ALLOWED, /**< new.target is not allowed in the given context */
#endif /* ENABLED (JERRY_ES2015) */
#if ENABLED (JERRY_ES2015_MODULE_SYSTEM)
PARSER_ERR_FILE_NOT_FOUND, /**< file not found*/
+36
View File
@@ -1752,6 +1752,42 @@ scanner_is_global_context_needed (parser_context_t *context_p) /**< context */
return false;
} /* scanner_is_global_context_needed */
/**
* Try to scan/parse the ".target" part in the "new.target" expression.
*
* Upon exiting with "true" the current token will point to the "target"
* literal.
*
* If the "target" literal is not after the "new." then a scanner/parser
* error will be raised.
*
* @returns true if the ".target" part was found
* false if there is no "." after the new.
*/
bool
scanner_try_scan_new_target (parser_context_t *context_p) /**< parser/scanner context */
{
JERRY_ASSERT (context_p->token.type == LEXER_KEYW_NEW);
if (lexer_check_next_character (context_p, LIT_CHAR_DOT))
{
lexer_next_token (context_p);
if (context_p->token.type != LEXER_DOT)
{
parser_raise_error (context_p, PARSER_ERR_INVALID_CHARACTER);
}
lexer_next_token (context_p);
if (!lexer_token_is_identifier (context_p, "target", 6))
{
parser_raise_error (context_p, PARSER_ERR_NEW_TARGET_EXPECTED);
}
return true;
}
return false;
} /* scanner_try_scan_new_target */
#endif /* ENABLED (JERRY_ES2015) */
/**
+7
View File
@@ -75,6 +75,13 @@ scanner_scan_primary_expression (parser_context_t *context_p, /**< context */
case LEXER_KEYW_NEW:
{
scanner_context_p->mode = SCAN_MODE_PRIMARY_EXPRESSION_AFTER_NEW;
#if ENABLED (JERRY_ES2015)
if (scanner_try_scan_new_target (context_p))
{
scanner_context_p->mode = SCAN_MODE_POST_PRIMARY_EXPRESSION;
}
#endif /* ENABLED (JERRY_ES2015) */
break;
}
case LEXER_DIVIDE:
+5
View File
@@ -665,8 +665,13 @@ opfunc_resume_executable_object (vm_executable_object_t *executable_object_p, /*
executable_object_p->frame_ctx.prev_context_p = JERRY_CONTEXT (vm_top_context_p);
JERRY_CONTEXT (vm_top_context_p) = &executable_object_p->frame_ctx;
/* inside the generators the "new.target" is always "undefined" as it can't be invoked with "new" */
ecma_object_t *old_new_target = JERRY_CONTEXT (current_new_target);
JERRY_CONTEXT (current_new_target) = NULL;
ecma_value_t result = vm_execute (&executable_object_p->frame_ctx);
JERRY_CONTEXT (current_new_target) = old_new_target;
executable_object_p->extended_object.u.class_prop.extra_info &= (uint16_t) ~ECMA_EXECUTABLE_OBJECT_RUNNING;
if (executable_object_p->frame_ctx.call_operation != VM_EXEC_RETURN)
+15
View File
@@ -2137,6 +2137,21 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
*stack_top_p++ = ecma_copy_value (collection_p->buffer_p[tagged_idx]);
continue;
}
case VM_OC_PUSH_NEW_TARGET:
{
ecma_object_t *new_target_object = JERRY_CONTEXT (current_new_target);
if (new_target_object == NULL)
{
*stack_top_p++ = ECMA_VALUE_UNDEFINED;
}
else
{
JERRY_ASSERT (new_target_object != JERRY_CONTEXT_INVALID_NEW_TARGET);
ecma_ref_object (new_target_object);
*stack_top_p++ = ecma_make_object_value (new_target_object);
}
continue;
}
#endif /* ENABLED (JERRY_ES2015) */
case VM_OC_PUSH_ELISON:
{
+2
View File
@@ -261,6 +261,7 @@ typedef enum
VM_OC_RETURN_PROMISE, /**< return from an async function */
VM_OC_STRING_CONCAT, /**< string concatenation */
VM_OC_GET_TEMPLATE_OBJECT, /**< GetTemplateObject operation */
VM_OC_PUSH_NEW_TARGET, /**< push new.target onto the stack */
#endif /* ENABLED (JERRY_ES2015) */
VM_OC_NONE, /**< a special opcode for unsupported byte codes */
} vm_oc_types;
@@ -317,6 +318,7 @@ typedef enum
VM_OC_RETURN_PROMISE = VM_OC_NONE, /**< return from an async function */
VM_OC_STRING_CONCAT = VM_OC_NONE, /**< string concatenation */
VM_OC_GET_TEMPLATE_OBJECT = VM_OC_NONE, /**< GetTemplateObject operation */
VM_OC_PUSH_NEW_TARGET = VM_OC_NONE, /**< push new.target onto the stack */
#endif /* !ENABLED (JERRY_ES2015) */
VM_OC_UNUSED = VM_OC_NONE /**< placeholder if the list is empty */