diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2022-01-27 13:36:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-27 13:36:36 +0100 |
commit | 884143218fad0e18f7553aaf079d52de703f7601 (patch) | |
tree | 9b9e9d30ea647041438ef8fa974b8d4234cabf73 /ext/web/13_message_port.js | |
parent | dcf8f144ab0516936bfa4e93357d71f1732d880e (diff) |
refactor: update runtime code for primordial checks for "instanceof" (#13497)
Diffstat (limited to 'ext/web/13_message_port.js')
-rw-r--r-- | ext/web/13_message_port.js | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/ext/web/13_message_port.js b/ext/web/13_message_port.js index ef332dd7a..8242f85f3 100644 --- a/ext/web/13_message_port.js +++ b/ext/web/13_message_port.js @@ -10,19 +10,21 @@ ((window) => { const core = window.Deno.core; - const { Interrupted } = core; + const { InterruptedPrototype } = core; const webidl = window.__bootstrap.webidl; const { setEventTargetData } = window.__bootstrap.eventTarget; const { defineEventHandler } = window.__bootstrap.event; const { DOMException } = window.__bootstrap.domException; const { - ArrayBuffer, + ArrayBufferPrototype, ArrayPrototypeFilter, ArrayPrototypeIncludes, ArrayPrototypePush, + ObjectPrototypeIsPrototypeOf, ObjectSetPrototypeOf, Symbol, SymbolFor, + SymbolIterator, TypeError, WeakSet, WeakSetPrototypeAdd, @@ -45,12 +47,12 @@ } get port1() { - webidl.assertBranded(this, MessageChannel); + webidl.assertBranded(this, MessageChannelPrototype); return this.#port1; } get port2() { - webidl.assertBranded(this, MessageChannel); + webidl.assertBranded(this, MessageChannelPrototype); return this.#port2; } @@ -62,6 +64,7 @@ } webidl.configurePrototype(MessageChannel); + const MessageChannelPrototype = MessageChannel.prototype; const _id = Symbol("id"); const _enabled = Symbol("enabled"); @@ -72,7 +75,7 @@ */ function createMessagePort(id) { const port = core.createHostObject(); - ObjectSetPrototypeOf(port, MessagePort.prototype); + ObjectSetPrototypeOf(port, MessagePortPrototype); port[webidl.brand] = webidl.brand; setEventTargetData(port); port[_id] = id; @@ -95,7 +98,7 @@ * @param {object[] | StructuredSerializeOptions} transferOrOptions */ postMessage(message, transferOrOptions = {}) { - webidl.assertBranded(this, MessagePort); + webidl.assertBranded(this, MessagePortPrototype); const prefix = "Failed to execute 'postMessage' on 'MessagePort'"; webidl.requiredArguments(arguments.length, 1, { prefix }); message = webidl.converters.any(message); @@ -103,7 +106,7 @@ if ( webidl.type(transferOrOptions) === "Object" && transferOrOptions !== undefined && - transferOrOptions[Symbol.iterator] !== undefined + transferOrOptions[SymbolIterator] !== undefined ) { const transfer = webidl.converters["sequence<object>"]( transferOrOptions, @@ -129,7 +132,7 @@ } start() { - webidl.assertBranded(this, MessagePort); + webidl.assertBranded(this, MessagePortPrototype); if (this[_enabled]) return; (async () => { this[_enabled] = true; @@ -142,7 +145,7 @@ this[_id], ); } catch (err) { - if (err instanceof Interrupted) break; + if (ObjectPrototypeIsPrototypeOf(InterruptedPrototype, err)) break; throw err; } if (data === null) break; @@ -160,7 +163,7 @@ data: message, ports: ArrayPrototypeFilter( transferables, - (t) => t instanceof MessagePort, + (t) => ObjectPrototypeIsPrototypeOf(MessagePortPrototype, t), ), }); this.dispatchEvent(event); @@ -170,7 +173,7 @@ } close() { - webidl.assertBranded(this, MessagePort); + webidl.assertBranded(this, MessagePortPrototype); if (this[_id] !== null) { core.close(this[_id]); this[_id] = null; @@ -184,6 +187,7 @@ defineEventHandler(MessagePort.prototype, "messageerror"); webidl.configurePrototype(MessagePort); + const MessagePortPrototype = MessagePort.prototype; /** * @returns {[number, number]} @@ -245,7 +249,7 @@ function serializeJsMessageData(data, transferables) { const transferedArrayBuffers = ArrayPrototypeFilter( transferables, - (a) => a instanceof ArrayBuffer, + (a) => ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, a), ); for (const arrayBuffer of transferedArrayBuffers) { @@ -266,7 +270,7 @@ serializedData = core.serialize(data, { hostObjects: ArrayPrototypeFilter( transferables, - (a) => a instanceof MessagePort, + (a) => ObjectPrototypeIsPrototypeOf(MessagePortPrototype, a), ), transferedArrayBuffers, }); @@ -279,8 +283,8 @@ let arrayBufferI = 0; for (const transferable of transferables) { - if (transferable instanceof MessagePort) { - webidl.assertBranded(transferable, MessagePort); + if (ObjectPrototypeIsPrototypeOf(MessagePortPrototype, transferable)) { + webidl.assertBranded(transferable, MessagePortPrototype); const id = transferable[_id]; if (id === null) { throw new DOMException( @@ -293,7 +297,9 @@ kind: "messagePort", data: id, }); - } else if (transferable instanceof ArrayBuffer) { + } else if ( + ObjectPrototypeIsPrototypeOf(ArrayBufferPrototype, transferable) + ) { ArrayPrototypePush(serializedTransferables, { kind: "arrayBuffer", data: transferedArrayBuffers[arrayBufferI], @@ -339,6 +345,7 @@ window.__bootstrap.messagePort = { MessageChannel, MessagePort, + MessagePortPrototype, deserializeJsMessageData, serializeJsMessageData, structuredClone, |