| 1 |
/** |
| 2 |
* AUTO-GENERATED - DO NOT EDIT. Source: https://github.com/gpuweb/cts |
| 3 |
**/import { assert, sortObjectByKey, isPlainObject } from '../../util/util.js'; |
| 4 |
// JSON can't represent various values and by default stores them as `null`. |
| 5 |
// Instead, storing them as a magic string values in JSON. |
| 6 |
const jsUndefinedMagicValue = '_undef_'; |
| 7 |
const jsNaNMagicValue = '_nan_'; |
| 8 |
const jsPositiveInfinityMagicValue = '_posinfinity_'; |
| 9 |
const jsNegativeInfinityMagicValue = '_neginfinity_'; |
| 10 |
|
| 11 |
// -0 needs to be handled separately, because -0 === +0 returns true. Not |
| 12 |
// special casing +0/0, since it behaves intuitively. Assuming that if -0 is |
| 13 |
// being used, the differentiation from +0 is desired. |
| 14 |
const jsNegativeZeroMagicValue = '_negzero_'; |
| 15 |
|
| 16 |
// bigint values are not defined in JSON, so need to wrap them up as strings |
| 17 |
const jsBigIntMagicPattern = /^(\d+)n$/; |
| 18 |
|
| 19 |
const toStringMagicValue = new Map([ |
| 20 |
[undefined, jsUndefinedMagicValue], |
| 21 |
[NaN, jsNaNMagicValue], |
| 22 |
[Number.POSITIVE_INFINITY, jsPositiveInfinityMagicValue], |
| 23 |
[Number.NEGATIVE_INFINITY, jsNegativeInfinityMagicValue] |
| 24 |
// No -0 handling because it is special cased. |
| 25 |
]); |
| 26 |
|
| 27 |
const fromStringMagicValue = new Map([ |
| 28 |
[jsUndefinedMagicValue, undefined], |
| 29 |
[jsNaNMagicValue, NaN], |
| 30 |
[jsPositiveInfinityMagicValue, Number.POSITIVE_INFINITY], |
| 31 |
[jsNegativeInfinityMagicValue, Number.NEGATIVE_INFINITY], |
| 32 |
// -0 is handled in this direction because there is no comparison issue. |
| 33 |
[jsNegativeZeroMagicValue, -0]] |
| 34 |
); |
| 35 |
|
| 36 |
function stringifyFilter(_k, v) { |
| 37 |
// Make sure no one actually uses a magic value as a parameter. |
| 38 |
if (typeof v === 'string') { |
| 39 |
assert( |
| 40 |
!fromStringMagicValue.has(v), |
| 41 |
`${v} is a magic value for stringification, so cannot be used` |
| 42 |
); |
| 43 |
|
| 44 |
assert( |
| 45 |
v !== jsNegativeZeroMagicValue, |
| 46 |
`${v} is a magic value for stringification, so cannot be used` |
| 47 |
); |
| 48 |
|
| 49 |
assert( |
| 50 |
v.match(jsBigIntMagicPattern) === null, |
| 51 |
`${v} matches bigint magic pattern for stringification, so cannot be used` |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
const isObject = v !== null && typeof v === 'object' && !Array.isArray(v); |
| 56 |
if (isObject) { |
| 57 |
assert( |
| 58 |
isPlainObject(v), |
| 59 |
`value must be a plain object but it appears to be a '${ |
| 60 |
Object.getPrototypeOf(v).constructor.name |
| 61 |
}` |
| 62 |
); |
| 63 |
} |
| 64 |
assert(typeof v !== 'function', `${v} can not be a function`); |
| 65 |
|
| 66 |
if (Object.is(v, -0)) { |
| 67 |
return jsNegativeZeroMagicValue; |
| 68 |
} |
| 69 |
|
| 70 |
if (typeof v === 'bigint') { |
| 71 |
return `${v}n`; |
| 72 |
} |
| 73 |
|
| 74 |
return toStringMagicValue.has(v) ? toStringMagicValue.get(v) : v; |
| 75 |
} |
| 76 |
|
| 77 |
export function stringifyParamValue(value) { |
| 78 |
return JSON.stringify(value, stringifyFilter); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Like stringifyParamValue but sorts dictionaries by key, for hashing. |
| 83 |
*/ |
| 84 |
export function stringifyParamValueUniquely(value) { |
| 85 |
return JSON.stringify(value, (k, v) => { |
| 86 |
if (typeof v === 'object' && v !== null) { |
| 87 |
return sortObjectByKey(v); |
| 88 |
} |
| 89 |
|
| 90 |
return stringifyFilter(k, v); |
| 91 |
}); |
| 92 |
} |
| 93 |
|
| 94 |
// 'any' is part of the JSON.parse reviver interface, so cannot be avoided. |
| 95 |
|
| 96 |
function parseParamValueReviver(_k, v) { |
| 97 |
if (fromStringMagicValue.has(v)) { |
| 98 |
return fromStringMagicValue.get(v); |
| 99 |
} |
| 100 |
|
| 101 |
if (typeof v === 'string') { |
| 102 |
const match = v.match(jsBigIntMagicPattern); |
| 103 |
if (match !== null) { |
| 104 |
// [0] is the entire match, and following entries are the capture groups |
| 105 |
return BigInt(match[1]); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return v; |
| 110 |
} |
| 111 |
|
| 112 |
export function parseParamValue(s) { |
| 113 |
return JSON.parse(s, parseParamValueReviver); |
| 114 |
} |