| 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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 Object.defineProperty(changeRecordWithAccessor, 'name', { | 103 Object.defineProperty(changeRecordWithAccessor, 'name', { |
| 104 get: function() { | 104 get: function() { |
| 105 recordCreated = true; | 105 recordCreated = true; |
| 106 return "bar"; | 106 return "bar"; |
| 107 }, | 107 }, |
| 108 enumerable: true | 108 enumerable: true |
| 109 }) | 109 }) |
| 110 | 110 |
| 111 | 111 |
| 112 // Object.observe | 112 // Object.observe |
| 113 assertThrows(function() { Object.observe("non-object", observer.callback); }, Ty
peError); | 113 assertThrows(function() { Object.observe("non-object", observer.callback); }, |
| 114 TypeError); |
| 114 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError); | 115 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError); |
| 115 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError); | 116 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError); |
| 116 assertThrows(function() { Object.observe(obj, function() {}, 1); }, TypeError); | 117 assertEquals(obj, Object.observe(obj, observer.callback, [1])); |
| 117 assertThrows(function() { Object.observe(obj, function() {}, [undefined]); }, Ty
peError); | 118 assertEquals(obj, Object.observe(obj, observer.callback, [true])); |
| 118 assertThrows(function() { Object.observe(obj, function() {}, [1]); }, TypeError)
; | 119 assertEquals(obj, Object.observe(obj, observer.callback, ['foo', null])); |
| 119 assertThrows(function() { Object.observe(obj, function() {}, ['foo', null]); },
TypeError); | 120 assertEquals(obj, Object.observe(obj, observer.callback, [undefined])); |
| 120 assertEquals(obj, Object.observe(obj, observer.callback, ['foo', 'bar', 'baz']))
; | 121 assertEquals(obj, Object.observe(obj, observer.callback, |
| 122 ['foo', 'bar', 'baz'])); |
| 121 assertEquals(obj, Object.observe(obj, observer.callback, [])); | 123 assertEquals(obj, Object.observe(obj, observer.callback, [])); |
| 122 assertEquals(obj, Object.observe(obj, observer.callback, undefined)); | 124 assertEquals(obj, Object.observe(obj, observer.callback, undefined)); |
| 123 assertEquals(obj, Object.observe(obj, observer.callback)); | 125 assertEquals(obj, Object.observe(obj, observer.callback)); |
| 124 | 126 |
| 125 // Object.unobserve | 127 // Object.unobserve |
| 126 assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError); | 128 assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError); |
| 127 assertThrows(function() { Object.unobserve(obj, nonFunction); }, TypeError); | 129 assertThrows(function() { Object.unobserve(obj, nonFunction); }, TypeError); |
| 128 assertEquals(obj, Object.unobserve(obj, observer.callback)); | 130 assertEquals(obj, Object.unobserve(obj, observer.callback)); |
| 129 | 131 |
| 130 | 132 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 type: 'deleted', | 197 type: 'deleted', |
| 196 name: 'bar', | 198 name: 'bar', |
| 197 expando2: 'str' | 199 expando2: 'str' |
| 198 }); | 200 }); |
| 199 Object.deliverChangeRecords(observer.callback); | 201 Object.deliverChangeRecords(observer.callback); |
| 200 observer.assertCallbackRecords([ | 202 observer.assertCallbackRecords([ |
| 201 { object: obj, name: 'foo', type: 'updated', expando: 1 }, | 203 { object: obj, name: 'foo', type: 'updated', expando: 1 }, |
| 202 { object: obj, name: 'bar', type: 'deleted', expando2: 'str' } | 204 { object: obj, name: 'bar', type: 'deleted', expando2: 'str' } |
| 203 ]); | 205 ]); |
| 204 | 206 |
| 207 // Non-string accept values are coerced to strings |
| 208 reset(); |
| 209 Object.observe(obj, observer.callback, [true, 1, null, undefined]); |
| 210 notifier = Object.getNotifier(obj); |
| 211 notifier.notify({ type: 'true' }); |
| 212 notifier.notify({ type: 'false' }); |
| 213 notifier.notify({ type: '1' }); |
| 214 notifier.notify({ type: '-1' }); |
| 215 notifier.notify({ type: 'null' }); |
| 216 notifier.notify({ type: 'nill' }); |
| 217 notifier.notify({ type: 'undefined' }); |
| 218 notifier.notify({ type: 'defined' }); |
| 219 Object.deliverChangeRecords(observer.callback); |
| 220 observer.assertCallbackRecords([ |
| 221 { object: obj, type: 'true' }, |
| 222 { object: obj, type: '1' }, |
| 223 { object: obj, type: 'null' }, |
| 224 { object: obj, type: 'undefined' } |
| 225 ]); |
| 205 | 226 |
| 206 // No delivery takes place if no records are pending | 227 // No delivery takes place if no records are pending |
| 207 reset(); | 228 reset(); |
| 208 Object.deliverChangeRecords(observer.callback); | 229 Object.deliverChangeRecords(observer.callback); |
| 209 observer.assertNotCalled(); | 230 observer.assertNotCalled(); |
| 210 | 231 |
| 211 | 232 |
| 212 // Multiple observation has no effect. | 233 // Multiple observation has no effect. |
| 213 reset(); | 234 reset(); |
| 214 Object.observe(obj, observer.callback); | 235 Object.observe(obj, observer.callback); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 Object.unobserve(obj, observer.callback); | 321 Object.unobserve(obj, observer.callback); |
| 301 Object.deliverChangeRecords(observer.callback); | 322 Object.deliverChangeRecords(observer.callback); |
| 302 observer.assertCallbackRecords([ | 323 observer.assertCallbackRecords([ |
| 303 { object: obj, type: 'updated', val: 1 }, | 324 { object: obj, type: 'updated', val: 1 }, |
| 304 { object: obj, type: 'updated', val: 3 }, | 325 { object: obj, type: 'updated', val: 3 }, |
| 305 { object: obj, type: 'updated', val: 5 } | 326 { object: obj, type: 'updated', val: 5 } |
| 306 ]); | 327 ]); |
| 307 | 328 |
| 308 // Accept | 329 // Accept |
| 309 reset(); | 330 reset(); |
| 310 Object.observe(obj, observer.callback, []); | 331 Object.observe(obj, observer.callback, ['somethingElse']); |
| 311 Object.getNotifier(obj).notify({ | 332 Object.getNotifier(obj).notify({ |
| 312 type: 'new' | 333 type: 'new' |
| 313 }); | 334 }); |
| 314 Object.getNotifier(obj).notify({ | 335 Object.getNotifier(obj).notify({ |
| 315 type: 'updated' | 336 type: 'updated' |
| 316 }); | 337 }); |
| 317 Object.getNotifier(obj).notify({ | 338 Object.getNotifier(obj).notify({ |
| 318 type: 'deleted' | 339 type: 'deleted' |
| 319 }); | 340 }); |
| 320 Object.getNotifier(obj).notify({ | 341 Object.getNotifier(obj).notify({ |
| (...skipping 905 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1226 assertEquals(3, array[2]); | 1247 assertEquals(3, array[2]); |
| 1227 Object.deliverChangeRecords(observer.callback); | 1248 Object.deliverChangeRecords(observer.callback); |
| 1228 observer.assertCallbackRecords([ | 1249 observer.assertCallbackRecords([ |
| 1229 { object: array, name: '1', type: 'updated', oldValue: 2 }, | 1250 { object: array, name: '1', type: 'updated', oldValue: 2 }, |
| 1230 { object: array, name: '0', type: 'updated', oldValue: 3 }, | 1251 { object: array, name: '0', type: 'updated', oldValue: 3 }, |
| 1231 { object: array, name: '2', type: 'updated', oldValue: 1 }, | 1252 { object: array, name: '2', type: 'updated', oldValue: 1 }, |
| 1232 { object: array, name: '1', type: 'updated', oldValue: 3 }, | 1253 { object: array, name: '1', type: 'updated', oldValue: 3 }, |
| 1233 { object: array, name: '0', type: 'updated', oldValue: 2 }, | 1254 { object: array, name: '0', type: 'updated', oldValue: 2 }, |
| 1234 ]); | 1255 ]); |
| 1235 | 1256 |
| 1257 // Splice emitted after Array mutation methods |
| 1258 function MockArray(initial, observer) { |
| 1259 for (var i = 0; i < initial.length; i++) |
| 1260 this[i] = initial[i]; |
| 1261 |
| 1262 this.length_ = initial.length; |
| 1263 this.observer = observer; |
| 1264 } |
| 1265 MockArray.prototype = { |
| 1266 set length(length) { |
| 1267 Object.getNotifier(this).notify({ type: 'lengthChange' }); |
| 1268 this.length_ = length; |
| 1269 Object.observe(this, this.observer.callback, ['splice']); |
| 1270 }, |
| 1271 get length() { |
| 1272 return this.length_; |
| 1273 } |
| 1274 } |
| 1275 |
| 1276 reset(); |
| 1277 var array = new MockArray([], observer); |
| 1278 Object.observe(array, observer.callback, ['lengthChange']); |
| 1279 Array.prototype.push.call(array, 1); |
| 1280 Object.deliverChangeRecords(observer.callback); |
| 1281 observer.assertCallbackRecords([ |
| 1282 { object: array, type: 'lengthChange' }, |
| 1283 { object: array, type: 'splice', index: 0, removed: [], addedCount: 1 }, |
| 1284 ]); |
| 1285 |
| 1286 reset(); |
| 1287 var array = new MockArray([1], observer); |
| 1288 Object.observe(array, observer.callback, ['lengthChange']); |
| 1289 Array.prototype.pop.call(array); |
| 1290 Object.deliverChangeRecords(observer.callback); |
| 1291 observer.assertCallbackRecords([ |
| 1292 { object: array, type: 'lengthChange' }, |
| 1293 { object: array, type: 'splice', index: 0, removed: [1], addedCount: 0 }, |
| 1294 ]); |
| 1295 |
| 1296 reset(); |
| 1297 var array = new MockArray([1], observer); |
| 1298 Object.observe(array, observer.callback, ['lengthChange']); |
| 1299 Array.prototype.shift.call(array); |
| 1300 Object.deliverChangeRecords(observer.callback); |
| 1301 observer.assertCallbackRecords([ |
| 1302 { object: array, type: 'lengthChange' }, |
| 1303 { object: array, type: 'splice', index: 0, removed: [1], addedCount: 0 }, |
| 1304 ]); |
| 1305 |
| 1306 reset(); |
| 1307 var array = new MockArray([], observer); |
| 1308 Object.observe(array, observer.callback, ['lengthChange']); |
| 1309 Array.prototype.unshift.call(array, 1); |
| 1310 Object.deliverChangeRecords(observer.callback); |
| 1311 observer.assertCallbackRecords([ |
| 1312 { object: array, type: 'lengthChange' }, |
| 1313 { object: array, type: 'splice', index: 0, removed: [], addedCount: 1 }, |
| 1314 ]); |
| 1315 |
| 1316 reset(); |
| 1317 var array = new MockArray([0, 1, 2], observer); |
| 1318 Object.observe(array, observer.callback, ['lengthChange']); |
| 1319 Array.prototype.splice.call(array, 1, 1); |
| 1320 Object.deliverChangeRecords(observer.callback); |
| 1321 observer.assertCallbackRecords([ |
| 1322 { object: array, type: 'lengthChange' }, |
| 1323 { object: array, type: 'splice', index: 1, removed: [1], addedCount: 0 }, |
| 1324 ]); |
| 1325 |
| 1236 // | 1326 // |
| 1237 // === PLAIN OBJECTS === | 1327 // === PLAIN OBJECTS === |
| 1238 // | 1328 // |
| 1239 // Push | 1329 // Push |
| 1240 reset() | 1330 reset() |
| 1241 var array = {0: 1, 1: 2, length: 2} | 1331 var array = {0: 1, 1: 2, length: 2} |
| 1242 Object.observe(array, observer.callback); | 1332 Object.observe(array, observer.callback); |
| 1243 Array.prototype.push.call(array, 3, 4); | 1333 Array.prototype.push.call(array, 3, 4); |
| 1244 Object.deliverChangeRecords(observer.callback); | 1334 Object.deliverChangeRecords(observer.callback); |
| 1245 observer.assertCallbackRecords([ | 1335 observer.assertCallbackRecords([ |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1525 for (var n1 = 0; n1 < 3; ++n1) | 1615 for (var n1 = 0; n1 < 3; ++n1) |
| 1526 for (var n2 = 0; n2 < 3; ++n2) | 1616 for (var n2 = 0; n2 < 3; ++n2) |
| 1527 for (var i in mutation) | 1617 for (var i in mutation) |
| 1528 TestFastElementsLength(mutation[i], b1 != 0, b2 != 0, 20*n1, 20*n2); | 1618 TestFastElementsLength(mutation[i], b1 != 0, b2 != 0, 20*n1, 20*n2); |
| 1529 | 1619 |
| 1530 for (var b1 = 0; b1 < 2; ++b1) | 1620 for (var b1 = 0; b1 < 2; ++b1) |
| 1531 for (var b2 = 0; b2 < 2; ++b2) | 1621 for (var b2 = 0; b2 < 2; ++b2) |
| 1532 for (var n = 0; n < 3; ++n) | 1622 for (var n = 0; n < 3; ++n) |
| 1533 for (var i in mutationByIncr) | 1623 for (var i in mutationByIncr) |
| 1534 TestFastElementsLength(mutationByIncr[i], b1 != 0, b2 != 0, 7*n, 7*n+1); | 1624 TestFastElementsLength(mutationByIncr[i], b1 != 0, b2 != 0, 7*n, 7*n+1); |
| OLD | NEW |