diff options
Diffstat (limited to 'js/src/tests/test262/built-ins/JSON/stringify')
68 files changed, 2584 insertions, 0 deletions
diff --git a/js/src/tests/test262/built-ins/JSON/stringify/browser.js b/js/src/tests/test262/built-ins/JSON/stringify/browser.js new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/browser.js diff --git a/js/src/tests/test262/built-ins/JSON/stringify/builtin.js b/js/src/tests/test262/built-ins/JSON/stringify/builtin.js new file mode 100644 index 0000000000..f125bcbeb5 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/builtin.js @@ -0,0 +1,29 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Tests that JSON.stringify meets the requirements + for built-in objects defined by the introduction of chapter 17 of + the ECMAScript Language Specification. +features: [Reflect.construct] +---*/ + +assert(Object.isExtensible(JSON.stringify), 'Object.isExtensible(JSON.stringify) must return true'); +assert.sameValue( + Object.prototype.toString.call(JSON.stringify), + '[object Function]', + 'Object.prototype.toString.call(JSON.stringify) must return "[object Function]"' +); +assert.sameValue( + Object.getPrototypeOf(JSON.stringify), + Function.prototype, + 'Object.getPrototypeOf(JSON.stringify) must return the value of Function.prototype' +); +assert.sameValue( + JSON.stringify.hasOwnProperty('prototype'), + false, + 'JSON.stringify.hasOwnProperty("prototype") must return false' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/length.js b/js/src/tests/test262/built-ins/JSON/stringify/length.js new file mode 100644 index 0000000000..ea819ab04d --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/length.js @@ -0,0 +1,27 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + JSON.stringify.length is 3. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + The "length" property of the stringify function is 3. + + ECMAScript Standard Built-in Objects + + Unless otherwise specified, the length property of a built-in Function + object has the attributes { [[Writable]]: false, [[Enumerable]]: false, + [[Configurable]]: true }. +includes: [propertyHelper.js] +---*/ + +verifyProperty(JSON.stringify, 'length', { + value: 3, + writable: false, + enumerable: false, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/name.js b/js/src/tests/test262/built-ins/JSON/stringify/name.js new file mode 100644 index 0000000000..d08ad4bf9d --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/name.js @@ -0,0 +1,29 @@ +// Copyright (C) 2015 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-json.stringify +description: > + JSON.stringify.name is "stringify". +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + 17 ECMAScript Standard Built-in Objects: + Every built-in Function object, including constructors, that is not + identified as an anonymous function has a name property whose value + is a String. + + Unless otherwise specified, the name property of a built-in Function + object, if it exists, has the attributes { [[Writable]]: false, + [[Enumerable]]: false, [[Configurable]]: true }. +includes: [propertyHelper.js] +---*/ + +verifyProperty(JSON.stringify, 'name', { + value: 'stringify', + writable: false, + enumerable: false, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/not-a-constructor.js b/js/src/tests/test262/built-ins/JSON/stringify/not-a-constructor.js new file mode 100644 index 0000000000..2458cdaf4a --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/not-a-constructor.js @@ -0,0 +1,31 @@ +// Copyright (C) 2020 Rick Waldron. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-ecmascript-standard-built-in-objects +description: > + JSON.stringify does not implement [[Construct]], is not new-able +info: | + ECMAScript Function Objects + + Built-in function objects that are not identified as constructors do not + implement the [[Construct]] internal method unless otherwise specified in + the description of a particular function. + + sec-evaluatenew + + ... + 7. If IsConstructor(constructor) is false, throw a TypeError exception. + ... +includes: [isConstructor.js] +features: [Reflect.construct, arrow-function] +---*/ + +assert.sameValue(isConstructor(JSON.stringify), false, 'isConstructor(JSON.stringify) must return false'); + +assert.throws(TypeError, () => { + new JSON.stringify({}); +}, '`new JSON.stringify({})` throws TypeError'); + + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/prop-desc.js b/js/src/tests/test262/built-ins/JSON/stringify/prop-desc.js new file mode 100644 index 0000000000..d61ee040a7 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/prop-desc.js @@ -0,0 +1,22 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Property descriptor of JSON.stringify. +info: | + ECMAScript Standard Built-in Objects + + Every other data property described in clauses 18 through 26 and in Annex B.2 + has the attributes { [[Writable]]: true, [[Enumerable]]: false, + [[Configurable]]: true } unless otherwise specified. +includes: [propertyHelper.js] +---*/ + +verifyProperty(JSON, 'stringify', { + writable: true, + enumerable: false, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/property-order.js b/js/src/tests/test262/built-ins/JSON/stringify/property-order.js new file mode 100644 index 0000000000..00bfa481ee --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/property-order.js @@ -0,0 +1,41 @@ +// Copyright 2019 Kevin Gibbons. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-serializejsonobject +description: JSON.stringify property enumeration order +features: [for-in-order] +---*/ + +var o = { + p1: 'p1', + p2: 'p2', + p3: 'p3', +}; + +// This getter will be triggered during enumeration, but the property it adds should not be enumerated. +Object.defineProperty(o, 'add', { + enumerable: true, + get: function () { + o.extra = 'extra'; + return 'add'; + } +}); + +o.p4 = 'p4'; + +o[2] = '2'; +o[0] = '0'; +o[1] = '1'; + +delete o.p1; +delete o.p3; +o.p1 = 'p1'; + +var actual = JSON.stringify(o); + +var expected = '{"0":"0","1":"1","2":"2","p2":"p2","add":"add","p4":"p4","p1":"p1"}'; + +assert.sameValue(actual, expected); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-abrupt.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-abrupt.js new file mode 100644 index 0000000000..56cac0ef49 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-abrupt.js @@ -0,0 +1,61 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Abrupt completion from Get. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + [...] + 2. Let len be ? LengthOfArrayLike(replacer). + 3. Let k be 0. + 4. Repeat, while k < len, + a. Let v be ? Get(replacer, ! ToString(k)). +features: [Proxy] +---*/ + +var abruptLength = new Proxy([], { + get: function(_target, key) { + if (key === 'length') { + throw new Test262Error(); + } + }, +}); + +assert.throws(Test262Error, function() { + JSON.stringify(null, abruptLength); +}); + +var abruptToPrimitive = { + valueOf: function() { + throw new Test262Error(); + }, +}; + +var abruptToLength = new Proxy([], { + get: function(_target, key) { + if (key === 'length') { + return abruptToPrimitive; + } + }, +}); + +assert.throws(Test262Error, function() { + JSON.stringify([], abruptToLength); +}); + +var abruptIndex = new Array(1); +Object.defineProperty(abruptIndex, '0', { + get: function() { + throw new Test262Error(); + }, +}); + +assert.throws(Test262Error, function() { + JSON.stringify({}, abruptIndex); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-duplicates.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-duplicates.js new file mode 100644 index 0000000000..5351d6fb56 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-duplicates.js @@ -0,0 +1,31 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Replacer array is deduped before Get. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + [...] + 4. Repeat, while k < len, + a. Let v be ? Get(replacer, ! ToString(k)). + [...] + f. If item is not undefined and item is not currently an element of PropertyList, then + i. Append item to the end of PropertyList. +---*/ + +var getCalls = 0; +var value = { + get key() { + getCalls += 1; + return true; + }, +}; + +assert.sameValue(JSON.stringify(value, ['key', 'key']), '{"key":true}'); +assert.sameValue(getCalls, 1); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-empty.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-empty.js new file mode 100644 index 0000000000..7992ce8a25 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-empty.js @@ -0,0 +1,42 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonobject +description: > + Objects are serialized to {} if replacer array is empty. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + [...] + c. Return ? SerializeJSONObject(value). + + SerializeJSONObject ( value ) + + [...] + 5. If PropertyList is not undefined, then + a. Let K be PropertyList. +---*/ + +assert.sameValue( + JSON.stringify({a: 1, b: 2}, []), + '{}' +); + +assert.sameValue( + JSON.stringify({a: 1, b: {c: 2}}, []), + '{}' +); + +assert.sameValue( + JSON.stringify([1, {a: 2}], []), + '[1,{}]' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-number-object.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-number-object.js new file mode 100644 index 0000000000..51886a4bba --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-number-object.js @@ -0,0 +1,33 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Converts Number objects from replacer array to primitives using ToString. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + [...] + 4. Repeat, while k < len, + a. Let v be ? Get(replacer, ! ToString(k)). + [...] + e. Else if Type(v) is Object, then + i. If v has a [[StringData]] or [[NumberData]] internal slot, + set item to ? ToString(v). +---*/ + +var num = new Number(10); +num.toString = function() { return 'toString'; }; +num.valueOf = function() { throw new Test262Error('should not be called'); }; + +var value = { + 10: 1, + toString: 2, + valueOf: 3, +}; + +assert.sameValue(JSON.stringify(value, [num]), '{"toString":2}'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-number.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-number.js new file mode 100644 index 0000000000..3f4744e85a --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-number.js @@ -0,0 +1,42 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Converts number primitives from replacer array to strings. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + [...] + 4. Repeat, while k < len, + a. Let v be ? Get(replacer, ! ToString(k)). + [...] + d. Else if Type(v) is Number, set item to ! ToString(v). +---*/ + +var obj = { + '0': 0, + '1': 1, + '-4': 2, + '0.3': 3, + '-Infinity': 4, + 'NaN': 5, +}; + +var replacer = [ + -0, + 1, + -4, + 0.3, + -Infinity, + NaN, +]; + +assert.sameValue( + JSON.stringify(obj, replacer), + JSON.stringify(obj) +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-order.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-order.js new file mode 100644 index 0000000000..7b424a1eda --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-order.js @@ -0,0 +1,39 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonobject +description: > + Keys order of serialized objects is determined by replacer array. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + [...] + c. Return ? SerializeJSONObject(value). + + SerializeJSONObject ( value ) + + [...] + 5. If PropertyList is not undefined, then + a. Let K be PropertyList. +---*/ + +var replacer = ['c', 'b', 'a']; + +assert.sameValue( + JSON.stringify({b: 1, a: 2, c: 3}, replacer), + '{"c":3,"b":1,"a":2}' +); + +assert.sameValue( + JSON.stringify({a: {b: 2, c: 3}}, replacer), + '{"a":{"c":3,"b":2}}' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-proxy-revoked-realm.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-proxy-revoked-realm.js new file mode 100644 index 0000000000..82a53a0a11 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-proxy-revoked-realm.js @@ -0,0 +1,37 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Revoked proxy of array as replacer produces a TypeError + (honoring the realm of the current execution context). +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + a. If IsCallable(replacer) is true, then + i. Let ReplacerFunction be replacer. + b. Else, + i. Let isArray be ? IsArray(replacer). + + IsArray ( argument ) + + [...] + 3. If argument is a Proxy exotic object, then + a. If argument.[[ProxyHandler]] is null, throw a TypeError exception. + b. Let target be argument.[[ProxyTarget]]. + c. Return ? IsArray(target). +features: [cross-realm, Proxy] +---*/ + +var OProxy = $262.createRealm().global.Proxy; +var handle = OProxy.revocable([], {}); + +handle.revoke(); + +assert.throws(TypeError, function() { + JSON.stringify({}, handle.proxy); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-proxy-revoked.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-proxy-revoked.js new file mode 100644 index 0000000000..77cf5ef5bf --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-proxy-revoked.js @@ -0,0 +1,34 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: Revoked proxy value produces a TypeError. +info: | + [...] + 4. If Type(replacer) is Object, then + a. If IsCallable(replacer) is true, then + i. Let ReplacerFunction be replacer. + b. Else, + i. Let isArray be ? IsArray(replacer). + + 7.2.2 IsArray + + [...] + 3. If argument is a Proxy exotic object, then + a. If the value of the [[ProxyHandler]] internal slot of argument is null, + throw a TypeError exception. + b. Let target be the value of the [[ProxyTarget]] internal slot of + argument. + c. Return ? IsArray(target). +features: [Proxy] +---*/ + +var handle = Proxy.revocable([], {}); + +handle.revoke(); + +assert.throws(TypeError, function() { + JSON.stringify({}, handle.proxy); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-proxy.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-proxy.js new file mode 100644 index 0000000000..02fe6deb93 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-proxy.js @@ -0,0 +1,32 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Array proxy replacer serves as a filter of object keys. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + a. If IsCallable(replacer) is true, then + i. Let ReplacerFunction be replacer. + b. Else, + i. Let isArray be ? IsArray(replacer). + + IsArray ( argument ) + + [...] + 3. If argument is a Proxy exotic object, then + a. If argument.[[ProxyHandler]] is null, throw a TypeError exception. + b. Let target be argument.[[ProxyTarget]]. + c. Return ? IsArray(target). +features: [Proxy] +---*/ + +var replacer = new Proxy(['b'], {}); + +assert.sameValue(JSON.stringify({a: 1, b: 2}, replacer), '{"b":2}'); +assert.sameValue(JSON.stringify({b: {a: 3, b: 4}}, replacer), '{"b":{"b":4}}'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-string-object.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-string-object.js new file mode 100644 index 0000000000..c1380049e2 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-string-object.js @@ -0,0 +1,33 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Converts String objects from replacer array to primitives using ToString. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + [...] + 4. Repeat, while k < len, + a. Let v be ? Get(replacer, ! ToString(k)). + [...] + e. Else if Type(v) is Object, then + i. If v has a [[StringData]] or [[NumberData]] internal slot, + set item to ? ToString(v). +---*/ + +var str = new String('str'); +str.toString = function() { return 'toString'; }; +str.valueOf = function() { throw new Test262Error('should not be called'); }; + +var value = { + str: 1, + toString: 2, + valueOf: 3, +}; + +assert.sameValue(JSON.stringify(value, [str]), '{"toString":2}'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-undefined.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-undefined.js new file mode 100644 index 0000000000..1f2a176f34 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-undefined.js @@ -0,0 +1,31 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Undefined values in replacer array are ignored. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + [...] + 4. Repeat, while k < len, + a. Let v be ? Get(replacer, ! ToString(k)). + [...] + f. If item is not undefined and item is not currently an element of PropertyList, then + i. Append item to the end of PropertyList. +---*/ + +assert.sameValue(JSON.stringify({undefined: 1}, [undefined]), '{}'); +assert.sameValue(JSON.stringify({key: 1, undefined: 2}, [,,,]), '{}'); + +var sparse = new Array(3); +sparse[1] = 'key'; + +assert.sameValue( + JSON.stringify({undefined: 1, key: 2}, sparse), + '{"key":2}' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-wrong-type.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-wrong-type.js new file mode 100644 index 0000000000..2e69bf94a8 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-array-wrong-type.js @@ -0,0 +1,39 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Values that are neither strings nor numbers are ignored. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + [...] + 4. Repeat, while k < len, + a. Let v be ? Get(replacer, ! ToString(k)). + [...] + f. If item is not undefined and item is not currently an element of PropertyList, then + i. Append item to the end of PropertyList. +features: [Proxy, Symbol] +---*/ + +var obj = new Proxy({}, { + get: function(_target, key) { + if (key !== 'toJSON') { + throw new Test262Error(); + } + }, +}); + +var replacer = [ + true, + false, + null, + {toString: function() { return 'toString'; }}, + Symbol(), +]; + +assert.sameValue(JSON.stringify(obj, replacer), '{}'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-abrupt.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-abrupt.js new file mode 100644 index 0000000000..434622fd70 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-abrupt.js @@ -0,0 +1,30 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Abrupt completion from Call. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + a. If IsCallable(replacer) is true, then + i. Let ReplacerFunction be replacer. + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 3. If ReplacerFunction is not undefined, then + a. Set value to ? Call(ReplacerFunction, holder, « key, value »). +---*/ + +assert.throws(Test262Error, function() { + JSON.stringify({}, function() { + throw new Test262Error(); + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-arguments.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-arguments.js new file mode 100644 index 0000000000..b072532e9f --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-arguments.js @@ -0,0 +1,55 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Replacer function is called with correct context and arguments. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + 1. Let value be ? Get(holder, key). + [...] + 3. If ReplacerFunction is not undefined, then + a. Set value to ? Call(ReplacerFunction, holder, « key, value »). +includes: [compareArray.js] +---*/ + +var calls = []; +var replacer = function(key, value) { + if (key !== '') { + calls.push([this, key, value]); + } + + return value; +}; + +var b1 = [1, 2]; +var b2 = {c1: true, c2: false}; +var a1 = { + b1: b1, + b2: { + toJSON: function() { return b2; }, + }, +}; +var obj = {a1: a1, a2: 'a2'}; + +assert.sameValue( + JSON.stringify(obj, replacer), + JSON.stringify(obj) +); + +assert.compareArray(calls[0], [obj, 'a1', a1]); +assert.compareArray(calls[1], [a1, 'b1', b1]); +assert.compareArray(calls[2], [b1, '0', 1]); +assert.compareArray(calls[3], [b1, '1', 2]); +assert.compareArray(calls[4], [a1, 'b2', b2]); +assert.compareArray(calls[5], [b2, 'c1', true]); +assert.compareArray(calls[6], [b2, 'c2', false]); +assert.compareArray(calls[7], [obj, 'a2', 'a2']); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-array-circular.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-array-circular.js new file mode 100644 index 0000000000..3e08de5694 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-array-circular.js @@ -0,0 +1,37 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonarray +description: > + Circular array value (returned from replacer function) throws a TypeError. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 3. If ReplacerFunction is not undefined, then + a. Set value to ? Call(ReplacerFunction, holder, « key, value »). + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + a. Let isArray be ? IsArray(value). + b. If isArray is true, return ? SerializeJSONArray(value). + + SerializeJSONArray ( value ) + + 1. If stack contains value, throw a TypeError exception because the structure is cyclical. +---*/ + +var circular = [{}]; +var circularReplacer = function(k, v) { + return circular; +}; + +assert.throws(TypeError, function() { + JSON.stringify(circular, circularReplacer); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-object-circular.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-object-circular.js new file mode 100644 index 0000000000..15ab0fbb00 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-object-circular.js @@ -0,0 +1,50 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonobject +description: > + Circular object value (returned from replacer function) throws a TypeError. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 3. If ReplacerFunction is not undefined, then + a. Set value to ? Call(ReplacerFunction, holder, « key, value »). + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + [...] + c. Return ? SerializeJSONObject(value). + + SerializeJSONObject ( value ) + + 1. If stack contains value, throw a TypeError exception because the structure is cyclical. +---*/ + +var direct = {prop: {}}; +var directReplacer = function(k, v) { + return direct; +}; + +assert.throws(TypeError, function() { + JSON.stringify(direct, directReplacer); +}); + +var indirect = {p1: {p2: {}}}; +var indirectReplacer = function(key, value) { + if (key === 'p2') { + return indirect; + } + + return value; +}; + +assert.throws(TypeError, function() { + JSON.stringify(indirect, indirectReplacer); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-object-deleted-property.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-object-deleted-property.js new file mode 100644 index 0000000000..7b9cea5d09 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-object-deleted-property.js @@ -0,0 +1,50 @@ +// Copyright (C) 2020 Alexey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Replacer function is called on properties, deleted during stringification. +info: | + SerializeJSONObject ( value ) + + [...] + 5. If PropertyList is not undefined, then + [...] + 6. Else, + a. Let K be ? EnumerableOwnPropertyNames(value, key). + [...] + 8. For each element P of K, do + a. Let strP be ? SerializeJSONProperty(P, value). + [...] + + SerializeJSONProperty ( key, holder ) + + 1. Let value be ? Get(holder, key). + [...] + 3. If ReplacerFunction is not undefined, then + a. Set value to ? Call(ReplacerFunction, holder, « key, value »). +---*/ + +var obj = { + get a() { + delete this.b; + return 1; + }, + b: 2, +}; + +var replacer = function(key, value) { + if (key === 'b') { + assert.sameValue(value, undefined); + return '<replaced>'; + } + + return value; +}; + +assert.sameValue( + JSON.stringify(obj, replacer), + '{"a":1,"b":"<replaced>"}' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-result-undefined.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-result-undefined.js new file mode 100644 index 0000000000..cb121cf55b --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-result-undefined.js @@ -0,0 +1,36 @@ +// 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: > + Result of replacer function is stringified. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 3. If ReplacerFunction is not undefined, then + a. Set value to ? Call(ReplacerFunction, holder, « key, value »). +---*/ + +assert.sameValue(JSON.stringify(1, function() {}), undefined); +assert.sameValue(JSON.stringify([1], function() {}), undefined); +assert.sameValue(JSON.stringify({prop: 1}, function() {}), undefined); + +var replacer = function(_key, value) { + return value === 1 ? undefined : value; +}; + +assert.sameValue(JSON.stringify([1], replacer), '[null]'); +assert.sameValue(JSON.stringify({prop: 1}, replacer), '{}'); +assert.sameValue(JSON.stringify({ + a: { + b: [1], + }, +}, replacer), '{"a":{"b":[null]}}'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-result.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-result.js new file mode 100644 index 0000000000..be705fdcd1 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-result.js @@ -0,0 +1,56 @@ +// 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: > + Result of replacer function is stringified. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 3. If ReplacerFunction is not undefined, then + a. Set value to ? Call(ReplacerFunction, holder, « key, value »). +---*/ + +var obj = { + a1: { + b1: [1, 2], + b2: { + c1: true, + c2: false, + }, + }, + a2: 'a2', +}; + +var replacer = function(key, value) { + assert.sameValue(value, null); + + switch (key) { + case '': return {a1: null, a2: null}; + case 'a1': return {b1: null, b2: null}; + case 'a2': return 'a2'; + + case 'b1': return [null, null]; + case 'b2': return {c1: null, c2: null}; + + case '0': return 1; + case '1': return 2; + case 'c1': return true; + case 'c2': return false; + } + + throw new Test262Error('unreachable'); +}; + +assert.sameValue( + JSON.stringify(null, replacer), + JSON.stringify(obj) +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-tojson.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-tojson.js new file mode 100644 index 0000000000..a6f034a924 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-tojson.js @@ -0,0 +1,54 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Replacer function is called on result of toJSON method. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + a. If IsCallable(replacer) is true, then + i. Let ReplacerFunction be replacer. + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 2. If Type(value) is Object, then + a. Let toJSON be ? Get(value, "toJSON"). + b. If IsCallable(toJSON) is true, then + i. Set value to ? Call(toJSON, value, « key »). + 3. If ReplacerFunction is not undefined, then + a. Set value to ? Call(ReplacerFunction, holder, « key, value »). +---*/ + +assert.sameValue( + JSON.stringify({ + toJSON: function() { + return 'toJSON'; + }, + }, function(_key, value) { + return value + '|replacer'; + }), + '"toJSON|replacer"' +); + +assert.sameValue( + JSON.stringify({ + toJSON: function() { + return {calls: 'toJSON'}; + }, + }, function(_key, value) { + if (value && value.calls) { + value.calls += '|replacer'; + } + + return value; + }), + '{"calls":"toJSON|replacer"}' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-wrapper.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-wrapper.js new file mode 100644 index 0000000000..7b21295ae8 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-function-wrapper.js @@ -0,0 +1,40 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Wrapper is plain extensible object with single data property. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 9. Let wrapper be ObjectCreate(%ObjectPrototype%). + 10. Let status be CreateDataProperty(wrapper, the empty String, value). +includes: [propertyHelper.js] +---*/ + +Object.defineProperty(Object.prototype, '', { + set: function() { + throw new Test262Error('[[Set]] should not be called.'); + }, +}); + +var value = {}; +var wrapper; +JSON.stringify(value, function() { + wrapper = this; +}); + +assert.sameValue(typeof wrapper, 'object'); +assert.sameValue(Object.getPrototypeOf(wrapper), Object.prototype); +assert.sameValue(Object.getOwnPropertyNames(wrapper).length, 1); +assert(Object.isExtensible(wrapper)); + +verifyProperty(wrapper, '', { + value: value, + writable: true, + enumerable: true, + configurable: true, +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/replacer-wrong-type.js b/js/src/tests/test262/built-ins/JSON/stringify/replacer-wrong-type.js new file mode 100644 index 0000000000..1e12a38fd7 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/replacer-wrong-type.js @@ -0,0 +1,34 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Replacer paramter of wrong type is silently ignored. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 4. If Type(replacer) is Object, then + a. If IsCallable(replacer) is true, then + i. Set ReplacerFunction to replacer. + b. Else, + i. Let isArray be ? IsArray(replacer). + ii. If isArray is true, then + 1. Set PropertyList to a new empty List. +features: [Symbol] +---*/ + +var obj = {key: [1]}; +var json = '{"key":[1]}'; + +assert.sameValue(JSON.stringify(obj, {}), json); +assert.sameValue(JSON.stringify(obj, new String('str')), json); +assert.sameValue(JSON.stringify(obj, new Number(6.1)), json); + +assert.sameValue(JSON.stringify(obj, null), json); +assert.sameValue(JSON.stringify(obj, ''), json); +assert.sameValue(JSON.stringify(obj, 0), json); +assert.sameValue(JSON.stringify(obj, Symbol()), json); +assert.sameValue(JSON.stringify(obj, true), json); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/shell.js b/js/src/tests/test262/built-ins/JSON/stringify/shell.js new file mode 100644 index 0000000000..eda1477282 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/shell.js @@ -0,0 +1,24 @@ +// GENERATED, DO NOT EDIT +// file: isConstructor.js +// Copyright (C) 2017 André Bargull. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: | + Test if a given function is a constructor function. +defines: [isConstructor] +features: [Reflect.construct] +---*/ + +function isConstructor(f) { + if (typeof f !== "function") { + throw new Test262Error("isConstructor invoked with a non-function value"); + } + + try { + Reflect.construct(function(){}, [], f); + } catch (e) { + return false; + } + return true; +} diff --git a/js/src/tests/test262/built-ins/JSON/stringify/space-number-float.js b/js/src/tests/test262/built-ins/JSON/stringify/space-number-float.js new file mode 100644 index 0000000000..ea2da90d0a --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/space-number-float.js @@ -0,0 +1,41 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Numeric space parameter is truncated to integer part. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 6. If Type(space) is Number, then + a. Set space to min(10, ! ToInteger(space)). +---*/ + +var obj = { + a1: { + b1: [1, 2, 3, 4], + b2: { + c1: 1, + c2: 2, + }, + }, + a2: 'a2', +}; + +assert.sameValue( + JSON.stringify(obj, null, -1.99999), + JSON.stringify(obj, null, -1) +); + +assert.sameValue( + JSON.stringify(obj, null, new Number(5.11111)), + JSON.stringify(obj, null, 5) +); + +assert.sameValue( + JSON.stringify(obj, null, 6.99999), + JSON.stringify(obj, null, 6) +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/space-number-object.js b/js/src/tests/test262/built-ins/JSON/stringify/space-number-object.js new file mode 100644 index 0000000000..8bce0f4b86 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/space-number-object.js @@ -0,0 +1,49 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Number objects are converted to primitives using ToNumber. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 5. If Type(space) is Object, then + a. If space has a [[NumberData]] internal slot, then + i. Set space to ? ToNumber(space). +---*/ + +var obj = { + a1: { + b1: [1, 2, 3, 4], + b2: { + c1: 1, + c2: 2, + }, + }, + a2: 'a2', +}; + +assert.sameValue( + JSON.stringify(obj, null, new Number(1)), + JSON.stringify(obj, null, 1) +); + +var num = new Number(1); +num.toString = function() { throw new Test262Error('should not be called'); }; +num.valueOf = function() { return 3; }; + +assert.sameValue( + JSON.stringify(obj, null, num), + JSON.stringify(obj, null, 3) +); + +var abrupt = new Number(4); +abrupt.toString = function() { throw new Test262Error(); }; +abrupt.valueOf = function() { throw new Test262Error(); }; + +assert.throws(Test262Error, function() { + JSON.stringify(obj, null, abrupt); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/space-number-range.js b/js/src/tests/test262/built-ins/JSON/stringify/space-number-range.js new file mode 100644 index 0000000000..24e7fae499 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/space-number-range.js @@ -0,0 +1,38 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Numeric space parameter is clamped to 0..10 range. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 6. If Type(space) is Number, then + a. Set space to min(10, ! ToInteger(space)). + b. If space < 1, let gap be the empty String; otherwise let gap be the + String value containing space occurrences of the code unit 0x0020 (SPACE). +---*/ + +var obj = { + a1: { + b1: [1, 2, 3, 4], + b2: { + c1: 1, + c2: 2, + }, + }, + a2: 'a2', +}; + +assert.sameValue( + JSON.stringify(obj, null, new Number(-5)), + JSON.stringify(obj, null, 0) +); + +assert.sameValue( + JSON.stringify(obj, null, 10), + JSON.stringify(obj, null, 100) +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/space-number.js b/js/src/tests/test262/built-ins/JSON/stringify/space-number.js new file mode 100644 index 0000000000..ff632d4a5e --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/space-number.js @@ -0,0 +1,39 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Numeric space parameter (integer in range 0..10) is equivalent + to string of spaces of that length. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 6. If Type(space) is Number, then + [...] + b. If space < 1, let gap be the empty String; otherwise let gap be the + String value containing space occurrences of the code unit 0x0020 (SPACE). +---*/ + +var obj = { + a1: { + b1: [1, 2, 3, 4], + b2: { + c1: 1, + c2: 2, + }, + }, + a2: 'a2', +}; + +assert.sameValue( + JSON.stringify(obj, null, 0), + JSON.stringify(obj, null, '') +); + +assert.sameValue( + JSON.stringify(obj, null, 4), + JSON.stringify(obj, null, ' ') // 4 spaces +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/space-string-object.js b/js/src/tests/test262/built-ins/JSON/stringify/space-string-object.js new file mode 100644 index 0000000000..ba0d5ac31a --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/space-string-object.js @@ -0,0 +1,50 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + String exotic objects are converted to primitives using ToString. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 5. If Type(space) is Object, then + [...] + b. Else if space has a [[StringData]] internal slot, then + i. Set space to ? ToString(space). +---*/ + +var obj = { + a1: { + b1: [1, 2, 3, 4], + b2: { + c1: 1, + c2: 2, + }, + }, + a2: 'a2', +}; + +assert.sameValue( + JSON.stringify(obj, null, new String('xxx')), + JSON.stringify(obj, null, 'xxx') +); + +var str = new String('xxx'); +str.toString = function() { return '---'; }; +str.valueOf = function() { throw new Test262Error('should not be called'); }; + +assert.sameValue( + JSON.stringify(obj, null, str), + JSON.stringify(obj, null, '---') +); + +var abrupt = new String('xxx'); +abrupt.toString = function() { throw new Test262Error(); }; +abrupt.valueOf = function() { throw new Test262Error(); }; + +assert.throws(Test262Error, function() { + JSON.stringify(obj, null, abrupt); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/space-string-range.js b/js/src/tests/test262/built-ins/JSON/stringify/space-string-range.js new file mode 100644 index 0000000000..e5157ac243 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/space-string-range.js @@ -0,0 +1,32 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Only first 10 code units of string space parameter are used. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 7. Else if Type(space) is String, then + a. If the length of space is 10 or less, let gap be space; otherwise + let gap be the String value consisting of the first 10 code units of space. +---*/ + +var obj = { + a1: { + b1: [1, 2, 3, 4], + b2: { + c1: 1, + c2: 2, + }, + }, + a2: 'a2', +}; + +assert.sameValue( + JSON.stringify(obj, null, '0123456789xxxxxxxxx'), + JSON.stringify(obj, null, '0123456789') +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/space-string.js b/js/src/tests/test262/built-ins/JSON/stringify/space-string.js new file mode 100644 index 0000000000..05551ce368 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/space-string.js @@ -0,0 +1,46 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + String space is used as gap. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 7. Else if Type(space) is String, then + a. If the length of space is 10 or less, let gap be space; otherwise + let gap be the String value consisting of the first 10 code units of space. +---*/ + +var obj = { + a1: { + b1: [1, 2, 3, 4], + b2: { + c1: 1, + c2: 2, + }, + }, + a2: 'a2', +}; + +assert.sameValue(JSON.stringify(obj, null, ''), JSON.stringify(obj)); +assert.sameValue(JSON.stringify(obj, null, ' '), [ + '{' +, ' "a1": {' +, ' "b1": [' +, ' 1,' +, ' 2,' +, ' 3,' +, ' 4' +, ' ],' +, ' "b2": {' +, ' "c1": 1,' +, ' "c2": 2' +, ' }' +, ' },' +, ' "a2": "a2"' +, '}' +].join('\n')); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/space-wrong-type.js b/js/src/tests/test262/built-ins/JSON/stringify/space-wrong-type.js new file mode 100644 index 0000000000..fe4c1ff827 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/space-wrong-type.js @@ -0,0 +1,33 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Space parameter of wrong type is silently ignored. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 8. Else, + a. Let gap be the empty String. +features: [Symbol] +---*/ + +var obj = { + a1: { + b1: [1, 2, 3, 4], + b2: { + c1: 1, + c2: 2, + }, + }, + a2: 'a2', +}; + +assert.sameValue(JSON.stringify(obj), JSON.stringify(obj, null, null)); +assert.sameValue(JSON.stringify(obj), JSON.stringify(obj, null, true)); +assert.sameValue(JSON.stringify(obj), JSON.stringify(obj, null, new Boolean(false))); +assert.sameValue(JSON.stringify(obj), JSON.stringify(obj, null, Symbol())); +assert.sameValue(JSON.stringify(obj), JSON.stringify(obj, null, {})); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-array-abrupt.js b/js/src/tests/test262/built-ins/JSON/stringify/value-array-abrupt.js new file mode 100644 index 0000000000..b60fa5c8d9 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-array-abrupt.js @@ -0,0 +1,68 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonarray +description: > + Abrupt completion from Get. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + a. Let isArray be ? IsArray(value). + b. If isArray is true, return ? SerializeJSONArray(value). + + SerializeJSONArray ( value ) + + [...] + 6. Let len be ? LengthOfArrayLike(value). +features: [Proxy] +---*/ + +var abruptLength = new Proxy([], { + get: function(_target, key) { + if (key === 'length') { + throw new Test262Error(); + } + }, +}); + +assert.throws(Test262Error, function() { + JSON.stringify(abruptLength); +}); + +var abruptToPrimitive = { + valueOf: function() { + throw new Test262Error(); + }, +}; + +var abruptToLength = new Proxy([], { + get: function(_target, key) { + if (key === 'length') { + return abruptToPrimitive; + } + }, +}); + +assert.throws(Test262Error, function() { + JSON.stringify([abruptToLength]); +}); + +var abruptIndex = new Array(1); +Object.defineProperty(abruptIndex, '0', { + get: function() { + throw new Test262Error(); + }, +}); + +assert.throws(Test262Error, function() { + JSON.stringify({key: abruptIndex}); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-array-circular.js b/js/src/tests/test262/built-ins/JSON/stringify/value-array-circular.js new file mode 100644 index 0000000000..d98050541d --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-array-circular.js @@ -0,0 +1,39 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonarray +description: > + Circular array value throws a TypeError. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + a. Let isArray be ? IsArray(value). + b. If isArray is true, return ? SerializeJSONArray(value). + + SerializeJSONArray ( value ) + + 1. If stack contains value, throw a TypeError exception because the structure is cyclical. +---*/ + +var direct = []; +direct.push(direct); + +assert.throws(TypeError, function() { + JSON.stringify(direct); +}); + +var indirect = []; +indirect.push([[indirect]]); + +assert.throws(TypeError, function() { + JSON.stringify(indirect); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-array-proxy-revoked.js b/js/src/tests/test262/built-ins/JSON/stringify/value-array-proxy-revoked.js new file mode 100644 index 0000000000..ad91e3512d --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-array-proxy-revoked.js @@ -0,0 +1,39 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-json.stringify +description: > + Revoked array proxy value produces a TypeError. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + a. Let isArray be ? IsArray(value). + + IsArray ( argument ) + + [...] + 3. If argument is a Proxy exotic object, then + a. If argument.[[ProxyHandler]] is null, throw a TypeError exception. +features: [Proxy] +---*/ + +var handle = Proxy.revocable([], {}); + +handle.revoke(); + +assert.throws(TypeError, function() { + JSON.stringify(handle.proxy); +}, 'top-level value'); + +assert.throws(TypeError, function() { + JSON.stringify([[[handle.proxy]]]); +}, 'nested value'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-array-proxy.js b/js/src/tests/test262/built-ins/JSON/stringify/value-array-proxy.js new file mode 100644 index 0000000000..2cea3b4ebb --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-array-proxy.js @@ -0,0 +1,51 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonarray +description: > + Proxy of an array is treated as an array. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + a. Let isArray be ? IsArray(value). + b. If isArray is true, return ? SerializeJSONArray(value). + + SerializeJSONArray ( value ) + + [...] + 6. Let len be ? LengthOfArrayLike(value). + 7. Let index be 0. + 8. Repeat, while index < len + a. Let strP be ? SerializeJSONProperty(! ToString(index), value). +features: [Proxy] +---*/ + +var arrayProxy = new Proxy([], { + get: function(_target, key) { + if (key === 'length') return 2; + return Number(key); + }, +}); + +assert.sameValue( + JSON.stringify(arrayProxy), '[0,1]', 'proxy for an array' +); +assert.sameValue( + JSON.stringify([[arrayProxy]]), '[[[0,1]]]', 'proxy for an array (nested)' +); + +var arrayProxyProxy = new Proxy(arrayProxy, {}); +assert.sameValue( + JSON.stringify([[arrayProxyProxy]]), + '[[[0,1]]]', + 'proxy for a proxy for an array (nested)' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-cross-realm.js b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-cross-realm.js new file mode 100644 index 0000000000..a9d54c9e9c --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-cross-realm.js @@ -0,0 +1,21 @@ +// Copyright 2018 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-serializejsonproperty +description: JSON.stringify called with a BigInt object from another realm +features: [BigInt, cross-realm] +---*/ + +var other = $262.createRealm().global; +var wrapped = other.Object(other.BigInt(100)); + +assert.throws(TypeError, () => JSON.stringify(wrapped), + "cross-realm BigInt object without toJSON method"); + +other.BigInt.prototype.toJSON = function () { return this.toString(); }; + +assert.sameValue(JSON.stringify(wrapped), "\"100\"", + "cross-realm BigInt object with toJSON method"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-order.js b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-order.js new file mode 100644 index 0000000000..d4d902dc04 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-order.js @@ -0,0 +1,44 @@ +// Copyright (C) 2017 Robin Templeton. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: BigInt stringify order of steps +esid: sec-serializejsonproperty +info: | + Runtime Semantics: SerializeJSONProperty ( key, holder ) + + 2. If Type(value) is Object or BigInt, then + a. Let toJSON be ? GetGetV(value, "toJSON"). + b. If IsCallable(toJSON) is true, then + i. Set value to ? Call(toJSON, value, « key »). + 3. If ReplacerFunction is not undefined, then + a. Set value to ? Call(ReplacerFunction, holder, « key, value »). + 4. If Type(value) is Object, then + [...] + d. Else if value has a [[BigIntData]] internal slot, then + i. Set value to value.[[BigIntData]]. + [...] + 10. If Type(value) is BigInt, throw a TypeError exception +features: [BigInt, arrow-function] +---*/ + +let step; + +function replacer(x, k, v) +{ + assert.sameValue(step++, 1); + assert.sameValue(v, 1n); + return x; +} + +BigInt.prototype.toJSON = function () { assert.sameValue(step++, 0); return 1n; }; + +step = 0; +assert.throws(TypeError, () => JSON.stringify(0n, (k, v) => replacer(2n, k, v))); +assert.sameValue(step, 2); + +step = 0; +assert.throws(TypeError, () => JSON.stringify(0n, (k, v) => replacer(Object(2n), k, v))); +assert.sameValue(step, 2); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-replacer.js b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-replacer.js new file mode 100644 index 0000000000..e7048872e5 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-replacer.js @@ -0,0 +1,26 @@ +// Copyright (C) 2017 Robin Templeton. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: JSON serialization of BigInt values with replacer +esid: sec-serializejsonproperty +info: | + Runtime Semantics: SerializeJSONProperty ( key, holder ) + + 3. If ReplacerFunction is not undefined, then + a. Set value to ? Call(ReplacerFunction, holder, « key, value »). +features: [BigInt] +---*/ + +function replacer(k, v) +{ + if (typeof v === "bigint") + return "bigint"; + else + return v; +} + +assert.sameValue(JSON.stringify(0n, replacer), '"bigint"'); +assert.sameValue(JSON.stringify({x: 0n}, replacer), '{"x":"bigint"}'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-tojson-receiver.js b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-tojson-receiver.js new file mode 100644 index 0000000000..3b5eb58411 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-tojson-receiver.js @@ -0,0 +1,27 @@ +// Copyright 2019 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +// Test case written by André Bargull. + +/*--- +esid: sec-serializejsonproperty +description: toJSON method called with BigInt as receiver +features: [BigInt] +---*/ + +assert.throws(TypeError, () => JSON.stringify(1n), + "toString throws for BigInt object"); + +// The BigInt proposal changes the SerializeJSONProperty algorithm to +// specifically allow passing BigInt objects as receivers for the toJSON +// method. +Object.defineProperty(BigInt.prototype, "toJSON", { + get() { + "use strict"; + return () => typeof this; + } +}); + +assert.sameValue(JSON.stringify(1n), "\"bigint\"", + "BigInt toJSON method called with value as receiver"); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-tojson.js b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-tojson.js new file mode 100644 index 0000000000..6f2966fded --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint-tojson.js @@ -0,0 +1,20 @@ +// Copyright (C) 2017 Robin Templeton. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: BigInt toJSON method +esid: sec-serializejsonproperty +info: | + Runtime Semantics: SerializeJSONProperty ( key, holder ) + + 2. If Type(value) is Object or BigInt, then + a. Let toJSON be ? GetGetV(value, "toJSON"). + b. If IsCallable(toJSON) is true, then + i. Set value to ? Call(toJSON, value, « key »). +features: [BigInt] +---*/ + +BigInt.prototype.toJSON = function () { return this.toString(); }; +assert.sameValue(JSON.stringify(0n), '"0"'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-bigint.js b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint.js new file mode 100644 index 0000000000..f924aec4a5 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-bigint.js @@ -0,0 +1,14 @@ +// Copyright (C) 2017 Robin Templeton. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +description: JSON serialization of BigInt values +esid: pending +features: [BigInt] +---*/ + +assert.throws(TypeError, () => JSON.stringify(0n)); +assert.throws(TypeError, () => JSON.stringify(Object(0n))); +assert.throws(TypeError, () => JSON.stringify({x: 0n})); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-boolean-object.js b/js/src/tests/test262/built-ins/JSON/stringify/value-boolean-object.js new file mode 100644 index 0000000000..21fddf5eec --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-boolean-object.js @@ -0,0 +1,40 @@ +// 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: > + Boolean objects are converted to primitives using [[BooleanData]]. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 4. If Type(value) is Object, then + [...] + c. Else if value has a [[BooleanData]] internal slot, then + i. Set value to value.[[BooleanData]]. + [...] + 6. If value is true, return "true". + 7. If value is false, return "false". +---*/ + +assert.sameValue(JSON.stringify(new Boolean(true)), 'true'); + +assert.sameValue( + JSON.stringify({ + toJSON: function() { + return {key: new Boolean(false)}; + }, + }), + '{"key":false}' +); + +assert.sameValue(JSON.stringify([1], function(_k, v) { + return v === 1 ? new Boolean(true) : v; +}), '[true]'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-function.js b/js/src/tests/test262/built-ins/JSON/stringify/value-function.js new file mode 100644 index 0000000000..e4b18d857f --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-function.js @@ -0,0 +1,25 @@ +// 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: > + Function values are ignored. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + [...] + 11. Return undefined. +---*/ + +assert.sameValue(JSON.stringify(function() {}), undefined); +assert.sameValue(JSON.stringify([function() {}]), '[null]'); +assert.sameValue(JSON.stringify({key: function() {}}), '{}'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-number-negative-zero.js b/js/src/tests/test262/built-ins/JSON/stringify/value-number-negative-zero.js new file mode 100644 index 0000000000..665340762e --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-number-negative-zero.js @@ -0,0 +1,29 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Negative zero numbers are stringified to "0". +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 9. If Type(value) is Number, then + a. If value is finite, return ! ToString(value). + + NumberToString ( m ) + + [...] + 2. If m is +0 or -0, return the String "0". +---*/ + +assert.sameValue(JSON.stringify(-0), '0'); +assert.sameValue(JSON.stringify(['-0', 0, -0]), '["-0",0,0]'); +assert.sameValue(JSON.stringify({key: -0}), '{"key":0}'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-number-non-finite.js b/js/src/tests/test262/built-ins/JSON/stringify/value-number-non-finite.js new file mode 100644 index 0000000000..1109b956ea --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-number-non-finite.js @@ -0,0 +1,29 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Non-finite numbers as serialized as null. +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). + b. Return "null". +---*/ + +assert.sameValue(JSON.stringify(Infinity), 'null'); +assert.sameValue(JSON.stringify({key: -Infinity}), '{"key":null}'); +assert.sameValue(JSON.stringify([NaN]), '[null]'); + +reportCompare(0, 0); 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); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-object-abrupt.js b/js/src/tests/test262/built-ins/JSON/stringify/value-object-abrupt.js new file mode 100644 index 0000000000..131d1041d0 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-object-abrupt.js @@ -0,0 +1,26 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Abrupt completion from Get. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + 1. Let value be ? Get(holder, key). +---*/ + +assert.throws(Test262Error, function() { + JSON.stringify({ + get key() { + throw new Test262Error(); + }, + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-object-circular.js b/js/src/tests/test262/built-ins/JSON/stringify/value-object-circular.js new file mode 100644 index 0000000000..067261957d --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-object-circular.js @@ -0,0 +1,46 @@ +// Copyright (C) 2012 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonobject +description: > + Circular object value throws a TypeError. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + [...] + c. Return ? SerializeJSONObject(value). + + SerializeJSONObject ( value ) + + 1. If stack contains value, throw a TypeError exception because the structure is cyclical. +---*/ + +var direct = {}; +direct.prop = direct; + +assert.throws(TypeError, function() { + JSON.stringify(direct); +}); + +var indirect = { + p1: { + p2: { + get p3() { + return indirect; + }, + }, + }, +}; + +assert.throws(TypeError, function() { + JSON.stringify(indirect); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-object-proxy-revoked.js b/js/src/tests/test262/built-ins/JSON/stringify/value-object-proxy-revoked.js new file mode 100644 index 0000000000..fef4cefdeb --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-object-proxy-revoked.js @@ -0,0 +1,40 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonobject +description: > + Revoked object proxy value produces a TypeError. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + [...] + c. Return ? SerializeJSONObject(value). + + SerializeJSONObject ( value ) + + [...] + 6. Else, + a. Let K be ? EnumerableOwnPropertyNames(value, "key"). +features: [Proxy] +---*/ + +var handle = Proxy.revocable({}, {}); + +handle.revoke(); + +assert.throws(TypeError, function() { + JSON.stringify(handle.proxy); +}, 'top-level value'); + +assert.throws(TypeError, function() { + JSON.stringify({a: {b: handle.proxy}}); +}, 'nested value'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-object-proxy.js b/js/src/tests/test262/built-ins/JSON/stringify/value-object-proxy.js new file mode 100644 index 0000000000..6a64a3cfee --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-object-proxy.js @@ -0,0 +1,59 @@ +// Copyright (C) 2016 the V8 project authors. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonobject +description: > + Proxy of an object is treated as regular object. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + [...] + c. Return ? SerializeJSONObject(value). + + SerializeJSONObject ( value ) + + [...] + 6. Else, + a. Let K be ? EnumerableOwnPropertyNames(value, "key"). + 7. Let partial be a new empty List. + 8. For each element P of K, do + a. Let strP be ? SerializeJSONProperty(P, value). +features: [Proxy] +---*/ + +var objectProxy = new Proxy({}, { + getOwnPropertyDescriptor: function() { + return {value: 1, writable: true, enumerable: true, configurable: true}; + }, + get: function() { + return 1; + }, + ownKeys: function() { + return ['a', 'b']; + }, +}); + +assert.sameValue( + JSON.stringify(objectProxy), '{"a":1,"b":1}', 'proxy for an object' +); +assert.sameValue( + JSON.stringify({l1: {l2: objectProxy}}), + '{"l1":{"l2":{"a":1,"b":1}}}', + 'proxy for an object (nested)' +); + +var objectProxyProxy = new Proxy(objectProxy, {}); +assert.sameValue( + JSON.stringify({l1: {l2: objectProxyProxy}}), + '{"l1":{"l2":{"a":1,"b":1}}}', + 'proxy for a proxy for an object (nested)' +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-primitive-top-level.js b/js/src/tests/test262/built-ins/JSON/stringify/value-primitive-top-level.js new file mode 100644 index 0000000000..f0b9ca86ac --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-primitive-top-level.js @@ -0,0 +1,33 @@ +// 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: > + Top-level primitive values are stringified correctly. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 5. If value is null, return "null". + 6. If value is true, return "true". + 7. If value is false, return "false". + 8. If Type(value) is String, return QuoteJSONString(value). + 9. If Type(value) is Number, then + a. If value is finite, return ! ToString(value). + [...] + 11. Return undefined. +---*/ + +assert.sameValue(JSON.stringify(null), 'null'); +assert.sameValue(JSON.stringify(true), 'true'); +assert.sameValue(JSON.stringify(false), 'false'); +assert.sameValue(JSON.stringify('str'), '"str"'); +assert.sameValue(JSON.stringify(123), '123'); +assert.sameValue(JSON.stringify(undefined), undefined); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-string-escape-ascii.js b/js/src/tests/test262/built-ins/JSON/stringify/value-string-escape-ascii.js new file mode 100644 index 0000000000..3ec26e928d --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-string-escape-ascii.js @@ -0,0 +1,68 @@ +// Copyright (c) 2018 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-quotejsonstring +description: > + JSON.stringify property names and values containing ASCII + characters that require escaping +---*/ + +var char_to_json = { + '"': '\\"', + "\\": "\\\\", + "\x00": "\\u0000", + "\x01": "\\u0001", + "\x02": "\\u0002", + "\x03": "\\u0003", + "\x04": "\\u0004", + "\x05": "\\u0005", + "\x06": "\\u0006", + "\x07": "\\u0007", + "\x08": "\\b", + "\x09": "\\t", + "\x0A": "\\n", + "\x0B": "\\u000b", + "\x0C": "\\f", + "\x0D": "\\r", + "\x0E": "\\u000e", + "\x0F": "\\u000f", + "\x10": "\\u0010", + "\x11": "\\u0011", + "\x12": "\\u0012", + "\x13": "\\u0013", + "\x14": "\\u0014", + "\x15": "\\u0015", + "\x16": "\\u0016", + "\x17": "\\u0017", + "\x18": "\\u0018", + "\x19": "\\u0019", + "\x1A": "\\u001a", + "\x1B": "\\u001b", + "\x1C": "\\u001c", + "\x1D": "\\u001d", + "\x1E": "\\u001e", + "\x1F": "\\u001f" +} + +var chars = Object.keys(char_to_json).join(""); +var chars_reversed = Object.keys(char_to_json).reverse().join(""); +var jsonChars = Object.values(char_to_json).join(""); +var jsonChars_reversed = Object.values(char_to_json).reverse().join(""); +var json = JSON.stringify({ + ["name" + chars + chars_reversed]: chars_reversed + chars + "value" +}); + +for (var char in char_to_json) { + var count = json.split(char_to_json[char]).length - 1; + assert.sameValue(count, 4, + "Every ASCII 0x" + char.charCodeAt(0).toString(16) + " serializes to " + char_to_json[char]); +} + +assert.sameValue( + json, + `{"${"name" + jsonChars + jsonChars_reversed}":"${jsonChars_reversed + jsonChars + "value"}"}`, + "JSON.stringify(objectUsingControlCharacters)" +); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-string-escape-unicode.js b/js/src/tests/test262/built-ins/JSON/stringify/value-string-escape-unicode.js new file mode 100644 index 0000000000..a333bcc996 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-string-escape-unicode.js @@ -0,0 +1,38 @@ +// Copyright (c) 2018 Ecma International. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-quotejsonstring +description: > + JSON.stringify strings containing surrogate code units +features: [well-formed-json-stringify] +---*/ + +assert.sameValue(JSON.stringify("\uD834"), '"\\ud834"', + 'JSON.stringify("\\uD834")'); +assert.sameValue(JSON.stringify("\uDF06"), '"\\udf06"', + 'JSON.stringify("\\uDF06")'); + +assert.sameValue(JSON.stringify("\uD834\uDF06"), '"𝌆"', + 'JSON.stringify("\\uD834\\uDF06")'); +assert.sameValue(JSON.stringify("\uD834\uD834\uDF06\uD834"), '"\\ud834𝌆\\ud834"', + 'JSON.stringify("\\uD834\\uD834\\uDF06\\uD834")'); +assert.sameValue(JSON.stringify("\uD834\uD834\uDF06\uDF06"), '"\\ud834𝌆\\udf06"', + 'JSON.stringify("\\uD834\\uD834\\uDF06\\uDF06")'); +assert.sameValue(JSON.stringify("\uDF06\uD834\uDF06\uD834"), '"\\udf06𝌆\\ud834"', + 'JSON.stringify("\\uDF06\\uD834\\uDF06\\uD834")'); +assert.sameValue(JSON.stringify("\uDF06\uD834\uDF06\uDF06"), '"\\udf06𝌆\\udf06"', + 'JSON.stringify("\\uDF06\\uD834\\uDF06\\uDF06")'); + +assert.sameValue(JSON.stringify("\uDF06\uD834"), '"\\udf06\\ud834"', + 'JSON.stringify("\\uDF06\\uD834")'); +assert.sameValue(JSON.stringify("\uD834\uDF06\uD834\uD834"), '"𝌆\\ud834\\ud834"', + 'JSON.stringify("\\uD834\\uDF06\\uD834\\uD834")'); +assert.sameValue(JSON.stringify("\uD834\uDF06\uD834\uDF06"), '"𝌆𝌆"', + 'JSON.stringify("\\uD834\\uDF06\\uD834\\uDF06")'); +assert.sameValue(JSON.stringify("\uDF06\uDF06\uD834\uD834"), '"\\udf06\\udf06\\ud834\\ud834"', + 'JSON.stringify("\\uDF06\\uDF06\\uD834\\uD834")'); +assert.sameValue(JSON.stringify("\uDF06\uDF06\uD834\uDF06"), '"\\udf06\\udf06𝌆"', + 'JSON.stringify("\\uDF06\\uDF06\\uD834\\uDF06")'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-string-object.js b/js/src/tests/test262/built-ins/JSON/stringify/value-string-object.js new file mode 100644 index 0000000000..b7f5cd32cc --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-string-object.js @@ -0,0 +1,57 @@ +// 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: > + String exotic objects are converted to primitives using ToString. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 4. If Type(value) is Object, then + [...] + b. Else if value has a [[StringData]] internal slot, then + i. Set value to ? ToString(value). + [...] + 8. If Type(value) is String, return QuoteJSONString(value). +---*/ + +assert.sameValue(JSON.stringify(new String('str')), '"str"'); + +var toJSON = function() { + var str = new String('str'); + str.toString = function() { return 'toString'; }; + str.valueOf = function() { throw new Test262Error('should not be called'); }; + return str; +}; + +assert.sameValue( + JSON.stringify({ + key: { + toJSON: toJSON, + }, + }), + '{"key":"toString"}' +); + +var abruptReplacer = function(_key, value) { + if (value === true) { + var str = new String('str'); + str.toString = function() { throw new Test262Error(); }; + str.valueOf = function() { throw new Test262Error(); }; + return str; + } + + return value; +}; + +assert.throws(Test262Error, function() { + JSON.stringify([true], abruptReplacer); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-symbol.js b/js/src/tests/test262/built-ins/JSON/stringify/value-symbol.js new file mode 100644 index 0000000000..16dffec422 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-symbol.js @@ -0,0 +1,29 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Symbol primitives are ignored, both as keys and as values. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 11. Return undefined. +features: [Symbol] +---*/ + +var sym = Symbol('desc'); +assert.sameValue(JSON.stringify(sym), undefined); +assert.sameValue(JSON.stringify([sym]), '[null]'); +assert.sameValue(JSON.stringify({key: sym}), '{}'); + +var obj = {}; +obj[sym] = 1; +assert.sameValue(JSON.stringify(obj), '{}'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-abrupt.js b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-abrupt.js new file mode 100644 index 0000000000..fb97cd9c4c --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-abrupt.js @@ -0,0 +1,38 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Abrupt completions from Get and Call. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 2. If Type(value) is Object, then + a. Let toJSON be ? Get(value, "toJSON"). + b. If IsCallable(toJSON) is true, then + i. Set value to ? Call(toJSON, value, « key »). +---*/ + +assert.throws(Test262Error, function() { + JSON.stringify({ + get toJSON() { + throw new Test262Error(); + }, + }); +}); + +assert.throws(Test262Error, function() { + JSON.stringify({ + toJSON() { + throw new Test262Error(); + }, + }); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-arguments.js b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-arguments.js new file mode 100644 index 0000000000..27f001ef9a --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-arguments.js @@ -0,0 +1,47 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + toJSON is called with correct context and arguments. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 2. If Type(value) is Object, then + a. Let toJSON be ? Get(value, "toJSON"). + b. If IsCallable(toJSON) is true, then + i. Set value to ? Call(toJSON, value, « key »). +---*/ + +var callCount = 0; +var _this, _key; +var obj = { + toJSON: function(key) { + callCount += 1; + _this = this; + _key = key; + }, +}; + +assert.sameValue(JSON.stringify(obj), undefined); +assert.sameValue(callCount, 1); +assert.sameValue(_this, obj); +assert.sameValue(_key, ''); + +assert.sameValue(JSON.stringify([1, obj, 3]), '[1,null,3]'); +assert.sameValue(callCount, 2); +assert.sameValue(_this, obj); +assert.sameValue(_key, '1'); + +assert.sameValue(JSON.stringify({key: obj}), '{}'); +assert.sameValue(callCount, 3); +assert.sameValue(_this, obj); +assert.sameValue(_key, 'key'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-array-circular.js b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-array-circular.js new file mode 100644 index 0000000000..4de99e7e17 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-array-circular.js @@ -0,0 +1,41 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonarray +description: > + Circular array value (returned from toJSON method) throws a TypeError. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 2. If Type(value) is Object, then + a. Let toJSON be ? Get(value, "toJSON"). + b. If IsCallable(toJSON) is true, then + i. Set value to ? Call(toJSON, value, « key »). + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + a. Let isArray be ? IsArray(value). + b. If isArray is true, return ? SerializeJSONArray(value). + + SerializeJSONArray ( value ) + + 1. If stack contains value, throw a TypeError exception because the structure is cyclical. +---*/ + +var arr = []; +var circular = [arr]; + +arr.toJSON = function() { + return circular; +}; + +assert.throws(TypeError, function() { + JSON.stringify(circular); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-not-function.js b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-not-function.js new file mode 100644 index 0000000000..beb13ad9bb --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-not-function.js @@ -0,0 +1,27 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + toJSON value is not callable. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 2. If Type(value) is Object, then + a. Let toJSON be ? Get(value, "toJSON"). + b. If IsCallable(toJSON) is true, then + i. Set value to ? Call(toJSON, value, « key »). +---*/ + +assert.sameValue(JSON.stringify({toJSON: null}), '{"toJSON":null}'); +assert.sameValue(JSON.stringify({toJSON: false}), '{"toJSON":false}'); +assert.sameValue(JSON.stringify({toJSON: []}), '{"toJSON":[]}'); +assert.sameValue(JSON.stringify({toJSON: /re/}), '{"toJSON":{}}'); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-object-circular.js b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-object-circular.js new file mode 100644 index 0000000000..d6487dedb2 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-object-circular.js @@ -0,0 +1,41 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonobject +description: > + Circular object value (returned from toJSON method) throws a TypeError. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 2. If Type(value) is Object, then + a. Let toJSON be ? Get(value, "toJSON"). + b. If IsCallable(toJSON) is true, then + i. Set value to ? Call(toJSON, value, « key »). + [...] + 10. If Type(value) is Object and IsCallable(value) is false, then + [...] + c. Return ? SerializeJSONObject(value). + + SerializeJSONObject ( value ) + + 1. If stack contains value, throw a TypeError exception because the structure is cyclical. +---*/ + +var obj = {}; +var circular = { prop: obj }; + +obj.toJSON = function() { + return circular; +}; + +assert.throws(TypeError, function() { + JSON.stringify(circular); +}); + +reportCompare(0, 0); diff --git a/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-result.js b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-result.js new file mode 100644 index 0000000000..f095e25566 --- /dev/null +++ b/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-result.js @@ -0,0 +1,41 @@ +// Copyright (C) 2019 Aleksey Shvayka. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. +/*--- +esid: sec-serializejsonproperty +description: > + Result of toJSON method is stringified. +info: | + JSON.stringify ( value [ , replacer [ , space ] ] ) + + [...] + 12. Return ? SerializeJSONProperty(the empty String, wrapper). + + SerializeJSONProperty ( key, holder ) + + [...] + 2. If Type(value) is Object, then + a. Let toJSON be ? Get(value, "toJSON"). + b. If IsCallable(toJSON) is true, then + i. Set value to ? Call(toJSON, value, « key »). +---*/ + +assert.sameValue( + JSON.stringify({ + toJSON: function() { return [false]; }, + }), + '[false]' +); + +var arr = [true]; +arr.toJSON = function() {}; +assert.sameValue(JSON.stringify(arr), undefined); + +var str = new String('str'); +str.toJSON = function() { return null; }; +assert.sameValue(JSON.stringify({key: str}), '{"key":null}'); + +var num = new Number(14); +num.toJSON = function() { return {key: 7}; }; +assert.sameValue(JSON.stringify([num]), '[{"key":7}]'); + +reportCompare(0, 0); |