Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 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 |
| 4 | // met: |
| 5 | // |
| 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. |
| 15 | // |
| 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. |
| 27 | |
| 28 | // Flags: --harmony-proxies |
| 29 | |
| 30 | function testStringify(expected, object) { |
| 31 | // Test fast case that bails out to slow case. |
| 32 | assertEquals(expected, JSON.stringify(object)); |
| 33 | // Test slow case. |
| 34 | assertEquals(expected, JSON.stringify(object, undefined, 0)); |
| 35 | } |
| 36 | |
| 37 | // Test serializing a proxy, function proxy and objects that contain them. |
| 38 | var handler1 = { |
| 39 | get: function(target, name) { |
| 40 | return name.toUpperCase(); |
| 41 | }, |
| 42 | enumerate: function(target) { |
| 43 | return ['a', 'b', 'c']; |
| 44 | }, |
| 45 | getOwnPropertyDescriptor: function(target, name) { |
| 46 | return { enumerable: true }; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | var proxy1 = Proxy.create(handler1); |
| 51 | testStringify('{"a":"A","b":"B","c":"C"}', proxy1); |
| 52 | |
| 53 | var proxy_fun = Proxy.createFunction(handler1, function() { return 1; }); |
| 54 | testStringify(undefined, proxy_fun); |
| 55 | testStringify('[1,null]', [1, proxy_fun]); |
| 56 | |
| 57 | var parent1a = { b: proxy1 }; |
| 58 | testStringify('{"b":{"a":"A","b":"B","c":"C"}}', parent1a); |
| 59 | |
| 60 | var parent1b = { a: 123, b: proxy1, c: true }; |
| 61 | testStringify('{"a":123,"b":{"a":"A","b":"B","c":"C"},"c":true}', parent1b); |
| 62 | |
| 63 | var parent1c = [123, proxy1, true]; |
| 64 | testStringify('[123,{"a":"A","b":"B","c":"C"},true]', parent1c); |
| 65 | |
| 66 | // Proxy with side effect. |
| 67 | var handler2 = { |
| 68 | get: function(target, name) { |
| 69 | delete parent2.c; |
| 70 | return name.toUpperCase(); |
| 71 | }, |
| 72 | enumerate: function(target) { |
| 73 | return ['a', 'b', 'c']; |
| 74 | }, |
| 75 | getOwnPropertyDescriptor: function(target, name) { |
| 76 | return { enumerable: true }; |
| 77 | } |
| 78 | } |
| 79 | |
| 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)); |
| 86 | |
| 87 | // Proxy with a get function that uses the first argument. |
| 88 | var handler3 = { |
| 89 | get: function(target, name) { |
| 90 | if (name == 'valueOf') return function() { return "proxy" }; |
| 91 | return name + "(" + target + ")"; |
| 92 | }, |
| 93 | enumerate: function(target) { |
| 94 | return ['a', 'b', 'c']; |
| 95 | }, |
| 96 | getOwnPropertyDescriptor: function(target, name) { |
| 97 | return { enumerable: true }; |
| 98 | } |
| 99 | } |
| 100 | |
| 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)"}}', |
| 104 | parent3); |
| 105 | |
| 106 | // Empty proxy. |
| 107 | var handler4 = { |
| 108 | get: function(target, name) { |
| 109 | return 0; |
| 110 | }, |
| 111 | enumerate: function(target) { |
| 112 | return []; |
| 113 | }, |
| 114 | getOwnPropertyDescriptor: function(target, name) { |
| 115 | return { enumerable: false }; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | var proxy4 = Proxy.create(handler4); |
| 120 | testStringify('{}', proxy4); |
| 121 | testStringify('{"a":{}}', { a: proxy4 }); |
| 122 | |
| 123 | // Proxy that provides a toJSON function that uses this. |
| 124 | var handler5 = { |
| 125 | get: function(target, name) { |
| 126 | if (name == 'z') return 97000; |
| 127 | return function(key) { return key.charCodeAt(0) + this.z; }; |
| 128 | }, |
| 129 | enumerate: function(target) { |
| 130 | return ['toJSON', 'z']; |
| 131 | }, |
| 132 | getOwnPropertyDescriptor: function(target, name) { |
| 133 | return { enumerable: true }; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | var proxy5 = Proxy.create(handler5); |
| 138 | testStringify('{"a":97097}', { a: proxy5 }); |
| 139 | |
| 140 | // Proxy that provides a toJSON function that returns undefined. |
| 141 | var handler6 = { |
| 142 | get: function(target, name) { |
| 143 | return function(key) { return undefined; }; |
| 144 | }, |
| 145 | enumerate: function(target) { |
| 146 | return ['toJSON']; |
| 147 | }, |
| 148 | getOwnPropertyDescriptor: function(target, name) { |
| 149 | return { enumerable: true }; |
| 150 | } |
| 151 | } |
| 152 | |
| 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}); |
| 156 | |
| 157 | // Object containing a proxy that changes the parent's properties. |
| 158 | var handler7 = { |
| 159 | get: function(target, name) { |
| 160 | delete parent7.a; |
| 161 | delete parent7.c; |
| 162 | parent7.e = "5"; |
| 163 | return name.toUpperCase(); |
| 164 | }, |
| 165 | enumerate: function(target) { |
| 166 | return ['a', 'b', 'c']; |
| 167 | }, |
| 168 | getOwnPropertyDescriptor: function(target, name) { |
| 169 | return { enumerable: true }; |
| 170 | } |
| 171 | } |
| 172 | |
| 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)); |