check descriptor before private field access (#12910)

* fix: check descriptor before private field access

* add test cases
This commit is contained in:
Huáng Jùnliàng 2021-03-03 15:06:38 -05:00 committed by GitHub
parent b12a4de457
commit 70c77e550c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 154 additions and 0 deletions

View File

@ -1336,6 +1336,9 @@ helpers.classPrivateFieldSet = helper("7.0.0-beta.0")`
helpers.classPrivateFieldDestructureSet = helper("7.4.4")` helpers.classPrivateFieldDestructureSet = helper("7.4.4")`
export default function _classPrivateFieldDestructureSet(receiver, privateMap) { export default function _classPrivateFieldDestructureSet(receiver, privateMap) {
if (privateMap === undefined) {
throw new TypeError("attempted to set private static field before its declaration");
}
if (!privateMap.has(receiver)) { if (!privateMap.has(receiver)) {
throw new TypeError("attempted to set private field on non-instance"); throw new TypeError("attempted to set private field on non-instance");
} }
@ -1367,6 +1370,9 @@ helpers.classStaticPrivateFieldSpecGet = helper("7.0.2")`
if (receiver !== classConstructor) { if (receiver !== classConstructor) {
throw new TypeError("Private static access of wrong provenance"); throw new TypeError("Private static access of wrong provenance");
} }
if (descriptor === undefined) {
throw new TypeError("attempted to get private static field before its declaration");
}
if (descriptor.get) { if (descriptor.get) {
return descriptor.get.call(receiver); return descriptor.get.call(receiver);
} }
@ -1379,6 +1385,9 @@ helpers.classStaticPrivateFieldSpecSet = helper("7.0.2")`
if (receiver !== classConstructor) { if (receiver !== classConstructor) {
throw new TypeError("Private static access of wrong provenance"); throw new TypeError("Private static access of wrong provenance");
} }
if (descriptor === undefined) {
throw new TypeError("attempted to set private static field before its declaration");
}
if (descriptor.set) { if (descriptor.set) {
descriptor.set.call(receiver, value); descriptor.set.call(receiver, value);
} else { } else {

View File

@ -0,0 +1,29 @@
expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
C.#p;
}
}
}).toThrow(/attempted to use private field on non-instance/);
expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
C.#p = 0;
}
}
}).toThrow(/attempted to use private field on non-instance/);
expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
for (C.#p of [0]);
}
}
}).toThrow(/attempted to use private field on non-instance/);

View File

@ -0,0 +1,29 @@
expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
C.#p;
}
}
}).toThrow(/attempted to get private static field before its declaration/);
expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
C.#p = 0;
}
}
}).toThrow(/attempted to set private static field before its declaration/);
expect(() => {
class C {
static #_ = new C;
static #p;
constructor() {
for (C.#p of [0]);
}
}
}).toThrow(/attempted to set private static field before its declaration/);

View File

@ -0,0 +1,29 @@
expect(() => {
class C {
static #_ = new C;
static get #p() { return C };
constructor() {
C.#p;
}
}
}).toThrow(/attempted to use private field on non-instance/);
expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
C.#p = 0;
}
}
}).toThrow(/attempted to use private field on non-instance/);
expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
for (C.#p of [0]);
}
}
}).toThrow(/attempted to use private field on non-instance/);

View File

@ -0,0 +1,29 @@
expect(() => {
class C {
static #_ = new C;
static get #p() { return C };
constructor() {
C.#p;
}
}
}).toThrow(/attempted to use private field on non-instance/);
expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
C.#p = 0;
}
}
}).toThrow(/attempted to use private field on non-instance/);
expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
for (C.#p of [0]);
}
}
}).toThrow(/attempted to use private field on non-instance/);

View File

@ -0,0 +1,29 @@
expect(() => {
class C {
static #_ = new C;
static get #p() { return C };
constructor() {
C.#p;
}
}
}).toThrow(/attempted to get private static field before its declaration/);
expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
C.#p = 0;
}
}
}).toThrow(/attempted to set private static field before its declaration/);
expect(() => {
class C {
static #_ = new C;
static set #p(v) {};
constructor() {
for (C.#p of [0]);
}
}
}).toThrow(/attempted to set private static field before its declaration/);