diff options
Diffstat (limited to 'js/src/tests/test262/built-ins/JSON/stringify/value-number-object.js')
-rw-r--r-- | js/src/tests/test262/built-ins/JSON/stringify/value-number-object.js | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-number-object.js b/js/src/tests/test262/built-ins/JSON/stringify/value-number-object.js new file mode 100644 index 0000000000..d153c63c57 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-number-object.js @@ -0,0 +1,54 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Number objects are converted to primitives using ToNumber. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 4. If Type(value) is Object, then + a. If value has a [[NumberData]] internal slot, then + i. Set value to ? ToNumber(value). + [...] + 9. If Type(value) is Number, then + a. If value is finite, return ! ToString(value). +---*/ + +assert.sameValue(JSON.stringify(new Number(8.5)), '8.5'); + +var toPrimitiveReplacer = function(_key, value) { + if (value === 'str') { + var num = new Number(42); + num.toString = function() { throw new Test262Error('should not be called'); }; + num.valueOf = function() { return 2; }; + return num; + } + + return value; +}; + +assert.sameValue(JSON.stringify(['str'], toPrimitiveReplacer), '[2]'); + +var abruptToJSON = function() { + var num = new Number(3.14); + num.toString = function() { throw new Test262Error(); }; + num.valueOf = function() { throw new Test262Error(); }; + return num; +}; + +assert.throws(Test262Error, function() { + JSON.stringify({ + key: { + toJSON: abruptToJSON, + }, + }); +}); + +reportCompare(0, 0); |