From e80862eee1d8d0990c5ed38fa439e8608ffbad30 Mon Sep 17 00:00:00 2001 From: Khafra Date: Sun, 25 Feb 2024 11:05:11 -0500 Subject: [PATCH 1/6] unite webidl stringification --- lib/web/fetch/webidl.js | 25 +++++++++++++++++++------ test/webidl/converters.js | 18 ++++++++++++++++++ types/webidl.d.ts | 5 +++++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/web/fetch/webidl.js b/lib/web/fetch/webidl.js index 41f5813db69..0c83ce988ca 100644 --- a/lib/web/fetch/webidl.js +++ b/lib/web/fetch/webidl.js @@ -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,19 @@ webidl.util.IntegerPart = function (n) { return r } +webidl.util.Stringify = function (V) { + const type = webidl.util.Type(V) + + switch (type) { + case 'Symbol': + return V.description + case 'Object': + return JSON.stringify(V) + default: + return `${V}` + } +} + // https://webidl.spec.whatwg.org/#es-sequence webidl.sequenceConverter = function (converter) { return (V, Iterable) => { @@ -324,7 +337,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}.` }) } @@ -515,8 +528,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 +569,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 +642,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..c26ce0920ce 100644 --- a/test/webidl/converters.js +++ b/test/webidl/converters.js @@ -183,3 +183,21 @@ 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'), 'sym'], + [Symbol.iterator, 'Symbol.iterator'], // well-known symbol + [true, 'true'], + [0, '0'], + ['hello', 'hello'], + [null, 'null'], + [undefined, 'undefined'] + ] + + for (const [value, expected] of pairs) { + assert.deepStrictEqual(webidl.util.Stringify(value), expected) + } +}) 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 { From 389beb9811247e64c242731d32eb2c105416ccbe Mon Sep 17 00:00:00 2001 From: Khafra Date: Sun, 25 Feb 2024 11:12:10 -0500 Subject: [PATCH 2/6] fixup --- lib/web/fetch/webidl.js | 2 +- test/webidl/converters.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/web/fetch/webidl.js b/lib/web/fetch/webidl.js index 0c83ce988ca..18f1c41c39e 100644 --- a/lib/web/fetch/webidl.js +++ b/lib/web/fetch/webidl.js @@ -221,7 +221,7 @@ webidl.util.Stringify = function (V) { switch (type) { case 'Symbol': - return V.description + return `Symbol(${V.description})` case 'Object': return JSON.stringify(V) default: diff --git a/test/webidl/converters.js b/test/webidl/converters.js index c26ce0920ce..b53b996c9de 100644 --- a/test/webidl/converters.js +++ b/test/webidl/converters.js @@ -188,8 +188,8 @@ test('webidl.util.Stringify', (t) => { const pairs = [ [Object.create(null), '{}'], [{ a: 'b' }, '{"a":"b"}'], - [Symbol('sym'), 'sym'], - [Symbol.iterator, 'Symbol.iterator'], // well-known symbol + [Symbol('sym'), 'Symbol(sym)'], + [Symbol.iterator, 'Symbol(Symbol.iterator)'], // well-known symbol [true, 'true'], [0, '0'], ['hello', 'hello'], From 83103b59d864aa7b3999f19140cdaa5646c3fd59 Mon Sep 17 00:00:00 2001 From: Khafra Date: Sun, 25 Feb 2024 11:13:10 -0500 Subject: [PATCH 3/6] fixup --- lib/web/fetch/webidl.js | 2 ++ test/webidl/converters.js | 1 + 2 files changed, 3 insertions(+) diff --git a/lib/web/fetch/webidl.js b/lib/web/fetch/webidl.js index 18f1c41c39e..51d3f03aad0 100644 --- a/lib/web/fetch/webidl.js +++ b/lib/web/fetch/webidl.js @@ -224,6 +224,8 @@ webidl.util.Stringify = function (V) { return `Symbol(${V.description})` case 'Object': return JSON.stringify(V) + case 'String': + return V.length ? V : '""' default: return `${V}` } diff --git a/test/webidl/converters.js b/test/webidl/converters.js index b53b996c9de..0fc093d71f9 100644 --- a/test/webidl/converters.js +++ b/test/webidl/converters.js @@ -193,6 +193,7 @@ test('webidl.util.Stringify', (t) => { [true, 'true'], [0, '0'], ['hello', 'hello'], + ['', '""'], [null, 'null'], [undefined, 'undefined'] ] From 7df10081015fbcba25d1abda767e7fb1c778eb13 Mon Sep 17 00:00:00 2001 From: Khafra Date: Sun, 25 Feb 2024 11:20:56 -0500 Subject: [PATCH 4/6] fixup --- test/websocket/messageevent.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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) From 613b816ddfffcb45c2bfc4a7ec47097171adb07e Mon Sep 17 00:00:00 2001 From: Khafra Date: Sun, 25 Feb 2024 15:06:21 -0500 Subject: [PATCH 5/6] fixup --- lib/web/fetch/webidl.js | 6 +++--- test/webidl/converters.js | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/web/fetch/webidl.js b/lib/web/fetch/webidl.js index 51d3f03aad0..e5bb5b84752 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} */ @@ -223,9 +223,9 @@ webidl.util.Stringify = function (V) { case 'Symbol': return `Symbol(${V.description})` case 'Object': - return JSON.stringify(V) + return inspect(V) case 'String': - return V.length ? V : '""' + return `"${V}"` default: return `${V}` } diff --git a/test/webidl/converters.js b/test/webidl/converters.js index 0fc093d71f9..f39c05a8d7b 100644 --- a/test/webidl/converters.js +++ b/test/webidl/converters.js @@ -185,17 +185,21 @@ test('ByteString', () => { }) test('webidl.util.Stringify', (t) => { + const circular = {} + circular.circular = circular + const pairs = [ - [Object.create(null), '{}'], - [{ a: 'b' }, '{"a":"b"}'], + [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'], + ['hello', '"hello"'], ['', '""'], [null, 'null'], - [undefined, 'undefined'] + [undefined, 'undefined'], + [circular, ' { circular: [Circular *1] }'] ] for (const [value, expected] of pairs) { From c9b7596f2dbbb53c0e65114187f6b298aaae8006 Mon Sep 17 00:00:00 2001 From: Khafra Date: Sun, 25 Feb 2024 15:08:09 -0500 Subject: [PATCH 6/6] fixup --- lib/web/fetch/webidl.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/web/fetch/webidl.js b/lib/web/fetch/webidl.js index e5bb5b84752..238ee88e07d 100644 --- a/lib/web/fetch/webidl.js +++ b/lib/web/fetch/webidl.js @@ -339,7 +339,7 @@ webidl.interfaceConverter = function (i) { if (opts.strict !== false && !(V instanceof i)) { throw webidl.errors.exception({ header: i.name, - message: `Expected ${JSON.stringify(V)} to be an instance of ${i.name}.` + message: `Expected ${webidl.util.Stringify(V)} to be an instance of ${i.name}.` }) }