From ea0668a27e2d89dd7924d577b6bcf3f87ed3d986 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 17 Feb 2022 23:15:01 +0800 Subject: [PATCH] lib: use class fields in Event and EventTarget MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://bugs.chromium.org/p/v8/issues/detail?id=10704 is already fixed, so switch back to class fields instead of using symbol properties. PR-URL: https://github.com/nodejs/node/pull/42361 Refs: https://github.com/nodejs/node/commit/b1c3909bd766327a569c2e4279a4670454f3f9db Reviewed-By: Michaƫl Zasso Reviewed-By: Benjamin Gruenbaum Reviewed-By: Luigi Pinca Reviewed-By: Antoine du Hamel Reviewed-By: Mestery Reviewed-By: Darshan Sen --- lib/internal/event_target.js | 48 +++++++++++++++++------------------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 7c539db2eb3c2a..0f9a896396f79c 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -63,16 +63,7 @@ const kTrustEvent = Symbol('kTrustEvent'); const { now } = require('internal/perf/utils'); -// TODO(joyeecheung): V8 snapshot does not support instance member -// initializers for now: -// https://bugs.chromium.org/p/v8/issues/detail?id=10704 const kType = Symbol('type'); -const kDefaultPrevented = Symbol('defaultPrevented'); -const kCancelable = Symbol('cancelable'); -const kTimestamp = Symbol('timestamp'); -const kBubbles = Symbol('bubbles'); -const kComposed = Symbol('composed'); -const kPropagationStopped = Symbol('propagationStopped'); const isTrustedSet = new SafeWeakSet(); const isTrusted = ObjectGetOwnPropertyDescriptor({ @@ -86,6 +77,13 @@ function isEvent(value) { } class Event { + #cancelable = false; + #bubbles = false; + #composed = false; + #defaultPrevented = false; + #timestamp = now(); + #propagationStopped = false; + /** * @param {string} type * @param {{ @@ -101,13 +99,11 @@ class Event { allowArray: true, allowFunction: true, nullable: true, }); const { cancelable, bubbles, composed } = { ...options }; - this[kCancelable] = !!cancelable; - this[kBubbles] = !!bubbles; - this[kComposed] = !!composed; + this.#cancelable = !!cancelable; + this.#bubbles = !!bubbles; + this.#composed = !!composed; + this[kType] = `${type}`; - this[kDefaultPrevented] = false; - this[kTimestamp] = now(); - this[kPropagationStopped] = false; if (options?.[kTrustEvent]) { isTrustedSet.add(this); } @@ -135,9 +131,9 @@ class Event { return `${name} ${inspect({ type: this[kType], - defaultPrevented: this[kDefaultPrevented], - cancelable: this[kCancelable], - timeStamp: this[kTimestamp], + defaultPrevented: this.#defaultPrevented, + cancelable: this.#cancelable, + timeStamp: this.#timestamp, }, opts)}`; } @@ -150,7 +146,7 @@ class Event { preventDefault() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - this[kDefaultPrevented] = true; + this.#defaultPrevented = true; } /** @@ -195,7 +191,7 @@ class Event { get cancelable() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kCancelable]; + return this.#cancelable; } /** @@ -204,7 +200,7 @@ class Event { get defaultPrevented() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kCancelable] && this[kDefaultPrevented]; + return this.#cancelable && this.#defaultPrevented; } /** @@ -213,7 +209,7 @@ class Event { get timeStamp() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kTimestamp]; + return this.#timestamp; } @@ -244,7 +240,7 @@ class Event { get bubbles() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kBubbles]; + return this.#bubbles; } /** @@ -253,7 +249,7 @@ class Event { get composed() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kComposed]; + return this.#composed; } /** @@ -271,7 +267,7 @@ class Event { get cancelBubble() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - return this[kPropagationStopped]; + return this.#propagationStopped; } /** @@ -288,7 +284,7 @@ class Event { stopPropagation() { if (!isEvent(this)) throw new ERR_INVALID_THIS('Event'); - this[kPropagationStopped] = true; + this.#propagationStopped = true; } static NONE = 0;