Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: test/mjsunit/harmony/object-observe.js

Issue 11416353: Object.observe support for Function 'prototype' property (Closed) Base URL: git://github.com/v8/v8.git@bleeding_edge
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/objects.cc ('K') | « src/objects.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 429 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 { object: obj, name: prop, type: "new" }, 440 { object: obj, name: prop, type: "new" },
441 { object: obj, name: prop, type: "reconfigured" }, 441 { object: obj, name: prop, type: "reconfigured" },
442 { object: obj, name: prop, type: "updated", oldValue: 9 }, 442 { object: obj, name: prop, type: "updated", oldValue: 9 },
443 { object: obj, name: prop, type: "deleted", oldValue: 10 }, 443 { object: obj, name: prop, type: "deleted", oldValue: 10 },
444 { object: obj, name: prop, type: "new" }, 444 { object: obj, name: prop, type: "new" },
445 ]); 445 ]);
446 Object.unobserve(obj, observer.callback); 446 Object.unobserve(obj, observer.callback);
447 delete obj[prop]; 447 delete obj[prop];
448 } 448 }
449 449
450 function TestObserveNonConfigurable(obj, prop) { 450 function TestObserveNonConfigurable(obj, prop, desc) {
451 reset(); 451 reset();
452 obj[prop] = 1; 452 obj[prop] = 1;
453 Object.observe(obj, observer.callback); 453 Object.observe(obj, observer.callback);
454 obj[prop] = 4; 454 obj[prop] = 4;
455 obj[prop] = 4; // ignored 455 obj[prop] = 4; // ignored
456 obj[prop] = 5; 456 obj[prop] = 5;
457 Object.defineProperty(obj, prop, {value: 6}); 457 Object.defineProperty(obj, prop, {value: 6});
458 Object.defineProperty(obj, prop, {value: 6}); // ignored 458 Object.defineProperty(obj, prop, {value: 6}); // ignored
459 Object.defineProperty(obj, prop, {value: 7}); 459 Object.defineProperty(obj, prop, {value: 7});
460 Object.defineProperty(obj, prop, {enumerable: true}); // ignored 460 Object.defineProperty(obj, prop,
461 {enumerable: desc.enumerable}); // ignored
461 Object.defineProperty(obj, prop, {writable: false}); 462 Object.defineProperty(obj, prop, {writable: false});
462 obj[prop] = 7; // ignored 463 obj[prop] = 7; // ignored
463 Object.defineProperty(obj, prop, {get: function() {}}); // ignored
464 Object.deliverChangeRecords(observer.callback); 464 Object.deliverChangeRecords(observer.callback);
465 observer.assertCallbackRecords([ 465 observer.assertCallbackRecords([
466 { object: obj, name: prop, type: "updated", oldValue: 1 }, 466 { object: obj, name: prop, type: "updated", oldValue: 1 },
467 { object: obj, name: prop, type: "updated", oldValue: 4 }, 467 { object: obj, name: prop, type: "updated", oldValue: 4 },
468 { object: obj, name: prop, type: "updated", oldValue: 5 }, 468 { object: obj, name: prop, type: "updated", oldValue: 5 },
469 { object: obj, name: prop, type: "updated", oldValue: 6 }, 469 { object: obj, name: prop, type: "updated", oldValue: 6 },
470 { object: obj, name: prop, type: "reconfigured", oldValue: 7 }, 470 { object: obj, name: prop, type: "reconfigured", oldValue: 7 },
471 ]); 471 ]);
472 Object.unobserve(obj, observer.callback); 472 Object.unobserve(obj, observer.callback);
473 } 473 }
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 var properties = ["a", "1", 1, "length", "prototype"]; 530 var properties = ["a", "1", 1, "length", "prototype"];
531 531
532 // Cases that yield non-standard results. 532 // Cases that yield non-standard results.
533 // TODO(observe): ...or don't work yet. 533 // TODO(observe): ...or don't work yet.
534 function blacklisted(obj, prop) { 534 function blacklisted(obj, prop) {
535 return (obj instanceof Int32Array && prop == 1) || 535 return (obj instanceof Int32Array && prop == 1) ||
536 (obj instanceof Int32Array && prop === "length") || 536 (obj instanceof Int32Array && prop === "length") ||
537 (obj instanceof ArrayBuffer && prop == 1) || 537 (obj instanceof ArrayBuffer && prop == 1) ||
538 // TODO(observe): oldValue when reconfiguring array length 538 // TODO(observe): oldValue when reconfiguring array length
539 (obj instanceof Array && prop === "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 540 // TODO(observe): global object
543 obj === this; 541 obj === this;
544 } 542 }
545 543
546 for (var i in objects) for (var j in properties) { 544 for (var i in objects) for (var j in properties) {
547 var obj = objects[i]; 545 var obj = objects[i];
548 var prop = properties[j]; 546 var prop = properties[j];
549 if (blacklisted(obj, prop)) continue; 547 if (blacklisted(obj, prop)) continue;
550 var desc = Object.getOwnPropertyDescriptor(obj, prop); 548 var desc = Object.getOwnPropertyDescriptor(obj, prop);
551 print("***", typeof obj, JSON.stringify(obj), prop); 549 print("***", typeof obj, JSON.stringify(obj), prop);
552 if (!desc || desc.configurable) 550 if (!desc || desc.configurable)
553 TestObserveConfigurable(obj, prop); 551 TestObserveConfigurable(obj, prop);
554 else if (desc.writable) 552 else if (desc.writable)
555 TestObserveNonConfigurable(obj, prop); 553 TestObserveNonConfigurable(obj, prop, desc);
556 } 554 }
557 555
558 556
559 // Observing array length (including truncation) 557 // Observing array length (including truncation)
560 reset(); 558 reset();
561 var arr = ['a', 'b', 'c', 'd']; 559 var arr = ['a', 'b', 'c', 'd'];
562 var arr2 = ['alpha', 'beta']; 560 var arr2 = ['alpha', 'beta'];
563 var arr3 = ['hello']; 561 var arr3 = ['hello'];
564 arr3[2] = 'goodbye'; 562 arr3[2] = 'goodbye';
565 arr3.length = 6; 563 arr3.length = 6;
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 Object.observe(dummy, observer.callback); 801 Object.observe(dummy, observer.callback);
804 Object.unobserve(dummy, observer.callback); 802 Object.unobserve(dummy, observer.callback);
805 var array = [0]; 803 var array = [0];
806 Object.observe(array, observer.callback); 804 Object.observe(array, observer.callback);
807 array.splice(0, 1); 805 array.splice(0, 1);
808 Object.deliverChangeRecords(observer.callback); 806 Object.deliverChangeRecords(observer.callback);
809 observer.assertCallbackRecords([ 807 observer.assertCallbackRecords([
810 { object: array, name: '0', type: 'deleted', oldValue: 0 }, 808 { object: array, name: '0', type: 'deleted', oldValue: 0 },
811 { object: array, name: 'length', type: 'updated', oldValue: 1}, 809 { object: array, name: 'length', type: 'updated', oldValue: 1},
812 ]); 810 ]);
811
812 // Function.prototype
813 reset();
814 var fun = function(){};
815 Object.observe(fun, observer.callback);
816 var myproto = {foo: 'bar'};
817 fun.prototype = myproto;
818 fun.prototype = 7;
819 fun.prototype = 7; // ignored
820 Object.deliverChangeRecords(observer.callback);
821 observer.assertRecordCount(2);
822 assertSame(fun, observer.records[0].object);
rossberg 2012/12/05 12:02:29 What is the reason you check the first record outs
adamk 2012/12/05 17:38:17 Because of line 827: the only handle to the oldVal
rossberg 2012/12/06 09:50:01 Well, you can save the old fun.prototype before re
823 assertEquals('prototype', observer.records[0].name);
824 assertEquals('updated', observer.records[0].type);
825 // The oldValue should be the same object it would have been if
826 // it had been accessed before being set.
827 assertSame(fun, observer.records[0].oldValue.constructor);
828 observer.records.splice(0, 1);
829 observer.assertCallbackRecords([
830 { object: fun, name: 'prototype', type: 'updated', oldValue: myproto },
831 ]);
832
833 // Function.prototype should not be observable except on the object itself
834 reset();
835 var fun = function(){};
836 var obj = { __proto__: fun };
837 Object.observe(obj, observer.callback);
838 obj.prototype = 7;
839 Object.deliverChangeRecords(observer.callback);
840 observer.assertNotCalled();
OLDNEW
« src/objects.cc ('K') | « src/objects.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698