Skip to content

Commit 070096f

Browse files
author
Robert Fancsik
authored
Fix arrow function this binding resolving if environment record is present (#4878)
This patch fixes #4872 and fixes #4876. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik [email protected]
1 parent 7e0aa08 commit 070096f

File tree

6 files changed

+90
-8
lines changed

6 files changed

+90
-8
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,13 @@ ecma_op_function_call_simple (ecma_object_t *func_obj_p, /**< Function object */
11591159
}
11601160

11611161
this_binding = arrow_func_p->this_binding;
1162+
1163+
if (JERRY_UNLIKELY (this_binding == ECMA_VALUE_UNINITIALIZED))
1164+
{
1165+
ecma_environment_record_t *env_record_p = ecma_op_get_environment_record (scope_p);
1166+
JERRY_ASSERT (env_record_p);
1167+
this_binding = env_record_p->this_binding;
1168+
}
11621169
break;
11631170
}
11641171

jerry-core/ecma/operations/ecma-lex-env.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,9 +579,15 @@ ecma_op_get_environment_record (ecma_object_t *lex_env_p) /**< lexical environme
579579
}
580580
}
581581

582-
JERRY_ASSERT (lex_env_p->u2.outer_reference_cp != JMEM_CP_NULL);
582+
if (lex_env_p->u2.outer_reference_cp == JMEM_CP_NULL)
583+
{
584+
break;
585+
}
586+
583587
lex_env_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, lex_env_p->u2.outer_reference_cp);
584588
}
589+
590+
return NULL;
585591
} /* ecma_op_get_environment_record */
586592

587593
/**

jerry-core/vm/opcodes.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,14 +1959,11 @@ opfunc_form_super_reference (ecma_value_t **vm_stack_top_p, /**< current vm stac
19591959
ecma_value_t prop_name, /**< property name to resolve */
19601960
uint8_t opcode) /**< current cbc opcode */
19611961
{
1962-
if (CBC_FUNCTION_GET_TYPE (frame_ctx_p->shared_p->bytecode_header_p->status_flags) == CBC_FUNCTION_CONSTRUCTOR)
1963-
{
1964-
ecma_environment_record_t *environment_record_p = ecma_op_get_environment_record (frame_ctx_p->lex_env_p);
1962+
ecma_environment_record_t *environment_record_p = ecma_op_get_environment_record (frame_ctx_p->lex_env_p);
19651963

1966-
if (!ecma_op_this_binding_is_initialized (environment_record_p))
1967-
{
1968-
return ecma_raise_reference_error (ECMA_ERR_CALL_SUPER_CONSTRUCTOR_DERIVED_CLASS_BEFORE_THIS);
1969-
}
1964+
if (environment_record_p && !ecma_op_this_binding_is_initialized (environment_record_p))
1965+
{
1966+
return ecma_raise_reference_error (ECMA_ERR_CALL_SUPER_CONSTRUCTOR_DERIVED_CLASS_BEFORE_THIS);
19701967
}
19711968

19721969
ecma_value_t parent = ecma_op_resolve_super_base (frame_ctx_p->lex_env_p);

jerry-core/vm/vm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
566566
ecma_value_t completion_value;
567567

568568
ecma_environment_record_t *environment_record_p = ecma_op_get_environment_record (frame_ctx_p->lex_env_p);
569+
JERRY_ASSERT (environment_record_p);
569570

570571
if (!ecma_is_constructor (func_value))
571572
{
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
class M {
16+
get foo() {
17+
return this._x;
18+
}
19+
set foo(x) {
20+
this._x = x;
21+
}
22+
}
23+
24+
class T5 extends M {
25+
constructor() {
26+
(() => super.foo = 20)();
27+
}
28+
}
29+
30+
try {
31+
new T5
32+
assert(false);
33+
} catch (e) {
34+
assert(e instanceof ReferenceError);
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
class M {
16+
constructor() {
17+
this._x = 45;
18+
}
19+
20+
get foo() {
21+
return this._x;
22+
}
23+
}
24+
25+
class N extends M {
26+
constructor(x = () => super.foo) {
27+
super();
28+
assert(x() === 45);
29+
}
30+
31+
x(x = () => super.foo) {
32+
return x();
33+
}
34+
}
35+
36+
assert(new N().x() === 45);

0 commit comments

Comments
 (0)