| 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 14 matching lines...) Expand all Loading... |
| 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-observation --harmony-proxies --harmony-collections | 28 // Flags: --harmony-observation --harmony-proxies --harmony-collections |
| 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 stringifyNoThrow(arg) { |
| 36 try { |
| 37 return JSON.stringify(arg); |
| 38 } catch (e) { |
| 39 return '{<circular reference>}'; |
| 40 } |
| 41 } |
| 42 |
| 35 function createObserver() { | 43 function createObserver() { |
| 36 "use strict"; // So that |this| in callback can be undefined. | 44 "use strict"; // So that |this| in callback can be undefined. |
| 37 | 45 |
| 38 var observer = { | 46 var observer = { |
| 39 records: undefined, | 47 records: undefined, |
| 40 callbackCount: 0, | 48 callbackCount: 0, |
| 41 reset: function() { | 49 reset: function() { |
| 42 this.records = undefined; | 50 this.records = undefined; |
| 43 this.callbackCount = 0; | 51 this.callbackCount = 0; |
| 44 }, | 52 }, |
| 45 assertNotCalled: function() { | 53 assertNotCalled: function() { |
| 46 assertEquals(undefined, this.records); | 54 assertEquals(undefined, this.records); |
| 47 assertEquals(0, this.callbackCount); | 55 assertEquals(0, this.callbackCount); |
| 48 }, | 56 }, |
| 49 assertCalled: function() { | 57 assertCalled: function() { |
| 50 assertEquals(1, this.callbackCount); | 58 assertEquals(1, this.callbackCount); |
| 51 }, | 59 }, |
| 52 assertRecordCount: function(count) { | 60 assertRecordCount: function(count) { |
| 53 this.assertCalled(); | 61 this.assertCalled(); |
| 54 assertEquals(count, this.records.length); | 62 assertEquals(count, this.records.length); |
| 55 }, | 63 }, |
| 56 assertCallbackRecords: function(recs) { | 64 assertCallbackRecords: function(recs) { |
| 57 this.assertRecordCount(recs.length); | 65 this.assertRecordCount(recs.length); |
| 58 for (var i = 0; i < recs.length; i++) { | 66 for (var i = 0; i < recs.length; i++) { |
| 59 if ('name' in recs[i]) | 67 if ('name' in recs[i]) |
| 60 recs[i].name = String(recs[i].name); | 68 recs[i].name = String(recs[i].name); |
| 61 print(i, JSON.stringify(this.records[i]), JSON.stringify(recs[i])); | 69 print(i, stringifyNoThrow(this.records[i]), stringifyNoThrow(recs[i])); |
| 62 assertSame(this.records[i].object, recs[i].object); | 70 assertSame(this.records[i].object, recs[i].object); |
| 63 assertEquals('string', typeof recs[i].type); | 71 assertEquals('string', typeof recs[i].type); |
| 64 assertPropertiesEqual(this.records[i], recs[i]); | 72 assertPropertiesEqual(this.records[i], recs[i]); |
| 65 } | 73 } |
| 66 } | 74 } |
| 67 }; | 75 }; |
| 68 | 76 |
| 69 observer.callback = function(r) { | 77 observer.callback = function(r) { |
| 70 assertEquals(undefined, this); | 78 assertEquals(undefined, this); |
| 71 assertEquals('object', typeof r); | 79 assertEquals('object', typeof r); |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 delete: function(k) { | 465 delete: function(k) { |
| 458 var x = delete this.target[k]; | 466 var x = delete this.target[k]; |
| 459 Object.deliverChangeRecords(this.callback); | 467 Object.deliverChangeRecords(this.callback); |
| 460 return x; | 468 return x; |
| 461 }, | 469 }, |
| 462 getPropertyNames: function() { | 470 getPropertyNames: function() { |
| 463 return Object.getOwnPropertyNames(this.target); | 471 return Object.getOwnPropertyNames(this.target); |
| 464 }, | 472 }, |
| 465 target: {isProxy: true}, | 473 target: {isProxy: true}, |
| 466 callback: function(changeRecords) { | 474 callback: function(changeRecords) { |
| 467 print("callback", JSON.stringify(handler.proxy), JSON.stringify(got)); | 475 print("callback", stringifyNoThrow(handler.proxy), stringifyNoThrow(got)); |
| 468 for (var i in changeRecords) { | 476 for (var i in changeRecords) { |
| 469 var got = changeRecords[i]; | 477 var got = changeRecords[i]; |
| 470 var change = {object: handler.proxy, name: got.name, type: got.type}; | 478 var change = {object: handler.proxy, name: got.name, type: got.type}; |
| 471 if ("oldValue" in got) change.oldValue = got.oldValue; | 479 if ("oldValue" in got) change.oldValue = got.oldValue; |
| 472 Object.getNotifier(handler.proxy).notify(change); | 480 Object.getNotifier(handler.proxy).notify(change); |
| 473 } | 481 } |
| 474 }, | 482 }, |
| 475 }; | 483 }; |
| 476 Object.observe(handler.target, handler.callback); | 484 Object.observe(handler.target, handler.callback); |
| 477 return handler.proxy = create(handler, x); | 485 return handler.proxy = create(handler, x); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 496 | 504 |
| 497 // Cases that yield non-standard results. | 505 // Cases that yield non-standard results. |
| 498 // TODO(observe): ...or don't work yet. | 506 // TODO(observe): ...or don't work yet. |
| 499 function blacklisted(obj, prop) { | 507 function blacklisted(obj, prop) { |
| 500 return (obj instanceof Int32Array && prop == 1) || | 508 return (obj instanceof Int32Array && prop == 1) || |
| 501 (obj instanceof Int32Array && prop === "length") || | 509 (obj instanceof Int32Array && prop === "length") || |
| 502 (obj instanceof ArrayBuffer && prop == 1) || | 510 (obj instanceof ArrayBuffer && prop == 1) || |
| 503 // TODO(observe): oldValue when reconfiguring array length | 511 // TODO(observe): oldValue when reconfiguring array length |
| 504 (obj instanceof Array && prop === "length") || | 512 (obj instanceof Array && prop === "length") || |
| 505 // TODO(observe): prototype property on functions | 513 // TODO(observe): prototype property on functions |
| 506 (obj instanceof Function && prop === "prototype") || | 514 (obj instanceof Function && prop === "prototype") |
| 507 // TODO(observe): global object | |
| 508 obj === this; | |
| 509 } | 515 } |
| 510 | 516 |
| 511 for (var i in objects) for (var j in properties) { | 517 for (var i in objects) for (var j in properties) { |
| 512 var obj = objects[i]; | 518 var obj = objects[i]; |
| 513 var prop = properties[j]; | 519 var prop = properties[j]; |
| 514 if (blacklisted(obj, prop)) continue; | 520 if (blacklisted(obj, prop)) continue; |
| 515 var desc = Object.getOwnPropertyDescriptor(obj, prop); | 521 var desc = Object.getOwnPropertyDescriptor(obj, prop); |
| 516 print("***", typeof obj, JSON.stringify(obj), prop); | 522 print("***", typeof obj, stringifyNoThrow(obj), prop); |
| 517 if (!desc || desc.configurable) | 523 if (!desc || desc.configurable) |
| 518 TestObserveConfigurable(obj, prop); | 524 TestObserveConfigurable(obj, prop); |
| 519 else if (desc.writable) | 525 else if (desc.writable) |
| 520 TestObserveNonConfigurable(obj, prop); | 526 TestObserveNonConfigurable(obj, prop); |
| 521 } | 527 } |
| 522 | 528 |
| 523 | 529 |
| 524 // Observing array length (including truncation) | 530 // Observing array length (including truncation) |
| 525 reset(); | 531 reset(); |
| 526 var arr = ['a', 'b', 'c', 'd']; | 532 var arr = ['a', 'b', 'c', 'd']; |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 769 Object.observe(dummy, observer.callback); | 775 Object.observe(dummy, observer.callback); |
| 770 Object.unobserve(dummy, observer.callback); | 776 Object.unobserve(dummy, observer.callback); |
| 771 var array = [0]; | 777 var array = [0]; |
| 772 Object.observe(array, observer.callback); | 778 Object.observe(array, observer.callback); |
| 773 array.splice(0, 1); | 779 array.splice(0, 1); |
| 774 Object.deliverChangeRecords(observer.callback); | 780 Object.deliverChangeRecords(observer.callback); |
| 775 observer.assertCallbackRecords([ | 781 observer.assertCallbackRecords([ |
| 776 { object: array, name: '0', type: 'deleted', oldValue: 0 }, | 782 { object: array, name: '0', type: 'deleted', oldValue: 0 }, |
| 777 { object: array, name: 'length', type: 'updated', oldValue: 1}, | 783 { object: array, name: 'length', type: 'updated', oldValue: 1}, |
| 778 ]); | 784 ]); |
| OLD | NEW |