From f5847a95667aa00caa4956609e2e74fdeadee9d5 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Fri, 13 Jan 2023 01:48:18 +0100 Subject: [PATCH] fix(webidl): properly implement setlike (#17363) --- ext/webgpu/src/01_webgpu.js | 59 +++---------------- ext/webidl/00_webidl.js | 112 ++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 52 deletions(-) diff --git a/ext/webgpu/src/01_webgpu.js b/ext/webgpu/src/01_webgpu.js index 02bdc65621..792267bda6 100644 --- a/ext/webgpu/src/01_webgpu.js +++ b/ext/webgpu/src/01_webgpu.js @@ -34,14 +34,9 @@ SafeArrayIterator, SafePromiseAll, Set, - SetPrototypeEntries, - SetPrototypeForEach, SetPrototypeHas, - SetPrototypeKeys, - SetPrototypeValues, Symbol, SymbolFor, - SymbolIterator, TypeError, Uint32Array, Uint32ArrayPrototype, @@ -602,60 +597,20 @@ function createGPUSupportedFeatures(features) { /** @type {GPUSupportedFeatures} */ - const adapterFeatures = webidl.createBranded(GPUSupportedFeatures); - adapterFeatures[_features] = new Set(features); - return adapterFeatures; + const supportedFeatures = webidl.createBranded(GPUSupportedFeatures); + supportedFeatures[webidl.setlikeInner] = new Set(features); + return webidl.setlike( + supportedFeatures, + GPUSupportedFeaturesPrototype, + true, + ); } class GPUSupportedFeatures { - /** @type {Set} */ - [_features]; - constructor() { webidl.illegalConstructor(); } - /** @return {IterableIterator<[string, string]>} */ - entries() { - webidl.assertBranded(this, GPUSupportedFeaturesPrototype); - return SetPrototypeEntries(this[_features]); - } - - /** @return {void} */ - forEach(callbackfn, thisArg) { - webidl.assertBranded(this, GPUSupportedFeaturesPrototype); - SetPrototypeForEach(this[_features], callbackfn, thisArg); - } - - /** @return {boolean} */ - has(value) { - webidl.assertBranded(this, GPUSupportedFeaturesPrototype); - return SetPrototypeHas(this[_features], value); - } - - /** @return {IterableIterator} */ - keys() { - webidl.assertBranded(this, GPUSupportedFeaturesPrototype); - return SetPrototypeKeys(this[_features]); - } - - /** @return {IterableIterator} */ - values() { - webidl.assertBranded(this, GPUSupportedFeaturesPrototype); - return SetPrototypeValues(this[_features]); - } - - /** @return {number} */ - get size() { - webidl.assertBranded(this, GPUSupportedFeaturesPrototype); - return this[_features].size; - } - - [SymbolIterator]() { - webidl.assertBranded(this, GPUSupportedFeaturesPrototype); - return this[_features][SymbolIterator](); - } - [SymbolFor("Deno.privateCustomInspect")](inspect) { return `${this.constructor.name} ${ inspect([...new SafeArrayIterator(this.values())]) diff --git a/ext/webidl/00_webidl.js b/ext/webidl/00_webidl.js index 4f51edfed9..a71993c877 100644 --- a/ext/webidl/00_webidl.js +++ b/ext/webidl/00_webidl.js @@ -61,6 +61,14 @@ ReflectOwnKeys, RegExpPrototypeTest, Set, + SetPrototypeEntries, + SetPrototypeForEach, + SetPrototypeKeys, + SetPrototypeValues, + SetPrototypeHas, + SetPrototypeClear, + SetPrototypeDelete, + SetPrototypeAdd, // TODO(lucacasonato): add SharedArrayBuffer to primordials // SharedArrayBuffer, String, @@ -1048,6 +1056,108 @@ }); } + const setlikeInner = Symbol("[[set]]"); + + // Ref: https://webidl.spec.whatwg.org/#es-setlike + function setlike(obj, objPrototype, readonly) { + ObjectDefineProperties(obj, { + size: { + configurable: true, + enumerable: true, + get() { + assertBranded(this, objPrototype); + return obj[setlikeInner].size; + }, + }, + [SymbolIterator]: { + configurable: true, + enumerable: false, + writable: true, + value() { + assertBranded(this, objPrototype); + return obj[setlikeInner][SymbolIterator](); + }, + }, + entries: { + configurable: true, + enumerable: true, + writable: true, + value() { + assertBranded(this, objPrototype); + return SetPrototypeEntries(obj[setlikeInner]); + }, + }, + keys: { + configurable: true, + enumerable: true, + writable: true, + value() { + assertBranded(this, objPrototype); + return SetPrototypeKeys(obj[setlikeInner]); + }, + }, + values: { + configurable: true, + enumerable: true, + writable: true, + value() { + assertBranded(this, objPrototype); + return SetPrototypeValues(obj[setlikeInner]); + }, + }, + forEach: { + configurable: true, + enumerable: true, + writable: true, + value(callbackfn, thisArg) { + assertBranded(this, objPrototype); + return SetPrototypeForEach(obj[setlikeInner], callbackfn, thisArg); + }, + }, + has: { + configurable: true, + enumerable: true, + writable: true, + value(value) { + assertBranded(this, objPrototype); + return SetPrototypeHas(obj[setlikeInner], value); + }, + }, + }); + + if (!readonly) { + ObjectDefineProperties(obj, { + add: { + configurable: true, + enumerable: true, + writable: true, + value(value) { + assertBranded(this, objPrototype); + return SetPrototypeAdd(obj[setlikeInner], value); + }, + }, + delete: { + configurable: true, + enumerable: true, + writable: true, + value(value) { + assertBranded(this, objPrototype); + return SetPrototypeDelete(obj[setlikeInner], value); + }, + }, + clear: { + configurable: true, + enumerable: true, + writable: true, + value() { + assertBranded(this, objPrototype); + return SetPrototypeClear(obj[setlikeInner]); + }, + }, + }); + } + } + window.__bootstrap ??= {}; window.__bootstrap.webidl = { type, @@ -1068,5 +1178,7 @@ illegalConstructor, mixinPairIterable, configurePrototype, + setlike, + setlikeInner, }; })(this);