mirror of
https://github.com/denoland/deno.git
synced 2024-11-25 15:29:32 -05:00
refactor(runtime): move performance API to timers extension (#10818)
Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
parent
89290741d1
commit
b6400a25a0
6 changed files with 82 additions and 67 deletions
|
@ -105,7 +105,7 @@
|
||||||
constructor(name) {
|
constructor(name) {
|
||||||
super();
|
super();
|
||||||
|
|
||||||
const prefix = "Failed to construct 'broadcastChannel'";
|
const prefix = "Failed to construct 'BroadcastChannel'";
|
||||||
webidl.requiredArguments(arguments.length, 1, { prefix });
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
||||||
|
|
||||||
this[_name] = webidl.converters["DOMString"](name, {
|
this[_name] = webidl.converters["DOMString"](name, {
|
||||||
|
|
|
@ -2,10 +2,9 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
((window) => {
|
((window) => {
|
||||||
|
const { webidl, structuredClone } = window.__bootstrap;
|
||||||
const { opNow } = window.__bootstrap.timers;
|
const { opNow } = window.__bootstrap.timers;
|
||||||
const { cloneValue, illegalConstructorKey } = window.__bootstrap.webUtil;
|
const illegalConstructorKey = Symbol("illegalConstructorKey");
|
||||||
const { requiredArguments } = window.__bootstrap.webUtil;
|
|
||||||
|
|
||||||
const customInspect = Symbol.for("Deno.customInspect");
|
const customInspect = Symbol.for("Deno.customInspect");
|
||||||
let performanceEntries = [];
|
let performanceEntries = [];
|
||||||
|
|
||||||
|
@ -118,7 +117,8 @@
|
||||||
name,
|
name,
|
||||||
options = {},
|
options = {},
|
||||||
) {
|
) {
|
||||||
requiredArguments("PerformanceMark", arguments.length, 1);
|
const prefix = "Failed to construct 'PerformanceMark'";
|
||||||
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
||||||
|
|
||||||
// ensure options is object-ish, or null-ish
|
// ensure options is object-ish, or null-ish
|
||||||
switch (typeof options) {
|
switch (typeof options) {
|
||||||
|
@ -138,7 +138,7 @@
|
||||||
if (startTime < 0) {
|
if (startTime < 0) {
|
||||||
throw new TypeError("startTime cannot be negative");
|
throw new TypeError("startTime cannot be negative");
|
||||||
}
|
}
|
||||||
this.#detail = cloneValue(detail);
|
this.#detail = structuredClone(detail);
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
|
@ -184,7 +184,7 @@
|
||||||
throw new TypeError("Illegal constructor.");
|
throw new TypeError("Illegal constructor.");
|
||||||
}
|
}
|
||||||
super(name, "measure", startTime, duration, illegalConstructorKey);
|
super(name, "measure", startTime, duration, illegalConstructorKey);
|
||||||
this.#detail = cloneValue(detail);
|
this.#detail = structuredClone(detail);
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
|
@ -45,6 +45,7 @@ pub fn init<P: TimersPermission + 'static>() -> Extension {
|
||||||
.js(include_js_files!(
|
.js(include_js_files!(
|
||||||
prefix "deno:extensions/timers",
|
prefix "deno:extensions/timers",
|
||||||
"01_timers.js",
|
"01_timers.js",
|
||||||
|
"02_performance.js",
|
||||||
))
|
))
|
||||||
.ops(vec![
|
.ops(vec![
|
||||||
("op_global_timer_stop", op_sync(op_global_timer_stop)),
|
("op_global_timer_stop", op_sync(op_global_timer_stop)),
|
||||||
|
|
73
extensions/web/02_structured_clone.js
Normal file
73
extensions/web/02_structured_clone.js
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||||
|
|
||||||
|
// @ts-check
|
||||||
|
/// <reference path="../../core/lib.deno_core.d.ts" />
|
||||||
|
/// <reference path="../web/internal.d.ts" />
|
||||||
|
/// <reference path="../web/lib.deno_web.d.ts" />
|
||||||
|
|
||||||
|
"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);
|
|
@ -33,6 +33,7 @@ pub fn init() -> Extension {
|
||||||
"01_dom_exception.js",
|
"01_dom_exception.js",
|
||||||
"01_mimesniff.js",
|
"01_mimesniff.js",
|
||||||
"02_event.js",
|
"02_event.js",
|
||||||
|
"02_structured_clone.js",
|
||||||
"03_abort_signal.js",
|
"03_abort_signal.js",
|
||||||
"04_global_interfaces.js",
|
"04_global_interfaces.js",
|
||||||
"05_base64.js",
|
"05_base64.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");
|
const handlerSymbol = Symbol("eventHandlers");
|
||||||
function makeWrappedHandler(handler) {
|
function makeWrappedHandler(handler) {
|
||||||
function wrappedHandler(...args) {
|
function wrappedHandler(...args) {
|
||||||
|
@ -114,6 +55,5 @@
|
||||||
illegalConstructorKey,
|
illegalConstructorKey,
|
||||||
requiredArguments,
|
requiredArguments,
|
||||||
defineEventHandler,
|
defineEventHandler,
|
||||||
cloneValue,
|
|
||||||
};
|
};
|
||||||
})(this);
|
})(this);
|
||||||
|
|
Loading…
Reference in a new issue