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 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 delete: function(k) { | 500 delete: function(k) { |
493 var x = delete this.target[k]; | 501 var x = delete this.target[k]; |
494 Object.deliverChangeRecords(this.callback); | 502 Object.deliverChangeRecords(this.callback); |
495 return x; | 503 return x; |
496 }, | 504 }, |
497 getPropertyNames: function() { | 505 getPropertyNames: function() { |
498 return Object.getOwnPropertyNames(this.target); | 506 return Object.getOwnPropertyNames(this.target); |
499 }, | 507 }, |
500 target: {isProxy: true}, | 508 target: {isProxy: true}, |
501 callback: function(changeRecords) { | 509 callback: function(changeRecords) { |
502 print("callback", JSON.stringify(handler.proxy), JSON.stringify(got)); | 510 print("callback", stringifyNoThrow(handler.proxy), stringifyNoThrow(got)); |
503 for (var i in changeRecords) { | 511 for (var i in changeRecords) { |
504 var got = changeRecords[i]; | 512 var got = changeRecords[i]; |
505 var change = {object: handler.proxy, name: got.name, type: got.type}; | 513 var change = {object: handler.proxy, name: got.name, type: got.type}; |
506 if ("oldValue" in got) change.oldValue = got.oldValue; | 514 if ("oldValue" in got) change.oldValue = got.oldValue; |
507 Object.getNotifier(handler.proxy).notify(change); | 515 Object.getNotifier(handler.proxy).notify(change); |
508 } | 516 } |
509 }, | 517 }, |
510 }; | 518 }; |
511 Object.observe(handler.target, handler.callback); | 519 Object.observe(handler.target, handler.callback); |
512 return handler.proxy = create(handler, x); | 520 return handler.proxy = create(handler, x); |
(...skipping 18 matching lines...) Expand all Loading... |
531 | 539 |
532 // Cases that yield non-standard results. | 540 // Cases that yield non-standard results. |
533 // TODO(observe): ...or don't work yet. | 541 // TODO(observe): ...or don't work yet. |
534 function blacklisted(obj, prop) { | 542 function blacklisted(obj, prop) { |
535 return (obj instanceof Int32Array && prop == 1) || | 543 return (obj instanceof Int32Array && prop == 1) || |
536 (obj instanceof Int32Array && prop === "length") || | 544 (obj instanceof Int32Array && prop === "length") || |
537 (obj instanceof ArrayBuffer && prop == 1) || | 545 (obj instanceof ArrayBuffer && prop == 1) || |
538 // TODO(observe): oldValue when reconfiguring array length | 546 // TODO(observe): oldValue when reconfiguring array length |
539 (obj instanceof Array && prop === "length") || | 547 (obj instanceof Array && prop === "length") || |
540 // TODO(observe): prototype property on functions | 548 // TODO(observe): prototype property on functions |
541 (obj instanceof Function && prop === "prototype") || | 549 (obj instanceof Function && prop === "prototype") |
542 // TODO(observe): global object | |
543 obj === this; | |
544 } | 550 } |
545 | 551 |
546 for (var i in objects) for (var j in properties) { | 552 for (var i in objects) for (var j in properties) { |
547 var obj = objects[i]; | 553 var obj = objects[i]; |
548 var prop = properties[j]; | 554 var prop = properties[j]; |
549 if (blacklisted(obj, prop)) continue; | 555 if (blacklisted(obj, prop)) continue; |
550 var desc = Object.getOwnPropertyDescriptor(obj, prop); | 556 var desc = Object.getOwnPropertyDescriptor(obj, prop); |
551 print("***", typeof obj, JSON.stringify(obj), prop); | 557 print("***", typeof obj, stringifyNoThrow(obj), prop); |
552 if (!desc || desc.configurable) | 558 if (!desc || desc.configurable) |
553 TestObserveConfigurable(obj, prop); | 559 TestObserveConfigurable(obj, prop); |
554 else if (desc.writable) | 560 else if (desc.writable) |
555 TestObserveNonConfigurable(obj, prop); | 561 TestObserveNonConfigurable(obj, prop); |
556 } | 562 } |
557 | 563 |
558 | 564 |
559 // Observing array length (including truncation) | 565 // Observing array length (including truncation) |
560 reset(); | 566 reset(); |
561 var arr = ['a', 'b', 'c', 'd']; | 567 var arr = ['a', 'b', 'c', 'd']; |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
803 Object.observe(dummy, observer.callback); | 809 Object.observe(dummy, observer.callback); |
804 Object.unobserve(dummy, observer.callback); | 810 Object.unobserve(dummy, observer.callback); |
805 var array = [0]; | 811 var array = [0]; |
806 Object.observe(array, observer.callback); | 812 Object.observe(array, observer.callback); |
807 array.splice(0, 1); | 813 array.splice(0, 1); |
808 Object.deliverChangeRecords(observer.callback); | 814 Object.deliverChangeRecords(observer.callback); |
809 observer.assertCallbackRecords([ | 815 observer.assertCallbackRecords([ |
810 { object: array, name: '0', type: 'deleted', oldValue: 0 }, | 816 { object: array, name: '0', type: 'deleted', oldValue: 0 }, |
811 { object: array, name: 'length', type: 'updated', oldValue: 1}, | 817 { object: array, name: 'length', type: 'updated', oldValue: 1}, |
812 ]); | 818 ]); |
OLD | NEW |