tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

reviver-context-source-array-literal.js (2409B)


      1 // Copyright (C) 2023 the V8 project authors. All rights reserved.
      2 // This code is governed by the BSD license found in the LICENSE file.
      3 
      4 /*---
      5 esid: sec-json.parse
      6 description: >
      7  Context argument and its source property behave as expected when parsing an
      8  ArrayLiteral JSON string
      9 includes: [compareArray.js, propertyHelper.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 function reviverWithExpectedSources(expectedSources) {
     19  let i = 0;
     20  return function reviver(key, value, context) {
     21    assert.sameValue(typeof context, "object", "context should be an object");
     22    assert.sameValue(Object.getPrototypeOf(context), Object.prototype, "context should be a plain object");
     23    if (expectedSources[i] !== undefined) {
     24      assertOnlyOwnProperties(context, ["source"],
     25        "the JSON value is a primitve value, its context should only have a source property");
     26      verifyProperty(context, "source", {
     27        value: expectedSources[i++],
     28        configurable: true,
     29        enumerable: true,
     30        writable: true,
     31      }, { restore: true });
     32    } else {
     33      assertOnlyOwnProperties(context, [],
     34        "the JSON value is an Array or Object, its context should have no property");
     35      i++;
     36    }
     37    return value;
     38  };
     39 }
     40 
     41 assert.compareArray(JSON.parse('[1.0]', reviverWithExpectedSources(['1.0'])), [1]);
     42 assert.compareArray(
     43  JSON.parse('[1.1]', reviverWithExpectedSources(['1.1'])),
     44  [1.1]
     45 );
     46 assert.compareArray(JSON.parse('[]', reviverWithExpectedSources([])), []);
     47 
     48 const longArray = JSON.parse(
     49  '[1, "2", true, null, {"x": 1, "y": 1}]',
     50  reviverWithExpectedSources(['1', '"2"', 'true', 'null', '1', '1'])
     51 );
     52 assert.sameValue(longArray[0], 1, "array, element 0");
     53 assert.sameValue(longArray[1], "2", "array, element 1");
     54 assert.sameValue(longArray[2], true, "array, element 2");
     55 assert.sameValue(longArray[3], null, "array, element 3");
     56 assertOnlyOwnProperties(longArray[4], ["x", "y"], "array, element 5");
     57 assert.sameValue(longArray[4].x, 1, "array, element 5, prop x");
     58 assert.sameValue(longArray[4].y, 1, "array, element 5, prop y");
     59 
     60 reportCompare(0, 0);