private-methods.js 5.61 KB
Newer Older
1 2 3 4 5 6
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

"use strict";

7
// Basic private method test
8
{
9
  let calledWith;
10
  class C {
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    #a(arg) { calledWith = arg; }
    callA(arg) { this.#a(arg); }
  }

  const c = new C;
  assertEquals(undefined, c.a);
  assertEquals(undefined, calledWith);
  c.callA(1);
  assertEquals(1, calledWith);
}

// Call private method in another instance
{
  class C {
    #a(arg) { this.calledWith = arg; }
    callAIn(obj, arg) { obj.#a(arg); }
27
  }
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

  const c = new C;
  const c2 = new C;

  assertEquals(undefined, c.a);
  assertEquals(undefined, c.calledWith);
  assertEquals(undefined, c2.calledWith);

  c2.callAIn(c, 'fromC2');
  assertEquals('fromC2', c.calledWith);
  assertEquals(undefined, c2.calledWith);

  c2.callAIn(c2, 'c2');
  assertEquals('fromC2', c.calledWith);
  assertEquals('c2', c2.calledWith);

  assertThrows(() => { c2.callAIn({}); }, TypeError);
45 46
}

47
// Private methods and private fields
48 49
{
  class C {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
    #a;
    constructor(a) {
      this.#a = a;
    }
    #getAPlus1() {
      return this.#a + 1;
    }
    equals(obj) {
      return this.#getAPlus1() === obj.#getAPlus1();
    }
  }
  const c = new C(0);
  const c2 = new C(2);
  const c3 = new C(2);
  assertEquals(true, c2.equals(c3));
  assertEquals(false, c2.equals(c));
  assertEquals(false, c3.equals(c));
}

// Class inheritance
{
  class A {
    #val;
    constructor(a) {
      this.#val = a;
    }
    #a() { return this.#val; }
    getA() { return this.#a(); }
  }
  class B extends A {
    constructor(b) {
      super(b);
    }
    b() { return this.getA() }
  }
  const b = new B(1);
  assertEquals(1, b.b());
}

// Private members should be accessed according to the class the
// invoked method is in.
{
  class A {
    #val;
    constructor(a) {
      this.#val = a;
    }
    #getVal() { return this.#val; }
    getA() { return this.#getVal(); }
    getVal() { return this.#getVal(); }
  }

  class B extends A {
    #val;
    constructor(a, b) {
      super(a);
      this.#val = b;
    }
    #getVal() { return this.#val; }
    getB() { return this.#getVal(); }
    getVal() { return this.#getVal(); }
  }

  const b = new B(1, 2);
  assertEquals(1, b.getA());
  assertEquals(2, b.getB());
  assertEquals(1, A.prototype.getVal.call(b));
  assertEquals(2, B.prototype.getVal.call(b));
  const a = new A(1);
  assertEquals(1, a.getA());
  assertThrows(() => B.prototype.getB.call(a), TypeError);
}

// Private methods in nested classes.
{
  class C {
    #b() {
127
      class B {
128 129
        #foo(arg) { return arg; }
        callFoo(arg) { return this.#foo(arg); }
130
      }
131
      return new B();
132
    }
133
    createB() { return this.#b(); }
134
  }
135 136 137
  const c = new C;
  const b = c.createB();
  assertEquals(1, b.callFoo(1));
138 139
}

140
// Private methods in nested classes with inheritance.
141
{
142 143 144 145 146
  class C {
    #b() {
      class B extends C {
        #foo(arg) { return arg; }
        callFoo(arg) { return this.#foo(arg); }
147
      }
148
      return new B();
149
    }
150
    createB() { return this.#b(); }
151 152
  }

153 154 155 156 157
  const c = new C;
  const b = c.createB();
  assertEquals(1, b.callFoo(1));
  const b2 = b.createB();
  assertEquals(1, b2.callFoo(1));
158 159
}

160
// Class expressions.
161 162
{
  const C = class {
163 164 165 166 167 168 169
    #a() { return 1; }
    callA(obj) { return obj.#a() }
  };
  const c = new C;
  const c2 = new C;
  assertEquals(1, c.callA(c));
  assertEquals(1, c.callA(c2));
170 171
}

172
// Nested class expressions.
173 174
{
  const C = class {
175
    #b() {
176
      const B = class {
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
        #foo(arg) { return arg; }
        callFoo(arg) { return this.#foo(arg); }
      };
      return new B();
    }
    createB() { return this.#b(); }
  };

  const c = new C;
  const b = c.createB();
  assertEquals(1, b.callFoo(1));
}


// Nested class expressions with hierarchy.
{
  const C = class {
    #b() {
      const B = class extends C {
        #foo(arg) { return arg; }
        callFoo(arg) { return this.#foo(arg); }
198
      }
199
      return new B();
200
    }
201
    createB() { return this.#b(); }
202
  }
203 204 205 206 207 208

  const c = new C;
  const b = c.createB();
  assertEquals(1, b.callFoo(1));
  const b2 = b.createB();
  assertEquals(1, b2.callFoo(1));
209 210
}

211
// Adding the brand twice on the same object should throw.
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
{
  class A {
    constructor(arg) {
      return arg;
    }
  }

  class C extends A {
    #x() { }

    constructor(arg) {
      super(arg);
    }
  }

  let c1 = new C({});
  assertThrows(() => new C(c1), TypeError);
}

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
// Private methods should be not visible to proxies.
{
  class X {
    #x() {}
    x() { this.#x(); };
    callX(obj) { obj.#x(); }
  }
  let handlerCalled = false;
  const x = new X();
  let p = new Proxy(new X, {
    apply(target, thisArg, argumentsList) {
      handlerCalled = true;
      Reflect.apply(target, thisArg, argumentsList);
    }
  });
  assertThrows(() => p.x(), TypeError);
  assertThrows(() => x.callX(p), TypeError);
  assertThrows(() => X.prototype.x.call(p), TypeError);
  assertThrows(() => X.prototype.callX(p), TypeError);
  assertEquals(false, handlerCalled);
}

// Reference outside of class.
{
  class C {
    #a() {}
  }
  assertThrows('new C().#a()');
}

// Duplicate private names.
{
  assertThrows('class C { #a = 1; #a() {} }');
}

266
{
267 268 269 270
  class A extends class { } {
    #a() {}
  }

271 272 273 274 275 276 277 278 279 280
  class D extends class {
    #c() {}
  } {
    #d() {}
  }

  class E extends D {
    #e() {}
  }

281
  new A;
282 283 284
  new D;
  new E;
}
285 286 287 288 289 290 291 292 293 294 295 296 297 298

// Super access within private methods.
{
  class A {
    foo() { return 1; }
  }

  class C extends A {
    #m() { return super.foo; }
    fn() { return this.#m()(); }
  }

  assertEquals(1, new C().fn());
}
299 300 301 302 303 304 305 306 307

{
  assertThrows(() => {
    class A {
      [this.#a] = 1;
      #a() { }
    }
  }, TypeError);
}