Skip to content

Commit c2da306

Browse files
authored
change webidl attribute to bitwise flag (#4505)
* change webidl attribute to bitwise flag * rename opts to flags
1 parent 13b2874 commit c2da306

9 files changed

Lines changed: 107 additions & 130 deletions

File tree

lib/web/fetch/response.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class Response {
118118
}
119119

120120
if (body !== null) {
121-
body = webidl.converters.BodyInit(body)
121+
body = webidl.converters.BodyInit(body, 'Response', 'body')
122122
}
123123

124124
init = webidl.converters.ResponseInit(init)

lib/web/webidl/index.js

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ webidl.util.TypeValueToString = function (o) {
160160
webidl.util.markAsUncloneable = markAsUncloneable || (() => {})
161161

162162
// https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
163-
webidl.util.ConvertToInt = function (V, bitLength, signedness, opts) {
163+
webidl.util.ConvertToInt = function (V, bitLength, signedness, flags) {
164164
let upperBound
165165
let lowerBound
166166

@@ -204,7 +204,7 @@ webidl.util.ConvertToInt = function (V, bitLength, signedness, opts) {
204204

205205
// 6. If the conversion is to an IDL type associated
206206
// with the [EnforceRange] extended attribute, then:
207-
if (opts?.enforceRange === true) {
207+
if (webidl.util.HasFlag(flags, webidl.attributes.EnforceRange)) {
208208
// 1. If x is NaN, +∞, or −∞, then throw a TypeError.
209209
if (
210210
Number.isNaN(x) ||
@@ -236,7 +236,7 @@ webidl.util.ConvertToInt = function (V, bitLength, signedness, opts) {
236236
// 7. If x is not NaN and the conversion is to an IDL
237237
// type associated with the [Clamp] extended
238238
// attribute, then:
239-
if (!Number.isNaN(x) && opts?.clamp === true) {
239+
if (!Number.isNaN(x) && webidl.util.HasFlag(flags, webidl.attributes.Clamp)) {
240240
// 1. Set x to min(max(x, lowerBound), upperBound).
241241
x = Math.min(Math.max(x, lowerBound), upperBound)
242242

@@ -325,6 +325,10 @@ webidl.util.IsResizableArrayBuffer = function (V) {
325325
})
326326
}
327327

328+
webidl.util.HasFlag = function (flags, attributes) {
329+
return typeof flags === 'number' && (flags & attributes) === attributes
330+
}
331+
328332
// https://webidl.spec.whatwg.org/#es-sequence
329333
webidl.sequenceConverter = function (converter) {
330334
return (V, prefix, argument, Iterable) => {
@@ -537,12 +541,12 @@ webidl.is.BufferSource = function (V) {
537541
}
538542

539543
// https://webidl.spec.whatwg.org/#es-DOMString
540-
webidl.converters.DOMString = function (V, prefix, argument, opts) {
544+
webidl.converters.DOMString = function (V, prefix, argument, flags) {
541545
// 1. If V is null and the conversion is to an IDL type
542546
// associated with the [LegacyNullToEmptyString]
543547
// extended attribute, then return the DOMString value
544548
// that represents the empty string.
545-
if (V === null && opts?.legacyNullToEmptyString) {
549+
if (V === null && webidl.util.HasFlag(flags, webidl.attributes.LegacyNullToEmptyString)) {
546550
return ''
547551
}
548552

@@ -621,7 +625,7 @@ webidl.converters.any = function (V) {
621625
// https://webidl.spec.whatwg.org/#es-long-long
622626
webidl.converters['long long'] = function (V, prefix, argument) {
623627
// 1. Let x be ? ConvertToInt(V, 64, "signed").
624-
const x = webidl.util.ConvertToInt(V, 64, 'signed', undefined, prefix, argument)
628+
const x = webidl.util.ConvertToInt(V, 64, 'signed', 0, prefix, argument)
625629

626630
// 2. Return the IDL long long value that represents
627631
// the same numeric value as x.
@@ -631,7 +635,7 @@ webidl.converters['long long'] = function (V, prefix, argument) {
631635
// https://webidl.spec.whatwg.org/#es-unsigned-long-long
632636
webidl.converters['unsigned long long'] = function (V, prefix, argument) {
633637
// 1. Let x be ? ConvertToInt(V, 64, "unsigned").
634-
const x = webidl.util.ConvertToInt(V, 64, 'unsigned', undefined, prefix, argument)
638+
const x = webidl.util.ConvertToInt(V, 64, 'unsigned', 0, prefix, argument)
635639

636640
// 2. Return the IDL unsigned long long value that
637641
// represents the same numeric value as x.
@@ -641,25 +645,25 @@ webidl.converters['unsigned long long'] = function (V, prefix, argument) {
641645
// https://webidl.spec.whatwg.org/#es-unsigned-long
642646
webidl.converters['unsigned long'] = function (V, prefix, argument) {
643647
// 1. Let x be ? ConvertToInt(V, 32, "unsigned").
644-
const x = webidl.util.ConvertToInt(V, 32, 'unsigned', undefined, prefix, argument)
648+
const x = webidl.util.ConvertToInt(V, 32, 'unsigned', 0, prefix, argument)
645649

646650
// 2. Return the IDL unsigned long value that
647651
// represents the same numeric value as x.
648652
return x
649653
}
650654

651655
// https://webidl.spec.whatwg.org/#es-unsigned-short
652-
webidl.converters['unsigned short'] = function (V, prefix, argument, opts) {
656+
webidl.converters['unsigned short'] = function (V, prefix, argument, flags) {
653657
// 1. Let x be ? ConvertToInt(V, 16, "unsigned").
654-
const x = webidl.util.ConvertToInt(V, 16, 'unsigned', opts, prefix, argument)
658+
const x = webidl.util.ConvertToInt(V, 16, 'unsigned', flags, prefix, argument)
655659

656660
// 2. Return the IDL unsigned short value that represents
657661
// the same numeric value as x.
658662
return x
659663
}
660664

661665
// https://webidl.spec.whatwg.org/#idl-ArrayBuffer
662-
webidl.converters.ArrayBuffer = function (V, prefix, argument, opts) {
666+
webidl.converters.ArrayBuffer = function (V, prefix, argument, flags) {
663667
// 1. If V is not an Object, or V does not have an
664668
// [[ArrayBufferData]] internal slot, then throw a
665669
// TypeError.
@@ -681,7 +685,7 @@ webidl.converters.ArrayBuffer = function (V, prefix, argument, opts) {
681685
// with the [AllowResizable] extended attribute, and
682686
// IsResizableArrayBuffer(V) is true, then throw a
683687
// TypeError.
684-
if (!opts?.allowResizable && webidl.util.IsResizableArrayBuffer(V)) {
688+
if (!webidl.util.HasFlag(flags, webidl.attributes.AllowResizable) && webidl.util.IsResizableArrayBuffer(V)) {
685689
throw webidl.errors.exception({
686690
header: prefix,
687691
message: `${argument} cannot be a resizable ArrayBuffer.`
@@ -694,7 +698,7 @@ webidl.converters.ArrayBuffer = function (V, prefix, argument, opts) {
694698
}
695699

696700
// https://webidl.spec.whatwg.org/#idl-SharedArrayBuffer
697-
webidl.converters.SharedArrayBuffer = function (V, prefix, argument, opts) {
701+
webidl.converters.SharedArrayBuffer = function (V, prefix, argument, flags) {
698702
// 1. If V is not an Object, or V does not have an
699703
// [[ArrayBufferData]] internal slot, then throw a
700704
// TypeError.
@@ -716,7 +720,7 @@ webidl.converters.SharedArrayBuffer = function (V, prefix, argument, opts) {
716720
// with the [AllowResizable] extended attribute, and
717721
// IsResizableArrayBuffer(V) is true, then throw a
718722
// TypeError.
719-
if (!opts?.allowResizable && webidl.util.IsResizableArrayBuffer(V)) {
723+
if (!webidl.util.HasFlag(flags, webidl.attributes.AllowResizable) && webidl.util.IsResizableArrayBuffer(V)) {
720724
throw webidl.errors.exception({
721725
header: prefix,
722726
message: `${argument} cannot be a resizable SharedArrayBuffer.`
@@ -729,7 +733,7 @@ webidl.converters.SharedArrayBuffer = function (V, prefix, argument, opts) {
729733
}
730734

731735
// https://webidl.spec.whatwg.org/#dfn-typed-array-type
732-
webidl.converters.TypedArray = function (V, T, prefix, argument, opts) {
736+
webidl.converters.TypedArray = function (V, T, prefix, argument, flags) {
733737
// 1. Let T be the IDL type V is being converted to.
734738

735739
// 2. If Type(V) is not Object, or V does not have a
@@ -751,7 +755,7 @@ webidl.converters.TypedArray = function (V, T, prefix, argument, opts) {
751755
// with the [AllowShared] extended attribute, and
752756
// IsSharedArrayBuffer(V.[[ViewedArrayBuffer]]) is
753757
// true, then throw a TypeError.
754-
if (!opts?.allowShared && types.isSharedArrayBuffer(V.buffer)) {
758+
if (!webidl.util.HasFlag(flags, webidl.attributes.AllowShared) && types.isSharedArrayBuffer(V.buffer)) {
755759
throw webidl.errors.exception({
756760
header: prefix,
757761
message: `${argument} cannot be a view on a shared array buffer.`
@@ -762,7 +766,7 @@ webidl.converters.TypedArray = function (V, T, prefix, argument, opts) {
762766
// with the [AllowResizable] extended attribute, and
763767
// IsResizableArrayBuffer(V.[[ViewedArrayBuffer]]) is
764768
// true, then throw a TypeError.
765-
if (!opts?.allowResizable && webidl.util.IsResizableArrayBuffer(V.buffer)) {
769+
if (!webidl.util.HasFlag(flags, webidl.attributes.AllowResizable) && webidl.util.IsResizableArrayBuffer(V.buffer)) {
766770
throw webidl.errors.exception({
767771
header: prefix,
768772
message: `${argument} cannot be a view on a resizable array buffer.`
@@ -775,7 +779,7 @@ webidl.converters.TypedArray = function (V, T, prefix, argument, opts) {
775779
}
776780

777781
// https://webidl.spec.whatwg.org/#idl-DataView
778-
webidl.converters.DataView = function (V, prefix, argument, opts) {
782+
webidl.converters.DataView = function (V, prefix, argument, flags) {
779783
// 1. If Type(V) is not Object, or V does not have a
780784
// [[DataView]] internal slot, then throw a TypeError.
781785
if (webidl.util.Type(V) !== OBJECT || !types.isDataView(V)) {
@@ -790,7 +794,7 @@ webidl.converters.DataView = function (V, prefix, argument, opts) {
790794
// with the [AllowShared] extended attribute, and
791795
// IsSharedArrayBuffer(V.[[ViewedArrayBuffer]]) is true,
792796
// then throw a TypeError.
793-
if (!opts?.allowShared && types.isSharedArrayBuffer(V.buffer)) {
797+
if (!webidl.util.HasFlag(flags, webidl.attributes.AllowShared) && types.isSharedArrayBuffer(V.buffer)) {
794798
throw webidl.errors.exception({
795799
header: prefix,
796800
message: `${argument} cannot be a view on a shared array buffer.`
@@ -801,7 +805,7 @@ webidl.converters.DataView = function (V, prefix, argument, opts) {
801805
// with the [AllowResizable] extended attribute, and
802806
// IsResizableArrayBuffer(V.[[ViewedArrayBuffer]]) is
803807
// true, then throw a TypeError.
804-
if (!opts?.allowResizable && webidl.util.IsResizableArrayBuffer(V.buffer)) {
808+
if (!webidl.util.HasFlag(flags, webidl.attributes.AllowResizable) && webidl.util.IsResizableArrayBuffer(V.buffer)) {
805809
throw webidl.errors.exception({
806810
header: prefix,
807811
message: `${argument} cannot be a view on a resizable array buffer.`
@@ -814,7 +818,7 @@ webidl.converters.DataView = function (V, prefix, argument, opts) {
814818
}
815819

816820
// https://webidl.spec.whatwg.org/#ArrayBufferView
817-
webidl.converters.ArrayBufferView = function (V, prefix, argument, opts) {
821+
webidl.converters.ArrayBufferView = function (V, prefix, argument, flags) {
818822
if (
819823
webidl.util.Type(V) !== OBJECT ||
820824
!types.isArrayBufferView(V)
@@ -826,14 +830,14 @@ webidl.converters.ArrayBufferView = function (V, prefix, argument, opts) {
826830
})
827831
}
828832

829-
if (!opts?.allowShared && types.isSharedArrayBuffer(V.buffer)) {
833+
if (!webidl.util.HasFlag(flags, webidl.attributes.AllowShared) && types.isSharedArrayBuffer(V.buffer)) {
830834
throw webidl.errors.exception({
831835
header: prefix,
832836
message: `${argument} cannot be a view on a shared array buffer.`
833837
})
834838
}
835839

836-
if (!opts?.allowResizable && webidl.util.IsResizableArrayBuffer(V.buffer)) {
840+
if (!webidl.util.HasFlag(flags, webidl.attributes.AllowResizable) && webidl.util.IsResizableArrayBuffer(V.buffer)) {
837841
throw webidl.errors.exception({
838842
header: prefix,
839843
message: `${argument} cannot be a view on a resizable array buffer.`
@@ -844,16 +848,15 @@ webidl.converters.ArrayBufferView = function (V, prefix, argument, opts) {
844848
}
845849

846850
// https://webidl.spec.whatwg.org/#BufferSource
847-
webidl.converters.BufferSource = function (V, prefix, argument, opts) {
851+
webidl.converters.BufferSource = function (V, prefix, argument, flags) {
848852
if (types.isArrayBuffer(V)) {
849-
return webidl.converters.ArrayBuffer(V, prefix, argument, opts)
853+
return webidl.converters.ArrayBuffer(V, prefix, argument, flags)
850854
}
851855

852856
if (types.isArrayBufferView(V)) {
853-
return webidl.converters.ArrayBufferView(V, prefix, argument, {
854-
...opts,
855-
allowShared: false
856-
})
857+
flags &= ~webidl.attributes.AllowShared
858+
859+
return webidl.converters.ArrayBufferView(V, prefix, argument, flags)
857860
}
858861

859862
// Make this explicit for easier debugging
@@ -872,20 +875,18 @@ webidl.converters.BufferSource = function (V, prefix, argument, opts) {
872875
}
873876

874877
// https://webidl.spec.whatwg.org/#AllowSharedBufferSource
875-
webidl.converters.AllowSharedBufferSource = function (V, prefix, argument, opts) {
878+
webidl.converters.AllowSharedBufferSource = function (V, prefix, argument, flags) {
876879
if (types.isArrayBuffer(V)) {
877-
return webidl.converters.ArrayBuffer(V, prefix, argument, opts)
880+
return webidl.converters.ArrayBuffer(V, prefix, argument, flags)
878881
}
879882

880883
if (types.isSharedArrayBuffer(V)) {
881-
return webidl.converters.SharedArrayBuffer(V, prefix, argument, opts)
884+
return webidl.converters.SharedArrayBuffer(V, prefix, argument, flags)
882885
}
883886

884887
if (types.isArrayBufferView(V)) {
885-
return webidl.converters.ArrayBufferView(V, prefix, argument, {
886-
...opts,
887-
allowShared: true
888-
})
888+
flags |= webidl.attributes.AllowShared
889+
return webidl.converters.ArrayBufferView(V, prefix, argument, flags)
889890
}
890891

891892
throw webidl.errors.conversionFailed({
@@ -935,6 +936,14 @@ webidl.converters.EventHandlerNonNull = function (V) {
935936
return () => {}
936937
}
937938

939+
webidl.attributes = {
940+
Clamp: 1 << 0,
941+
EnforceRange: 1 << 1,
942+
AllowShared: 1 << 2,
943+
AllowResizable: 1 << 3,
944+
LegacyNullToEmptyString: 1 << 4
945+
}
946+
938947
module.exports = {
939948
webidl
940949
}

lib/web/websocket/stream/websocketstream.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ webidl.converters.WebSocketStreamOptions = webidl.dictionaryConverter([
478478
webidl.converters.WebSocketCloseInfo = webidl.dictionaryConverter([
479479
{
480480
key: 'closeCode',
481-
converter: (V) => webidl.converters['unsigned short'](V, { enforceRange: true })
481+
converter: (V) => webidl.converters['unsigned short'](V, webidl.attributes.EnforceRange)
482482
},
483483
{
484484
key: 'reason',

lib/web/websocket/websocket.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class WebSocket extends EventTarget {
195195
const prefix = 'WebSocket.close'
196196

197197
if (code !== undefined) {
198-
code = webidl.converters['unsigned short'](code, prefix, 'code', { clamp: true })
198+
code = webidl.converters['unsigned short'](code, prefix, 'code', webidl.attributes.Clamp)
199199
}
200200

201201
if (reason !== undefined) {
@@ -739,7 +739,11 @@ webidl.converters['DOMString or sequence<DOMString> or WebSocketInit'] = functio
739739

740740
webidl.converters.WebSocketSendData = function (V) {
741741
if (webidl.util.Type(V) === webidl.util.Types.OBJECT) {
742-
if (webidl.is.Blob(V) || webidl.is.BufferSource(V)) {
742+
if (webidl.is.Blob(V)) {
743+
return V
744+
}
745+
746+
if (webidl.is.BufferSource(V)) {
743747
return V
744748
}
745749
}

test/fetch/request.js

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const {
1111
fetch
1212
} = require('../../')
1313

14-
const hasSignalReason = 'reason' in AbortSignal.prototype
15-
1614
test('arg validation', async (t) => {
1715
// constructor
1816
assert.throws(() => {
@@ -261,23 +259,17 @@ test('pre aborted signal', () => {
261259
ac.abort('gwak')
262260
const req = new Request('http://asd', { signal: ac.signal })
263261
assert.strictEqual(req.signal.aborted, true)
264-
if (hasSignalReason) {
265-
assert.strictEqual(req.signal.reason, 'gwak')
266-
}
262+
assert.strictEqual(req.signal.reason, 'gwak')
267263
})
268264

269265
test('post aborted signal', (t) => {
270-
const { strictEqual, ok } = tspl(t, { plan: 2 })
266+
const { strictEqual } = tspl(t, { plan: 2 })
271267

272268
const ac = new AbortController()
273269
const req = new Request('http://asd', { signal: ac.signal })
274270
strictEqual(req.signal.aborted, false)
275271
ac.signal.addEventListener('abort', () => {
276-
if (hasSignalReason) {
277-
strictEqual(req.signal.reason, 'gwak')
278-
} else {
279-
ok(true)
280-
}
272+
strictEqual(req.signal.reason, 'gwak')
281273
}, { once: true })
282274
ac.abort('gwak')
283275
})
@@ -287,9 +279,7 @@ test('pre aborted signal cloned', () => {
287279
ac.abort('gwak')
288280
const req = new Request('http://asd', { signal: ac.signal }).clone()
289281
assert.strictEqual(req.signal.aborted, true)
290-
if (hasSignalReason) {
291-
assert.strictEqual(req.signal.reason, 'gwak')
292-
}
282+
assert.strictEqual(req.signal.reason, 'gwak')
293283
})
294284

295285
test('URLSearchParams body with Headers object - issue #1407', async () => {
@@ -314,17 +304,13 @@ test('URLSearchParams body with Headers object - issue #1407', async () => {
314304
})
315305

316306
test('post aborted signal cloned', (t) => {
317-
const { strictEqual, ok } = tspl(t, { plan: 2 })
307+
const { strictEqual } = tspl(t, { plan: 2 })
318308

319309
const ac = new AbortController()
320310
const req = new Request('http://asd', { signal: ac.signal }).clone()
321311
strictEqual(req.signal.aborted, false)
322312
ac.signal.addEventListener('abort', () => {
323-
if (hasSignalReason) {
324-
strictEqual(req.signal.reason, 'gwak')
325-
} else {
326-
ok(true)
327-
}
313+
strictEqual(req.signal.reason, 'gwak')
328314
}, { once: true })
329315
ac.abort('gwak')
330316
})

0 commit comments

Comments
 (0)