tor-browser

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

reviver-context-source-primitive-literal.js (2890B)


      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 a
      8  NumericLiteral, NullLiteral, BoolLiteral, or StringLiteral 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.sameValue(1, JSON.parse('1', reviverWithExpectedSources(['1'])));
     42 assert.sameValue(1.1, JSON.parse('1.1', reviverWithExpectedSources(['1.1'])));
     43 assert.sameValue(-1, JSON.parse('-1', reviverWithExpectedSources(['-1'])));
     44 assert.sameValue(
     45  -1.1,
     46  JSON.parse('-1.1', reviverWithExpectedSources(['-1.1']))
     47 );
     48 assert.sameValue(
     49  11,
     50  JSON.parse('1.1e1', reviverWithExpectedSources(['1.1e1']))
     51 );
     52 assert.sameValue(
     53  11,
     54  JSON.parse('1.1e+1', reviverWithExpectedSources(['1.1e+1']))
     55 );
     56 assert.sameValue(
     57  0.11,
     58  JSON.parse('1.1e-1', reviverWithExpectedSources(['1.1e-1']))
     59 );
     60 assert.sameValue(
     61  11,
     62  JSON.parse('1.1E1', reviverWithExpectedSources(['1.1E1']))
     63 );
     64 assert.sameValue(
     65  11,
     66  JSON.parse('1.1E+1', reviverWithExpectedSources(['1.1E+1']))
     67 );
     68 assert.sameValue(
     69  0.11,
     70  JSON.parse('1.1E-1', reviverWithExpectedSources(['1.1E-1']))
     71 );
     72 
     73 // Test NullLiteral, BoolLiteral, StringLiteral
     74 assert.sameValue(
     75  JSON.parse('null', reviverWithExpectedSources(['null'])),
     76  null
     77 );
     78 assert.sameValue(
     79  JSON.parse('true', reviverWithExpectedSources(['true'])),
     80  true
     81 );
     82 assert.sameValue(
     83  JSON.parse('false', reviverWithExpectedSources(['false'])),
     84  false
     85 );
     86 assert.sameValue(
     87  JSON.parse('"foo"', reviverWithExpectedSources(['"foo"'])),
     88  "foo"
     89 );
     90 
     91 reportCompare(0, 0);