| 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 |
ObjectLiteral 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 |
assertOnlyOwnProperties( |
| 43 |
JSON.parse('{}', reviverWithExpectedSources([])), |
| 44 |
[], |
| 45 |
"empty object" |
| 46 |
); |
| 47 |
|
| 48 |
const singleProp = JSON.parse('{"42":37}', reviverWithExpectedSources(['37'])); |
| 49 |
assertOnlyOwnProperties(singleProp, ["42"], "single numeric property key"); |
| 50 |
assert.sameValue(singleProp[42], 37, "value of single numeric property key"); |
| 51 |
|
| 52 |
const multipleProps = JSON.parse('{"x": 1, "y": 2}', reviverWithExpectedSources(['1', '2'])); |
| 53 |
assertOnlyOwnProperties(multipleProps, ["x", "y"], "multiple properties"); |
| 54 |
assert.sameValue(multipleProps.x, 1, "multiple properties, value of x"); |
| 55 |
assert.sameValue(multipleProps.y, 2, "multiple properties, value of y"); |
| 56 |
|
| 57 |
// undefined means the json value is JSObject or JSArray and the passed |
| 58 |
// context to the reviver function has no source property. |
| 59 |
const arrayProps = JSON.parse( |
| 60 |
'{"x": [1,2], "y": [2,3]}', |
| 61 |
reviverWithExpectedSources(['1', '2', undefined, '2', '3', undefined]) |
| 62 |
); |
| 63 |
assertOnlyOwnProperties(arrayProps, ["x", "y"], "array-valued properties"); |
| 64 |
assert.compareArray(arrayProps.x, [1, 2], "array-valued properties, value of x"); |
| 65 |
assert.compareArray(arrayProps.y, [2, 3], "array-valued properties, value of y"); |
| 66 |
|
| 67 |
const objectProps = JSON.parse( |
| 68 |
'{"x": {"x": 1, "y": 2}}', |
| 69 |
reviverWithExpectedSources(['1', '2', undefined, undefined]) |
| 70 |
); |
| 71 |
assertOnlyOwnProperties(objectProps, ["x"], "object-valued properties"); |
| 72 |
assertOnlyOwnProperties(objectProps.x, ["x", "y"], "object-valued properties, value of x"); |
| 73 |
assert.sameValue(objectProps.x.x, 1, "object-valued properties, value of x.x"); |
| 74 |
assert.sameValue(objectProps.x.y, 2, "object-valued properties, value of x.y"); |
| 75 |
|
| 76 |
reportCompare(0, 0); |