| 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.parse |
| 7 |
description: > |
| 8 |
Context argument and its source property behave as expected when parsing an |
| 9 |
ArrayLiteral JSON string |
| 10 |
includes: [compareArray.js, propertyHelper.js] |
| 11 |
features: [json-parse-with-source] |
| 12 |
---*/ |
| 13 |
|
| 14 |
function assertOnlyOwnProperties(object, props, message) { |
| 15 |
assert.compareArray(Object.getOwnPropertyNames(object), props, `${message}: object should have no other properties than expected`); |
| 16 |
assert.compareArray(Object.getOwnPropertySymbols(object), [], `${message}: object should have no own symbol properties`); |
| 17 |
} |
| 18 |
|
| 19 |
function reviverWithExpectedSources(expectedSources) { |
| 20 |
let i = 0; |
| 21 |
return function reviver(key, value, context) { |
| 22 |
assert.sameValue(typeof context, "object", "context should be an object"); |
| 23 |
assert.sameValue(Object.getPrototypeOf(context), Object.prototype, "context should be a plain object"); |
| 24 |
if (expectedSources[i] !== undefined) { |
| 25 |
assertOnlyOwnProperties(context, ["source"], |
| 26 |
"the JSON value is a primitve value, its context should only have a source property"); |
| 27 |
verifyProperty(context, "source", { |
| 28 |
value: expectedSources[i++], |
| 29 |
configurable: true, |
| 30 |
enumerable: true, |
| 31 |
writable: true, |
| 32 |
}, { restore: true }); |
| 33 |
} else { |
| 34 |
assertOnlyOwnProperties(context, [], |
| 35 |
"the JSON value is an Array or Object, its context should have no property"); |
| 36 |
i++; |
| 37 |
} |
| 38 |
return value; |
| 39 |
}; |
| 40 |
} |
| 41 |
|
| 42 |
assert.compareArray(JSON.parse('[1.0]', reviverWithExpectedSources(['1.0'])), [1]); |
| 43 |
assert.compareArray( |
| 44 |
JSON.parse('[1.1]', reviverWithExpectedSources(['1.1'])), |
| 45 |
[1.1] |
| 46 |
); |
| 47 |
assert.compareArray(JSON.parse('[]', reviverWithExpectedSources([])), []); |
| 48 |
|
| 49 |
const longArray = JSON.parse( |
| 50 |
'[1, "2", true, null, {"x": 1, "y": 1}]', |
| 51 |
reviverWithExpectedSources(['1', '"2"', 'true', 'null', '1', '1']) |
| 52 |
); |
| 53 |
assert.sameValue(longArray[0], 1, "array, element 0"); |
| 54 |
assert.sameValue(longArray[1], "2", "array, element 1"); |
| 55 |
assert.sameValue(longArray[2], true, "array, element 2"); |
| 56 |
assert.sameValue(longArray[3], null, "array, element 3"); |
| 57 |
assertOnlyOwnProperties(longArray[4], ["x", "y"], "array, element 5"); |
| 58 |
assert.sameValue(longArray[4].x, 1, "array, element 5, prop x"); |
| 59 |
assert.sameValue(longArray[4].y, 1, "array, element 5, prop y"); |
| 60 |
|
| 61 |
reportCompare(0, 0); |