Skip to content

Commit a5a55cc

Browse files
committed
Add more static class property ordering tests
Since tc39/ecma262#1490, the "length" and "name" properties of a class are defined before any static methods. This is tested by tc39#2057, in test/language/computed-property-names of all places. At the same time, static methods with "name" as the name would overwrite the original property, but retain the original property enumeration order. This was not previously tested. In fact, the overwriting behavior was not tested at all for the "length" property. This commit mends both holes in test coverage.
1 parent bad7c04 commit a5a55cc

File tree

2 files changed

+102
-7
lines changed

2 files changed

+102
-7
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (C) 2015 the V8 project authors. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-runtime-semantics-classdefinitionevaluation
6+
description: >
7+
Function `length` attribute not inferred in presence of static `length` method
8+
info: |
9+
ClassTail : ClassHeritage_opt { ClassBody_opt }
10+
11+
14. If constructor is empty, then [...]
12+
b. Let F be ! CreateBuiltinFunction(steps, 0, className, « [[ConstructorKind]], [[SourceText]] », empty, constructorParent).
13+
15. Else,
14+
a. Let constructorInfo be ! DefineMethod of constructor with arguments proto and constructorParent.
15+
[ This sets the length property on constructorInfo.[[Closure]]. ]
16+
b. Let F be constructorInfo.[[Closure]].
17+
[...]
18+
25. For each ClassElement e of elements, do
19+
a. If IsStatic of e is false, then [...]
20+
b. Else,
21+
i. Let field be ClassElementEvaluation of e with arguments F and false.
22+
[ This overwrites the length property on F. ]
23+
includes: [compareArray.js]
24+
features: [generators]
25+
---*/
26+
27+
class A {
28+
static method() {
29+
$ERROR('Static method should not be executed during definition');
30+
}
31+
static length() {
32+
$ERROR('Static method should not be executed during definition');
33+
}
34+
}
35+
36+
assert.sameValue(typeof A.length, 'function');
37+
assert(compareArray(Object.getOwnPropertyNames(A), ['length', 'name', 'prototype', 'method']))
38+
39+
var attr = 'length';
40+
class B {
41+
static [attr]() {
42+
$ERROR(
43+
'Static method defined via computed property should not be executed ' +
44+
'during definition'
45+
);
46+
}
47+
}
48+
49+
assert.sameValue(typeof B.length, 'function');
50+
assert(compareArray(Object.getOwnPropertyNames(B), ['length', 'name', 'prototype']))
51+
52+
var isDefined = false;
53+
class C {
54+
static get length() {
55+
if (isDefined) {
56+
return 'pass';
57+
}
58+
$ERROR('Static `get` accessor should not be executed during definition');
59+
}
60+
}
61+
62+
isDefined = true;
63+
assert.sameValue(C.length, 'pass');
64+
assert(compareArray(Object.getOwnPropertyNames(C), ['length', 'name', 'prototype']))
65+
66+
class D {
67+
static set length(_) {
68+
$ERROR('Static `set` accessor should not be executed during definition');
69+
}
70+
}
71+
72+
assert.sameValue(D.length, undefined);
73+
assert(compareArray(Object.getOwnPropertyNames(D), ['length', 'name', 'prototype']))
74+
75+
class E {
76+
static *length() {
77+
$ERROR('Static GeneratorMethod should not be executed during definition');
78+
}
79+
}
80+
81+
assert.sameValue(typeof E.length, 'function');
82+
assert(compareArray(Object.getOwnPropertyNames(E), ['length', 'name', 'prototype']))

test/language/statements/class/definition/fn-name-static-precedence.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,36 @@
22
// This code is governed by the BSD license found in the LICENSE file.
33

44
/*---
5-
es6id: 14.5.15
5+
esid: sec-runtime-semantics-classdefinitionevaluation
66
description: >
77
Function `name` attribute not inferred in presence of static `name` method
88
info: |
9-
ClassDeclaration : class BindingIdentifier ClassTail
9+
ClassTail : ClassHeritage_opt { ClassBody_opt }
1010
11-
[...]
12-
4. Let hasNameProperty be HasOwnProperty(value, "name").
13-
5. ReturnIfAbrupt(hasNameProperty).
14-
6. If hasNameProperty is false, then perform SetFunctionName(value,
15-
className).
11+
14. If constructor is empty, then [...]
12+
b. Let F be ! CreateBuiltinFunction(steps, 0, className, « [[ConstructorKind]], [[SourceText]] », empty, constructorParent).
13+
15. Else, [...]
14+
d. Perform ! SetFunctionName(F, className).
15+
25. For each ClassElement e of elements, do
16+
a. If IsStatic of e is false, then [...]
17+
b. Else,
18+
i. Let field be ClassElementEvaluation of e with arguments F and false.
19+
[ This overwrites the name property on F. ]
20+
includes: [compareArray.js]
1621
features: [generators]
1722
---*/
1823

1924
class A {
25+
static method() {
26+
$ERROR('Static method should not be executed during definition');
27+
}
2028
static name() {
2129
$ERROR('Static method should not be executed during definition');
2230
}
2331
}
2432

2533
assert.sameValue(typeof A.name, 'function');
34+
assert(compareArray(Object.getOwnPropertyNames(A), ['length', 'name', 'prototype', 'method']))
2635

2736
var attr = 'name';
2837
class B {
@@ -35,6 +44,7 @@ class B {
3544
}
3645

3746
assert.sameValue(typeof B.name, 'function');
47+
assert(compareArray(Object.getOwnPropertyNames(B), ['length', 'name', 'prototype']))
3848

3949
var isDefined = false;
4050
class C {
@@ -48,6 +58,7 @@ class C {
4858

4959
isDefined = true;
5060
assert.sameValue(C.name, 'pass');
61+
assert(compareArray(Object.getOwnPropertyNames(C), ['length', 'name', 'prototype']))
5162

5263
class D {
5364
static set name(_) {
@@ -56,6 +67,7 @@ class D {
5667
}
5768

5869
assert.sameValue(D.name, undefined);
70+
assert(compareArray(Object.getOwnPropertyNames(D), ['length', 'name', 'prototype']))
5971

6072
class E {
6173
static *name() {
@@ -64,3 +76,4 @@ class E {
6476
}
6577

6678
assert.sameValue(typeof E.name, 'function');
79+
assert(compareArray(Object.getOwnPropertyNames(E), ['length', 'name', 'prototype']))

0 commit comments

Comments
 (0)