js/src/tests/test262/language/statements/class/elements/private-getter-visible-to-direct-eval.js
raw symbols SCIP| 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: Private getter is visible to direct eval code |
| 6 | esid: sec-privatefieldget |
| 7 | info: | |
| 8 | PrivateFieldGet (P, O) |
| 9 | 1. Assert: P is a Private Name. |
| 10 | 2. If O is not an object, throw a TypeError exception. |
| 11 | 3. If P.[[Kind]] is "field", |
| 12 | a. Let entry be PrivateFieldFind(P, O). |
| 13 | b. If entry is empty, throw a TypeError exception. |
| 14 | c. Return entry.[[PrivateFieldValue]]. |
| 15 | 4. Perform ? PrivateBrandCheck(O, P). |
| 16 | 5. If P.[[Kind]] is "method", |
| 17 | a. Return P.[[Value]]. |
| 18 | 6. Else, |
| 19 | a. Assert: P.[[Kind]] is "accessor". |
| 20 | b. If P does not have a [[Get]] field, throw a TypeError exception. |
| 21 | c. Let getter be P.[[Get]]. |
| 22 | d. Return ? Call(getter, O). |
| 23 | |
| 24 | ClassElementName : PrivateIdentifier |
| 25 | 1. Let privateIdentifier be StringValue of PrivateIdentifier. |
| 26 | 2. Let privateName be NewPrivateName(privateIdentifier). |
| 27 | 3. Let scope be the running execution context's PrivateEnvironment. |
| 28 | 4. Let scopeEnvRec be scope's EnvironmentRecord. |
| 29 | 5. Perform ! scopeEnvRec.InitializeBinding(privateIdentifier, privateName). |
| 30 | 6. Return privateName. |
| 31 | |
| 32 | MakePrivateReference ( baseValue, privateIdentifier ) |
| 33 | 1. Let env be the running execution context's PrivateEnvironment. |
| 34 | 2. Let privateNameBinding be ? ResolveBinding(privateIdentifier, env). |
| 35 | 3. Let privateName be GetValue(privateNameBinding). |
| 36 | 4. Assert: privateName is a Private Name. |
| 37 | 5. Return a value of type Reference whose base value is baseValue, whose referenced name is privateName, whose strict reference flag is true. |
| 38 | features: [class-methods-private, class] |
| 39 | ---*/ |
| 40 | |
| 41 | class C { |
| 42 | get #m() { return "Test262"; }; |
| 43 | |
| 44 | getWithEval() { |
| 45 | return eval("this.#m"); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | class D { |
| 50 | get #m() { throw new Test262Error(); }; |
| 51 | } |
| 52 | |
| 53 | let c = new C(); |
| 54 | assert.sameValue(c.getWithEval(), "Test262"); |
| 55 | |
| 56 | let d = new D(); |
| 57 | assert.throws(TypeError, function() { |
| 58 | c.getWithEval.call(d); |
| 59 | }, "invalid access to a private getter"); |
| 60 | |
| 61 | reportCompare(0, 0); |