tor-browser

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

reviver-context-source-object-literal.js (3199B)


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