1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // Flags: --harmony-proxies
30 function testStringify(expected, object) {
31 // Test fast case that bails out to slow case.
32 assertEquals(expected, JSON.stringify(object));
34 assertEquals(expected, JSON.stringify(object, undefined, 0));
37 // Test serializing a proxy, function proxy and objects that contain them.
39 get: function(target, name) {
40 return name.toUpperCase();
42 enumerate: function(target) {
43 return ['a', 'b', 'c'];
45 getOwnPropertyDescriptor: function(target, name) {
46 return { enumerable: true };
50 var proxy1 = Proxy.create(handler1);
51 testStringify('{"a":"A","b":"B","c":"C"}', proxy1);
53 var proxy_fun = Proxy.createFunction(handler1, function() { return 1; });
54 testStringify(undefined, proxy_fun);
55 testStringify('[1,null]', [1, proxy_fun]);
57 var parent1a = { b: proxy1 };
58 testStringify('{"b":{"a":"A","b":"B","c":"C"}}', parent1a);
60 var parent1b = { a: 123, b: proxy1, c: true };
61 testStringify('{"a":123,"b":{"a":"A","b":"B","c":"C"},"c":true}', parent1b);
63 var parent1c = [123, proxy1, true];
64 testStringify('[123,{"a":"A","b":"B","c":"C"},true]', parent1c);
66 // Proxy with side effect.
68 get: function(target, name) {
70 return name.toUpperCase();
72 enumerate: function(target) {
73 return ['a', 'b', 'c'];
75 getOwnPropertyDescriptor: function(target, name) {
76 return { enumerable: true };
80 var proxy2 = Proxy.create(handler2);
81 var parent2 = { a: "delete", b: proxy2, c: "remove" };
82 var expected2 = '{"a":"delete","b":{"a":"A","b":"B","c":"C"}}';
83 assertEquals(expected2, JSON.stringify(parent2));
84 parent2.c = "remove"; // Revert side effect.
85 assertEquals(expected2, JSON.stringify(parent2, undefined, 0));
87 // Proxy with a get function that uses the first argument.
89 get: function(target, name) {
90 if (name == 'valueOf') return function() { return "proxy" };
91 return name + "(" + target + ")";
93 enumerate: function(target) {
94 return ['a', 'b', 'c'];
96 getOwnPropertyDescriptor: function(target, name) {
97 return { enumerable: true };
101 var proxy3 = Proxy.create(handler3);
102 var parent3 = { x: 123, y: proxy3 }
103 testStringify('{"x":123,"y":{"a":"a(proxy)","b":"b(proxy)","c":"c(proxy)"}}',
108 get: function(target, name) {
111 enumerate: function(target) {
114 getOwnPropertyDescriptor: function(target, name) {
115 return { enumerable: false };
119 var proxy4 = Proxy.create(handler4);
120 testStringify('{}', proxy4);
121 testStringify('{"a":{}}', { a: proxy4 });
123 // Proxy that provides a toJSON function that uses this.
125 get: function(target, name) {
126 if (name == 'z') return 97000;
127 return function(key) { return key.charCodeAt(0) + this.z; };
129 enumerate: function(target) {
130 return ['toJSON', 'z'];
132 getOwnPropertyDescriptor: function(target, name) {
133 return { enumerable: true };
137 var proxy5 = Proxy.create(handler5);
138 testStringify('{"a":97097}', { a: proxy5 });
140 // Proxy that provides a toJSON function that returns undefined.
142 get: function(target, name) {
143 return function(key) { return undefined; };
145 enumerate: function(target) {
148 getOwnPropertyDescriptor: function(target, name) {
149 return { enumerable: true };
153 var proxy6 = Proxy.create(handler6);
154 testStringify('[1,null,true]', [1, proxy6, true]);
155 testStringify('{"a":1,"c":true}', {a: 1, b: proxy6, c: true});
157 // Object containing a proxy that changes the parent's properties.
159 get: function(target, name) {
163 return name.toUpperCase();
165 enumerate: function(target) {
166 return ['a', 'b', 'c'];
168 getOwnPropertyDescriptor: function(target, name) {
169 return { enumerable: true };
173 var proxy7 = Proxy.create(handler7);
174 var parent7 = { a: "1", b: proxy7, c: "3", d: "4" };
175 assertEquals('{"a":"1","b":{"a":"A","b":"B","c":"C"},"d":"4"}',
176 JSON.stringify(parent7));
177 assertEquals('{"b":{"a":"A","b":"B","c":"C"},"d":"4","e":"5"}',
178 JSON.stringify(parent7));