| 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Flags: --harmony-object-observe | 28 // Flags: --harmony-observation |
| 29 | 29 |
| 30 var allObservers = []; | 30 var allObservers = []; |
| 31 function reset() { | 31 function reset() { |
| 32 allObservers.forEach(function(observer) { observer.reset(); }); | 32 allObservers.forEach(function(observer) { observer.reset(); }); |
| 33 } | 33 } |
| 34 | 34 |
| 35 function createObserver() { | 35 function createObserver() { |
| 36 "use strict"; // So that |this| in callback can be undefined. | 36 "use strict"; // So that |this| in callback can be undefined. |
| 37 | 37 |
| 38 var observer = { | 38 var observer = { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 var obj = {}; | 81 var obj = {}; |
| 82 | 82 |
| 83 function frozenFunction() {} | 83 function frozenFunction() {} |
| 84 Object.freeze(frozenFunction); | 84 Object.freeze(frozenFunction); |
| 85 var nonFunction = {}; | 85 var nonFunction = {}; |
| 86 var changeRecordWithAccessor = { type: 'foo' }; | 86 var changeRecordWithAccessor = { type: 'foo' }; |
| 87 var recordCreated = false; | 87 var recordCreated = false; |
| 88 Object.defineProperty(changeRecordWithAccessor, 'name', { | 88 Object.defineProperty(changeRecordWithAccessor, 'name', { |
| 89 get: function() { | 89 get: function() { |
| 90 recordCreated = true; | 90 recordCreated = true; |
| 91 return "bar"; |
| 91 }, | 92 }, |
| 92 enumerable: true | 93 enumerable: true |
| 93 }) | 94 }) |
| 94 | 95 |
| 95 // Object.observe | 96 // Object.observe |
| 96 assertThrows(function() { Object.observe("non-object", observer.callback); }, Ty
peError); | 97 assertThrows(function() { Object.observe("non-object", observer.callback); }, Ty
peError); |
| 97 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError); | 98 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError); |
| 98 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError); | 99 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError); |
| 99 | 100 |
| 100 // Object.unobserve | 101 // Object.unobserve |
| 101 assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError); | 102 assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError); |
| 102 | 103 |
| 103 // Object.notify | 104 // Object.notify |
| 104 assertThrows(function() { Object.notify(obj, {}); }, TypeError); | 105 assertThrows(function() { Object.notify(obj, {}); }, TypeError); |
| 105 assertThrows(function() { Object.notify(obj, { type: 4 }); }, TypeError); | 106 assertThrows(function() { Object.notify(obj, { type: 4 }); }, TypeError); |
| 107 assertFalse(recordCreated); |
| 106 Object.notify(obj, changeRecordWithAccessor); | 108 Object.notify(obj, changeRecordWithAccessor); |
| 107 assertFalse(recordCreated); | 109 assertFalse(recordCreated); |
| 108 | 110 |
| 109 // Object.deliverChangeRecords | 111 // Object.deliverChangeRecords |
| 110 assertThrows(function() { Object.deliverChangeRecords(nonFunction); }, TypeError
); | 112 assertThrows(function() { Object.deliverChangeRecords(nonFunction); }, TypeError
); |
| 111 | 113 |
| 112 // Multiple records are delivered. | 114 // Multiple records are delivered. |
| 113 Object.observe(obj, observer.callback); | 115 Object.observe(obj, observer.callback); |
| 114 Object.notify(obj, { | 116 Object.notify(obj, { |
| 115 object: obj, | 117 object: obj, |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 }); | 212 }); |
| 211 | 213 |
| 212 Object.unobserve(obj, observer.callback); | 214 Object.unobserve(obj, observer.callback); |
| 213 Object.deliverChangeRecords(observer.callback); | 215 Object.deliverChangeRecords(observer.callback); |
| 214 observer.assertCallbackRecords([ | 216 observer.assertCallbackRecords([ |
| 215 { object: obj, type: 'foo', val: 1 }, | 217 { object: obj, type: 'foo', val: 1 }, |
| 216 { object: obj, type: 'foo', val: 3 }, | 218 { object: obj, type: 'foo', val: 3 }, |
| 217 { object: obj, type: 'foo', val: 5 } | 219 { object: obj, type: 'foo', val: 5 } |
| 218 ]); | 220 ]); |
| 219 | 221 |
| 220 // Observing multiple objects; records appear in order;. | 222 // Observing multiple objects; records appear in order. |
| 221 reset(); | 223 reset(); |
| 222 var obj2 = {}; | 224 var obj2 = {}; |
| 223 var obj3 = {} | 225 var obj3 = {} |
| 224 Object.observe(obj, observer.callback); | 226 Object.observe(obj, observer.callback); |
| 225 Object.observe(obj3, observer.callback); | 227 Object.observe(obj3, observer.callback); |
| 226 Object.observe(obj2, observer.callback); | 228 Object.observe(obj2, observer.callback); |
| 227 Object.notify(obj, { | 229 Object.notify(obj, { |
| 228 type: 'foo', | 230 type: 'foo', |
| 229 }); | 231 }); |
| 230 Object.notify(obj2, { | 232 Object.notify(obj2, { |
| 231 type: 'foo', | 233 type: 'foo', |
| 232 }); | 234 }); |
| 233 Object.notify(obj3, { | 235 Object.notify(obj3, { |
| 234 type: 'foo', | 236 type: 'foo', |
| 235 }); | 237 }); |
| 236 Object.deliverChangeRecords(observer.callback); | 238 Object.deliverChangeRecords(observer.callback); |
| 237 observer.assertCallbackRecords([ | 239 observer.assertCallbackRecords([ |
| 238 { object: obj, type: 'foo' }, | 240 { object: obj, type: 'foo' }, |
| 239 { object: obj2, type: 'foo' }, | 241 { object: obj2, type: 'foo' }, |
| 240 { object: obj3, type: 'foo' } | 242 { object: obj3, type: 'foo' } |
| 241 ]); | 243 ]); |
| 244 |
| 245 // Observing named properties. |
| 246 reset(); |
| 247 var obj = {a: 1} |
| 248 Object.observe(obj, observer.callback); |
| 249 obj.a = 2; |
| 250 obj["a"] = 3; |
| 251 delete obj.a; |
| 252 obj.a = 4; |
| 253 obj.a = 5; |
| 254 Object.defineProperty(obj, "a", {value: 6}); |
| 255 Object.defineProperty(obj, "a", {writable: false}); |
| 256 obj.a = 7; // ignored |
| 257 Object.defineProperty(obj, "a", {value: 8}); |
| 258 Object.defineProperty(obj, "a", {get: function() {}}); |
| 259 delete obj.a; |
| 260 Object.defineProperty(obj, "a", {get: function() {}}); |
| 261 Object.deliverChangeRecords(observer.callback); |
| 262 // TODO(observe): oldValue not included yet. |
| 263 observer.assertCallbackRecords([ |
| 264 { object: obj, name: "a", type: "updated" }, |
| 265 { object: obj, name: "a", type: "updated" }, |
| 266 { object: obj, name: "a", type: "deleted" }, |
| 267 { object: obj, name: "a", type: "new" }, |
| 268 { object: obj, name: "a", type: "updated" }, |
| 269 { object: obj, name: "a", type: "updated" }, |
| 270 { object: obj, name: "a", type: "reconfigured" }, |
| 271 { object: obj, name: "a", type: "updated" }, |
| 272 { object: obj, name: "a", type: "reconfigured" }, |
| 273 { object: obj, name: "a", type: "deleted" }, |
| 274 { object: obj, name: "a", type: "new" }, |
| 275 ]); |
| OLD | NEW |