Skip to content

Commit 608bc9e

Browse files
galpeterrerobika
authored andcommitted
Improve new.target handling in case of eval calls (#3517)
Only direct eval calls propagate the "new.target" information. In any other eval cases the "new.target" invocation is a syntax error. Added test cases also. JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent 05b4bda commit 608bc9e

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

jerry-core/ecma/operations/ecma-eval.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters b
100100
#if ENABLED (JERRY_ES2015)
101101
ECMA_CLEAR_SUPER_EVAL_PARSER_OPTS ();
102102

103-
/* If an eval is used inside the function the info should be propagated. */
104-
if (JERRY_CONTEXT (current_new_target) != JERRY_CONTEXT_INVALID_NEW_TARGET)
103+
/* If a direct eval is used inside the function the info should be propagated. */
104+
if (JERRY_CONTEXT (current_new_target) != JERRY_CONTEXT_INVALID_NEW_TARGET
105+
&& (JERRY_CONTEXT (status_flags) & ECMA_STATUS_DIRECT_EVAL))
105106
{
106107
parse_opts |= ECMA_PARSE_FUNCTION;
107108
}

jerry-core/ecma/operations/ecma-function-object.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,8 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
11381138
{
11391139
#if ENABLED (JERRY_ES2015)
11401140
ecma_object_t *old_new_target = JERRY_CONTEXT (current_new_target);
1141-
if (JERRY_LIKELY (!ecma_get_object_is_builtin (func_obj_p)))
1141+
/* If the current function is not a direct eval call the "new.target" must be updated. */
1142+
if ((JERRY_CONTEXT (status_flags) & ECMA_STATUS_DIRECT_EVAL) == 0)
11421143
{
11431144
JERRY_CONTEXT (current_new_target) = NULL;
11441145
}

tests/jerry/es2015/new-target.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,30 @@ function eval_eval_test () {
9191

9292
new eval_eval_test ();
9393

94+
/* new.target is only valid in direct eval */
95+
function eval_test_2 () {
96+
var ev = eval;
97+
try {
98+
ev ("new.target");
99+
assert (false);
100+
} catch (ex) {
101+
assert (ex instanceof SyntaxError);
102+
}
103+
}
104+
105+
new eval_test_2 ();
106+
107+
function eval_test_3 () {
108+
var ev = eval;
109+
try {
110+
eval ("ev ('new.target')");
111+
assert (false);
112+
} catch (ex) {
113+
assert (ex instanceof SyntaxError);
114+
}
115+
}
116+
117+
new eval_test_3 ();
94118

95119
/* test assignment of the "new.target" */
96120
function expect_syntax_error (src)

0 commit comments

Comments
 (0)