mirror of
https://github.com/nodejs/node.git
synced 2025-05-21 12:25:12 +00:00

PR-URL: https://github.com/nodejs/node/pull/47251 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Jiawen Geng <technicalcute@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Richard Lau <rlau@redhat.com>
93 lines
2.5 KiB
JavaScript
93 lines
2.5 KiB
JavaScript
// Copyright 2023 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// Flags: --harmony-json-parse-with-source
|
|
|
|
const replacements = [42,
|
|
['foo'],
|
|
{foo:'bar'},
|
|
'foo'];
|
|
|
|
function TestArrayForwardModify(replacement) {
|
|
let alreadyReplaced = false;
|
|
let expectedKeys = ['0','1',''];
|
|
// lol who designed reviver semantics
|
|
if (typeof replacement === 'object') {
|
|
expectedKeys.splice(1, 0, ...Object.keys(replacement));
|
|
}
|
|
const o = JSON.parse('[1, 2]', function (k, v, { source }) {
|
|
assertEquals(expectedKeys.shift(), k);
|
|
if (k === '0') {
|
|
if (!alreadyReplaced) {
|
|
this[1] = replacement;
|
|
alreadyReplaced = true;
|
|
}
|
|
} else if (k !== '') {
|
|
assertSame(undefined, source);
|
|
}
|
|
return this[k];
|
|
});
|
|
assertEquals(0, expectedKeys.length);
|
|
assertEquals([1, replacement], o);
|
|
}
|
|
|
|
function TestObjectForwardModify(replacement) {
|
|
let alreadyReplaced = false;
|
|
let expectedKeys = ['p','q',''];
|
|
if (typeof replacement === 'object') {
|
|
expectedKeys.splice(1, 0, ...Object.keys(replacement));
|
|
}
|
|
const o = JSON.parse('{"p":1, "q":2}', function (k, v, { source }) {
|
|
assertEquals(expectedKeys.shift(), k);
|
|
if (k === 'p') {
|
|
if (!alreadyReplaced) {
|
|
this.q = replacement;
|
|
alreadyReplaced = true;
|
|
}
|
|
} else if (k !== '') {
|
|
assertSame(undefined, source);
|
|
}
|
|
return this[k];
|
|
});
|
|
assertEquals(0, expectedKeys.length);
|
|
assertEquals({p:1, q:replacement}, o);
|
|
}
|
|
|
|
for (const r of replacements) {
|
|
TestArrayForwardModify(r);
|
|
TestObjectForwardModify(r);
|
|
}
|
|
|
|
(function TestArrayAppend() {
|
|
let log = [];
|
|
const o = JSON.parse('[1,[]]', function (k, v, { source }) {
|
|
log.push([k, v, source]);
|
|
if (v === 1) {
|
|
this[1].push('barf');
|
|
}
|
|
return this[k];
|
|
});
|
|
assertEquals([['0', 1, '1'],
|
|
['0', 'barf', undefined],
|
|
['1', ['barf'], undefined],
|
|
['', [1, ['barf']], undefined]],
|
|
log);
|
|
})();
|
|
|
|
(function TestObjectAddProperty() {
|
|
let log = [];
|
|
const o = JSON.parse('{"p":1,"q":{}}', function (k, v, { source }) {
|
|
log.push([k, v, source]);
|
|
if (v === 1) {
|
|
this.q.added = 'barf';
|
|
}
|
|
return this[k];
|
|
});
|
|
assertEquals([['p', 1, '1'],
|
|
['added', 'barf', undefined],
|
|
['q', {added:'barf'}, undefined],
|
|
['', {p:1, q:{added:'barf'}}, undefined]],
|
|
log);
|
|
})();
|