tor-browser

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

value-number-object.js (1411B)


      1 // Copyright (C) 2012 Ecma International. All rights reserved.
      2 // This code is governed by the BSD license found in the LICENSE file.
      3 /*---
      4 esid: sec-serializejsonproperty
      5 description: >
      6  Number objects are converted to primitives using ToNumber.
      7 info: |
      8  JSON.stringify ( value [ , replacer [ , space ] ] )
      9 
     10  [...]
     11  12. Return ? SerializeJSONProperty(the empty String, wrapper).
     12 
     13  SerializeJSONProperty ( key, holder )
     14 
     15  [...]
     16  4. If Type(value) is Object, then
     17    a. If value has a [[NumberData]] internal slot, then
     18      i. Set value to ? ToNumber(value).
     19  [...]
     20  9. If Type(value) is Number, then
     21    a. If value is finite, return ! ToString(value).
     22 ---*/
     23 
     24 assert.sameValue(JSON.stringify(new Number(8.5)), '8.5');
     25 
     26 var toPrimitiveReplacer = function(_key, value) {
     27  if (value === 'str') {
     28    var num = new Number(42);
     29    num.toString = function() { throw new Test262Error('should not be called'); };
     30    num.valueOf = function() { return 2; };
     31    return num;
     32  }
     33 
     34  return value;
     35 };
     36 
     37 assert.sameValue(JSON.stringify(['str'], toPrimitiveReplacer), '[2]');
     38 
     39 var abruptToJSON = function() {
     40  var num = new Number(3.14);
     41  num.toString = function() { throw new Test262Error(); };
     42  num.valueOf = function() { throw new Test262Error(); };
     43  return num;
     44 };
     45 
     46 assert.throws(Test262Error, function() {
     47  JSON.stringify({
     48    key: {
     49      toJSON: abruptToJSON,
     50    },
     51  });
     52 });
     53 
     54 reportCompare(0, 0);