From b6400a25a0ee60467a0287d725e61c876677e103 Mon Sep 17 00:00:00 2001 From: Satya Rohith Date: Mon, 7 Jun 2021 17:49:33 +0530 Subject: refactor(runtime): move performance API to timers extension (#10818) Co-authored-by: Luca Casonato --- .../broadcast_channel/01_broadcast_channel.js | 2 +- extensions/timers/02_performance.js | 364 +++++++++++++++++++++ extensions/timers/lib.rs | 1 + extensions/web/02_structured_clone.js | 73 +++++ extensions/web/lib.rs | 1 + runtime/js/01_web_util.js | 60 ---- runtime/js/40_performance.js | 364 --------------------- 7 files changed, 440 insertions(+), 425 deletions(-) create mode 100644 extensions/timers/02_performance.js create mode 100644 extensions/web/02_structured_clone.js delete mode 100644 runtime/js/40_performance.js diff --git a/extensions/broadcast_channel/01_broadcast_channel.js b/extensions/broadcast_channel/01_broadcast_channel.js index 7670b0cfd..c2937105e 100644 --- a/extensions/broadcast_channel/01_broadcast_channel.js +++ b/extensions/broadcast_channel/01_broadcast_channel.js @@ -105,7 +105,7 @@ constructor(name) { super(); - const prefix = "Failed to construct 'broadcastChannel'"; + const prefix = "Failed to construct 'BroadcastChannel'"; webidl.requiredArguments(arguments.length, 1, { prefix }); this[_name] = webidl.converters["DOMString"](name, { diff --git a/extensions/timers/02_performance.js b/extensions/timers/02_performance.js new file mode 100644 index 000000000..bca98fdbd --- /dev/null +++ b/extensions/timers/02_performance.js @@ -0,0 +1,364 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +"use strict"; + +((window) => { + const { webidl, structuredClone } = window.__bootstrap; + const { opNow } = window.__bootstrap.timers; + const illegalConstructorKey = Symbol("illegalConstructorKey"); + const customInspect = Symbol.for("Deno.customInspect"); + let performanceEntries = []; + + function findMostRecent( + name, + type, + ) { + return performanceEntries + .slice() + .reverse() + .find((entry) => entry.name === name && entry.entryType === type); + } + + function convertMarkToTimestamp(mark) { + if (typeof mark === "string") { + const entry = findMostRecent(mark, "mark"); + if (!entry) { + throw new DOMException( + `Cannot find mark: "${mark}".`, + "SyntaxError", + ); + } + return entry.startTime; + } + if (mark < 0) { + throw new TypeError("Mark cannot be negative."); + } + return mark; + } + + function filterByNameType( + name, + type, + ) { + return performanceEntries.filter( + (entry) => + (name ? entry.name === name : true) && + (type ? entry.entryType === type : true), + ); + } + + const now = opNow; + + class PerformanceEntry { + #name = ""; + #entryType = ""; + #startTime = 0; + #duration = 0; + + get name() { + return this.#name; + } + + get entryType() { + return this.#entryType; + } + + get startTime() { + return this.#startTime; + } + + get duration() { + return this.#duration; + } + + constructor( + name = null, + entryType = null, + startTime = null, + duration = null, + key = null, + ) { + if (key != illegalConstructorKey) { + throw new TypeError("Illegal constructor."); + } + this.#name = name; + this.#entryType = entryType; + this.#startTime = startTime; + this.#duration = duration; + } + + toJSON() { + return { + name: this.#name, + entryType: this.#entryType, + startTime: this.#startTime, + duration: this.#duration, + }; + } + + [customInspect]() { + return `${this.constructor.name} { name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`; + } + } + + class PerformanceMark extends PerformanceEntry { + [Symbol.toStringTag] = "PerformanceMark"; + + #detail = null; + + get detail() { + return this.#detail; + } + + get entryType() { + return "mark"; + } + + constructor( + name, + options = {}, + ) { + const prefix = "Failed to construct 'PerformanceMark'"; + webidl.requiredArguments(arguments.length, 1, { prefix }); + + // ensure options is object-ish, or null-ish + switch (typeof options) { + case "object": // includes null + case "function": + case "undefined": { + break; + } + default: { + throw new TypeError("Invalid options"); + } + } + + const { detail = null, startTime = now() } = options ?? {}; + + super(name, "mark", startTime, 0, illegalConstructorKey); + if (startTime < 0) { + throw new TypeError("startTime cannot be negative"); + } + this.#detail = structuredClone(detail); + } + + toJSON() { + return { + name: this.name, + entryType: this.entryType, + startTime: this.startTime, + duration: this.duration, + detail: this.detail, + }; + } + + [customInspect]() { + return this.detail + ? `${this.constructor.name} {\n detail: ${ + JSON.stringify(this.detail, null, 2) + },\n name: "${this.name}",\n entryType: "${this.entryType}",\n startTime: ${this.startTime},\n duration: ${this.duration}\n}` + : `${this.constructor.name} { detail: ${this.detail}, name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`; + } + } + + class PerformanceMeasure extends PerformanceEntry { + [Symbol.toStringTag] = "PerformanceMeasure"; + + #detail = null; + + get detail() { + return this.#detail; + } + + get entryType() { + return "measure"; + } + + constructor( + name, + startTime, + duration, + detail = null, + key, + ) { + if (key != illegalConstructorKey) { + throw new TypeError("Illegal constructor."); + } + super(name, "measure", startTime, duration, illegalConstructorKey); + this.#detail = structuredClone(detail); + } + + toJSON() { + return { + name: this.name, + entryType: this.entryType, + startTime: this.startTime, + duration: this.duration, + detail: this.detail, + }; + } + + [customInspect]() { + return this.detail + ? `${this.constructor.name} {\n detail: ${ + JSON.stringify(this.detail, null, 2) + },\n name: "${this.name}",\n entryType: "${this.entryType}",\n startTime: ${this.startTime},\n duration: ${this.duration}\n}` + : `${this.constructor.name} { detail: ${this.detail}, name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`; + } + } + + class Performance { + constructor(key = null) { + if (key != illegalConstructorKey) { + throw new TypeError("Illegal constructor."); + } + } + + clearMarks(markName) { + if (markName == null) { + performanceEntries = performanceEntries.filter( + (entry) => entry.entryType !== "mark", + ); + } else { + performanceEntries = performanceEntries.filter( + (entry) => !(entry.name === markName && entry.entryType === "mark"), + ); + } + } + + clearMeasures(measureName) { + if (measureName == null) { + performanceEntries = performanceEntries.filter( + (entry) => entry.entryType !== "measure", + ); + } else { + performanceEntries = performanceEntries.filter( + (entry) => + !(entry.name === measureName && entry.entryType === "measure"), + ); + } + } + + getEntries() { + return filterByNameType(); + } + + getEntriesByName( + name, + type, + ) { + return filterByNameType(name, type); + } + + getEntriesByType(type) { + return filterByNameType(undefined, type); + } + + mark( + markName, + options = {}, + ) { + // 3.1.1.1 If the global object is a Window object and markName uses the + // same name as a read only attribute in the PerformanceTiming interface, + // throw a SyntaxError. - not implemented + const entry = new PerformanceMark(markName, options); + // 3.1.1.7 Queue entry - not implemented + performanceEntries.push(entry); + return entry; + } + + measure( + measureName, + startOrMeasureOptions = {}, + endMark, + ) { + if ( + startOrMeasureOptions && typeof startOrMeasureOptions === "object" && + Object.keys(startOrMeasureOptions).length > 0 + ) { + if (endMark) { + throw new TypeError("Options cannot be passed with endMark."); + } + if ( + !("start" in startOrMeasureOptions) && + !("end" in startOrMeasureOptions) + ) { + throw new TypeError( + "A start or end mark must be supplied in options.", + ); + } + if ( + "start" in startOrMeasureOptions && + "duration" in startOrMeasureOptions && + "end" in startOrMeasureOptions + ) { + throw new TypeError( + "Cannot specify start, end, and duration together in options.", + ); + } + } + let endTime; + if (endMark) { + endTime = convertMarkToTimestamp(endMark); + } else if ( + typeof startOrMeasureOptions === "object" && + "end" in startOrMeasureOptions + ) { + endTime = convertMarkToTimestamp(startOrMeasureOptions.end); + } else if ( + typeof startOrMeasureOptions === "object" && + "start" in startOrMeasureOptions && + "duration" in startOrMeasureOptions + ) { + const start = convertMarkToTimestamp(startOrMeasureOptions.start); + const duration = convertMarkToTimestamp(startOrMeasureOptions.duration); + endTime = start + duration; + } else { + endTime = now(); + } + let startTime; + if ( + typeof startOrMeasureOptions === "object" && + "start" in startOrMeasureOptions + ) { + startTime = convertMarkToTimestamp(startOrMeasureOptions.start); + } else if ( + typeof startOrMeasureOptions === "object" && + "end" in startOrMeasureOptions && + "duration" in startOrMeasureOptions + ) { + const end = convertMarkToTimestamp(startOrMeasureOptions.end); + const duration = convertMarkToTimestamp(startOrMeasureOptions.duration); + startTime = end - duration; + } else if (typeof startOrMeasureOptions === "string") { + startTime = convertMarkToTimestamp(startOrMeasureOptions); + } else { + startTime = 0; + } + const entry = new PerformanceMeasure( + measureName, + startTime, + endTime - startTime, + typeof startOrMeasureOptions === "object" + ? startOrMeasureOptions.detail ?? null + : null, + illegalConstructorKey, + ); + performanceEntries.push(entry); + return entry; + } + + now() { + return now(); + } + } + + const performance = new Performance(illegalConstructorKey); + + window.__bootstrap.performance = { + PerformanceEntry, + PerformanceMark, + PerformanceMeasure, + Performance, + performance, + }; +})(this); diff --git a/extensions/timers/lib.rs b/extensions/timers/lib.rs index e9580c4b5..66e7d05ed 100644 --- a/extensions/timers/lib.rs +++ b/extensions/timers/lib.rs @@ -45,6 +45,7 @@ pub fn init() -> Extension { .js(include_js_files!( prefix "deno:extensions/timers", "01_timers.js", + "02_performance.js", )) .ops(vec![ ("op_global_timer_stop", op_sync(op_global_timer_stop)), diff --git a/extensions/web/02_structured_clone.js b/extensions/web/02_structured_clone.js new file mode 100644 index 000000000..2170095d9 --- /dev/null +++ b/extensions/web/02_structured_clone.js @@ -0,0 +1,73 @@ +// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. + +// @ts-check +/// +/// +/// + +"use strict"; + +((window) => { + const core = window.Deno.core; + + const objectCloneMemo = new WeakMap(); + + function cloneArrayBuffer( + srcBuffer, + srcByteOffset, + srcLength, + _cloneConstructor, + ) { + // this function fudges the return type but SharedArrayBuffer is disabled for a while anyway + return srcBuffer.slice( + srcByteOffset, + srcByteOffset + srcLength, + ); + } + + /** Clone a value in a similar way to structured cloning. It is similar to a +* StructureDeserialize(StructuredSerialize(...)). */ + function structuredClone(value) { + // Performance optimization for buffers, otherwise + // `serialize/deserialize` will allocate new buffer. + if (value instanceof ArrayBuffer) { + const cloned = cloneArrayBuffer( + value, + 0, + value.byteLength, + ArrayBuffer, + ); + objectCloneMemo.set(value, cloned); + return cloned; + } + if (ArrayBuffer.isView(value)) { + const clonedBuffer = structuredClone(value.buffer); + // Use DataViewConstructor type purely for type-checking, can be a + // DataView or TypedArray. They use the same constructor signature, + // only DataView has a length in bytes and TypedArrays use a length in + // terms of elements, so we adjust for that. + let length; + if (value instanceof DataView) { + length = value.byteLength; + } else { + length = value.length; + } + return new (value.constructor)( + clonedBuffer, + value.byteOffset, + length, + ); + } + + try { + return core.deserialize(core.serialize(value)); + } catch (e) { + if (e instanceof TypeError) { + throw new DOMException("Uncloneable value", "DataCloneError"); + } + throw e; + } + } + + window.__bootstrap.structuredClone = structuredClone; +})(globalThis); diff --git a/extensions/web/lib.rs b/extensions/web/lib.rs index 2f2f15942..95adb822a 100644 --- a/extensions/web/lib.rs +++ b/extensions/web/lib.rs @@ -33,6 +33,7 @@ pub fn init() -> Extension { "01_dom_exception.js", "01_mimesniff.js", "02_event.js", + "02_structured_clone.js", "03_abort_signal.js", "04_global_interfaces.js", "05_base64.js", diff --git a/runtime/js/01_web_util.js b/runtime/js/01_web_util.js index 5960691eb..11294a9bb 100644 --- a/runtime/js/01_web_util.js +++ b/runtime/js/01_web_util.js @@ -17,65 +17,6 @@ } } - const objectCloneMemo = new WeakMap(); - - function cloneArrayBuffer( - srcBuffer, - srcByteOffset, - srcLength, - _cloneConstructor, - ) { - // this function fudges the return type but SharedArrayBuffer is disabled for a while anyway - return srcBuffer.slice( - srcByteOffset, - srcByteOffset + srcLength, - ); - } - - /** Clone a value in a similar way to structured cloning. It is similar to a - * StructureDeserialize(StructuredSerialize(...)). */ - function cloneValue(value) { - // Performance optimization for buffers, otherwise - // `serialize/deserialize` will allocate new buffer. - if (value instanceof ArrayBuffer) { - const cloned = cloneArrayBuffer( - value, - 0, - value.byteLength, - ArrayBuffer, - ); - objectCloneMemo.set(value, cloned); - return cloned; - } - if (ArrayBuffer.isView(value)) { - const clonedBuffer = cloneValue(value.buffer); - // Use DataViewConstructor type purely for type-checking, can be a - // DataView or TypedArray. They use the same constructor signature, - // only DataView has a length in bytes and TypedArrays use a length in - // terms of elements, so we adjust for that. - let length; - if (value instanceof DataView) { - length = value.byteLength; - } else { - length = value.length; - } - return new (value.constructor)( - clonedBuffer, - value.byteOffset, - length, - ); - } - - try { - return Deno.core.deserialize(Deno.core.serialize(value)); - } catch (e) { - if (e instanceof TypeError) { - throw new DOMException("Uncloneable value", "DataCloneError"); - } - throw e; - } - } - const handlerSymbol = Symbol("eventHandlers"); function makeWrappedHandler(handler) { function wrappedHandler(...args) { @@ -114,6 +55,5 @@ illegalConstructorKey, requiredArguments, defineEventHandler, - cloneValue, }; })(this); diff --git a/runtime/js/40_performance.js b/runtime/js/40_performance.js deleted file mode 100644 index 24c35b5c0..000000000 --- a/runtime/js/40_performance.js +++ /dev/null @@ -1,364 +0,0 @@ -// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. -"use strict"; - -((window) => { - const { opNow } = window.__bootstrap.timers; - const { cloneValue, illegalConstructorKey } = window.__bootstrap.webUtil; - const { requiredArguments } = window.__bootstrap.webUtil; - - const customInspect = Symbol.for("Deno.customInspect"); - let performanceEntries = []; - - function findMostRecent( - name, - type, - ) { - return performanceEntries - .slice() - .reverse() - .find((entry) => entry.name === name && entry.entryType === type); - } - - function convertMarkToTimestamp(mark) { - if (typeof mark === "string") { - const entry = findMostRecent(mark, "mark"); - if (!entry) { - throw new DOMException( - `Cannot find mark: "${mark}".`, - "SyntaxError", - ); - } - return entry.startTime; - } - if (mark < 0) { - throw new TypeError("Mark cannot be negative."); - } - return mark; - } - - function filterByNameType( - name, - type, - ) { - return performanceEntries.filter( - (entry) => - (name ? entry.name === name : true) && - (type ? entry.entryType === type : true), - ); - } - - const now = opNow; - - class PerformanceEntry { - #name = ""; - #entryType = ""; - #startTime = 0; - #duration = 0; - - get name() { - return this.#name; - } - - get entryType() { - return this.#entryType; - } - - get startTime() { - return this.#startTime; - } - - get duration() { - return this.#duration; - } - - constructor( - name = null, - entryType = null, - startTime = null, - duration = null, - key = null, - ) { - if (key != illegalConstructorKey) { - throw new TypeError("Illegal constructor."); - } - this.#name = name; - this.#entryType = entryType; - this.#startTime = startTime; - this.#duration = duration; - } - - toJSON() { - return { - name: this.#name, - entryType: this.#entryType, - startTime: this.#startTime, - duration: this.#duration, - }; - } - - [customInspect]() { - return `${this.constructor.name} { name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`; - } - } - - class PerformanceMark extends PerformanceEntry { - [Symbol.toStringTag] = "PerformanceMark"; - - #detail = null; - - get detail() { - return this.#detail; - } - - get entryType() { - return "mark"; - } - - constructor( - name, - options = {}, - ) { - requiredArguments("PerformanceMark", arguments.length, 1); - - // ensure options is object-ish, or null-ish - switch (typeof options) { - case "object": // includes null - case "function": - case "undefined": { - break; - } - default: { - throw new TypeError("Invalid options"); - } - } - - const { detail = null, startTime = now() } = options ?? {}; - - super(name, "mark", startTime, 0, illegalConstructorKey); - if (startTime < 0) { - throw new TypeError("startTime cannot be negative"); - } - this.#detail = cloneValue(detail); - } - - toJSON() { - return { - name: this.name, - entryType: this.entryType, - startTime: this.startTime, - duration: this.duration, - detail: this.detail, - }; - } - - [customInspect]() { - return this.detail - ? `${this.constructor.name} {\n detail: ${ - JSON.stringify(this.detail, null, 2) - },\n name: "${this.name}",\n entryType: "${this.entryType}",\n startTime: ${this.startTime},\n duration: ${this.duration}\n}` - : `${this.constructor.name} { detail: ${this.detail}, name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`; - } - } - - class PerformanceMeasure extends PerformanceEntry { - [Symbol.toStringTag] = "PerformanceMeasure"; - - #detail = null; - - get detail() { - return this.#detail; - } - - get entryType() { - return "measure"; - } - - constructor( - name, - startTime, - duration, - detail = null, - key, - ) { - if (key != illegalConstructorKey) { - throw new TypeError("Illegal constructor."); - } - super(name, "measure", startTime, duration, illegalConstructorKey); - this.#detail = cloneValue(detail); - } - - toJSON() { - return { - name: this.name, - entryType: this.entryType, - startTime: this.startTime, - duration: this.duration, - detail: this.detail, - }; - } - - [customInspect]() { - return this.detail - ? `${this.constructor.name} {\n detail: ${ - JSON.stringify(this.detail, null, 2) - },\n name: "${this.name}",\n entryType: "${this.entryType}",\n startTime: ${this.startTime},\n duration: ${this.duration}\n}` - : `${this.constructor.name} { detail: ${this.detail}, name: "${this.name}", entryType: "${this.entryType}", startTime: ${this.startTime}, duration: ${this.duration} }`; - } - } - - class Performance { - constructor(key = null) { - if (key != illegalConstructorKey) { - throw new TypeError("Illegal constructor."); - } - } - - clearMarks(markName) { - if (markName == null) { - performanceEntries = performanceEntries.filter( - (entry) => entry.entryType !== "mark", - ); - } else { - performanceEntries = performanceEntries.filter( - (entry) => !(entry.name === markName && entry.entryType === "mark"), - ); - } - } - - clearMeasures(measureName) { - if (measureName == null) { - performanceEntries = performanceEntries.filter( - (entry) => entry.entryType !== "measure", - ); - } else { - performanceEntries = performanceEntries.filter( - (entry) => - !(entry.name === measureName && entry.entryType === "measure"), - ); - } - } - - getEntries() { - return filterByNameType(); - } - - getEntriesByName( - name, - type, - ) { - return filterByNameType(name, type); - } - - getEntriesByType(type) { - return filterByNameType(undefined, type); - } - - mark( - markName, - options = {}, - ) { - // 3.1.1.1 If the global object is a Window object and markName uses the - // same name as a read only attribute in the PerformanceTiming interface, - // throw a SyntaxError. - not implemented - const entry = new PerformanceMark(markName, options); - // 3.1.1.7 Queue entry - not implemented - performanceEntries.push(entry); - return entry; - } - - measure( - measureName, - startOrMeasureOptions = {}, - endMark, - ) { - if ( - startOrMeasureOptions && typeof startOrMeasureOptions === "object" && - Object.keys(startOrMeasureOptions).length > 0 - ) { - if (endMark) { - throw new TypeError("Options cannot be passed with endMark."); - } - if ( - !("start" in startOrMeasureOptions) && - !("end" in startOrMeasureOptions) - ) { - throw new TypeError( - "A start or end mark must be supplied in options.", - ); - } - if ( - "start" in startOrMeasureOptions && - "duration" in startOrMeasureOptions && - "end" in startOrMeasureOptions - ) { - throw new TypeError( - "Cannot specify start, end, and duration together in options.", - ); - } - } - let endTime; - if (endMark) { - endTime = convertMarkToTimestamp(endMark); - } else if ( - typeof startOrMeasureOptions === "object" && - "end" in startOrMeasureOptions - ) { - endTime = convertMarkToTimestamp(startOrMeasureOptions.end); - } else if ( - typeof startOrMeasureOptions === "object" && - "start" in startOrMeasureOptions && - "duration" in startOrMeasureOptions - ) { - const start = convertMarkToTimestamp(startOrMeasureOptions.start); - const duration = convertMarkToTimestamp(startOrMeasureOptions.duration); - endTime = start + duration; - } else { - endTime = now(); - } - let startTime; - if ( - typeof startOrMeasureOptions === "object" && - "start" in startOrMeasureOptions - ) { - startTime = convertMarkToTimestamp(startOrMeasureOptions.start); - } else if ( - typeof startOrMeasureOptions === "object" && - "end" in startOrMeasureOptions && - "duration" in startOrMeasureOptions - ) { - const end = convertMarkToTimestamp(startOrMeasureOptions.end); - const duration = convertMarkToTimestamp(startOrMeasureOptions.duration); - startTime = end - duration; - } else if (typeof startOrMeasureOptions === "string") { - startTime = convertMarkToTimestamp(startOrMeasureOptions); - } else { - startTime = 0; - } - const entry = new PerformanceMeasure( - measureName, - startTime, - endTime - startTime, - typeof startOrMeasureOptions === "object" - ? startOrMeasureOptions.detail ?? null - : null, - illegalConstructorKey, - ); - performanceEntries.push(entry); - return entry; - } - - now() { - return now(); - } - } - - const performance = new Performance(illegalConstructorKey); - - window.__bootstrap.performance = { - PerformanceEntry, - PerformanceMark, - PerformanceMeasure, - Performance, - performance, - }; -})(this); -- cgit v1.2.3