| 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: Codepaths involving InternaliseJSONProperty behave as expected |
| 8 |
|
| 9 |
includes: [compareArray.js] |
| 10 |
features: [json-parse-with-source] |
| 11 |
---*/ |
| 12 |
|
| 13 |
function assertOnlyOwnProperties(object, props, message) { |
| 14 |
assert.compareArray(Object.getOwnPropertyNames(object), props, `${message}: object should have no other properties than expected`); |
| 15 |
assert.compareArray(Object.getOwnPropertySymbols(object), [], `${message}: object should have no own symbol properties`); |
| 16 |
} |
| 17 |
|
| 18 |
const replacements = [ |
| 19 |
42, |
| 20 |
["foo"], |
| 21 |
{ foo: "bar" }, |
| 22 |
"foo" |
| 23 |
]; |
| 24 |
|
| 25 |
// Test Array forward modify |
| 26 |
for (const replacement of replacements) { |
| 27 |
let alreadyReplaced = false; |
| 28 |
let expectedKeys = ["0", "1", ""]; |
| 29 |
// if the replacement is an object, add its keys to the expected keys |
| 30 |
if (typeof replacement === "object") { |
| 31 |
expectedKeys.splice(1, 0, ...Object.keys(replacement)); |
| 32 |
} |
| 33 |
const o = JSON.parse("[1, 2]", function (k, v, { source }) { |
| 34 |
assert.sameValue(k, expectedKeys.shift()); |
| 35 |
if (k === "0") { |
| 36 |
if (!alreadyReplaced) { |
| 37 |
this[1] = replacement; |
| 38 |
alreadyReplaced = true; |
| 39 |
} |
| 40 |
} else if (k !== "") { |
| 41 |
assert.sameValue(source, undefined); |
| 42 |
} |
| 43 |
return this[k]; |
| 44 |
}); |
| 45 |
assert.sameValue(expectedKeys.length, 0); |
| 46 |
assert.compareArray(o, [1, replacement], `array forward-modified with ${replacement}`); |
| 47 |
} |
| 48 |
|
| 49 |
function assertOnlyOwnProperties(object, props, message) { |
| 50 |
assert.compareArray(Object.getOwnPropertyNames(object), props, `${message}: object should have no other properties than expected`); |
| 51 |
assert.compareArray(Object.getOwnPropertySymbols(object), [], `${message}: object should have no own symbol properties`); |
| 52 |
} |
| 53 |
|
| 54 |
// Test Object forward modify |
| 55 |
for (const replacement of replacements) { |
| 56 |
let alreadyReplaced = false; |
| 57 |
let expectedKeys = ["p", "q", ""]; |
| 58 |
if (typeof replacement === "object") { |
| 59 |
expectedKeys.splice(1, 0, ...Object.keys(replacement)); |
| 60 |
} |
| 61 |
const o = JSON.parse('{"p":1, "q":2}', function (k, v, { source }) { |
| 62 |
assert.sameValue(k, expectedKeys.shift()); |
| 63 |
if (k === 'p') { |
| 64 |
if (!alreadyReplaced) { |
| 65 |
this.q = replacement; |
| 66 |
alreadyReplaced = true; |
| 67 |
} |
| 68 |
} else if (k !== "") { |
| 69 |
assert.sameValue(source, undefined); |
| 70 |
} |
| 71 |
return this[k]; |
| 72 |
}); |
| 73 |
assert.sameValue(expectedKeys.length, 0); |
| 74 |
assertOnlyOwnProperties(o, ["p", "q"], `object forward-modified with ${replacement}`); |
| 75 |
assert.sameValue(o.p, 1, "property p should not be replaced"); |
| 76 |
assert.sameValue(o.q, replacement, `property q should be replaced with ${replacement}`); |
| 77 |
} |
| 78 |
|
| 79 |
// Test combinations of possible JSON input with multiple forward modifications |
| 80 |
|
| 81 |
{ |
| 82 |
let reviverCallIndex = 0; |
| 83 |
const expectedKeys = ["a", "b", "c", ""]; |
| 84 |
const reviver = function(key, value, {source}) { |
| 85 |
assert.sameValue(key, expectedKeys[reviverCallIndex++]); |
| 86 |
if (key === "a") { |
| 87 |
this.b = 2; |
| 88 |
assert.sameValue(source, "0"); |
| 89 |
} else if (key === "b") { |
| 90 |
this.c = 3; |
| 91 |
assert.sameValue(value, 2); |
| 92 |
assert.sameValue(source, undefined); |
| 93 |
} else if (key === "c") { |
| 94 |
assert.sameValue(value, 3); |
| 95 |
assert.sameValue(source, undefined); |
| 96 |
} |
| 97 |
return value; |
| 98 |
} |
| 99 |
const parsed = JSON.parse('{"a": 0, "b": 1, "c": [1, 2]}', reviver); |
| 100 |
assertOnlyOwnProperties(parsed, ["a", "b", "c"], "object with forward-modified properties"); |
| 101 |
assert.sameValue(parsed.a, 0, "'a' property should be unmodified"); |
| 102 |
assert.sameValue(parsed.b, 2, "'b' property should be modified to 2"); |
| 103 |
assert.sameValue(parsed.c, 3, "'c' property should be modified to 3"); |
| 104 |
} |
| 105 |
|
| 106 |
{ |
| 107 |
let reviverCallIndex = 0; |
| 108 |
const expectedKeys = ["0", "1", "2", "3", ""]; |
| 109 |
const reviver = function(key, value, {source}) { |
| 110 |
assert.sameValue(key, expectedKeys[reviverCallIndex++]); |
| 111 |
if (key === "0") { |
| 112 |
this[1] = 3; |
| 113 |
assert.sameValue(value, 1); |
| 114 |
assert.sameValue(source, "1"); |
| 115 |
} else if (key === "1") { |
| 116 |
this[2] = 4; |
| 117 |
assert.sameValue(value, 3); |
| 118 |
assert.sameValue(source, undefined); |
| 119 |
} else if(key === "2") { |
| 120 |
this[3] = 5; |
| 121 |
assert.sameValue(value, 4); |
| 122 |
assert.sameValue(source, undefined); |
| 123 |
} else if(key === "5") { |
| 124 |
assert.sameValue(value, 5); |
| 125 |
assert.sameValue(source, undefined); |
| 126 |
} |
| 127 |
return value; |
| 128 |
} |
| 129 |
assert.compareArray(JSON.parse('[1, 2, 3, {"a": 1}]', reviver), [1, 3, 4, 5], "array with forward-modified elements"); |
| 130 |
} |
| 131 |
|
| 132 |
reportCompare(0, 0); |