| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 if (set__proto__) { | 49 if (set__proto__) { |
| 50 (new Sub()).__proto__ = proto; | 50 (new Sub()).__proto__ = proto; |
| 51 } else { | 51 } else { |
| 52 Sub.prototype = proto; | 52 Sub.prototype = proto; |
| 53 // Need to instantiate Sub to mark .prototype as prototype. | 53 // Need to instantiate Sub to mark .prototype as prototype. |
| 54 new Sub(); | 54 new Sub(); |
| 55 } | 55 } |
| 56 } | 56 } |
| 57 | 57 |
| 58 | 58 |
| 59 function test(use_new, add_first, set__proto__, same_map_as) { | 59 function test(use_new, add_first, set__proto__) { |
| 60 var proto = use_new ? new Super() : {}; | 60 var proto = use_new ? new Super() : {}; |
| 61 | 61 |
| 62 // New object is fast. | 62 // New object is fast. |
| 63 assertTrue(%HasFastProperties(proto)); | 63 assertTrue(%HasFastProperties(proto)); |
| 64 | 64 |
| 65 if (add_first) { | 65 if (add_first) { |
| 66 AddProps(proto); | 66 AddProps(proto); |
| 67 // Adding this many properties makes it slow. | 67 // Adding this many properties makes it slow. |
| 68 assertFalse(%HasFastProperties(proto)); | 68 assertFalse(%HasFastProperties(proto)); |
| 69 DoProtoMagic(proto, set__proto__); | 69 DoProtoMagic(proto, set__proto__); |
| 70 // Making it a prototype makes it fast again. | 70 // Making it a prototype makes it fast again. |
| 71 assertTrue(%HasFastProperties(proto)); | 71 assertTrue(%HasFastProperties(proto)); |
| 72 } else { | 72 } else { |
| 73 DoProtoMagic(proto, set__proto__); | 73 DoProtoMagic(proto, set__proto__); |
| 74 // Still fast | 74 // Still fast |
| 75 assertTrue(%HasFastProperties(proto)); | 75 assertTrue(%HasFastProperties(proto)); |
| 76 AddProps(proto); | 76 AddProps(proto); |
| 77 if (set__proto__) { | 77 // Still fast. |
| 78 // After we add all those properties it went slow mode again :-( | 78 assertTrue(%HasFastProperties(proto)); |
| 79 assertFalse(%HasFastProperties(proto)); | |
| 80 } else { | |
| 81 // .prototype keeps it fast. | |
| 82 assertTrue(%HasFastProperties(proto)); | |
| 83 } | |
| 84 } | |
| 85 if (same_map_as && !add_first && set__proto__) { | |
| 86 assertTrue(%HaveSameMap(same_map_as, proto)); | |
| 87 } | 79 } |
| 88 return proto; | 80 return proto; |
| 89 } | 81 } |
| 90 | 82 |
| 91 // TODO(mstarzinger): This test fails easily if gc happens at the wrong time. | 83 // TODO(mstarzinger): This test fails easily if gc happens at the wrong time. |
| 92 gc(); | 84 gc(); |
| 93 | 85 |
| 94 for (var i = 0; i < 4; i++) { | 86 for (var i = 0; i < 4; i++) { |
| 95 var set__proto__ = ((i & 1) != 0); | 87 var set__proto__ = ((i & 1) != 0); |
| 96 var use_new = ((i & 2) != 0); | 88 var use_new = ((i & 2) != 0); |
| 97 | 89 |
| 98 test(use_new, true, set__proto__); | 90 test(use_new, true, set__proto__); |
| 99 | 91 test(use_new, false, set__proto__); |
| 100 var last = test(use_new, false, set__proto__); | |
| 101 test(use_new, false, set__proto__, last); | |
| 102 } | 92 } |
| 103 | 93 |
| 104 | 94 |
| 105 var x = {a: 1, b: 2, c: 3}; | 95 var x = {a: 1, b: 2, c: 3}; |
| 106 var o = { __proto__: x }; | 96 var o = { __proto__: x }; |
| 107 assertTrue(%HasFastProperties(x)); | 97 assertTrue(%HasFastProperties(x)); |
| 108 for (key in x) { | 98 for (key in x) { |
| 109 assertTrue(key == 'a'); | 99 assertTrue(key == 'a'); |
| 110 break; | 100 break; |
| 111 } | 101 } |
| 112 delete x.b; | 102 delete x.b; |
| 113 for (key in x) { | 103 for (key in x) { |
| 114 assertTrue(key == 'a'); | 104 assertTrue(key == 'a'); |
| 115 break; | 105 break; |
| 116 } | 106 } |
| 117 assertTrue(%HasFastProperties(x)); | 107 assertTrue(%HasFastProperties(x)); |
| 118 x.d = 4; | 108 x.d = 4; |
| 119 assertTrue(%HasFastProperties(x)); | 109 assertTrue(%HasFastProperties(x)); |
| 120 for (key in x) { | 110 for (key in x) { |
| 121 assertTrue(key == 'a'); | 111 assertTrue(key == 'a'); |
| 122 break; | 112 break; |
| 123 } | 113 } |
| OLD | NEW |