refactor: update runtime code for primordial checks for "instanceof" (#13497)

This commit is contained in:
Bartek Iwańczuk 2022-01-27 13:36:36 +01:00 committed by GitHub
parent dcf8f144ab
commit 884143218f
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,6 +16,7 @@
const webidl = window.__bootstrap.webidl;
const {
ArrayBufferIsView,
ObjectPrototypeIsPrototypeOf,
PromiseReject,
PromiseResolve,
StringPrototypeCharCodeAt,
@ -59,19 +60,19 @@
/** @returns {string} */
get encoding() {
webidl.assertBranded(this, TextDecoder);
webidl.assertBranded(this, TextDecoderPrototype);
return this.#encoding;
}
/** @returns {boolean} */
get fatal() {
webidl.assertBranded(this, TextDecoder);
webidl.assertBranded(this, TextDecoderPrototype);
return this.#fatal;
}
/** @returns {boolean} */
get ignoreBOM() {
webidl.assertBranded(this, TextDecoder);
webidl.assertBranded(this, TextDecoderPrototype);
return this.#ignoreBOM;
}
@ -80,7 +81,7 @@
* @param {TextDecodeOptions} options
*/
decode(input = new Uint8Array(), options = {}) {
webidl.assertBranded(this, TextDecoder);
webidl.assertBranded(this, TextDecoderPrototype);
const prefix = "Failed to execute 'decode' on 'TextDecoder'";
if (input !== undefined) {
input = webidl.converters.BufferSource(input, {
@ -119,7 +120,12 @@
// If the buffer is detached, just create a new empty Uint8Array.
input = new Uint8Array();
}
if (input.buffer instanceof SharedArrayBuffer) {
if (
ObjectPrototypeIsPrototypeOf(
SharedArrayBuffer.prototype,
input.buffer,
)
) {
// We clone the data into a non-shared ArrayBuffer so we can pass it
// to Rust.
// `input` is now a Uint8Array, and calling the TypedArray constructor
@ -140,6 +146,7 @@
}
webidl.configurePrototype(TextDecoder);
const TextDecoderPrototype = TextDecoder.prototype;
class TextEncoder {
constructor() {
@ -148,7 +155,7 @@
/** @returns {string} */
get encoding() {
webidl.assertBranded(this, TextEncoder);
webidl.assertBranded(this, TextEncoderPrototype);
return "utf-8";
}
@ -157,7 +164,7 @@
* @returns {Uint8Array}
*/
encode(input = "") {
webidl.assertBranded(this, TextEncoder);
webidl.assertBranded(this, TextEncoderPrototype);
const prefix = "Failed to execute 'encode' on 'TextEncoder'";
// The WebIDL type of `input` is `USVString`, but `core.encode` already
// converts lone surrogates to the replacement character.
@ -174,7 +181,7 @@
* @returns {TextEncoderEncodeIntoResult}
*/
encodeInto(source, destination) {
webidl.assertBranded(this, TextEncoder);
webidl.assertBranded(this, TextEncoderPrototype);
const prefix = "Failed to execute 'encodeInto' on 'TextEncoder'";
// The WebIDL type of `source` is `USVString`, but the ops bindings
// already convert lone surrogates to the replacement character.
@ -192,6 +199,7 @@
}
webidl.configurePrototype(TextEncoder);
const TextEncoderPrototype = TextEncoder.prototype;
class TextDecoderStream {
/** @type {TextDecoder} */
@ -248,36 +256,37 @@
/** @returns {string} */
get encoding() {
webidl.assertBranded(this, TextDecoderStream);
webidl.assertBranded(this, TextDecoderStreamPrototype);
return this.#decoder.encoding;
}
/** @returns {boolean} */
get fatal() {
webidl.assertBranded(this, TextDecoderStream);
webidl.assertBranded(this, TextDecoderStreamPrototype);
return this.#decoder.fatal;
}
/** @returns {boolean} */
get ignoreBOM() {
webidl.assertBranded(this, TextDecoderStream);
webidl.assertBranded(this, TextDecoderStreamPrototype);
return this.#decoder.ignoreBOM;
}
/** @returns {ReadableStream<string>} */
get readable() {
webidl.assertBranded(this, TextDecoderStream);
webidl.assertBranded(this, TextDecoderStreamPrototype);
return this.#transform.readable;
}
/** @returns {WritableStream<BufferSource>} */
get writable() {
webidl.assertBranded(this, TextDecoderStream);
webidl.assertBranded(this, TextDecoderStreamPrototype);
return this.#transform.writable;
}
}
webidl.configurePrototype(TextDecoderStream);
const TextDecoderStreamPrototype = TextDecoderStream.prototype;
class TextEncoderStream {
/** @type {string | null} */
@ -332,24 +341,25 @@
/** @returns {string} */
get encoding() {
webidl.assertBranded(this, TextEncoderStream);
webidl.assertBranded(this, TextEncoderStreamPrototype);
return "utf-8";
}
/** @returns {ReadableStream<Uint8Array>} */
get readable() {
webidl.assertBranded(this, TextEncoderStream);
webidl.assertBranded(this, TextEncoderStreamPrototype);
return this.#transform.readable;
}
/** @returns {WritableStream<string>} */
get writable() {
webidl.assertBranded(this, TextEncoderStream);
webidl.assertBranded(this, TextEncoderStreamPrototype);
return this.#transform.writable;
}
}
webidl.configurePrototype(TextEncoderStream);
const TextEncoderStreamPrototype = TextEncoderStream.prototype;
webidl.converters.TextDecoderOptions = webidl.createDictionaryConverter(
"TextDecoderOptions",