Skip to content

Commit

Permalink
unite webidl stringification (#2843)
Browse files Browse the repository at this point in the history
* unite webidl stringification

* fixup

* fixup

* fixup

* fixup

* fixup
  • Loading branch information
KhafraDev authored Feb 26, 2024
1 parent 0abea11 commit 6bb0c9b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
29 changes: 22 additions & 7 deletions lib/web/fetch/webidl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { types } = require('node:util')
const { types, inspect } = require('node:util')
const { toUSVString } = require('../../core/util')

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

Expand Down Expand Up @@ -216,6 +216,21 @@ webidl.util.IntegerPart = function (n) {
return r
}

webidl.util.Stringify = function (V) {
const type = webidl.util.Type(V)

switch (type) {
case 'Symbol':
return `Symbol(${V.description})`
case 'Object':
return inspect(V)
case 'String':
return `"${V}"`
default:
return `${V}`
}
}

// https://webidl.spec.whatwg.org/#es-sequence
webidl.sequenceConverter = function (converter) {
return (V, Iterable) => {
Expand Down Expand Up @@ -324,7 +339,7 @@ webidl.interfaceConverter = function (i) {
if (opts.strict !== false && !(V instanceof i)) {
throw webidl.errors.exception({
header: i.name,
message: `Expected ${V} to be an instance of ${i.name}.`
message: `Expected ${webidl.util.Stringify(V)} to be an instance of ${i.name}.`
})
}

Expand Down Expand Up @@ -515,8 +530,8 @@ webidl.converters.ArrayBuffer = function (V, opts = {}) {
!types.isAnyArrayBuffer(V)
) {
throw webidl.errors.conversionFailed({
prefix: `${V}`,
argument: `${V}`,
prefix: webidl.util.Stringify(V),
argument: webidl.util.Stringify(V),
types: ['ArrayBuffer']
})
}
Expand Down Expand Up @@ -561,7 +576,7 @@ webidl.converters.TypedArray = function (V, T, opts = {}) {
) {
throw webidl.errors.conversionFailed({
prefix: `${T.name}`,
argument: `${V}`,
argument: webidl.util.Stringify(V),
types: [T.name]
})
}
Expand Down Expand Up @@ -644,7 +659,7 @@ webidl.converters.BufferSource = function (V, opts = {}) {
return webidl.converters.DataView(V, opts, { ...opts, allowShared: false })
}

throw new TypeError(`Could not convert ${V} to a BufferSource.`)
throw new TypeError(`Could not convert ${webidl.util.Stringify(V)} to a BufferSource.`)
}

webidl.converters['sequence<ByteString>'] = webidl.sequenceConverter(
Expand Down
23 changes: 23 additions & 0 deletions test/webidl/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,26 @@ test('ByteString', () => {
'index 7 has a value of 256 which is greater than 255.'
})
})

test('webidl.util.Stringify', (t) => {
const circular = {}
circular.circular = circular

const pairs = [
[Object.create(null), '[Object: null prototype] {}'],
[{ a: 'b' }, "{ a: 'b' }"],
[Symbol('sym'), 'Symbol(sym)'],
[Symbol.iterator, 'Symbol(Symbol.iterator)'], // well-known symbol
[true, 'true'],
[0, '0'],
['hello', '"hello"'],
['', '""'],
[null, 'null'],
[undefined, 'undefined'],
[circular, '<ref *1> { circular: [Circular *1] }']
]

for (const [value, expected] of pairs) {
assert.deepStrictEqual(webidl.util.Stringify(value), expected)
}
})
4 changes: 2 additions & 2 deletions test/websocket/messageevent.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ test('test/parallel/test-worker-message-port.js', () => {
})
assert.throws(() => new MessageEvent('message', { source: {} }), {
constructor: TypeError,
message: 'MessagePort: Expected [object Object] to be an instance of MessagePort.'
message: 'MessagePort: Expected {} to be an instance of MessagePort.'
})
assert.throws(() => new MessageEvent('message', { ports: 0 }), {
constructor: TypeError,
Expand All @@ -117,7 +117,7 @@ test('test/parallel/test-worker-message-port.js', () => {
new MessageEvent('message', { ports: [{}] })
, {
constructor: TypeError,
message: 'MessagePort: Expected [object Object] to be an instance of MessagePort.'
message: 'MessagePort: Expected {} to be an instance of MessagePort.'
})

assert(new MessageEvent('message') instanceof Event)
Expand Down
5 changes: 5 additions & 0 deletions types/webidl.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ interface WebidlUtil {
* @see https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
*/
IntegerPart (N: number): number

/**
* Stringifies {@param V}
*/
Stringify (V: any): string
}

interface WebidlConverters {
Expand Down

0 comments on commit 6bb0c9b

Please sign in to comment.