summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/harmony/json-parse-with-source-snapshot.js
blob: 8f5565bd629d37fe09a66719e17bee74bfb7efc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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);
})();