| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 return name.toUpperCase(); | 40 return name.toUpperCase(); |
| 41 }, | 41 }, |
| 42 enumerate: function(target) { | 42 enumerate: function(target) { |
| 43 return ['a', 'b', 'c']; | 43 return ['a', 'b', 'c']; |
| 44 }, | 44 }, |
| 45 getOwnPropertyDescriptor: function(target, name) { | 45 getOwnPropertyDescriptor: function(target, name) { |
| 46 return { enumerable: true }; | 46 return { enumerable: true }; |
| 47 } | 47 } |
| 48 } | 48 } |
| 49 | 49 |
| 50 var proxy1 = Proxy.create(handler1); | 50 var proxy1 = new Proxy({}, handler1); |
| 51 testStringify('{"a":"A","b":"B","c":"C"}', proxy1); | 51 testStringify('{"a":"A","b":"B","c":"C"}', proxy1); |
| 52 | 52 |
| 53 var proxy_fun = Proxy.createFunction(handler1, function() { return 1; }); | 53 var proxy_fun = Proxy.createFunction(handler1, function() { return 1; }); |
| 54 testStringify(undefined, proxy_fun); | 54 testStringify(undefined, proxy_fun); |
| 55 testStringify('[1,null]', [1, proxy_fun]); | 55 testStringify('[1,null]', [1, proxy_fun]); |
| 56 | 56 |
| 57 var parent1a = { b: proxy1 }; | 57 var parent1a = { b: proxy1 }; |
| 58 testStringify('{"b":{"a":"A","b":"B","c":"C"}}', parent1a); | 58 testStringify('{"b":{"a":"A","b":"B","c":"C"}}', parent1a); |
| 59 | 59 |
| 60 var parent1b = { a: 123, b: proxy1, c: true }; | 60 var parent1b = { a: 123, b: proxy1, c: true }; |
| 61 testStringify('{"a":123,"b":{"a":"A","b":"B","c":"C"},"c":true}', parent1b); | 61 testStringify('{"a":123,"b":{"a":"A","b":"B","c":"C"},"c":true}', parent1b); |
| 62 | 62 |
| 63 var parent1c = [123, proxy1, true]; | 63 var parent1c = [123, proxy1, true]; |
| 64 testStringify('[123,{"a":"A","b":"B","c":"C"},true]', parent1c); | 64 testStringify('[123,{"a":"A","b":"B","c":"C"},true]', parent1c); |
| 65 | 65 |
| 66 // Proxy with side effect. | 66 // Proxy with side effect. |
| 67 var handler2 = { | 67 var handler2 = { |
| 68 get: function(target, name) { | 68 get: function(target, name) { |
| 69 delete parent2.c; | 69 delete parent2.c; |
| 70 return name.toUpperCase(); | 70 return name.toUpperCase(); |
| 71 }, | 71 }, |
| 72 enumerate: function(target) { | 72 enumerate: function(target) { |
| 73 return ['a', 'b', 'c']; | 73 return ['a', 'b', 'c']; |
| 74 }, | 74 }, |
| 75 getOwnPropertyDescriptor: function(target, name) { | 75 getOwnPropertyDescriptor: function(target, name) { |
| 76 return { enumerable: true }; | 76 return { enumerable: true }; |
| 77 } | 77 } |
| 78 } | 78 } |
| 79 | 79 |
| 80 var proxy2 = Proxy.create(handler2); | 80 var proxy2 = new Proxy({}, handler2); |
| 81 var parent2 = { a: "delete", b: proxy2, c: "remove" }; | 81 var parent2 = { a: "delete", b: proxy2, c: "remove" }; |
| 82 var expected2 = '{"a":"delete","b":{"a":"A","b":"B","c":"C"}}'; | 82 var expected2 = '{"a":"delete","b":{"a":"A","b":"B","c":"C"}}'; |
| 83 assertEquals(expected2, JSON.stringify(parent2)); | 83 assertEquals(expected2, JSON.stringify(parent2)); |
| 84 parent2.c = "remove"; // Revert side effect. | 84 parent2.c = "remove"; // Revert side effect. |
| 85 assertEquals(expected2, JSON.stringify(parent2, undefined, 0)); | 85 assertEquals(expected2, JSON.stringify(parent2, undefined, 0)); |
| 86 | 86 |
| 87 // Proxy with a get function that uses the first argument. | 87 // Proxy with a get function that uses the first argument. |
| 88 var handler3 = { | 88 var handler3 = { |
| 89 get: function(target, name) { | 89 get: function(target, name) { |
| 90 if (name == 'valueOf') return function() { return "proxy" }; | 90 if (name == 'valueOf') return function() { return "proxy" }; |
| 91 return name + "(" + target + ")"; | 91 return name + "(" + target + ")"; |
| 92 }, | 92 }, |
| 93 enumerate: function(target) { | 93 enumerate: function(target) { |
| 94 return ['a', 'b', 'c']; | 94 return ['a', 'b', 'c']; |
| 95 }, | 95 }, |
| 96 getOwnPropertyDescriptor: function(target, name) { | 96 getOwnPropertyDescriptor: function(target, name) { |
| 97 return { enumerable: true }; | 97 return { enumerable: true }; |
| 98 } | 98 } |
| 99 } | 99 } |
| 100 | 100 |
| 101 var proxy3 = Proxy.create(handler3); | 101 var proxy3 = new Proxy({}, handler3); |
| 102 var parent3 = { x: 123, y: proxy3 } | 102 var parent3 = { x: 123, y: proxy3 } |
| 103 testStringify('{"x":123,"y":{"a":"a(proxy)","b":"b(proxy)","c":"c(proxy)"}}', | 103 testStringify('{"x":123,"y":{"a":"a(proxy)","b":"b(proxy)","c":"c(proxy)"}}', |
| 104 parent3); | 104 parent3); |
| 105 | 105 |
| 106 // Empty proxy. | 106 // Empty proxy. |
| 107 var handler4 = { | 107 var handler4 = { |
| 108 get: function(target, name) { | 108 get: function(target, name) { |
| 109 return 0; | 109 return 0; |
| 110 }, | 110 }, |
| 111 enumerate: function(target) { | 111 enumerate: function(target) { |
| 112 return []; | 112 return []; |
| 113 }, | 113 }, |
| 114 getOwnPropertyDescriptor: function(target, name) { | 114 getOwnPropertyDescriptor: function(target, name) { |
| 115 return { enumerable: false }; | 115 return { enumerable: false }; |
| 116 } | 116 } |
| 117 } | 117 } |
| 118 | 118 |
| 119 var proxy4 = Proxy.create(handler4); | 119 var proxy4 = new Proxy({}, handler4); |
| 120 testStringify('{}', proxy4); | 120 testStringify('{}', proxy4); |
| 121 testStringify('{"a":{}}', { a: proxy4 }); | 121 testStringify('{"a":{}}', { a: proxy4 }); |
| 122 | 122 |
| 123 // Proxy that provides a toJSON function that uses this. | 123 // Proxy that provides a toJSON function that uses this. |
| 124 var handler5 = { | 124 var handler5 = { |
| 125 get: function(target, name) { | 125 get: function(target, name) { |
| 126 if (name == 'z') return 97000; | 126 if (name == 'z') return 97000; |
| 127 return function(key) { return key.charCodeAt(0) + this.z; }; | 127 return function(key) { return key.charCodeAt(0) + this.z; }; |
| 128 }, | 128 }, |
| 129 enumerate: function(target) { | 129 enumerate: function(target) { |
| 130 return ['toJSON', 'z']; | 130 return ['toJSON', 'z']; |
| 131 }, | 131 }, |
| 132 getOwnPropertyDescriptor: function(target, name) { | 132 getOwnPropertyDescriptor: function(target, name) { |
| 133 return { enumerable: true }; | 133 return { enumerable: true }; |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 var proxy5 = Proxy.create(handler5); | 137 var proxy5 = new Proxy({}, handler5); |
| 138 testStringify('{"a":97097}', { a: proxy5 }); | 138 testStringify('{"a":97097}', { a: proxy5 }); |
| 139 | 139 |
| 140 // Proxy that provides a toJSON function that returns undefined. | 140 // Proxy that provides a toJSON function that returns undefined. |
| 141 var handler6 = { | 141 var handler6 = { |
| 142 get: function(target, name) { | 142 get: function(target, name) { |
| 143 return function(key) { return undefined; }; | 143 return function(key) { return undefined; }; |
| 144 }, | 144 }, |
| 145 enumerate: function(target) { | 145 enumerate: function(target) { |
| 146 return ['toJSON']; | 146 return ['toJSON']; |
| 147 }, | 147 }, |
| 148 getOwnPropertyDescriptor: function(target, name) { | 148 getOwnPropertyDescriptor: function(target, name) { |
| 149 return { enumerable: true }; | 149 return { enumerable: true }; |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 | 152 |
| 153 var proxy6 = Proxy.create(handler6); | 153 var proxy6 = new Proxy({}, handler6); |
| 154 testStringify('[1,null,true]', [1, proxy6, true]); | 154 testStringify('[1,null,true]', [1, proxy6, true]); |
| 155 testStringify('{"a":1,"c":true}', {a: 1, b: proxy6, c: true}); | 155 testStringify('{"a":1,"c":true}', {a: 1, b: proxy6, c: true}); |
| 156 | 156 |
| 157 // Object containing a proxy that changes the parent's properties. | 157 // Object containing a proxy that changes the parent's properties. |
| 158 var handler7 = { | 158 var handler7 = { |
| 159 get: function(target, name) { | 159 get: function(target, name) { |
| 160 delete parent7.a; | 160 delete parent7.a; |
| 161 delete parent7.c; | 161 delete parent7.c; |
| 162 parent7.e = "5"; | 162 parent7.e = "5"; |
| 163 return name.toUpperCase(); | 163 return name.toUpperCase(); |
| 164 }, | 164 }, |
| 165 enumerate: function(target) { | 165 enumerate: function(target) { |
| 166 return ['a', 'b', 'c']; | 166 return ['a', 'b', 'c']; |
| 167 }, | 167 }, |
| 168 getOwnPropertyDescriptor: function(target, name) { | 168 getOwnPropertyDescriptor: function(target, name) { |
| 169 return { enumerable: true }; | 169 return { enumerable: true }; |
| 170 } | 170 } |
| 171 } | 171 } |
| 172 | 172 |
| 173 var proxy7 = Proxy.create(handler7); | 173 var proxy7 = new Proxy({}, handler7); |
| 174 var parent7 = { a: "1", b: proxy7, c: "3", d: "4" }; | 174 var parent7 = { a: "1", b: proxy7, c: "3", d: "4" }; |
| 175 assertEquals('{"a":"1","b":{"a":"A","b":"B","c":"C"},"d":"4"}', | 175 assertEquals('{"a":"1","b":{"a":"A","b":"B","c":"C"},"d":"4"}', |
| 176 JSON.stringify(parent7)); | 176 JSON.stringify(parent7)); |
| 177 assertEquals('{"b":{"a":"A","b":"B","c":"C"},"d":"4","e":"5"}', | 177 assertEquals('{"b":{"a":"A","b":"B","c":"C"},"d":"4","e":"5"}', |
| 178 JSON.stringify(parent7)); | 178 JSON.stringify(parent7)); |
| OLD | NEW |