blob: aa0a62e954e4fadbc6b83cb06af0d8a8f232027b [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
33function Super() {
34}
35
36
37function Sub() {
38}
39
40
41function AddProps(obj) {
42 for (var i = 0; i < 26; i++) {
43 obj["x" + i] = 0;
44 }
45}
46
47
48function DoProtoMagic(proto, set__proto__) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010049 var receiver;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050 if (set__proto__) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010051 receiver = new Sub();
52 receiver.__proto__ = proto;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 } else {
54 Sub.prototype = proto;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055 // Need to instantiate Sub to mark .prototype as prototype. Make sure the
56 // instantiated object is used so that the allocation is not optimized away.
Ben Murdoch61f157c2016-09-16 13:49:30 +010057 receiver = new Sub();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058 }
Ben Murdoch61f157c2016-09-16 13:49:30 +010059 // Prototypes are made fast when ICs encounter them.
60 function ic() { return typeof receiver.foo; }
61 ic();
62 ic();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063}
64
65
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066function test(use_new, add_first, set__proto__) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 var proto = use_new ? new Super() : {};
68
69 // New object is fast.
70 assertTrue(%HasFastProperties(proto));
71
72 if (add_first) {
73 AddProps(proto);
74 // Adding this many properties makes it slow.
75 assertFalse(%HasFastProperties(proto));
76 DoProtoMagic(proto, set__proto__);
77 // Making it a prototype makes it fast again.
78 assertTrue(%HasFastProperties(proto));
79 } else {
80 DoProtoMagic(proto, set__proto__);
81 // Still fast
82 assertTrue(%HasFastProperties(proto));
83 AddProps(proto);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000084 // Still fast.
85 assertTrue(%HasFastProperties(proto));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 }
87 return proto;
88}
89
90// TODO(mstarzinger): This test fails easily if gc happens at the wrong time.
91gc();
92
93for (var i = 0; i < 4; i++) {
94 var set__proto__ = ((i & 1) != 0);
95 var use_new = ((i & 2) != 0);
96
97 test(use_new, true, set__proto__);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098 test(use_new, false, set__proto__);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099}
100
101
102var x = {a: 1, b: 2, c: 3};
103var o = { __proto__: x };
104assertTrue(%HasFastProperties(x));
105for (key in x) {
106 assertTrue(key == 'a');
107 break;
108}
109delete x.b;
110for (key in x) {
111 assertTrue(key == 'a');
112 break;
113}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400114assertTrue(%HasFastProperties(x));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115x.d = 4;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400116assertTrue(%HasFastProperties(x));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117for (key in x) {
118 assertTrue(key == 'a');
119 break;
120}