0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

refactor: use primordials in extensions/web (#11273)

Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
This commit is contained in:
Bartek Iwańczuk 2021-07-06 14:38:12 +02:00 committed by GitHub
parent f139a0cc11
commit 1aac47720b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 131 additions and 54 deletions

4
core/internal.d.ts vendored
View file

@ -834,6 +834,10 @@ declare namespace __bootstrap {
export const TypeErrorLength: typeof TypeError.length; export const TypeErrorLength: typeof TypeError.length;
export const TypeErrorName: typeof TypeError.name; export const TypeErrorName: typeof TypeError.name;
export const TypeErrorPrototype: typeof TypeError.prototype; export const TypeErrorPrototype: typeof TypeError.prototype;
export const TypedArrayFrom: (
constructor: Uint8ArrayConstructor,
arrayLike: ArrayLike<number>,
) => Uint8Array;
export const TypedArrayPrototypeCopyWithin: UncurryThis< export const TypedArrayPrototypeCopyWithin: UncurryThis<
typeof Uint8Array.prototype.copyWithin typeof Uint8Array.prototype.copyWithin
>; >;

View file

@ -2,6 +2,7 @@
// @ts-check // @ts-check
/// <reference path="../../core/lib.deno_core.d.ts" /> /// <reference path="../../core/lib.deno_core.d.ts" />
/// <reference path="../../core/internal.d.ts" />
/// <reference path="../web/internal.d.ts" /> /// <reference path="../web/internal.d.ts" />
/// <reference path="../web/lib.deno_web.d.ts" /> /// <reference path="../web/lib.deno_web.d.ts" />
@ -10,6 +11,15 @@
((window) => { ((window) => {
const core = window.Deno.core; const core = window.Deno.core;
const { DOMException } = window.__bootstrap.domException; const { DOMException } = window.__bootstrap.domException;
const {
ArrayBuffer,
ArrayBufferIsView,
DataView,
TypedArrayPrototypeSlice,
TypeError,
WeakMap,
WeakMapPrototypeSet,
} = window.__bootstrap.primordials;
const objectCloneMemo = new WeakMap(); const objectCloneMemo = new WeakMap();
@ -20,7 +30,8 @@
_cloneConstructor, _cloneConstructor,
) { ) {
// this function fudges the return type but SharedArrayBuffer is disabled for a while anyway // this function fudges the return type but SharedArrayBuffer is disabled for a while anyway
return srcBuffer.slice( return TypedArrayPrototypeSlice(
srcBuffer,
srcByteOffset, srcByteOffset,
srcByteOffset + srcLength, srcByteOffset + srcLength,
); );
@ -38,10 +49,10 @@
value.byteLength, value.byteLength,
ArrayBuffer, ArrayBuffer,
); );
objectCloneMemo.set(value, cloned); WeakMapPrototypeSet(objectCloneMemo, value, cloned);
return cloned; return cloned;
} }
if (ArrayBuffer.isView(value)) { if (ArrayBufferIsView(value)) {
const clonedBuffer = structuredClone(value.buffer); const clonedBuffer = structuredClone(value.buffer);
// Use DataViewConstructor type purely for type-checking, can be a // Use DataViewConstructor type purely for type-checking, can be a
// DataView or TypedArray. They use the same constructor signature, // DataView or TypedArray. They use the same constructor signature,

View file

@ -1,9 +1,22 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict"; "use strict";
// @ts-check
/// <reference path="../../core/internal.d.ts" />
((window) => { ((window) => {
const webidl = window.__bootstrap.webidl; const webidl = window.__bootstrap.webidl;
const { setIsTrusted, defineEventHandler } = window.__bootstrap.event; const { setIsTrusted, defineEventHandler } = window.__bootstrap.event;
const {
Boolean,
Set,
SetPrototypeAdd,
SetPrototypeClear,
SetPrototypeDelete,
Symbol,
SymbolToStringTag,
TypeError,
} = window.__bootstrap.primordials;
const add = Symbol("add"); const add = Symbol("add");
const signalAbort = Symbol("signalAbort"); const signalAbort = Symbol("signalAbort");
@ -22,7 +35,7 @@
} }
[add](algorithm) { [add](algorithm) {
this.#abortAlgorithms.add(algorithm); SetPrototypeAdd(this.#abortAlgorithms, algorithm);
} }
[signalAbort]() { [signalAbort]() {
@ -33,14 +46,14 @@
for (const algorithm of this.#abortAlgorithms) { for (const algorithm of this.#abortAlgorithms) {
algorithm(); algorithm();
} }
this.#abortAlgorithms.clear(); SetPrototypeClear(this.#abortAlgorithms);
const event = new Event("abort"); const event = new Event("abort");
setIsTrusted(event, true); setIsTrusted(event, true);
this.dispatchEvent(event); this.dispatchEvent(event);
} }
[remove](algorithm) { [remove](algorithm) {
this.#abortAlgorithms.delete(algorithm); SetPrototypeDelete(this.#abortAlgorithms, algorithm);
} }
constructor(key = null) { constructor(key = null) {
@ -55,7 +68,7 @@
return Boolean(this.#aborted); return Boolean(this.#aborted);
} }
get [Symbol.toStringTag]() { get [SymbolToStringTag]() {
return "AbortSignal"; return "AbortSignal";
} }
} }
@ -74,7 +87,7 @@
this.#signal[signalAbort](); this.#signal[signalAbort]();
} }
get [Symbol.toStringTag]() { get [SymbolToStringTag]() {
return "AbortController"; return "AbortController";
} }
} }

View file

@ -1,7 +1,16 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict"; "use strict";
// @ts-check
/// <reference path="../../core/internal.d.ts" />
((window) => { ((window) => {
const { EventTarget } = window; const { EventTarget } = window;
const {
Symbol,
SymbolToStringTag,
TypeError,
} = window.__bootstrap.primordials;
const illegalConstructorKey = Symbol("illegalConstructorKey"); const illegalConstructorKey = Symbol("illegalConstructorKey");
@ -13,7 +22,7 @@
super(); super();
} }
get [Symbol.toStringTag]() { get [SymbolToStringTag]() {
return "Window"; return "Window";
} }
} }
@ -26,7 +35,7 @@
super(); super();
} }
get [Symbol.toStringTag]() { get [SymbolToStringTag]() {
return "WorkerGlobalScope"; return "WorkerGlobalScope";
} }
} }
@ -39,7 +48,7 @@
super(); super();
} }
get [Symbol.toStringTag]() { get [SymbolToStringTag]() {
return "DedicatedWorkerGlobalScope"; return "DedicatedWorkerGlobalScope";
} }
} }

View file

@ -1,6 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// @ts-check // @ts-check
/// <reference path="../../core/internal.d.ts" />
/// <reference path="../webidl/internal.d.ts" /> /// <reference path="../webidl/internal.d.ts" />
/// <reference path="../web/internal.d.ts" /> /// <reference path="../web/internal.d.ts" />
/// <reference lib="esnext" /> /// <reference lib="esnext" />
@ -14,6 +15,13 @@
forgivingBase64Decode, forgivingBase64Decode,
} = window.__bootstrap.infra; } = window.__bootstrap.infra;
const { DOMException } = window.__bootstrap.domException; const { DOMException } = window.__bootstrap.domException;
const {
ArrayPrototypeMap,
StringPrototypeCharCodeAt,
ArrayPrototypeJoin,
StringFromCharCode,
TypedArrayFrom,
} = window.__bootstrap.primordials;
/** /**
* @param {string} data * @param {string} data
@ -26,10 +34,11 @@
}); });
const uint8Array = forgivingBase64Decode(data); const uint8Array = forgivingBase64Decode(data);
const result = [...uint8Array] const result = ArrayPrototypeMap(
.map((byte) => String.fromCharCode(byte)) [...uint8Array],
.join(""); (byte) => StringFromCharCode(byte),
return result; );
return ArrayPrototypeJoin(result, "");
} }
/** /**
@ -43,8 +52,8 @@
prefix, prefix,
context: "Argument 1", context: "Argument 1",
}); });
const byteArray = [...data].map((char) => { const byteArray = ArrayPrototypeMap([...data], (char) => {
const charCode = char.charCodeAt(0); const charCode = StringPrototypeCharCodeAt(char, 0);
if (charCode > 0xff) { if (charCode > 0xff) {
throw new DOMException( throw new DOMException(
"The string to be encoded contains characters outside of the Latin1 range.", "The string to be encoded contains characters outside of the Latin1 range.",
@ -53,7 +62,7 @@
} }
return charCode; return charCode;
}); });
return forgivingBase64Encode(Uint8Array.from(byteArray)); return forgivingBase64Encode(TypedArrayFrom(Uint8Array, byteArray));
} }
window.__bootstrap.base64 = { window.__bootstrap.base64 = {

View file

@ -2,6 +2,7 @@
// @ts-check // @ts-check
/// <reference path="../../core/lib.deno_core.d.ts" /> /// <reference path="../../core/lib.deno_core.d.ts" />
/// <reference path="../../core/internal.d.ts" />
/// <reference path="../webidl/internal.d.ts" /> /// <reference path="../webidl/internal.d.ts" />
/// <reference path="../fetch/lib.deno_fetch.d.ts" /> /// <reference path="../fetch/lib.deno_fetch.d.ts" />
/// <reference path="../web/internal.d.ts" /> /// <reference path="../web/internal.d.ts" />
@ -13,6 +14,17 @@
((window) => { ((window) => {
const core = Deno.core; const core = Deno.core;
const webidl = window.__bootstrap.webidl; const webidl = window.__bootstrap.webidl;
const {
ArrayBufferIsView,
PromiseReject,
PromiseResolve,
StringPrototypeCharCodeAt,
StringPrototypeSlice,
SymbolToStringTag,
TypedArrayPrototypeSubarray,
TypedArrayPrototypeSlice,
Uint8Array,
} = window.__bootstrap.primordials;
class TextDecoder { class TextDecoder {
/** @type {string} */ /** @type {string} */
@ -95,7 +107,7 @@
} }
try { try {
if (ArrayBuffer.isView(input)) { if (ArrayBufferIsView(input)) {
input = new Uint8Array( input = new Uint8Array(
input.buffer, input.buffer,
input.byteOffset, input.byteOffset,
@ -116,7 +128,7 @@
} }
} }
get [Symbol.toStringTag]() { get [SymbolToStringTag]() {
return "TextDecoder"; return "TextDecoder";
} }
} }
@ -172,7 +184,7 @@
return core.opSync("op_encoding_encode_into", source, destination); return core.opSync("op_encoding_encode_into", source, destination);
} }
get [Symbol.toStringTag]() { get [SymbolToStringTag]() {
return "TextEncoder"; return "TextEncoder";
} }
} }
@ -213,9 +225,9 @@
if (decoded) { if (decoded) {
controller.enqueue(decoded); controller.enqueue(decoded);
} }
return Promise.resolve(); return PromiseResolve();
} catch (err) { } catch (err) {
return Promise.reject(err); return PromiseReject(err);
} }
}, },
flush: (controller) => { flush: (controller) => {
@ -224,9 +236,9 @@
if (final) { if (final) {
controller.enqueue(final); controller.enqueue(final);
} }
return Promise.resolve(); return PromiseResolve();
} catch (err) { } catch (err) {
return Promise.reject(err); return PromiseReject(err);
} }
}, },
}); });
@ -263,7 +275,7 @@
return this.#transform.writable; return this.#transform.writable;
} }
get [Symbol.toStringTag]() { get [SymbolToStringTag]() {
return "TextDecoderStream"; return "TextDecoderStream";
} }
} }
@ -286,19 +298,22 @@
if (this.#pendingHighSurrogate !== null) { if (this.#pendingHighSurrogate !== null) {
chunk = this.#pendingHighSurrogate + chunk; chunk = this.#pendingHighSurrogate + chunk;
} }
const lastCodeUnit = chunk.charCodeAt(chunk.length - 1); const lastCodeUnit = StringPrototypeCharCodeAt(
chunk,
chunk.length - 1,
);
if (0xD800 <= lastCodeUnit && lastCodeUnit <= 0xDBFF) { if (0xD800 <= lastCodeUnit && lastCodeUnit <= 0xDBFF) {
this.#pendingHighSurrogate = chunk.slice(-1); this.#pendingHighSurrogate = StringPrototypeSlice(chunk, -1);
chunk = chunk.slice(0, -1); chunk = StringPrototypeSlice(chunk, 0, -1);
} else { } else {
this.#pendingHighSurrogate = null; this.#pendingHighSurrogate = null;
} }
if (chunk) { if (chunk) {
controller.enqueue(core.encode(chunk)); controller.enqueue(core.encode(chunk));
} }
return Promise.resolve(); return PromiseResolve();
} catch (err) { } catch (err) {
return Promise.reject(err); return PromiseReject(err);
} }
}, },
flush: (controller) => { flush: (controller) => {
@ -306,9 +321,9 @@
if (this.#pendingHighSurrogate !== null) { if (this.#pendingHighSurrogate !== null) {
controller.enqueue(new Uint8Array([0xEF, 0xBF, 0xBD])); controller.enqueue(new Uint8Array([0xEF, 0xBF, 0xBD]));
} }
return Promise.resolve(); return PromiseResolve();
} catch (err) { } catch (err) {
return Promise.reject(err); return PromiseReject(err);
} }
}, },
}); });
@ -333,7 +348,7 @@
return this.#transform.writable; return this.#transform.writable;
} }
get [Symbol.toStringTag]() { get [SymbolToStringTag]() {
return "TextEncoderStream"; return "TextEncoderStream";
} }
} }
@ -377,14 +392,16 @@
if (BOMEncoding === "UTF-8") start = 3; if (BOMEncoding === "UTF-8") start = 3;
else start = 2; else start = 2;
} }
return new TextDecoder(encoding).decode(bytes.slice(start)); return new TextDecoder(encoding).decode(
TypedArrayPrototypeSlice(bytes, start),
);
} }
/** /**
* @param {Uint8Array} bytes * @param {Uint8Array} bytes
*/ */
function BOMSniff(bytes) { function BOMSniff(bytes) {
const BOM = bytes.subarray(0, 3); const BOM = TypedArrayPrototypeSubarray(bytes, 0, 3);
if (BOM[0] === 0xEF && BOM[1] === 0xBB && BOM[2] === 0xBF) { if (BOM[0] === 0xEF && BOM[1] === 0xBB && BOM[2] === 0xBF) {
return "UTF-8"; return "UTF-8";
} }

View file

@ -1,9 +1,23 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict"; "use strict";
/// <reference path="../../core/internal.d.ts" />
((window) => { ((window) => {
const { URL } = window.__bootstrap.url; const { URL } = window.__bootstrap.url;
const { DOMException } = window.__bootstrap.domException; const { DOMException } = window.__bootstrap.domException;
const {
Error,
ObjectDefineProperties,
ReferenceError,
Symbol,
SymbolFor,
SymbolToStringTag,
TypeError,
WeakMap,
WeakMapPrototypeGet,
WeakMapPrototypeSet,
} = window.__bootstrap.primordials;
const locationConstructorKey = Symbol("locationConstuctorKey"); const locationConstructorKey = Symbol("locationConstuctorKey");
@ -20,7 +34,7 @@
const url = new URL(href); const url = new URL(href);
url.username = ""; url.username = "";
url.password = ""; url.password = "";
Object.defineProperties(this, { ObjectDefineProperties(this, {
hash: { hash: {
get() { get() {
return url.hash; return url.hash;
@ -167,7 +181,7 @@
}, },
enumerable: true, enumerable: true,
}, },
[Symbol.for("Deno.privateCustomInspect")]: { [SymbolFor("Deno.privateCustomInspect")]: {
value: function (inspect) { value: function (inspect) {
const object = { const object = {
hash: this.hash, hash: this.hash,
@ -187,8 +201,8 @@
} }
} }
Object.defineProperties(Location.prototype, { ObjectDefineProperties(Location.prototype, {
[Symbol.toStringTag]: { [SymbolToStringTag]: {
value: "Location", value: "Location",
configurable: true, configurable: true,
}, },
@ -204,14 +218,14 @@
const url = new URL(href); const url = new URL(href);
url.username = ""; url.username = "";
url.password = ""; url.password = "";
workerLocationUrls.set(this, url); WeakMapPrototypeSet(workerLocationUrls, this, url);
} }
} }
Object.defineProperties(WorkerLocation.prototype, { ObjectDefineProperties(WorkerLocation.prototype, {
hash: { hash: {
get() { get() {
const url = workerLocationUrls.get(this); const url = WeakMapPrototypeGet(workerLocationUrls, this);
if (url == null) { if (url == null) {
throw new TypeError("Illegal invocation."); throw new TypeError("Illegal invocation.");
} }
@ -222,7 +236,7 @@
}, },
host: { host: {
get() { get() {
const url = workerLocationUrls.get(this); const url = WeakMapPrototypeGet(workerLocationUrls, this);
if (url == null) { if (url == null) {
throw new TypeError("Illegal invocation."); throw new TypeError("Illegal invocation.");
} }
@ -233,7 +247,7 @@
}, },
hostname: { hostname: {
get() { get() {
const url = workerLocationUrls.get(this); const url = WeakMapPrototypeGet(workerLocationUrls, this);
if (url == null) { if (url == null) {
throw new TypeError("Illegal invocation."); throw new TypeError("Illegal invocation.");
} }
@ -244,7 +258,7 @@
}, },
href: { href: {
get() { get() {
const url = workerLocationUrls.get(this); const url = WeakMapPrototypeGet(workerLocationUrls, this);
if (url == null) { if (url == null) {
throw new TypeError("Illegal invocation."); throw new TypeError("Illegal invocation.");
} }
@ -255,7 +269,7 @@
}, },
origin: { origin: {
get() { get() {
const url = workerLocationUrls.get(this); const url = WeakMapPrototypeGet(workerLocationUrls, this);
if (url == null) { if (url == null) {
throw new TypeError("Illegal invocation."); throw new TypeError("Illegal invocation.");
} }
@ -266,7 +280,7 @@
}, },
pathname: { pathname: {
get() { get() {
const url = workerLocationUrls.get(this); const url = WeakMapPrototypeGet(workerLocationUrls, this);
if (url == null) { if (url == null) {
throw new TypeError("Illegal invocation."); throw new TypeError("Illegal invocation.");
} }
@ -277,7 +291,7 @@
}, },
port: { port: {
get() { get() {
const url = workerLocationUrls.get(this); const url = WeakMapPrototypeGet(workerLocationUrls, this);
if (url == null) { if (url == null) {
throw new TypeError("Illegal invocation."); throw new TypeError("Illegal invocation.");
} }
@ -288,7 +302,7 @@
}, },
protocol: { protocol: {
get() { get() {
const url = workerLocationUrls.get(this); const url = WeakMapPrototypeGet(workerLocationUrls, this);
if (url == null) { if (url == null) {
throw new TypeError("Illegal invocation."); throw new TypeError("Illegal invocation.");
} }
@ -299,7 +313,7 @@
}, },
search: { search: {
get() { get() {
const url = workerLocationUrls.get(this); const url = WeakMapPrototypeGet(workerLocationUrls, this);
if (url == null) { if (url == null) {
throw new TypeError("Illegal invocation."); throw new TypeError("Illegal invocation.");
} }
@ -310,7 +324,7 @@
}, },
toString: { toString: {
value: function toString() { value: function toString() {
const url = workerLocationUrls.get(this); const url = WeakMapPrototypeGet(workerLocationUrls, this);
if (url == null) { if (url == null) {
throw new TypeError("Illegal invocation."); throw new TypeError("Illegal invocation.");
} }
@ -320,11 +334,11 @@
enumerable: true, enumerable: true,
writable: true, writable: true,
}, },
[Symbol.toStringTag]: { [SymbolToStringTag]: {
value: "WorkerLocation", value: "WorkerLocation",
configurable: true, configurable: true,
}, },
[Symbol.for("Deno.privateCustomInspect")]: { [SymbolFor("Deno.privateCustomInspect")]: {
value: function (inspect) { value: function (inspect) {
const object = { const object = {
hash: this.hash, hash: this.hash,