Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use primordials in extensions/web #11273

Merged
merged 9 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions extensions/web/02_structured_clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// @ts-check
/// <reference path="../../core/lib.deno_core.d.ts" />
/// <reference path="../../core/internal.d.ts" />
/// <reference path="../web/internal.d.ts" />
/// <reference path="../web/lib.deno_web.d.ts" />

Expand All @@ -10,6 +11,15 @@
((window) => {
const core = window.Deno.core;
const { DOMException } = window.__bootstrap.domException;
const {
ArrayBuffer,
ArrayBufferIsView,
DataView,
TypedArrayPrototypeSlice,
TypeError,
WeakMap,
WeakMapPrototypeSet,
} = window.__bootstrap.primordials;

const objectCloneMemo = new WeakMap();

Expand All @@ -20,7 +30,8 @@
_cloneConstructor,
) {
// this function fudges the return type but SharedArrayBuffer is disabled for a while anyway
return srcBuffer.slice(
return TypedArrayPrototypeSlice(
srcBuffer,
srcByteOffset,
srcByteOffset + srcLength,
);
Expand All @@ -38,10 +49,10 @@
value.byteLength,
ArrayBuffer,
);
objectCloneMemo.set(value, cloned);
WeakMapPrototypeSet(objectCloneMemo, value, cloned);
return cloned;
}
if (ArrayBuffer.isView(value)) {
if (ArrayBufferIsView(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,
Expand Down
23 changes: 18 additions & 5 deletions extensions/web/03_abort_signal.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict";

// @ts-check
/// <reference path="../../core/internal.d.ts" />

((window) => {
const webidl = window.__bootstrap.webidl;
const { setIsTrusted, defineEventHandler } = window.__bootstrap.event;
const {
Boolean,
Set,
SetPrototypeAdd,
SetPrototypeClear,
SetPrototypeDelete,
Symbol,
SymbolToStringTag,
TypeError,
} = window.__bootstrap.primordials;

const add = Symbol("add");
const signalAbort = Symbol("signalAbort");
Expand All @@ -22,7 +35,7 @@
}

[add](algorithm) {
this.#abortAlgorithms.add(algorithm);
SetPrototypeAdd(this.#abortAlgorithms, algorithm);
}

[signalAbort]() {
Expand All @@ -33,14 +46,14 @@
for (const algorithm of this.#abortAlgorithms) {
algorithm();
}
this.#abortAlgorithms.clear();
SetPrototypeClear(this.#abortAlgorithms);
const event = new Event("abort");
setIsTrusted(event, true);
this.dispatchEvent(event);
}

[remove](algorithm) {
this.#abortAlgorithms.delete(algorithm);
SetPrototypeDelete(this.#abortAlgorithms, algorithm);
}

constructor(key = null) {
Expand All @@ -55,7 +68,7 @@
return Boolean(this.#aborted);
}

get [Symbol.toStringTag]() {
get [SymbolToStringTag]() {
return "AbortSignal";
}
}
Expand All @@ -74,7 +87,7 @@
this.#signal[signalAbort]();
}

get [Symbol.toStringTag]() {
get [SymbolToStringTag]() {
return "AbortController";
}
}
Expand Down
15 changes: 12 additions & 3 deletions extensions/web/04_global_interfaces.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
"use strict";

// @ts-check
/// <reference path="../../core/internal.d.ts" />

((window) => {
const { EventTarget } = window;
const {
Symbol,
SymbolToStringTag,
TypeError,
} = window.__bootstrap.primordials;

const illegalConstructorKey = Symbol("illegalConstructorKey");

Expand All @@ -13,7 +22,7 @@
super();
}

get [Symbol.toStringTag]() {
get [SymbolToStringTag]() {
return "Window";
}
}
Expand All @@ -26,7 +35,7 @@
super();
}

get [Symbol.toStringTag]() {
get [SymbolToStringTag]() {
return "WorkerGlobalScope";
}
}
Expand All @@ -39,7 +48,7 @@
super();
}

get [Symbol.toStringTag]() {
get [SymbolToStringTag]() {
return "DedicatedWorkerGlobalScope";
}
}
Expand Down
19 changes: 14 additions & 5 deletions extensions/web/05_base64.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.

// @ts-check
/// <reference path="../../core/internal.d.ts" />
/// <reference path="../webidl/internal.d.ts" />
/// <reference path="../web/internal.d.ts" />
/// <reference lib="esnext" />
Expand All @@ -14,6 +15,13 @@
forgivingBase64Decode,
} = window.__bootstrap.infra;
const { DOMException } = window.__bootstrap.domException;
const {
ArrayPrototypeMap,
StringPrototypeCharCodeAt,
ArrayPrototypeJoin,
StringFromCharCode,
Uint8Array,
} = window.__bootstrap.primordials;

/**
* @param {string} data
Expand All @@ -26,10 +34,11 @@
});

const uint8Array = forgivingBase64Decode(data);
const result = [...uint8Array]
.map((byte) => String.fromCharCode(byte))
.join("");
return result;
const result = ArrayPrototypeMap(
[...uint8Array],
(byte) => StringFromCharCode(byte),
);
return ArrayPrototypeJoin(result, "");
}

/**
Expand All @@ -44,7 +53,7 @@
context: "Argument 1",
});
const byteArray = [...data].map((char) => {
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved
const charCode = char.charCodeAt(0);
const charCode = StringPrototypeCharCodeAt(char, 0);
if (charCode > 0xff) {
throw new DOMException(
"The string to be encoded contains characters outside of the Latin1 range.",
Expand Down
47 changes: 30 additions & 17 deletions extensions/web/08_text_encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// @ts-check
/// <reference path="../../core/lib.deno_core.d.ts" />
/// <reference path="../../core/internal.d.ts" />
/// <reference path="../webidl/internal.d.ts" />
/// <reference path="../fetch/lib.deno_fetch.d.ts" />
/// <reference path="../web/internal.d.ts" />
Expand All @@ -13,6 +14,15 @@
((window) => {
const core = Deno.core;
const webidl = window.__bootstrap.webidl;
const {
ArrayBufferIsView,
PromiseReject,
PromiseResolve,
StringPrototypeCharCodeAt,
SymbolToStringTag,
TypedArrayPrototypeSubarray,
Uint8Array,
} = window.__bootstrap.primordials;

class TextDecoder {
/** @type {string} */
Expand Down Expand Up @@ -95,7 +105,7 @@
}

try {
if (ArrayBuffer.isView(input)) {
if (ArrayBufferIsView(input)) {
input = new Uint8Array(
input.buffer,
input.byteOffset,
Expand All @@ -116,7 +126,7 @@
}
}

get [Symbol.toStringTag]() {
get [SymbolToStringTag]() {
return "TextDecoder";
}
}
Expand Down Expand Up @@ -168,7 +178,7 @@
return core.opSync("op_encoding_encode_into", source, destination);
}

get [Symbol.toStringTag]() {
get [SymbolToStringTag]() {
return "TextEncoder";
}
}
Expand Down Expand Up @@ -209,9 +219,9 @@
if (decoded) {
controller.enqueue(decoded);
}
return Promise.resolve();
return PromiseResolve();
} catch (err) {
return Promise.reject(err);
return PromiseReject(err);
}
},
flush: (controller) => {
Expand All @@ -220,9 +230,9 @@
if (final) {
controller.enqueue(final);
}
return Promise.resolve();
return PromiseResolve();
} catch (err) {
return Promise.reject(err);
return PromiseReject(err);
}
},
});
Expand Down Expand Up @@ -259,7 +269,7 @@
return this.#transform.writable;
}

get [Symbol.toStringTag]() {
get [SymbolToStringTag]() {
return "TextDecoderStream";
}
}
Expand All @@ -282,29 +292,32 @@
if (this.#pendingHighSurrogate !== null) {
chunk = this.#pendingHighSurrogate + chunk;
}
const lastCodeUnit = chunk.charCodeAt(chunk.length - 1);
const lastCodeUnit = StringPrototypeCharCodeAt(
chunk,
chunk.length - 1,
);
if (0xD800 <= lastCodeUnit && lastCodeUnit <= 0xDBFF) {
this.#pendingHighSurrogate = chunk.slice(-1);
chunk = chunk.slice(0, -1);
this.#pendingHighSurrogate = StringPrototypeSlice(chunk, -1);
chunk = StringPrototypeSlice(chunk, 0, -1);
} else {
this.#pendingHighSurrogate = null;
}
if (chunk) {
controller.enqueue(core.encode(chunk));
}
return Promise.resolve();
return PromiseResolve();
} catch (err) {
return Promise.reject(err);
return PromiseReject(err);
}
},
flush: (controller) => {
try {
if (this.#pendingHighSurrogate !== null) {
controller.enqueue(new Uint8Array([0xEF, 0xBF, 0xBD]));
}
return Promise.resolve();
return PromiseResolve();
} catch (err) {
return Promise.reject(err);
return PromiseReject(err);
}
},
});
Expand All @@ -329,7 +342,7 @@
return this.#transform.writable;
}

get [Symbol.toStringTag]() {
get [SymbolToStringTag]() {
return "TextEncoderStream";
}
}
Expand Down Expand Up @@ -380,7 +393,7 @@
* @param {Uint8Array} 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) {
return "UTF-8";
}
Expand Down
Loading