Skip to content

Commit

Permalink
additional BigInt JSON tests (#1235)
Browse files Browse the repository at this point in the history
* additional BigInt JSON tests

* single quotes

* BigInt stringify order of steps
  • Loading branch information
Robin Templeton authored and rwaldron committed Oct 3, 2017
1 parent 9737a5f commit 184a37f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
42 changes: 42 additions & 0 deletions test/built-ins/JSON/stringify/bigint-order.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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);
24 changes: 24 additions & 0 deletions test/built-ins/JSON/stringify/bigint-replacer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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"}');
18 changes: 18 additions & 0 deletions test/built-ins/JSON/stringify/bigint-tojson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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"');
2 changes: 2 additions & 0 deletions test/built-ins/JSON/stringify/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ features: [BigInt]
---*/

assert.throws(TypeError, () => JSON.stringify(0n));
assert.throws(TypeError, () => JSON.stringify(Object(0n)));
assert.throws(TypeError, () => JSON.stringify({x: 0n}));

0 comments on commit 184a37f

Please sign in to comment.