Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unite webidl stringification #2843

Merged
merged 6 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions lib/web/fetch/webidl.js
Original file line number Diff line number Diff line change
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 JSON.stringify(V)
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
case 'String':
return V.length ? V : '""'
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
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 ${JSON.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 @@ -556,7 +571,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 @@ -629,7 +644,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
19 changes: 19 additions & 0 deletions test/webidl/converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,22 @@ test('ByteString', () => {
'index 7 has a value of 256 which is greater than 255.'
})
})

test('webidl.util.Stringify', (t) => {
const pairs = [
[Object.create(null), '{}'],
[{ 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']
]
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved

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
Loading