From 95312ab53a3a118955278a27a0297a17889ad36e Mon Sep 17 00:00:00 2001 From: cjihrig Date: Wed, 11 May 2022 21:15:54 -0400 Subject: [PATCH] fix: make Performance global an EventTarget This commit updates the Performance global to extend EventTarget. --- cli/dts/lib.deno.shared_globals.d.ts | 2 +- cli/tests/unit/performance_test.ts | 13 +++++++++++++ ext/web/15_performance.js | 18 ++++++++++++++---- tools/wpt/expectation.json | 8 ++------ 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/cli/dts/lib.deno.shared_globals.d.ts b/cli/dts/lib.deno.shared_globals.d.ts index d430c16b07..efd59ca2ee 100644 --- a/cli/dts/lib.deno.shared_globals.d.ts +++ b/cli/dts/lib.deno.shared_globals.d.ts @@ -449,7 +449,7 @@ declare class Worker extends EventTarget { declare type PerformanceEntryList = PerformanceEntry[]; -declare class Performance { +declare class Performance extends EventTarget { /** Returns a timestamp representing the start of the performance measurement. */ readonly timeOrigin: number; constructor(); diff --git a/cli/tests/unit/performance_test.ts b/cli/tests/unit/performance_test.ts index 917546e2ca..687bb024d0 100644 --- a/cli/tests/unit/performance_test.ts +++ b/cli/tests/unit/performance_test.ts @@ -140,3 +140,16 @@ Deno.test(function performanceMeasureIllegalConstructor() { "Illegal constructor", ); }); + +Deno.test(function performanceIsEventTarget() { + assert(performance instanceof EventTarget); + + return new Promise((resolve) => { + const handler = () => { + resolve(); + }; + + performance.addEventListener("test", handler, { once: true }); + performance.dispatchEvent(new Event("test")); + }); +}); diff --git a/ext/web/15_performance.js b/ext/web/15_performance.js index 10e0140083..e7eafd5337 100644 --- a/ext/web/15_performance.js +++ b/ext/web/15_performance.js @@ -327,9 +327,14 @@ } webidl.configurePrototype(PerformanceMeasure); const PerformanceMeasurePrototype = PerformanceMeasure.prototype; - class Performance { - constructor() { - webidl.illegalConstructor(); + class Performance extends EventTarget { + constructor(key = null) { + if (key != illegalConstructorKey) { + webidl.illegalConstructor(); + } + + super(); + this[webidl.brand] = webidl.brand; } get timeOrigin() { @@ -572,12 +577,17 @@ webidl.configurePrototype(Performance); const PerformancePrototype = Performance.prototype; + webidl.converters["Performance"] = webidl.createInterfaceConverter( + "Performance", + PerformancePrototype, + ); + window.__bootstrap.performance = { PerformanceEntry, PerformanceMark, PerformanceMeasure, Performance, - performance: webidl.createBranded(Performance), + performance: new Performance(illegalConstructorKey), setTimeOrigin, }; })(this); diff --git a/tools/wpt/expectation.json b/tools/wpt/expectation.json index e3e87a44c1..c3f6b19f39 100644 --- a/tools/wpt/expectation.json +++ b/tools/wpt/expectation.json @@ -1161,12 +1161,8 @@ "hr-time": { "monotonic-clock.any.html": true, "monotonic-clock.any.worker.html": true, - "basic.any.html": [ - "Performance interface extends EventTarget." - ], - "basic.any.worker.html": [ - "Performance interface extends EventTarget." - ], + "basic.any.html": true, + "basic.any.worker.html": true, "idlharness.any.html": [ "Performance interface: existence and properties of interface object", "Performance interface: existence and properties of interface prototype object",