Skip to content

Commit 6bb0c9b

Browse files
authored
unite webidl stringification (#2843)
* unite webidl stringification * fixup * fixup * fixup * fixup * fixup
1 parent 0abea11 commit 6bb0c9b

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

lib/web/fetch/webidl.js

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { types } = require('node:util')
3+
const { types, inspect } = require('node:util')
44
const { toUSVString } = require('../../core/util')
55

66
/** @type {import('../../../types/webidl').Webidl} */
@@ -136,7 +136,7 @@ webidl.util.ConvertToInt = function (V, bitLength, signedness, opts = {}) {
136136
) {
137137
throw webidl.errors.exception({
138138
header: 'Integer conversion',
139-
message: `Could not convert ${V} to an integer.`
139+
message: `Could not convert ${webidl.util.Stringify(V)} to an integer.`
140140
})
141141
}
142142

@@ -216,6 +216,21 @@ webidl.util.IntegerPart = function (n) {
216216
return r
217217
}
218218

219+
webidl.util.Stringify = function (V) {
220+
const type = webidl.util.Type(V)
221+
222+
switch (type) {
223+
case 'Symbol':
224+
return `Symbol(${V.description})`
225+
case 'Object':
226+
return inspect(V)
227+
case 'String':
228+
return `"${V}"`
229+
default:
230+
return `${V}`
231+
}
232+
}
233+
219234
// https://webidl.spec.whatwg.org/#es-sequence
220235
webidl.sequenceConverter = function (converter) {
221236
return (V, Iterable) => {
@@ -324,7 +339,7 @@ webidl.interfaceConverter = function (i) {
324339
if (opts.strict !== false && !(V instanceof i)) {
325340
throw webidl.errors.exception({
326341
header: i.name,
327-
message: `Expected ${V} to be an instance of ${i.name}.`
342+
message: `Expected ${webidl.util.Stringify(V)} to be an instance of ${i.name}.`
328343
})
329344
}
330345

@@ -515,8 +530,8 @@ webidl.converters.ArrayBuffer = function (V, opts = {}) {
515530
!types.isAnyArrayBuffer(V)
516531
) {
517532
throw webidl.errors.conversionFailed({
518-
prefix: `${V}`,
519-
argument: `${V}`,
533+
prefix: webidl.util.Stringify(V),
534+
argument: webidl.util.Stringify(V),
520535
types: ['ArrayBuffer']
521536
})
522537
}
@@ -561,7 +576,7 @@ webidl.converters.TypedArray = function (V, T, opts = {}) {
561576
) {
562577
throw webidl.errors.conversionFailed({
563578
prefix: `${T.name}`,
564-
argument: `${V}`,
579+
argument: webidl.util.Stringify(V),
565580
types: [T.name]
566581
})
567582
}
@@ -644,7 +659,7 @@ webidl.converters.BufferSource = function (V, opts = {}) {
644659
return webidl.converters.DataView(V, opts, { ...opts, allowShared: false })
645660
}
646661

647-
throw new TypeError(`Could not convert ${V} to a BufferSource.`)
662+
throw new TypeError(`Could not convert ${webidl.util.Stringify(V)} to a BufferSource.`)
648663
}
649664

650665
webidl.converters['sequence<ByteString>'] = webidl.sequenceConverter(

test/webidl/converters.js

+23
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,26 @@ test('ByteString', () => {
183183
'index 7 has a value of 256 which is greater than 255.'
184184
})
185185
})
186+
187+
test('webidl.util.Stringify', (t) => {
188+
const circular = {}
189+
circular.circular = circular
190+
191+
const pairs = [
192+
[Object.create(null), '[Object: null prototype] {}'],
193+
[{ a: 'b' }, "{ a: 'b' }"],
194+
[Symbol('sym'), 'Symbol(sym)'],
195+
[Symbol.iterator, 'Symbol(Symbol.iterator)'], // well-known symbol
196+
[true, 'true'],
197+
[0, '0'],
198+
['hello', '"hello"'],
199+
['', '""'],
200+
[null, 'null'],
201+
[undefined, 'undefined'],
202+
[circular, '<ref *1> { circular: [Circular *1] }']
203+
]
204+
205+
for (const [value, expected] of pairs) {
206+
assert.deepStrictEqual(webidl.util.Stringify(value), expected)
207+
}
208+
})

test/websocket/messageevent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ test('test/parallel/test-worker-message-port.js', () => {
103103
})
104104
assert.throws(() => new MessageEvent('message', { source: {} }), {
105105
constructor: TypeError,
106-
message: 'MessagePort: Expected [object Object] to be an instance of MessagePort.'
106+
message: 'MessagePort: Expected {} to be an instance of MessagePort.'
107107
})
108108
assert.throws(() => new MessageEvent('message', { ports: 0 }), {
109109
constructor: TypeError,
@@ -117,7 +117,7 @@ test('test/parallel/test-worker-message-port.js', () => {
117117
new MessageEvent('message', { ports: [{}] })
118118
, {
119119
constructor: TypeError,
120-
message: 'MessagePort: Expected [object Object] to be an instance of MessagePort.'
120+
message: 'MessagePort: Expected {} to be an instance of MessagePort.'
121121
})
122122

123123
assert(new MessageEvent('message') instanceof Event)

types/webidl.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ interface WebidlUtil {
6262
* @see https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
6363
*/
6464
IntegerPart (N: number): number
65+
66+
/**
67+
* Stringifies {@param V}
68+
*/
69+
Stringify (V: any): string
6570
}
6671

6772
interface WebidlConverters {

0 commit comments

Comments
 (0)