refactor: primordials for instanceof (#13527)

This commit is contained in:
Bartek Iwańczuk 2022-02-01 18:06:11 +01:00 committed by GitHub
parent abf89f8c46
commit 8176a4d166
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
41 changed files with 1030 additions and 660 deletions

View file

@ -16,23 +16,35 @@
const core = window.Deno.core;
const webidl = globalThis.__bootstrap.webidl;
const { parseUrlEncoded } = globalThis.__bootstrap.url;
const { parseFormData, formDataFromEntries, formDataToBlob } =
globalThis.__bootstrap.formData;
const mimesniff = globalThis.__bootstrap.mimesniff;
const { isReadableStreamDisturbed, errorReadableStream, createProxy } =
globalThis.__bootstrap.streams;
const { URLSearchParamsPrototype } = globalThis.__bootstrap.url;
const {
ArrayBuffer,
parseFormData,
formDataFromEntries,
formDataToBlob,
FormDataPrototype,
} = globalThis.__bootstrap.formData;
const mimesniff = globalThis.__bootstrap.mimesniff;
const { BlobPrototype } = globalThis.__bootstrap.file;
const {
isReadableStreamDisturbed,
errorReadableStream,
createProxy,
ReadableStreamPrototype,
} = globalThis.__bootstrap.streams;
const {
ArrayBufferPrototype,
ArrayBufferIsView,
ArrayPrototypePush,
ArrayPrototypeMap,
JSONParse,
ObjectDefineProperties,
ObjectPrototypeIsPrototypeOf,
PromiseResolve,
TypedArrayPrototypeSet,
TypedArrayPrototypeSlice,
TypeError,
Uint8Array,
Uint8ArrayPrototype,
} = window.__bootstrap.primordials;
/**
@ -66,7 +78,12 @@
}
get stream() {
if (!(this.streamOrStatic instanceof ReadableStream)) {
if (
!ObjectPrototypeIsPrototypeOf(
ReadableStreamPrototype,
this.streamOrStatic,
)
) {
const { body, consumed } = this.streamOrStatic;
if (consumed) {
this.streamOrStatic = new ReadableStream();
@ -88,7 +105,12 @@
* @returns {boolean}
*/
unusable() {
if (this.streamOrStatic instanceof ReadableStream) {
if (
ObjectPrototypeIsPrototypeOf(
ReadableStreamPrototype,
this.streamOrStatic,
)
) {
return this.streamOrStatic.locked ||
isReadableStreamDisturbed(this.streamOrStatic);
}
@ -99,7 +121,12 @@
* @returns {boolean}
*/
consumed() {
if (this.streamOrStatic instanceof ReadableStream) {
if (
ObjectPrototypeIsPrototypeOf(
ReadableStreamPrototype,
this.streamOrStatic,
)
) {
return isReadableStreamDisturbed(this.streamOrStatic);
}
return this.streamOrStatic.consumed;
@ -111,7 +138,12 @@
*/
async consume() {
if (this.unusable()) throw new TypeError("Body already consumed.");
if (this.streamOrStatic instanceof ReadableStream) {
if (
ObjectPrototypeIsPrototypeOf(
ReadableStreamPrototype,
this.streamOrStatic,
)
) {
const reader = this.stream.getReader();
/** @type {Uint8Array[]} */
const chunks = [];
@ -136,7 +168,12 @@
}
cancel(error) {
if (this.streamOrStatic instanceof ReadableStream) {
if (
ObjectPrototypeIsPrototypeOf(
ReadableStreamPrototype,
this.streamOrStatic,
)
) {
this.streamOrStatic.cancel(error);
} else {
this.streamOrStatic.consumed = true;
@ -144,7 +181,12 @@
}
error(error) {
if (this.streamOrStatic instanceof ReadableStream) {
if (
ObjectPrototypeIsPrototypeOf(
ReadableStreamPrototype,
this.streamOrStatic,
)
) {
errorReadableStream(this.streamOrStatic, error);
} else {
this.streamOrStatic.consumed = true;
@ -168,7 +210,12 @@
*/
createProxy() {
let proxyStreamOrStatic;
if (this.streamOrStatic instanceof ReadableStream) {
if (
ObjectPrototypeIsPrototypeOf(
ReadableStreamPrototype,
this.streamOrStatic,
)
) {
proxyStreamOrStatic = createProxy(this.streamOrStatic);
} else {
proxyStreamOrStatic = { ...this.streamOrStatic };
@ -282,7 +329,7 @@
enumerable: true,
},
};
return ObjectDefineProperties(prototype.prototype, mixin);
return ObjectDefineProperties(prototype, mixin);
}
/**
@ -341,18 +388,21 @@
let source = null;
let length = null;
let contentType = null;
if (object instanceof Blob) {
if (ObjectPrototypeIsPrototypeOf(BlobPrototype, object)) {
stream = object.stream();
source = object;
length = object.size;
if (object.type.length !== 0) {
contentType = object.type;
}
} else if (object instanceof Uint8Array) {
} else if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, object)) {
// Fast(er) path for common case of Uint8Array
const copy = TypedArrayPrototypeSlice(object, 0, object.byteLength);
source = copy;
} else if (ArrayBufferIsView(object) || object instanceof ArrayBuffer) {
} else if (
ArrayBufferIsView(object) ||
ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, object)
) {
const u8 = ArrayBufferIsView(object)
? new Uint8Array(
object.buffer,
@ -362,26 +412,28 @@
: new Uint8Array(object);
const copy = TypedArrayPrototypeSlice(u8, 0, u8.byteLength);
source = copy;
} else if (object instanceof FormData) {
} else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, object)) {
const res = formDataToBlob(object);
stream = res.stream();
source = res;
length = res.size;
contentType = res.type;
} else if (object instanceof URLSearchParams) {
} else if (
ObjectPrototypeIsPrototypeOf(URLSearchParamsPrototype, object)
) {
// TODO(@satyarohith): not sure what primordial here.
source = object.toString();
contentType = "application/x-www-form-urlencoded;charset=UTF-8";
} else if (typeof object === "string") {
source = object;
contentType = "text/plain;charset=UTF-8";
} else if (object instanceof ReadableStream) {
} else if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, object)) {
stream = object;
if (object.locked || isReadableStreamDisturbed(object)) {
throw new TypeError("ReadableStream is locked or disturbed");
}
}
if (source instanceof Uint8Array) {
if (ObjectPrototypeIsPrototypeOf(Uint8ArrayPrototype, source)) {
stream = { body: source, consumed: false };
length = source.byteLength;
} else if (typeof source === "string") {
@ -399,19 +451,22 @@
webidl.converters["BodyInit_DOMString"] = (V, opts) => {
// Union for (ReadableStream or Blob or ArrayBufferView or ArrayBuffer or FormData or URLSearchParams or USVString)
if (V instanceof ReadableStream) {
if (ObjectPrototypeIsPrototypeOf(ReadableStreamPrototype, V)) {
// TODO(lucacasonato): ReadableStream is not branded
return V;
} else if (V instanceof Blob) {
} else if (ObjectPrototypeIsPrototypeOf(BlobPrototype, V)) {
return webidl.converters["Blob"](V, opts);
} else if (V instanceof FormData) {
} else if (ObjectPrototypeIsPrototypeOf(FormDataPrototype, V)) {
return webidl.converters["FormData"](V, opts);
} else if (V instanceof URLSearchParams) {
} else if (ObjectPrototypeIsPrototypeOf(URLSearchParamsPrototype, V)) {
// TODO(lucacasonato): URLSearchParams is not branded
return V;
}
if (typeof V === "object") {
if (V instanceof ArrayBuffer || V instanceof SharedArrayBuffer) {
if (
ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, V) ||
ObjectPrototypeIsPrototypeOf(SharedArrayBuffer.prototype, V)
) {
return webidl.converters["ArrayBuffer"](V, opts);
}
if (ArrayBufferIsView(V)) {