|
1 | 1 | 'use strict'
|
2 | 2 |
|
3 |
| -const { types } = require('node:util') |
| 3 | +const { types, inspect } = require('node:util') |
4 | 4 | const { toUSVString } = require('../../core/util')
|
5 | 5 |
|
6 | 6 | /** @type {import('../../../types/webidl').Webidl} */
|
@@ -136,7 +136,7 @@ webidl.util.ConvertToInt = function (V, bitLength, signedness, opts = {}) {
|
136 | 136 | ) {
|
137 | 137 | throw webidl.errors.exception({
|
138 | 138 | header: 'Integer conversion',
|
139 |
| - message: `Could not convert ${V} to an integer.` |
| 139 | + message: `Could not convert ${webidl.util.Stringify(V)} to an integer.` |
140 | 140 | })
|
141 | 141 | }
|
142 | 142 |
|
@@ -216,6 +216,21 @@ webidl.util.IntegerPart = function (n) {
|
216 | 216 | return r
|
217 | 217 | }
|
218 | 218 |
|
| 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 | + |
219 | 234 | // https://webidl.spec.whatwg.org/#es-sequence
|
220 | 235 | webidl.sequenceConverter = function (converter) {
|
221 | 236 | return (V, Iterable) => {
|
@@ -324,7 +339,7 @@ webidl.interfaceConverter = function (i) {
|
324 | 339 | if (opts.strict !== false && !(V instanceof i)) {
|
325 | 340 | throw webidl.errors.exception({
|
326 | 341 | 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}.` |
328 | 343 | })
|
329 | 344 | }
|
330 | 345 |
|
@@ -515,8 +530,8 @@ webidl.converters.ArrayBuffer = function (V, opts = {}) {
|
515 | 530 | !types.isAnyArrayBuffer(V)
|
516 | 531 | ) {
|
517 | 532 | throw webidl.errors.conversionFailed({
|
518 |
| - prefix: `${V}`, |
519 |
| - argument: `${V}`, |
| 533 | + prefix: webidl.util.Stringify(V), |
| 534 | + argument: webidl.util.Stringify(V), |
520 | 535 | types: ['ArrayBuffer']
|
521 | 536 | })
|
522 | 537 | }
|
@@ -561,7 +576,7 @@ webidl.converters.TypedArray = function (V, T, opts = {}) {
|
561 | 576 | ) {
|
562 | 577 | throw webidl.errors.conversionFailed({
|
563 | 578 | prefix: `${T.name}`,
|
564 |
| - argument: `${V}`, |
| 579 | + argument: webidl.util.Stringify(V), |
565 | 580 | types: [T.name]
|
566 | 581 | })
|
567 | 582 | }
|
@@ -644,7 +659,7 @@ webidl.converters.BufferSource = function (V, opts = {}) {
|
644 | 659 | return webidl.converters.DataView(V, opts, { ...opts, allowShared: false })
|
645 | 660 | }
|
646 | 661 |
|
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.`) |
648 | 663 | }
|
649 | 664 |
|
650 | 665 | webidl.converters['sequence<ByteString>'] = webidl.sequenceConverter(
|
|
0 commit comments