Debian Codegraph @

js/src/tests/test262/built-ins/JSON/rawJSON/bigint-raw-json-can-be-stringified.js

raw
1 // |reftest| shell-option(--enable-json-parse-with-source) skip-if(!JSON.hasOwnProperty('isRawJSON')||!xulRuntime.shell) -- json-parse-with-source is not enabled unconditionally, requires shell-options
2 // Copyright (C) 2023 the V8 project authors. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4
5 /*---
6 esid: sec-json.rawjson
7 description: BigInt rawJSON can be stringified.
8 info: |
9 JSON.rawJSON ( text )
10
11 1. Let jsonString be ? ToString(text).
12 ...
13 4. Let internalSlotsList be « [[IsRawJSON]] ».
14 5. Let obj be OrdinaryObjectCreate(null, internalSlotsList).
15 6. Perform ! CreateDataPropertyOrThrow(obj, "rawJSON", jsonString).
16 7. Perform ! SetIntegrityLevel(obj, frozen).
17 8. Return obj.
18
19 features: [BigInt, json-parse-with-source]
20 ---*/
21
22 const tooBigForNumber = BigInt(Number.MAX_SAFE_INTEGER) + 2n;
23 const intToBigInt = (key, val, { source }) =>
24 typeof val === 'number' && val % 1 === 0 ? BigInt(source) : val;
25 const roundTripped = JSON.parse(String(tooBigForNumber), intToBigInt);
26 assert.sameValue(roundTripped, tooBigForNumber);
27
28 const bigIntToRawJSON = (key, val) =>
29 typeof val === 'bigint' ? JSON.rawJSON(val) : val;
30 const embedded = JSON.stringify({ tooBigForNumber }, bigIntToRawJSON);
31 assert.sameValue(embedded, '{"tooBigForNumber":9007199254740993}');
32
33 reportCompare(0, 0);