Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | |
| 28 | // Flags: --allow-natives-syntax --expose-gc |
| 29 | // Flags: --noincremental-marking |
| 30 | |
| 31 | // Check that objects that are used for prototypes are in the fast mode. |
| 32 | |
| 33 | function Super() { |
| 34 | } |
| 35 | |
| 36 | |
| 37 | function Sub() { |
| 38 | } |
| 39 | |
| 40 | |
| 41 | function AddProps(obj) { |
| 42 | for (var i = 0; i < 26; i++) { |
| 43 | obj["x" + i] = 0; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | |
| 48 | function DoProtoMagic(proto, set__proto__) { |
| 49 | if (set__proto__) { |
| 50 | (new Sub()).__proto__ = proto; |
| 51 | } else { |
| 52 | Sub.prototype = proto; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 53 | // Need to instantiate Sub to mark .prototype as prototype. Make sure the |
| 54 | // instantiated object is used so that the allocation is not optimized away. |
| 55 | %DebugPrint(new Sub()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 60 | function test(use_new, add_first, set__proto__) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 61 | var proto = use_new ? new Super() : {}; |
| 62 | |
| 63 | // New object is fast. |
| 64 | assertTrue(%HasFastProperties(proto)); |
| 65 | |
| 66 | if (add_first) { |
| 67 | AddProps(proto); |
| 68 | // Adding this many properties makes it slow. |
| 69 | assertFalse(%HasFastProperties(proto)); |
| 70 | DoProtoMagic(proto, set__proto__); |
| 71 | // Making it a prototype makes it fast again. |
| 72 | assertTrue(%HasFastProperties(proto)); |
| 73 | } else { |
| 74 | DoProtoMagic(proto, set__proto__); |
| 75 | // Still fast |
| 76 | assertTrue(%HasFastProperties(proto)); |
| 77 | AddProps(proto); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 78 | // Still fast. |
| 79 | assertTrue(%HasFastProperties(proto)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 80 | } |
| 81 | return proto; |
| 82 | } |
| 83 | |
| 84 | // TODO(mstarzinger): This test fails easily if gc happens at the wrong time. |
| 85 | gc(); |
| 86 | |
| 87 | for (var i = 0; i < 4; i++) { |
| 88 | var set__proto__ = ((i & 1) != 0); |
| 89 | var use_new = ((i & 2) != 0); |
| 90 | |
| 91 | test(use_new, true, set__proto__); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame^] | 92 | test(use_new, false, set__proto__); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | var x = {a: 1, b: 2, c: 3}; |
| 97 | var o = { __proto__: x }; |
| 98 | assertTrue(%HasFastProperties(x)); |
| 99 | for (key in x) { |
| 100 | assertTrue(key == 'a'); |
| 101 | break; |
| 102 | } |
| 103 | delete x.b; |
| 104 | for (key in x) { |
| 105 | assertTrue(key == 'a'); |
| 106 | break; |
| 107 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 108 | assertTrue(%HasFastProperties(x)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 109 | x.d = 4; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 110 | assertTrue(%HasFastProperties(x)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 111 | for (key in x) { |
| 112 | assertTrue(key == 'a'); |
| 113 | break; |
| 114 | } |