| 1 |
// Copyright (C) 2019 Caio Lima (Igalia SL). All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
description: Every new evaluation of a class creates a different Private Name (private static field) |
| 6 |
esid: sec-runtime-semantics-evaluate-name |
| 7 |
info: | |
| 8 |
ClassElementName : PrivateIdentifier |
| 9 |
1. Let privateIdentifier be StringValue of PrivateIdentifier. |
| 10 |
2. Let privateName be NewPrivateName(privateIdentifier). |
| 11 |
3. Let scope be the running execution context's PrivateEnvironment. |
| 12 |
4. Let scopeEnvRec be scope's EnvironmentRecord. |
| 13 |
5. Perform ! scopeEnvRec.InitializeBinding(privateIdentifier, privateName). |
| 14 |
6. Return privateName. |
| 15 |
|
| 16 |
ClassTail : ClassHeritage { ClassBody } |
| 17 |
... |
| 18 |
27. Let staticFields be a new empty List. |
| 19 |
28. For each ClassElement e in order from elements, |
| 20 |
a. If IsStatic of e is false, then |
| 21 |
... |
| 22 |
b. Else, |
| 23 |
i. Let field be the result of performing PropertyDefinitionEvaluation for m ClassElementEvaluation for e with arguments F and false. |
| 24 |
c. If field is an abrupt completion, then |
| 25 |
... |
| 26 |
d. If field is not empty, |
| 27 |
i. If IsStatic of e is false, append field to instanceFields. |
| 28 |
ii. Otherwise, append field to staticFields. |
| 29 |
... |
| 30 |
34. For each item fieldRecord in order from staticFields, |
| 31 |
a. Perform ? DefineField(F, field). |
| 32 |
... |
| 33 |
|
| 34 |
DefineField(receiver, fieldRecord) |
| 35 |
... |
| 36 |
8. If fieldName is a Private Name, |
| 37 |
a. Perform ? PrivateFieldAdd(fieldName, receiver, initValue). |
| 38 |
features: [class, class-static-fields-private] |
| 39 |
flags: [noStrict] |
| 40 |
---*/ |
| 41 |
|
| 42 |
let classStringExpression = `( |
| 43 |
class { |
| 44 |
static #m = 'test262'; |
| 45 |
|
| 46 |
static access() { |
| 47 |
return this.#m; |
| 48 |
} |
| 49 |
} |
| 50 |
)`; |
| 51 |
|
| 52 |
let evalClass = function (_eval) { |
| 53 |
return _eval(classStringExpression); |
| 54 |
}; |
| 55 |
|
| 56 |
let C1 = evalClass(eval); |
| 57 |
let C2 = evalClass(eval); |
| 58 |
|
| 59 |
assert.sameValue(C1.access(), 'test262'); |
| 60 |
assert.sameValue(C2.access(), 'test262'); |
| 61 |
|
| 62 |
assert.throws(TypeError, function() { |
| 63 |
C1.access.call(C2); |
| 64 |
}, 'invalid access of c1 private static field'); |
| 65 |
|
| 66 |
assert.throws(TypeError, function() { |
| 67 |
C2.access.call(C1); |
| 68 |
}, 'invalid access of c2 private static field'); |
| 69 |
|
| 70 |
reportCompare(0, 0); |