danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +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 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 28 | // Flags: --harmony-observation --harmony-proxies --harmony-collections |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 29 | |
| 30 | var allObservers = []; |
| 31 | function reset() { |
| 32 | allObservers.forEach(function(observer) { observer.reset(); }); |
| 33 | } |
| 34 | |
| 35 | function createObserver() { |
| 36 | "use strict"; // So that |this| in callback can be undefined. |
| 37 | |
| 38 | var observer = { |
| 39 | records: undefined, |
| 40 | callbackCount: 0, |
| 41 | reset: function() { |
| 42 | this.records = undefined; |
| 43 | this.callbackCount = 0; |
| 44 | }, |
| 45 | assertNotCalled: function() { |
| 46 | assertEquals(undefined, this.records); |
| 47 | assertEquals(0, this.callbackCount); |
| 48 | }, |
| 49 | assertCalled: function() { |
| 50 | assertEquals(1, this.callbackCount); |
| 51 | }, |
| 52 | assertRecordCount: function(count) { |
| 53 | this.assertCalled(); |
| 54 | assertEquals(count, this.records.length); |
| 55 | }, |
| 56 | assertCallbackRecords: function(recs) { |
| 57 | this.assertRecordCount(recs.length); |
| 58 | for (var i = 0; i < recs.length; i++) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 59 | if ('name' in recs[i]) |
| 60 | recs[i].name = String(recs[i].name); |
| 61 | print(i, JSON.stringify(this.records[i]), JSON.stringify(recs[i])); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 62 | assertSame(this.records[i].object, recs[i].object); |
| 63 | assertEquals('string', typeof recs[i].type); |
| 64 | assertPropertiesEqual(this.records[i], recs[i]); |
| 65 | } |
| 66 | } |
| 67 | }; |
| 68 | |
| 69 | observer.callback = function(r) { |
| 70 | assertEquals(undefined, this); |
| 71 | assertEquals('object', typeof r); |
| 72 | assertTrue(r instanceof Array) |
| 73 | observer.records = r; |
| 74 | observer.callbackCount++; |
| 75 | }; |
| 76 | |
| 77 | observer.reset(); |
| 78 | allObservers.push(observer); |
| 79 | return observer; |
| 80 | } |
| 81 | |
| 82 | var observer = createObserver(); |
| 83 | assertEquals("function", typeof observer.callback); |
| 84 | var obj = {}; |
| 85 | |
| 86 | function frozenFunction() {} |
| 87 | Object.freeze(frozenFunction); |
| 88 | var nonFunction = {}; |
| 89 | var changeRecordWithAccessor = { type: 'foo' }; |
| 90 | var recordCreated = false; |
| 91 | Object.defineProperty(changeRecordWithAccessor, 'name', { |
| 92 | get: function() { |
| 93 | recordCreated = true; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 94 | return "bar"; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 95 | }, |
| 96 | enumerable: true |
| 97 | }) |
| 98 | |
| 99 | // Object.observe |
| 100 | assertThrows(function() { Object.observe("non-object", observer.callback); }, TypeError); |
| 101 | assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError); |
| 102 | assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 103 | assertEquals(obj, Object.observe(obj, observer.callback)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 104 | |
| 105 | // Object.unobserve |
| 106 | assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 107 | assertThrows(function() { Object.unobserve(obj, nonFunction); }, TypeError); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 108 | assertEquals(obj, Object.unobserve(obj, observer.callback)); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 109 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 110 | // Object.getNotifier |
| 111 | var notifier = Object.getNotifier(obj); |
| 112 | assertSame(notifier, Object.getNotifier(obj)); |
| 113 | assertEquals(null, Object.getNotifier(Object.freeze({}))); |
| 114 | assertFalse(notifier.hasOwnProperty('notify')); |
| 115 | assertEquals([], Object.keys(notifier)); |
| 116 | var notifyDesc = Object.getOwnPropertyDescriptor(notifier.__proto__, 'notify'); |
| 117 | assertTrue(notifyDesc.configurable); |
| 118 | assertTrue(notifyDesc.writable); |
| 119 | assertFalse(notifyDesc.enumerable); |
| 120 | assertThrows(function() { notifier.notify({}); }, TypeError); |
| 121 | assertThrows(function() { notifier.notify({ type: 4 }); }, TypeError); |
| 122 | var notify = notifier.notify; |
| 123 | assertThrows(function() { notify.call(undefined, { type: 'a' }); }, TypeError); |
| 124 | assertThrows(function() { notify.call(null, { type: 'a' }); }, TypeError); |
| 125 | assertThrows(function() { notify.call(5, { type: 'a' }); }, TypeError); |
| 126 | assertThrows(function() { notify.call('hello', { type: 'a' }); }, TypeError); |
| 127 | assertThrows(function() { notify.call(false, { type: 'a' }); }, TypeError); |
| 128 | assertThrows(function() { notify.call({}, { type: 'a' }); }, TypeError); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 129 | assertFalse(recordCreated); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 130 | notifier.notify(changeRecordWithAccessor); |
| 131 | assertFalse(recordCreated); // not observed yet |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 132 | |
| 133 | // Object.deliverChangeRecords |
| 134 | assertThrows(function() { Object.deliverChangeRecords(nonFunction); }, TypeError); |
| 135 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 136 | Object.observe(obj, observer.callback); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 137 | |
| 138 | // notify uses to [[CreateOwnProperty]] to create changeRecord; |
| 139 | reset(); |
| 140 | var protoExpandoAccessed = false; |
| 141 | Object.defineProperty(Object.prototype, 'protoExpando', |
| 142 | { |
| 143 | configurable: true, |
| 144 | set: function() { protoExpandoAccessed = true; } |
| 145 | } |
| 146 | ); |
| 147 | notifier.notify({ type: 'foo', protoExpando: 'val'}); |
| 148 | assertFalse(protoExpandoAccessed); |
| 149 | delete Object.prototype.protoExpando; |
| 150 | Object.deliverChangeRecords(observer.callback); |
| 151 | |
| 152 | // Multiple records are delivered. |
| 153 | reset(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 154 | notifier.notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 155 | type: 'updated', |
| 156 | name: 'foo', |
| 157 | expando: 1 |
| 158 | }); |
| 159 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 160 | notifier.notify({ |
| 161 | object: notifier, // object property is ignored |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 162 | type: 'deleted', |
| 163 | name: 'bar', |
| 164 | expando2: 'str' |
| 165 | }); |
| 166 | Object.deliverChangeRecords(observer.callback); |
| 167 | observer.assertCallbackRecords([ |
| 168 | { object: obj, name: 'foo', type: 'updated', expando: 1 }, |
| 169 | { object: obj, name: 'bar', type: 'deleted', expando2: 'str' } |
| 170 | ]); |
| 171 | |
| 172 | // No delivery takes place if no records are pending |
| 173 | reset(); |
| 174 | Object.deliverChangeRecords(observer.callback); |
| 175 | observer.assertNotCalled(); |
| 176 | |
| 177 | // Multiple observation has no effect. |
| 178 | reset(); |
| 179 | Object.observe(obj, observer.callback); |
| 180 | Object.observe(obj, observer.callback); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 181 | Object.getNotifier(obj).notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 182 | type: 'foo', |
| 183 | }); |
| 184 | Object.deliverChangeRecords(observer.callback); |
| 185 | observer.assertCalled(); |
| 186 | |
| 187 | // Observation can be stopped. |
| 188 | reset(); |
| 189 | Object.unobserve(obj, observer.callback); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 190 | Object.getNotifier(obj).notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 191 | type: 'foo', |
| 192 | }); |
| 193 | Object.deliverChangeRecords(observer.callback); |
| 194 | observer.assertNotCalled(); |
| 195 | |
| 196 | // Multiple unobservation has no effect |
| 197 | reset(); |
| 198 | Object.unobserve(obj, observer.callback); |
| 199 | Object.unobserve(obj, observer.callback); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 200 | Object.getNotifier(obj).notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 201 | type: 'foo', |
| 202 | }); |
| 203 | Object.deliverChangeRecords(observer.callback); |
| 204 | observer.assertNotCalled(); |
| 205 | |
| 206 | // Re-observation works and only includes changeRecords after of call. |
| 207 | reset(); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 208 | Object.getNotifier(obj).notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 209 | type: 'foo', |
| 210 | }); |
| 211 | Object.observe(obj, observer.callback); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 212 | Object.getNotifier(obj).notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 213 | type: 'foo', |
| 214 | }); |
| 215 | records = undefined; |
| 216 | Object.deliverChangeRecords(observer.callback); |
| 217 | observer.assertRecordCount(1); |
| 218 | |
| 219 | // Observing a continuous stream of changes, while itermittantly unobserving. |
| 220 | reset(); |
| 221 | Object.observe(obj, observer.callback); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 222 | Object.getNotifier(obj).notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 223 | type: 'foo', |
| 224 | val: 1 |
| 225 | }); |
| 226 | |
| 227 | Object.unobserve(obj, observer.callback); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 228 | Object.getNotifier(obj).notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 229 | type: 'foo', |
| 230 | val: 2 |
| 231 | }); |
| 232 | |
| 233 | Object.observe(obj, observer.callback); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 234 | Object.getNotifier(obj).notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 235 | type: 'foo', |
| 236 | val: 3 |
| 237 | }); |
| 238 | |
| 239 | Object.unobserve(obj, observer.callback); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 240 | Object.getNotifier(obj).notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 241 | type: 'foo', |
| 242 | val: 4 |
| 243 | }); |
| 244 | |
| 245 | Object.observe(obj, observer.callback); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 246 | Object.getNotifier(obj).notify({ |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 247 | type: 'foo', |
| 248 | val: 5 |
| 249 | }); |
| 250 | |
| 251 | Object.unobserve(obj, observer.callback); |
| 252 | Object.deliverChangeRecords(observer.callback); |
| 253 | observer.assertCallbackRecords([ |
| 254 | { object: obj, type: 'foo', val: 1 }, |
| 255 | { object: obj, type: 'foo', val: 3 }, |
| 256 | { object: obj, type: 'foo', val: 5 } |
| 257 | ]); |
| 258 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 259 | // Observing multiple objects; records appear in order. |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 260 | reset(); |
| 261 | var obj2 = {}; |
| 262 | var obj3 = {} |
| 263 | Object.observe(obj, observer.callback); |
| 264 | Object.observe(obj3, observer.callback); |
| 265 | Object.observe(obj2, observer.callback); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 266 | Object.getNotifier(obj).notify({ |
| 267 | type: 'foo1', |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 268 | }); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 269 | Object.getNotifier(obj2).notify({ |
| 270 | type: 'foo2', |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 271 | }); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 272 | Object.getNotifier(obj3).notify({ |
| 273 | type: 'foo3', |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 274 | }); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 275 | Object.observe(obj3, observer.callback); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 276 | Object.deliverChangeRecords(observer.callback); |
| 277 | observer.assertCallbackRecords([ |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 278 | { object: obj, type: 'foo1' }, |
| 279 | { object: obj2, type: 'foo2' }, |
| 280 | { object: obj3, type: 'foo3' } |
| 281 | ]); |
| 282 | |
| 283 | // Observing named properties. |
| 284 | reset(); |
| 285 | var obj = {a: 1} |
| 286 | Object.observe(obj, observer.callback); |
| 287 | obj.a = 2; |
| 288 | obj["a"] = 3; |
| 289 | delete obj.a; |
| 290 | obj.a = 4; |
| 291 | obj.a = 4; // ignored |
| 292 | obj.a = 5; |
| 293 | Object.defineProperty(obj, "a", {value: 6}); |
| 294 | Object.defineProperty(obj, "a", {writable: false}); |
| 295 | obj.a = 7; // ignored |
| 296 | Object.defineProperty(obj, "a", {value: 8}); |
| 297 | Object.defineProperty(obj, "a", {value: 7, writable: true}); |
| 298 | Object.defineProperty(obj, "a", {get: function() {}}); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 299 | Object.defineProperty(obj, "a", {get: frozenFunction}); |
| 300 | Object.defineProperty(obj, "a", {get: frozenFunction}); // ignored |
| 301 | Object.defineProperty(obj, "a", {get: frozenFunction, set: frozenFunction}); |
| 302 | Object.defineProperty(obj, "a", {set: frozenFunction}); // ignored |
| 303 | Object.defineProperty(obj, "a", {get: undefined, set: frozenFunction}); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 304 | delete obj.a; |
| 305 | delete obj.a; |
| 306 | Object.defineProperty(obj, "a", {get: function() {}, configurable: true}); |
| 307 | Object.defineProperty(obj, "a", {value: 9, writable: true}); |
| 308 | obj.a = 10; |
| 309 | delete obj.a; |
| 310 | Object.defineProperty(obj, "a", {value: 11, configurable: true}); |
| 311 | Object.deliverChangeRecords(observer.callback); |
| 312 | observer.assertCallbackRecords([ |
| 313 | { object: obj, name: "a", type: "updated", oldValue: 1 }, |
| 314 | { object: obj, name: "a", type: "updated", oldValue: 2 }, |
| 315 | { object: obj, name: "a", type: "deleted", oldValue: 3 }, |
| 316 | { object: obj, name: "a", type: "new" }, |
| 317 | { object: obj, name: "a", type: "updated", oldValue: 4 }, |
| 318 | { object: obj, name: "a", type: "updated", oldValue: 5 }, |
| 319 | { object: obj, name: "a", type: "reconfigured", oldValue: 6 }, |
| 320 | { object: obj, name: "a", type: "updated", oldValue: 6 }, |
| 321 | { object: obj, name: "a", type: "reconfigured", oldValue: 8 }, |
| 322 | { object: obj, name: "a", type: "reconfigured", oldValue: 7 }, |
| 323 | { object: obj, name: "a", type: "reconfigured" }, |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 324 | { object: obj, name: "a", type: "reconfigured" }, |
| 325 | { object: obj, name: "a", type: "reconfigured" }, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 326 | { object: obj, name: "a", type: "deleted" }, |
| 327 | { object: obj, name: "a", type: "new" }, |
| 328 | { object: obj, name: "a", type: "reconfigured" }, |
| 329 | { object: obj, name: "a", type: "updated", oldValue: 9 }, |
| 330 | { object: obj, name: "a", type: "deleted", oldValue: 10 }, |
| 331 | { object: obj, name: "a", type: "new" }, |
| 332 | ]); |
| 333 | |
| 334 | // Observing indexed properties. |
| 335 | reset(); |
| 336 | var obj = {'1': 1} |
| 337 | Object.observe(obj, observer.callback); |
| 338 | obj[1] = 2; |
| 339 | obj[1] = 3; |
| 340 | delete obj[1]; |
| 341 | obj[1] = 4; |
| 342 | obj[1] = 4; // ignored |
| 343 | obj[1] = 5; |
| 344 | Object.defineProperty(obj, "1", {value: 6}); |
| 345 | Object.defineProperty(obj, "1", {writable: false}); |
| 346 | obj[1] = 7; // ignored |
| 347 | Object.defineProperty(obj, "1", {value: 8}); |
| 348 | Object.defineProperty(obj, "1", {value: 7, writable: true}); |
| 349 | Object.defineProperty(obj, "1", {get: function() {}}); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 350 | Object.defineProperty(obj, "1", {get: frozenFunction}); |
| 351 | Object.defineProperty(obj, "1", {get: frozenFunction}); // ignored |
| 352 | Object.defineProperty(obj, "1", {get: frozenFunction, set: frozenFunction}); |
| 353 | Object.defineProperty(obj, "1", {set: frozenFunction}); // ignored |
| 354 | Object.defineProperty(obj, "1", {get: undefined, set: frozenFunction}); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 355 | delete obj[1]; |
| 356 | delete obj[1]; |
| 357 | Object.defineProperty(obj, "1", {get: function() {}, configurable: true}); |
| 358 | Object.defineProperty(obj, "1", {value: 9, writable: true}); |
| 359 | obj[1] = 10; |
| 360 | delete obj[1]; |
| 361 | Object.defineProperty(obj, "1", {value: 11, configurable: true}); |
| 362 | Object.deliverChangeRecords(observer.callback); |
| 363 | observer.assertCallbackRecords([ |
| 364 | { object: obj, name: "1", type: "updated", oldValue: 1 }, |
| 365 | { object: obj, name: "1", type: "updated", oldValue: 2 }, |
| 366 | { object: obj, name: "1", type: "deleted", oldValue: 3 }, |
| 367 | { object: obj, name: "1", type: "new" }, |
| 368 | { object: obj, name: "1", type: "updated", oldValue: 4 }, |
| 369 | { object: obj, name: "1", type: "updated", oldValue: 5 }, |
| 370 | { object: obj, name: "1", type: "reconfigured", oldValue: 6 }, |
| 371 | { object: obj, name: "1", type: "updated", oldValue: 6 }, |
| 372 | { object: obj, name: "1", type: "reconfigured", oldValue: 8 }, |
| 373 | { object: obj, name: "1", type: "reconfigured", oldValue: 7 }, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 374 | { object: obj, name: "1", type: "reconfigured" }, |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 375 | { object: obj, name: "1", type: "reconfigured" }, |
| 376 | { object: obj, name: "1", type: "reconfigured" }, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 377 | { object: obj, name: "1", type: "deleted" }, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 378 | { object: obj, name: "1", type: "new" }, |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 379 | { object: obj, name: "1", type: "reconfigured" }, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 380 | { object: obj, name: "1", type: "updated", oldValue: 9 }, |
| 381 | { object: obj, name: "1", type: "deleted", oldValue: 10 }, |
| 382 | { object: obj, name: "1", type: "new" }, |
| 383 | ]); |
| 384 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 385 | |
| 386 | // Test all kinds of objects generically. |
| 387 | function TestObserveConfigurable(obj, prop) { |
| 388 | reset(); |
| 389 | obj[prop] = 1; |
| 390 | Object.observe(obj, observer.callback); |
| 391 | obj[prop] = 2; |
| 392 | obj[prop] = 3; |
| 393 | delete obj[prop]; |
| 394 | obj[prop] = 4; |
| 395 | obj[prop] = 4; // ignored |
| 396 | obj[prop] = 5; |
| 397 | Object.defineProperty(obj, prop, {value: 6}); |
| 398 | Object.defineProperty(obj, prop, {writable: false}); |
| 399 | obj[prop] = 7; // ignored |
| 400 | Object.defineProperty(obj, prop, {value: 8}); |
| 401 | Object.defineProperty(obj, prop, {value: 7, writable: true}); |
| 402 | Object.defineProperty(obj, prop, {get: function() {}}); |
| 403 | Object.defineProperty(obj, prop, {get: frozenFunction}); |
| 404 | Object.defineProperty(obj, prop, {get: frozenFunction}); // ignored |
| 405 | Object.defineProperty(obj, prop, {get: frozenFunction, set: frozenFunction}); |
| 406 | Object.defineProperty(obj, prop, {set: frozenFunction}); // ignored |
| 407 | Object.defineProperty(obj, prop, {get: undefined, set: frozenFunction}); |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 408 | obj.__defineSetter__(prop, frozenFunction); // ignored |
| 409 | obj.__defineSetter__(prop, function() {}); |
| 410 | obj.__defineGetter__(prop, function() {}); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 411 | delete obj[prop]; |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 412 | delete obj[prop]; // ignored |
| 413 | obj.__defineGetter__(prop, function() {}); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 414 | delete obj[prop]; |
| 415 | Object.defineProperty(obj, prop, {get: function() {}, configurable: true}); |
| 416 | Object.defineProperty(obj, prop, {value: 9, writable: true}); |
| 417 | obj[prop] = 10; |
| 418 | delete obj[prop]; |
| 419 | Object.defineProperty(obj, prop, {value: 11, configurable: true}); |
| 420 | Object.deliverChangeRecords(observer.callback); |
| 421 | observer.assertCallbackRecords([ |
| 422 | { object: obj, name: prop, type: "updated", oldValue: 1 }, |
| 423 | { object: obj, name: prop, type: "updated", oldValue: 2 }, |
| 424 | { object: obj, name: prop, type: "deleted", oldValue: 3 }, |
| 425 | { object: obj, name: prop, type: "new" }, |
| 426 | { object: obj, name: prop, type: "updated", oldValue: 4 }, |
| 427 | { object: obj, name: prop, type: "updated", oldValue: 5 }, |
| 428 | { object: obj, name: prop, type: "reconfigured", oldValue: 6 }, |
| 429 | { object: obj, name: prop, type: "updated", oldValue: 6 }, |
| 430 | { object: obj, name: prop, type: "reconfigured", oldValue: 8 }, |
| 431 | { object: obj, name: prop, type: "reconfigured", oldValue: 7 }, |
| 432 | { object: obj, name: prop, type: "reconfigured" }, |
| 433 | { object: obj, name: prop, type: "reconfigured" }, |
| 434 | { object: obj, name: prop, type: "reconfigured" }, |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 435 | { object: obj, name: prop, type: "reconfigured" }, |
| 436 | { object: obj, name: prop, type: "reconfigured" }, |
| 437 | { object: obj, name: prop, type: "deleted" }, |
| 438 | { object: obj, name: prop, type: "new" }, |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 439 | { object: obj, name: prop, type: "deleted" }, |
| 440 | { object: obj, name: prop, type: "new" }, |
| 441 | { object: obj, name: prop, type: "reconfigured" }, |
| 442 | { object: obj, name: prop, type: "updated", oldValue: 9 }, |
| 443 | { object: obj, name: prop, type: "deleted", oldValue: 10 }, |
| 444 | { object: obj, name: prop, type: "new" }, |
| 445 | ]); |
| 446 | Object.unobserve(obj, observer.callback); |
| 447 | delete obj[prop]; |
| 448 | } |
| 449 | |
| 450 | function TestObserveNonConfigurable(obj, prop) { |
| 451 | reset(); |
| 452 | obj[prop] = 1; |
| 453 | Object.observe(obj, observer.callback); |
| 454 | obj[prop] = 4; |
| 455 | obj[prop] = 4; // ignored |
| 456 | obj[prop] = 5; |
| 457 | Object.defineProperty(obj, prop, {value: 6}); |
| 458 | Object.defineProperty(obj, prop, {value: 6}); // ignored |
| 459 | Object.defineProperty(obj, prop, {value: 7}); |
| 460 | Object.defineProperty(obj, prop, {enumerable: true}); // ignored |
| 461 | Object.defineProperty(obj, prop, {writable: false}); |
| 462 | obj[prop] = 7; // ignored |
| 463 | Object.defineProperty(obj, prop, {get: function() {}}); // ignored |
| 464 | Object.deliverChangeRecords(observer.callback); |
| 465 | observer.assertCallbackRecords([ |
| 466 | { object: obj, name: prop, type: "updated", oldValue: 1 }, |
| 467 | { object: obj, name: prop, type: "updated", oldValue: 4 }, |
| 468 | { object: obj, name: prop, type: "updated", oldValue: 5 }, |
| 469 | { object: obj, name: prop, type: "updated", oldValue: 6 }, |
| 470 | { object: obj, name: prop, type: "reconfigured", oldValue: 7 }, |
| 471 | ]); |
| 472 | Object.unobserve(obj, observer.callback); |
| 473 | } |
| 474 | |
| 475 | function createProxy(create, x) { |
| 476 | var handler = { |
| 477 | getPropertyDescriptor: function(k) { |
danno@chromium.org | 1f34ad3 | 2012-11-26 14:53:56 +0000 | [diff] [blame] | 478 | for (var o = this.target; o; o = Object.getPrototypeOf(o)) { |
| 479 | var desc = Object.getOwnPropertyDescriptor(o, k); |
| 480 | if (desc) return desc; |
| 481 | } |
| 482 | return undefined; |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 483 | }, |
| 484 | getOwnPropertyDescriptor: function(k) { |
| 485 | return Object.getOwnPropertyDescriptor(this.target, k); |
| 486 | }, |
| 487 | defineProperty: function(k, desc) { |
| 488 | var x = Object.defineProperty(this.target, k, desc); |
| 489 | Object.deliverChangeRecords(this.callback); |
| 490 | return x; |
| 491 | }, |
| 492 | delete: function(k) { |
| 493 | var x = delete this.target[k]; |
| 494 | Object.deliverChangeRecords(this.callback); |
| 495 | return x; |
| 496 | }, |
| 497 | getPropertyNames: function() { |
| 498 | return Object.getOwnPropertyNames(this.target); |
| 499 | }, |
| 500 | target: {isProxy: true}, |
| 501 | callback: function(changeRecords) { |
| 502 | print("callback", JSON.stringify(handler.proxy), JSON.stringify(got)); |
| 503 | for (var i in changeRecords) { |
| 504 | var got = changeRecords[i]; |
| 505 | var change = {object: handler.proxy, name: got.name, type: got.type}; |
| 506 | if ("oldValue" in got) change.oldValue = got.oldValue; |
| 507 | Object.getNotifier(handler.proxy).notify(change); |
| 508 | } |
| 509 | }, |
| 510 | }; |
| 511 | Object.observe(handler.target, handler.callback); |
| 512 | return handler.proxy = create(handler, x); |
| 513 | } |
| 514 | |
| 515 | var objects = [ |
| 516 | {}, |
| 517 | [], |
| 518 | this, // global object |
| 519 | function(){}, |
| 520 | (function(){ return arguments })(), |
| 521 | (function(){ "use strict"; return arguments })(), |
| 522 | Object(1), Object(true), Object("bla"), |
| 523 | new Date(), |
| 524 | Object, Function, Date, RegExp, |
| 525 | new Set, new Map, new WeakMap, |
| 526 | new ArrayBuffer(10), new Int32Array(5), |
| 527 | createProxy(Proxy.create, null), |
| 528 | createProxy(Proxy.createFunction, function(){}), |
| 529 | ]; |
| 530 | var properties = ["a", "1", 1, "length", "prototype"]; |
| 531 | |
| 532 | // Cases that yield non-standard results. |
| 533 | // TODO(observe): ...or don't work yet. |
| 534 | function blacklisted(obj, prop) { |
| 535 | return (obj instanceof Int32Array && prop == 1) || |
| 536 | (obj instanceof Int32Array && prop === "length") || |
| 537 | (obj instanceof ArrayBuffer && prop == 1) || |
| 538 | // TODO(observe): oldValue when reconfiguring array length |
| 539 | (obj instanceof Array && prop === "length") || |
| 540 | // TODO(observe): prototype property on functions |
| 541 | (obj instanceof Function && prop === "prototype") || |
| 542 | // TODO(observe): global object |
| 543 | obj === this; |
| 544 | } |
| 545 | |
| 546 | for (var i in objects) for (var j in properties) { |
| 547 | var obj = objects[i]; |
| 548 | var prop = properties[j]; |
| 549 | if (blacklisted(obj, prop)) continue; |
| 550 | var desc = Object.getOwnPropertyDescriptor(obj, prop); |
| 551 | print("***", typeof obj, JSON.stringify(obj), prop); |
| 552 | if (!desc || desc.configurable) |
| 553 | TestObserveConfigurable(obj, prop); |
| 554 | else if (desc.writable) |
| 555 | TestObserveNonConfigurable(obj, prop); |
| 556 | } |
| 557 | |
| 558 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 559 | // Observing array length (including truncation) |
| 560 | reset(); |
| 561 | var arr = ['a', 'b', 'c', 'd']; |
| 562 | var arr2 = ['alpha', 'beta']; |
| 563 | var arr3 = ['hello']; |
| 564 | arr3[2] = 'goodbye'; |
| 565 | arr3.length = 6; |
| 566 | // TODO(adamk): Enable this test case when it can run in a reasonable |
| 567 | // amount of time. |
| 568 | //var slow_arr = new Array(1000000000); |
| 569 | //slow_arr[500000000] = 'hello'; |
| 570 | Object.defineProperty(arr, '0', {configurable: false}); |
| 571 | Object.defineProperty(arr, '2', {get: function(){}}); |
| 572 | Object.defineProperty(arr2, '0', {get: function(){}, configurable: false}); |
| 573 | Object.observe(arr, observer.callback); |
| 574 | Object.observe(arr2, observer.callback); |
| 575 | Object.observe(arr3, observer.callback); |
| 576 | arr.length = 2; |
| 577 | arr.length = 0; |
| 578 | arr.length = 10; |
| 579 | arr2.length = 0; |
| 580 | arr2.length = 1; // no change expected |
| 581 | arr3.length = 0; |
| 582 | Object.defineProperty(arr3, 'length', {value: 5}); |
| 583 | Object.defineProperty(arr3, 'length', {value: 10, writable: false}); |
| 584 | Object.deliverChangeRecords(observer.callback); |
| 585 | observer.assertCallbackRecords([ |
| 586 | { object: arr, name: '3', type: 'deleted', oldValue: 'd' }, |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame^] | 587 | { object: arr, name: '2', type: 'deleted' }, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 588 | { object: arr, name: 'length', type: 'updated', oldValue: 4 }, |
| 589 | { object: arr, name: '1', type: 'deleted', oldValue: 'b' }, |
| 590 | { object: arr, name: 'length', type: 'updated', oldValue: 2 }, |
| 591 | { object: arr, name: 'length', type: 'updated', oldValue: 1 }, |
| 592 | { object: arr2, name: '1', type: 'deleted', oldValue: 'beta' }, |
| 593 | { object: arr2, name: 'length', type: 'updated', oldValue: 2 }, |
| 594 | { object: arr3, name: '2', type: 'deleted', oldValue: 'goodbye' }, |
| 595 | { object: arr3, name: '0', type: 'deleted', oldValue: 'hello' }, |
| 596 | { object: arr3, name: 'length', type: 'updated', oldValue: 6 }, |
| 597 | { object: arr3, name: 'length', type: 'updated', oldValue: 0 }, |
| 598 | { object: arr3, name: 'length', type: 'updated', oldValue: 5 }, |
| 599 | // TODO(adamk): This record should be merged with the above |
| 600 | { object: arr3, name: 'length', type: 'reconfigured' }, |
| 601 | ]); |
| 602 | |
| 603 | // Assignments in loops (checking different IC states). |
| 604 | reset(); |
| 605 | var obj = {}; |
| 606 | Object.observe(obj, observer.callback); |
| 607 | for (var i = 0; i < 5; i++) { |
| 608 | obj["a" + i] = i; |
| 609 | } |
| 610 | Object.deliverChangeRecords(observer.callback); |
| 611 | observer.assertCallbackRecords([ |
| 612 | { object: obj, name: "a0", type: "new" }, |
| 613 | { object: obj, name: "a1", type: "new" }, |
| 614 | { object: obj, name: "a2", type: "new" }, |
| 615 | { object: obj, name: "a3", type: "new" }, |
| 616 | { object: obj, name: "a4", type: "new" }, |
| 617 | ]); |
| 618 | |
| 619 | reset(); |
| 620 | var obj = {}; |
| 621 | Object.observe(obj, observer.callback); |
| 622 | for (var i = 0; i < 5; i++) { |
| 623 | obj[i] = i; |
| 624 | } |
| 625 | Object.deliverChangeRecords(observer.callback); |
| 626 | observer.assertCallbackRecords([ |
| 627 | { object: obj, name: "0", type: "new" }, |
| 628 | { object: obj, name: "1", type: "new" }, |
| 629 | { object: obj, name: "2", type: "new" }, |
| 630 | { object: obj, name: "3", type: "new" }, |
| 631 | { object: obj, name: "4", type: "new" }, |
| 632 | ]); |
| 633 | |
| 634 | // Adding elements past the end of an array should notify on length |
| 635 | reset(); |
| 636 | var arr = [1, 2, 3]; |
| 637 | Object.observe(arr, observer.callback); |
| 638 | arr[3] = 10; |
| 639 | arr[100] = 20; |
| 640 | Object.defineProperty(arr, '200', {value: 7}); |
| 641 | Object.defineProperty(arr, '400', {get: function(){}}); |
| 642 | arr[50] = 30; // no length change expected |
| 643 | Object.deliverChangeRecords(observer.callback); |
| 644 | observer.assertCallbackRecords([ |
| 645 | { object: arr, name: '3', type: 'new' }, |
| 646 | { object: arr, name: 'length', type: 'updated', oldValue: 3 }, |
| 647 | { object: arr, name: '100', type: 'new' }, |
| 648 | { object: arr, name: 'length', type: 'updated', oldValue: 4 }, |
| 649 | { object: arr, name: '200', type: 'new' }, |
| 650 | { object: arr, name: 'length', type: 'updated', oldValue: 101 }, |
| 651 | { object: arr, name: '400', type: 'new' }, |
| 652 | { object: arr, name: 'length', type: 'updated', oldValue: 201 }, |
| 653 | { object: arr, name: '50', type: 'new' }, |
| 654 | ]); |
| 655 | |
| 656 | // Tests for array methods, first on arrays and then on plain objects |
| 657 | // |
| 658 | // === ARRAYS === |
| 659 | // |
| 660 | // Push |
| 661 | reset(); |
| 662 | var array = [1, 2]; |
| 663 | Object.observe(array, observer.callback); |
| 664 | array.push(3, 4); |
| 665 | Object.deliverChangeRecords(observer.callback); |
| 666 | observer.assertCallbackRecords([ |
| 667 | { object: array, name: '2', type: 'new' }, |
| 668 | { object: array, name: 'length', type: 'updated', oldValue: 2 }, |
| 669 | { object: array, name: '3', type: 'new' }, |
| 670 | { object: array, name: 'length', type: 'updated', oldValue: 3 }, |
| 671 | ]); |
| 672 | |
| 673 | // Pop |
| 674 | reset(); |
| 675 | var array = [1, 2]; |
| 676 | Object.observe(array, observer.callback); |
| 677 | array.pop(); |
| 678 | array.pop(); |
| 679 | Object.deliverChangeRecords(observer.callback); |
| 680 | observer.assertCallbackRecords([ |
| 681 | { object: array, name: '1', type: 'deleted', oldValue: 2 }, |
| 682 | { object: array, name: 'length', type: 'updated', oldValue: 2 }, |
| 683 | { object: array, name: '0', type: 'deleted', oldValue: 1 }, |
| 684 | { object: array, name: 'length', type: 'updated', oldValue: 1 }, |
| 685 | ]); |
| 686 | |
| 687 | // Shift |
| 688 | reset(); |
| 689 | var array = [1, 2]; |
| 690 | Object.observe(array, observer.callback); |
| 691 | array.shift(); |
| 692 | array.shift(); |
| 693 | Object.deliverChangeRecords(observer.callback); |
| 694 | observer.assertCallbackRecords([ |
| 695 | { object: array, name: '0', type: 'updated', oldValue: 1 }, |
| 696 | { object: array, name: '1', type: 'deleted', oldValue: 2 }, |
| 697 | { object: array, name: 'length', type: 'updated', oldValue: 2 }, |
| 698 | { object: array, name: '0', type: 'deleted', oldValue: 2 }, |
| 699 | { object: array, name: 'length', type: 'updated', oldValue: 1 }, |
| 700 | ]); |
| 701 | |
| 702 | // Unshift |
| 703 | reset(); |
| 704 | var array = [1, 2]; |
| 705 | Object.observe(array, observer.callback); |
| 706 | array.unshift(3, 4); |
| 707 | Object.deliverChangeRecords(observer.callback); |
| 708 | observer.assertCallbackRecords([ |
| 709 | { object: array, name: '3', type: 'new' }, |
| 710 | { object: array, name: 'length', type: 'updated', oldValue: 2 }, |
| 711 | { object: array, name: '2', type: 'new' }, |
| 712 | { object: array, name: '0', type: 'updated', oldValue: 1 }, |
| 713 | { object: array, name: '1', type: 'updated', oldValue: 2 }, |
| 714 | ]); |
| 715 | |
| 716 | // Splice |
| 717 | reset(); |
| 718 | var array = [1, 2, 3]; |
| 719 | Object.observe(array, observer.callback); |
| 720 | array.splice(1, 1, 4, 5); |
| 721 | Object.deliverChangeRecords(observer.callback); |
| 722 | observer.assertCallbackRecords([ |
| 723 | { object: array, name: '3', type: 'new' }, |
| 724 | { object: array, name: 'length', type: 'updated', oldValue: 3 }, |
| 725 | { object: array, name: '1', type: 'updated', oldValue: 2 }, |
| 726 | { object: array, name: '2', type: 'updated', oldValue: 3 }, |
| 727 | ]); |
| 728 | |
| 729 | // |
| 730 | // === PLAIN OBJECTS === |
| 731 | // |
| 732 | // Push |
| 733 | reset() |
| 734 | var array = {0: 1, 1: 2, length: 2} |
| 735 | Object.observe(array, observer.callback); |
| 736 | Array.prototype.push.call(array, 3, 4); |
| 737 | Object.deliverChangeRecords(observer.callback); |
| 738 | observer.assertCallbackRecords([ |
| 739 | { object: array, name: '2', type: 'new' }, |
| 740 | { object: array, name: '3', type: 'new' }, |
| 741 | { object: array, name: 'length', type: 'updated', oldValue: 2 }, |
| 742 | ]); |
| 743 | |
| 744 | // Pop |
| 745 | reset() |
| 746 | var array = {0: 1, 1: 2, length: 2}; |
| 747 | Object.observe(array, observer.callback); |
| 748 | Array.prototype.pop.call(array); |
| 749 | Array.prototype.pop.call(array); |
| 750 | Object.deliverChangeRecords(observer.callback); |
| 751 | observer.assertCallbackRecords([ |
| 752 | { object: array, name: '1', type: 'deleted', oldValue: 2 }, |
| 753 | { object: array, name: 'length', type: 'updated', oldValue: 2 }, |
| 754 | { object: array, name: '0', type: 'deleted', oldValue: 1 }, |
| 755 | { object: array, name: 'length', type: 'updated', oldValue: 1 }, |
| 756 | ]); |
| 757 | |
| 758 | // Shift |
| 759 | reset() |
| 760 | var array = {0: 1, 1: 2, length: 2}; |
| 761 | Object.observe(array, observer.callback); |
| 762 | Array.prototype.shift.call(array); |
| 763 | Array.prototype.shift.call(array); |
| 764 | Object.deliverChangeRecords(observer.callback); |
| 765 | observer.assertCallbackRecords([ |
| 766 | { object: array, name: '0', type: 'updated', oldValue: 1 }, |
| 767 | { object: array, name: '1', type: 'deleted', oldValue: 2 }, |
| 768 | { object: array, name: 'length', type: 'updated', oldValue: 2 }, |
| 769 | { object: array, name: '0', type: 'deleted', oldValue: 2 }, |
| 770 | { object: array, name: 'length', type: 'updated', oldValue: 1 }, |
| 771 | ]); |
| 772 | |
| 773 | // Unshift |
| 774 | reset() |
| 775 | var array = {0: 1, 1: 2, length: 2}; |
| 776 | Object.observe(array, observer.callback); |
| 777 | Array.prototype.unshift.call(array, 3, 4); |
| 778 | Object.deliverChangeRecords(observer.callback); |
| 779 | observer.assertCallbackRecords([ |
| 780 | { object: array, name: '3', type: 'new' }, |
| 781 | { object: array, name: '2', type: 'new' }, |
| 782 | { object: array, name: '0', type: 'updated', oldValue: 1 }, |
| 783 | { object: array, name: '1', type: 'updated', oldValue: 2 }, |
| 784 | { object: array, name: 'length', type: 'updated', oldValue: 2 }, |
| 785 | ]); |
| 786 | |
| 787 | // Splice |
| 788 | reset() |
| 789 | var array = {0: 1, 1: 2, 2: 3, length: 3}; |
| 790 | Object.observe(array, observer.callback); |
| 791 | Array.prototype.splice.call(array, 1, 1, 4, 5); |
| 792 | Object.deliverChangeRecords(observer.callback); |
| 793 | observer.assertCallbackRecords([ |
| 794 | { object: array, name: '3', type: 'new' }, |
| 795 | { object: array, name: '1', type: 'updated', oldValue: 2 }, |
| 796 | { object: array, name: '2', type: 'updated', oldValue: 3 }, |
| 797 | { object: array, name: 'length', type: 'updated', oldValue: 3 }, |
| 798 | ]); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 799 | |
| 800 | // Exercise StoreIC_ArrayLength |
| 801 | reset(); |
| 802 | var dummy = {}; |
| 803 | Object.observe(dummy, observer.callback); |
| 804 | Object.unobserve(dummy, observer.callback); |
| 805 | var array = [0]; |
| 806 | Object.observe(array, observer.callback); |
| 807 | array.splice(0, 1); |
| 808 | Object.deliverChangeRecords(observer.callback); |
| 809 | observer.assertCallbackRecords([ |
| 810 | { object: array, name: '0', type: 'deleted', oldValue: 0 }, |
| 811 | { object: array, name: 'length', type: 'updated', oldValue: 1}, |
| 812 | ]); |