2019-07-30 12:34:09 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Flags: --harmony-private-methods
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// Complementary private accessors.
|
|
|
|
{
|
2019-08-30 01:09:00 +00:00
|
|
|
let store = 1;
|
2019-07-30 12:34:09 +00:00
|
|
|
class C {
|
2019-08-30 01:09:00 +00:00
|
|
|
get #a() { return store; }
|
|
|
|
set #a(val) { store = val; }
|
|
|
|
incA() { this.#a++; } // CountOperation
|
|
|
|
setA(val) { this.#a = val; }
|
|
|
|
getA() { return this.#a; }
|
2019-07-30 12:34:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 01:09:00 +00:00
|
|
|
const c = new C;
|
|
|
|
assertEquals(store, c.getA());
|
|
|
|
assertEquals(1, c.getA());
|
|
|
|
c.setA(2);
|
|
|
|
assertEquals(store, c.getA());
|
|
|
|
assertEquals(2, c.getA());
|
|
|
|
c.incA();
|
|
|
|
assertEquals(store, c.getA());
|
|
|
|
assertEquals(3, c.getA());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compound assignment.
|
|
|
|
{
|
|
|
|
let store;
|
|
|
|
class A {
|
|
|
|
get #a() { return store; }
|
|
|
|
set #a(val) { store = val; }
|
|
|
|
getA() { return this.#a; }
|
|
|
|
constructor(val) {
|
|
|
|
({ y: this.#a } = val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const a = new A({y: 'test'});
|
|
|
|
assertEquals('test', a.getA());
|
2019-07-30 12:34:09 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 14:48:28 +00:00
|
|
|
// Accessing super in private accessors.
|
|
|
|
{
|
|
|
|
class A { foo(val) {} }
|
|
|
|
class C extends A {
|
|
|
|
set #a(val) { super.foo(val); }
|
|
|
|
}
|
|
|
|
new C();
|
|
|
|
|
|
|
|
class D extends A {
|
|
|
|
get #a() { return super.foo; }
|
|
|
|
}
|
|
|
|
new D();
|
|
|
|
|
|
|
|
class E extends A {
|
|
|
|
set #a(val) { super.foo(val); }
|
|
|
|
get #a() { return super.foo; }
|
|
|
|
}
|
|
|
|
new E();
|
|
|
|
}
|
|
|
|
|
2019-07-30 12:34:09 +00:00
|
|
|
// Nested private accessors.
|
|
|
|
{
|
|
|
|
class C {
|
|
|
|
get #a() {
|
2019-08-30 01:09:00 +00:00
|
|
|
let storeD = 'd';
|
|
|
|
class D {
|
|
|
|
// Shadows outer #a
|
|
|
|
get #a() { return storeD; }
|
|
|
|
getD() { return this.#a; }
|
|
|
|
}
|
2019-07-30 12:34:09 +00:00
|
|
|
return new D;
|
|
|
|
}
|
2019-08-30 01:09:00 +00:00
|
|
|
getA() {
|
|
|
|
return this.#a;
|
|
|
|
}
|
2019-07-30 12:34:09 +00:00
|
|
|
}
|
2019-08-30 01:09:00 +00:00
|
|
|
assertEquals('d', new C().getA().getD());
|
2019-07-30 12:34:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-10 19:00:28 +00:00
|
|
|
{
|
|
|
|
assertThrows(() => {
|
|
|
|
class A {
|
|
|
|
[this.#a] = 1;
|
|
|
|
get #a() {}
|
|
|
|
}
|
|
|
|
}, TypeError);
|
|
|
|
|
|
|
|
assertThrows(() => {
|
|
|
|
class A {
|
|
|
|
[this.#a] = 1;
|
|
|
|
set #a(val) {}
|
|
|
|
}
|
|
|
|
}, TypeError);
|
|
|
|
|
|
|
|
assertThrows(() => {
|
|
|
|
class A {
|
|
|
|
[this.#a] = 1;
|
|
|
|
set #a(val) {}
|
|
|
|
get #a() {}
|
|
|
|
}
|
|
|
|
}, TypeError);
|
|
|
|
}
|
|
|
|
|
2019-07-30 12:34:09 +00:00
|
|
|
// Duplicate private accessors.
|
|
|
|
// https://tc39.es/proposal-private-methods/#sec-static-semantics-early-errors
|
|
|
|
{
|
|
|
|
assertThrows('class C { get #a() {} get #a() {} }', SyntaxError);
|
|
|
|
assertThrows('class C { set #a(val) {} set #a(val) {} }', SyntaxError);
|
|
|
|
}
|