Skip to content

Commit c903064

Browse files
galpeterrerobika
authored andcommitted
Correctly handle call arguments when using 'super' calls (#2996)
There are two interconnected changes here: * Added missing argument number handling for `super.propname(..)` calls. * The `super.propname.call(...)` is does not need extra 'this' binding helper. Fixes #2990. JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent dfafb1a commit c903064

File tree

5 files changed

+332
-1
lines changed

5 files changed

+332
-1
lines changed

jerry-core/parser/js/js-parser-expr.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,9 +1532,31 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
15321532
static void
15331533
parser_process_unary_expression (parser_context_t *context_p) /**< context */
15341534
{
1535+
#if ENABLED (JERRY_ES2015_CLASS)
1536+
/* Track to see if a property was accessed or not */
1537+
bool property_accessed = false;
1538+
#endif /* ENABLED (JERRY_ES2015_CLASS) */
1539+
15351540
/* Parse postfix part of a primary expression. */
15361541
while (true)
15371542
{
1543+
#if ENABLED (JERRY_ES2015_CLASS)
1544+
if (context_p->token.type == LEXER_DOT || context_p->token.type == LEXER_LEFT_SQUARE)
1545+
{
1546+
if (property_accessed)
1547+
{
1548+
/**
1549+
* In the case of "super.prop1.prop2(...)" the second property access should not
1550+
* generate a super prop call thus the 'PARSER_CLASS_SUPER_PROP_REFERENCE' flags should be removed.
1551+
*
1552+
* Similar case: "super[propname].prop2(...)"
1553+
*/
1554+
context_p->status_flags &= (uint32_t) ~PARSER_CLASS_SUPER_PROP_REFERENCE;
1555+
}
1556+
property_accessed = true;
1557+
}
1558+
#endif /* ENABLED (JERRY_ES2015_CLASS) */
1559+
15381560
/* Since break would only break the switch, we use
15391561
* continue to continue this loop. Without continue,
15401562
* the code abandons the loop. */

jerry-core/vm/vm.c

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,48 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
15661566
}
15671567
case VM_OC_SUPER_PROP_REFERENCE:
15681568
{
1569-
const int index = (byte_code_start_p[1] == CBC_EXT_SUPER_PROP_ASSIGN) ? -1 : -3;
1569+
/**
1570+
* In case of this VM_OC_SUPER_PROP_REFERENCE the previously pushed 'super' must be replaced
1571+
* with the current 'this' value to correctly access the properties.
1572+
*/
1573+
int index = -1; /* -1 in case of CBC_EXT_SUPER_PROP_ASSIGN */
1574+
1575+
if (JERRY_LIKELY (byte_code_start_p[1] == CBC_EXT_SUPER_PROP_CALL))
1576+
{
1577+
cbc_opcode_t next_call_opcode = (cbc_opcode_t) byte_code_start_p[2];
1578+
/* The next opcode must be a call opcode */
1579+
JERRY_ASSERT (CBC_CALL <= next_call_opcode && next_call_opcode <= CBC_CALL2_PROP_BLOCK);
1580+
1581+
int arguments_list_len;
1582+
if (next_call_opcode >= CBC_CALL0)
1583+
{
1584+
/* CBC_CALL{0,1,2}* have their arg count encoded, extract it from there */
1585+
arguments_list_len = (int) ((next_call_opcode - CBC_CALL0) / 6);
1586+
}
1587+
else
1588+
{
1589+
/**
1590+
* In this case the arguments are coded into the byte code stream as a byte argument
1591+
* following the call opcode.
1592+
*/
1593+
arguments_list_len = (int) byte_code_start_p[3];
1594+
}
1595+
/* The old 'super' value is at least '-3' element away from the current position on the stack. */
1596+
index = -3 - arguments_list_len;
1597+
}
1598+
else
1599+
{
1600+
/**
1601+
* The bytecode order for super assignment should be one of this:
1602+
* - CBC_EXT_PUSH_SUPER, CBC_EXT_SUPER_PROP_ASSIGN.
1603+
* - CBC_EXT_PUSH_CONSTRUCTOR_SUPER_PROP, CBC_EXT_SUPER_PROP_ASSIGN.
1604+
* That is one ext opcode back (-1).
1605+
*/
1606+
JERRY_ASSERT (byte_code_start_p[-1] == CBC_EXT_PUSH_SUPER
1607+
|| byte_code_start_p[-1] == CBC_EXT_PUSH_CONSTRUCTOR_SUPER_PROP);
1608+
}
1609+
1610+
/* Replace the old 'super' value with the correct 'this' binding */
15701611
ecma_free_value (stack_top_p[index]);
15711612
stack_top_p[index] = ecma_copy_value (frame_ctx_p->this_binding);
15721613
continue;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 Base {
16+
constructor () {
17+
this.parent_value = 100;
18+
}
19+
20+
parent_value () {
21+
return this.parent_value;
22+
}
23+
24+
parent_value_arg (a, b, c) {
25+
if (c) {
26+
return this.parent_value + a + b + c;
27+
} else if (b) {
28+
return this.parent_value + a + b;
29+
} else {
30+
return this.parent_value + a;
31+
}
32+
}
33+
34+
method () {
35+
return {
36+
method: function (a, b, c, d, e) { return -50 + a + b + c + d + e; }
37+
}
38+
}
39+
}
40+
41+
class Target extends Base {
42+
constructor () {
43+
super ();
44+
this.parent_value = -10;
45+
}
46+
47+
parent_value () {
48+
throw new Error ('(parent_value)');
49+
}
50+
51+
parent_value_direct () {
52+
return super.parent_value ();
53+
}
54+
55+
parent_value_direct_arg (a, b, c) {
56+
if (c) {
57+
return super.parent_value_arg (a, b, c);
58+
} else if (b) {
59+
return super.parent_value_arg (a, b);
60+
} else {
61+
return super.parent_value_arg (a);
62+
}
63+
}
64+
65+
method () {
66+
throw new Error ("(method)");
67+
}
68+
69+
parent_method_dot () {
70+
return super.method ().method (1, 2, 3, 4, 5)
71+
}
72+
73+
parent_method_index () {
74+
return super['method']()['method'](1, 2, 3, 4, 5);
75+
}
76+
}
77+
78+
79+
var obj = new Target ();
80+
81+
assert (obj.parent_value_direct () === -10);
82+
assert (obj.parent_value_direct_arg (1) === -9);
83+
assert (obj.parent_value_direct_arg (1, 2) === -7);
84+
assert (obj.parent_value_direct_arg (1, 2, 3) === -4);
85+
86+
try {
87+
obj.parent_value();
88+
assert (false)
89+
} catch (ex) {
90+
/* 'obj.parent_value is a number! */
91+
assert (ex instanceof TypeError);
92+
}
93+
94+
assert (obj.parent_method_dot () === -35);
95+
assert (obj.parent_method_index () === -35);
96+
97+
try {
98+
obj.method ();
99+
assert (false);
100+
} catch (ex) {
101+
assert (ex.message === '(method)');
102+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 Base {
16+
constructor () {
17+
this.parent_value = 100;
18+
}
19+
20+
parent_value () {
21+
return this.parent_value;
22+
}
23+
24+
parent_value_arg (a, b, c) {
25+
if (c) {
26+
return this.parent_value + a + b + c;
27+
} else if (b) {
28+
return this.parent_value + a + b;
29+
} else {
30+
return this.parent_value + a;
31+
}
32+
}
33+
34+
method () {
35+
return {
36+
method: function (a, b, c, d, e) { return -50 + a + b + c + d + e; }
37+
}
38+
}
39+
}
40+
41+
class Target extends Base {
42+
constructor () {
43+
super ();
44+
this.parent_value = -10;
45+
}
46+
47+
parent_value () {
48+
throw new Error ('(parent_value)');
49+
}
50+
51+
parent_value_indirect () {
52+
return super.parent_value.call (this);
53+
}
54+
55+
parent_value_indirect_arg (a, b, c) {
56+
if (c) {
57+
return super.parent_value_arg.call (this, a, b, c);
58+
} else if (b) {
59+
return super.parent_value_arg.call (this, a, b);
60+
} else {
61+
return super.parent_value_arg.call (this, a);
62+
}
63+
}
64+
65+
method () {
66+
throw new Error ("(method)");
67+
}
68+
69+
parent_method_dot () {
70+
return super.method.call (this).method (1, 2, 3, 4, 5)
71+
}
72+
73+
parent_method_index() {
74+
return super['method'].call (this)['method'] (1, 2, 3, 4, 5);
75+
}
76+
}
77+
78+
79+
var obj = new Target();
80+
81+
assert (obj.parent_value_indirect () === -10);
82+
assert (obj.parent_value_indirect_arg (1) === -9);
83+
assert (obj.parent_value_indirect_arg (1, 2) === -7);
84+
assert (obj.parent_value_indirect_arg (1, 2, 3) === -4);
85+
86+
try {
87+
obj.parent_value ();
88+
assert (false);
89+
} catch (ex) {
90+
/* 'obj.parent_value is a number! */
91+
assert (ex instanceof TypeError);
92+
}
93+
94+
assert (obj.parent_method_dot () === -35);
95+
assert (obj.parent_method_index () === -35);
96+
97+
try {
98+
obj.method();
99+
assert (false);
100+
} catch (ex) {
101+
assert (ex.message === '(method)');
102+
}
103+
104+
var demo_object = {
105+
parent_value: 1000,
106+
method: function () {
107+
throw new Error ('Very bad!');
108+
}
109+
}
110+
111+
assert (obj.parent_value_indirect_arg.call (demo_object, 1) === 1001);
112+
assert (obj.parent_value_indirect_arg.call (demo_object, 1, 2) === 1003);
113+
114+
assert (obj.parent_method_dot.call (demo_object) === -35);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 A {
16+
constructor() {
17+
this.id = 50000;
18+
}
19+
20+
getid(a) {
21+
if (typeof a === 'number') {
22+
this.id += a;
23+
}
24+
return this.id;
25+
}
26+
}
27+
28+
class B extends A {
29+
constructor() {
30+
super();
31+
this.id = 100;
32+
}
33+
34+
getid_A() {
35+
return super.getid.call(this);
36+
}
37+
38+
getid_B(a) {
39+
return super.getid.call(this, a);
40+
}
41+
getid_C(a) {
42+
var fn = super.getid.bind(this);
43+
return fn(a);
44+
}
45+
}
46+
47+
var obj = new B();
48+
49+
assert (obj.getid_A() === 100);
50+
assert (obj.getid_B(1) === 101);
51+
assert (obj.getid_C(1) === 102);
52+
assert (obj.id === 102);

0 commit comments

Comments
 (0)