diff --git a/lib/web/fetch/webidl.js b/lib/web/fetch/webidl.js index 41f5813db69..238ee88e07d 100644 --- a/lib/web/fetch/webidl.js +++ b/lib/web/fetch/webidl.js @@ -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} */ @@ -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.` }) } @@ -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) => { @@ -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}.` }) } @@ -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'] }) } @@ -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] }) } @@ -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'] = webidl.sequenceConverter( diff --git a/test/webidl/converters.js b/test/webidl/converters.js index 0e906ed6719..f39c05a8d7b 100644 --- a/test/webidl/converters.js +++ b/test/webidl/converters.js @@ -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, ' { circular: [Circular *1] }'] + ] + + for (const [value, expected] of pairs) { + assert.deepStrictEqual(webidl.util.Stringify(value), expected) + } +}) diff --git a/test/websocket/messageevent.js b/test/websocket/messageevent.js index e1ab3d16dd7..de3b6c5fc8e 100644 --- a/test/websocket/messageevent.js +++ b/test/websocket/messageevent.js @@ -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, @@ -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) diff --git a/types/webidl.d.ts b/types/webidl.d.ts index f29bebbb1e8..1e362d6f40d 100644 --- a/types/webidl.d.ts +++ b/types/webidl.d.ts @@ -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 {