summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/JSON/stringify/value-tojson-arguments.js
blob: 27f001ef9aab65a660f5db50dff429e553c9366a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);