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); }, | 113 assertThrows(function() { Object.observe("non-object", observer.callback); }, Ty
peError); |
114 TypeError); | |
115 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError); | 114 assertThrows(function() { Object.observe(obj, nonFunction); }, TypeError); |
116 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError); | 115 assertThrows(function() { Object.observe(obj, frozenFunction); }, TypeError); |
117 assertEquals(obj, Object.observe(obj, observer.callback, [1])); | 116 assertThrows(function() { Object.observe(obj, function() {}, 1); }, TypeError); |
118 assertEquals(obj, Object.observe(obj, observer.callback, [true])); | 117 assertThrows(function() { Object.observe(obj, function() {}, [undefined]); }, Ty
peError); |
119 assertEquals(obj, Object.observe(obj, observer.callback, ['foo', null])); | 118 assertThrows(function() { Object.observe(obj, function() {}, [1]); }, TypeError)
; |
120 assertEquals(obj, Object.observe(obj, observer.callback, [undefined])); | 119 assertThrows(function() { Object.observe(obj, function() {}, ['foo', null]); },
TypeError); |
121 assertEquals(obj, Object.observe(obj, observer.callback, | 120 assertEquals(obj, Object.observe(obj, observer.callback, ['foo', 'bar', 'baz']))
; |
122 ['foo', 'bar', 'baz'])); | |
123 assertEquals(obj, Object.observe(obj, observer.callback, [])); | 121 assertEquals(obj, Object.observe(obj, observer.callback, [])); |
124 assertEquals(obj, Object.observe(obj, observer.callback, undefined)); | 122 assertEquals(obj, Object.observe(obj, observer.callback, undefined)); |
125 assertEquals(obj, Object.observe(obj, observer.callback)); | 123 assertEquals(obj, Object.observe(obj, observer.callback)); |
126 | 124 |
127 // Object.unobserve | 125 // Object.unobserve |
128 assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError); | 126 assertThrows(function() { Object.unobserve(4, observer.callback); }, TypeError); |
129 assertThrows(function() { Object.unobserve(obj, nonFunction); }, TypeError); | 127 assertThrows(function() { Object.unobserve(obj, nonFunction); }, TypeError); |
130 assertEquals(obj, Object.unobserve(obj, observer.callback)); | 128 assertEquals(obj, Object.unobserve(obj, observer.callback)); |
131 | 129 |
132 | 130 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 type: 'deleted', | 195 type: 'deleted', |
198 name: 'bar', | 196 name: 'bar', |
199 expando2: 'str' | 197 expando2: 'str' |
200 }); | 198 }); |
201 Object.deliverChangeRecords(observer.callback); | 199 Object.deliverChangeRecords(observer.callback); |
202 observer.assertCallbackRecords([ | 200 observer.assertCallbackRecords([ |
203 { object: obj, name: 'foo', type: 'updated', expando: 1 }, | 201 { object: obj, name: 'foo', type: 'updated', expando: 1 }, |
204 { object: obj, name: 'bar', type: 'deleted', expando2: 'str' } | 202 { object: obj, name: 'bar', type: 'deleted', expando2: 'str' } |
205 ]); | 203 ]); |
206 | 204 |
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 ]); | |
226 | 205 |
227 // No delivery takes place if no records are pending | 206 // No delivery takes place if no records are pending |
228 reset(); | 207 reset(); |
229 Object.deliverChangeRecords(observer.callback); | 208 Object.deliverChangeRecords(observer.callback); |
230 observer.assertNotCalled(); | 209 observer.assertNotCalled(); |
231 | 210 |
232 | 211 |
233 // Multiple observation has no effect. | 212 // Multiple observation has no effect. |
234 reset(); | 213 reset(); |
235 Object.observe(obj, observer.callback); | 214 Object.observe(obj, observer.callback); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
321 Object.unobserve(obj, observer.callback); | 300 Object.unobserve(obj, observer.callback); |
322 Object.deliverChangeRecords(observer.callback); | 301 Object.deliverChangeRecords(observer.callback); |
323 observer.assertCallbackRecords([ | 302 observer.assertCallbackRecords([ |
324 { object: obj, type: 'updated', val: 1 }, | 303 { object: obj, type: 'updated', val: 1 }, |
325 { object: obj, type: 'updated', val: 3 }, | 304 { object: obj, type: 'updated', val: 3 }, |
326 { object: obj, type: 'updated', val: 5 } | 305 { object: obj, type: 'updated', val: 5 } |
327 ]); | 306 ]); |
328 | 307 |
329 // Accept | 308 // Accept |
330 reset(); | 309 reset(); |
331 Object.observe(obj, observer.callback, ['somethingElse']); | 310 Object.observe(obj, observer.callback, []); |
332 Object.getNotifier(obj).notify({ | 311 Object.getNotifier(obj).notify({ |
333 type: 'new' | 312 type: 'new' |
334 }); | 313 }); |
335 Object.getNotifier(obj).notify({ | 314 Object.getNotifier(obj).notify({ |
336 type: 'updated' | 315 type: 'updated' |
337 }); | 316 }); |
338 Object.getNotifier(obj).notify({ | 317 Object.getNotifier(obj).notify({ |
339 type: 'deleted' | 318 type: 'deleted' |
340 }); | 319 }); |
341 Object.getNotifier(obj).notify({ | 320 Object.getNotifier(obj).notify({ |
(...skipping 905 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1247 assertEquals(3, array[2]); | 1226 assertEquals(3, array[2]); |
1248 Object.deliverChangeRecords(observer.callback); | 1227 Object.deliverChangeRecords(observer.callback); |
1249 observer.assertCallbackRecords([ | 1228 observer.assertCallbackRecords([ |
1250 { object: array, name: '1', type: 'updated', oldValue: 2 }, | 1229 { object: array, name: '1', type: 'updated', oldValue: 2 }, |
1251 { object: array, name: '0', type: 'updated', oldValue: 3 }, | 1230 { object: array, name: '0', type: 'updated', oldValue: 3 }, |
1252 { object: array, name: '2', type: 'updated', oldValue: 1 }, | 1231 { object: array, name: '2', type: 'updated', oldValue: 1 }, |
1253 { object: array, name: '1', type: 'updated', oldValue: 3 }, | 1232 { object: array, name: '1', type: 'updated', oldValue: 3 }, |
1254 { object: array, name: '0', type: 'updated', oldValue: 2 }, | 1233 { object: array, name: '0', type: 'updated', oldValue: 2 }, |
1255 ]); | 1234 ]); |
1256 | 1235 |
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 | |
1326 // | 1236 // |
1327 // === PLAIN OBJECTS === | 1237 // === PLAIN OBJECTS === |
1328 // | 1238 // |
1329 // Push | 1239 // Push |
1330 reset() | 1240 reset() |
1331 var array = {0: 1, 1: 2, length: 2} | 1241 var array = {0: 1, 1: 2, length: 2} |
1332 Object.observe(array, observer.callback); | 1242 Object.observe(array, observer.callback); |
1333 Array.prototype.push.call(array, 3, 4); | 1243 Array.prototype.push.call(array, 3, 4); |
1334 Object.deliverChangeRecords(observer.callback); | 1244 Object.deliverChangeRecords(observer.callback); |
1335 observer.assertCallbackRecords([ | 1245 observer.assertCallbackRecords([ |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1615 for (var n1 = 0; n1 < 3; ++n1) | 1525 for (var n1 = 0; n1 < 3; ++n1) |
1616 for (var n2 = 0; n2 < 3; ++n2) | 1526 for (var n2 = 0; n2 < 3; ++n2) |
1617 for (var i in mutation) | 1527 for (var i in mutation) |
1618 TestFastElementsLength(mutation[i], b1 != 0, b2 != 0, 20*n1, 20*n2); | 1528 TestFastElementsLength(mutation[i], b1 != 0, b2 != 0, 20*n1, 20*n2); |
1619 | 1529 |
1620 for (var b1 = 0; b1 < 2; ++b1) | 1530 for (var b1 = 0; b1 < 2; ++b1) |
1621 for (var b2 = 0; b2 < 2; ++b2) | 1531 for (var b2 = 0; b2 < 2; ++b2) |
1622 for (var n = 0; n < 3; ++n) | 1532 for (var n = 0; n < 3; ++n) |
1623 for (var i in mutationByIncr) | 1533 for (var i in mutationByIncr) |
1624 TestFastElementsLength(mutationByIncr[i], b1 != 0, b2 != 0, 7*n, 7*n+1); | 1534 TestFastElementsLength(mutationByIncr[i], b1 != 0, b2 != 0, 7*n, 7*n+1); |
OLD | NEW |