v8/test/mjsunit/harmony/private-accessors.js
Joyee Cheung 7fa12e2a4f [class] fix undefined private name access in computed property keys
This patch implements https://github.com/tc39/proposal-class-fields/pull/269
and makes sure we always throw TypeError when there is invalid private
name access in computed property keys.

Before this patch, private name variables of private fields and methods
are initialized together with computed property keys in the order they
are declared. Accessing undefined private names in the computed property
keys thus fail silently.

After this patch, we initialize the private name variables of private
fields before we initialize the computed property keys, so that invalid
access to private fields in the computed keys can be checked in the IC.
We now also initialize the brand early, so that invalid access to private
methods or accessors in the computed keys throw TypeError during brand
checks - and since these accesses are guarded by brand checks, we can
create the private methods and accessors after the class is
defined, and merge the home object setting with the creation
of the closures.

Bug: v8:8330, v8:9611
Change-Id: I01363f7befac6cf9dd28ec229b99a99102bcf012
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1846571
Commit-Queue: Joyee Cheung <joyee@igalia.com>
Reviewed-by: Mythri Alle <mythria@chromium.org>
Reviewed-by: Toon Verwaest <verwaest@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64225}
2019-10-10 19:43:01 +00:00

116 lines
2.2 KiB
JavaScript

// 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.
{
let store = 1;
class C {
get #a() { return store; }
set #a(val) { store = val; }
incA() { this.#a++; } // CountOperation
setA(val) { this.#a = val; }
getA() { return this.#a; }
}
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());
}
// 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();
}
// Nested private accessors.
{
class C {
get #a() {
let storeD = 'd';
class D {
// Shadows outer #a
get #a() { return storeD; }
getD() { return this.#a; }
}
return new D;
}
getA() {
return this.#a;
}
}
assertEquals('d', new C().getA().getD());
}
{
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);
}
// 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);
}