static-private-method-and-instance-method-brand-check.js (1989B)
1 // This file was procedurally generated from the following sources: 2 // - src/class-elements/static-private-method-and-instance-method-brand-check.case 3 // - src/class-elements/default/cls-expr.template 4 /*--- 5 description: Brand for static private names and instance private names are different (field definitions in a class expression) 6 esid: prod-FieldDefinition 7 features: [class-static-methods-private, class-methods-private, class] 8 flags: [generated] 9 info: | 10 ClassTail : ClassHeritage { ClassBody } 11 ... 12 32. If PrivateBoundIdentifiers of ClassBody contains a Private Name P such that P's [[Kind]] field is either "method" or "accessor" and P's [[Brand]] field is proto, 13 a. Set F.[[PrivateBrand]] to proto. 14 33. If PrivateBoundIdentifiers of ClassBody contains a Private Name P such that P's [[Kind]] field is either "method" or "accessor" and P's [[Brand]] is F, 15 a. PrivateBrandAdd(F, F). 16 ... 17 18 PrivateFieldGet (P, O) 19 1. Assert: P is a Private Name. 20 2. If O is not an object, throw a TypeError exception. 21 3. If P.[[Kind]] is "field", 22 ... 23 4. Perform ? PrivateBrandCheck(O, P). 24 5. If P.[[Kind]] is "method", 25 a. Return P.[[Value]]. 26 ... 27 28 PrivateBrandCheck(O, P) 29 1. If O.[[PrivateBrands]] does not contain an entry e such that SameValue(e, P.[[Brand]]) is true, 30 a. Throw a TypeError exception. 31 32 ---*/ 33 34 35 var C = class { 36 static #f() { 37 return 'static'; 38 } 39 40 static access() { 41 return this.#f(); 42 } 43 44 #instanceMethod() { 45 return 'instance'; 46 } 47 48 instanceAccess() { 49 return this.#instanceMethod(); 50 } 51 } 52 53 let c = new C(); 54 assert.sameValue(C.access(), 'static'); 55 assert.sameValue(c.instanceAccess(), 'instance'); 56 57 assert.throws(TypeError, function() { 58 C.access.call(c); 59 }, 'Accessed static private method from instance of C'); 60 61 assert.throws(TypeError, function() { 62 c.instanceAccess.call(C); 63 }, 'Accessed instance private method from C'); 64 65 66 reportCompare(0, 0);