blob: 8f2a00a9c461ef1457c1e238ea26db9e8f0e5ebb [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2014-2015 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// This is adapted from mjsunit/harmony/set-prototype-of.js.
29
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030
31
32function getObjects() {
33 function func() {}
34 return [
35 func,
36 new func(),
37 {x: 5},
38 /regexp/,
39 ['array'],
40 // new Error(),
41 new Date(),
42 new Number(1),
43 new Boolean(true),
44 new String('str'),
45 Object(Symbol())
46 ];
47}
48
49
50var coercibleValues = [
51 1,
52 true,
53 'string',
54 Symbol()
55];
56
57
58var nonCoercibleValues = [
59 undefined,
60 null
61];
62
63
64var valuesWithoutNull = coercibleValues.concat(undefined);
65
66
67function TestSetPrototypeOfCoercibleValues() {
68 for (var i = 0; i < coercibleValues.length; i++) {
69 var value = coercibleValues[i];
70 var proto = Object.getPrototypeOf(value);
71 assertThrows(function() { Reflect.setPrototypeOf(value, {}) }, TypeError);
72 assertSame(proto, Object.getPrototypeOf(value));
73 }
74}
75TestSetPrototypeOfCoercibleValues();
76
77
78function TestSetPrototypeOfNonCoercibleValues() {
79 for (var i = 0; i < nonCoercibleValues.length; i++) {
80 var value = nonCoercibleValues[i];
81 assertThrows(function() {
82 Reflect.setPrototypeOf(value, {});
83 }, TypeError);
84 }
85}
86TestSetPrototypeOfNonCoercibleValues();
87
88
89function TestSetPrototypeToNonObject(proto) {
90 var objects = getObjects();
91 for (var i = 0; i < objects.length; i++) {
92 var object = objects[i];
93 for (var j = 0; j < valuesWithoutNull.length; j++) {
94 var proto = valuesWithoutNull[j];
95 assertThrows(function() {
96 Reflect.setPrototypeOf(object, proto);
97 }, TypeError);
98 }
99 }
100}
101TestSetPrototypeToNonObject();
102
103
104function TestSetPrototypeOf(object, proto) {
105 assertTrue(Reflect.setPrototypeOf(object, proto));
106 assertEquals(Object.getPrototypeOf(object), proto);
107}
108
109
110function TestSetPrototypeOfForObjects() {
111 var objects1 = getObjects();
112 var objects2 = getObjects();
113 for (var i = 0; i < objects1.length; i++) {
114 for (var j = 0; j < objects2.length; j++) {
115 TestSetPrototypeOf(objects1[i], objects2[j]);
116 }
117 }
118}
119TestSetPrototypeOfForObjects();
120
121
122function TestSetPrototypeToNull() {
123 var objects = getObjects();
124 for (var i = 0; i < objects.length; i++) {
125 TestSetPrototypeOf(objects[i], null);
126 }
127}
128TestSetPrototypeToNull();
129
130
131function TestSetPrototypeOfNonExtensibleObject() {
132 var objects = getObjects();
133 var proto = {};
134 for (var i = 0; i < objects.length; i++) {
135 var object = objects[i];
136 Object.preventExtensions(object);
137 // Setting the current prototype must succeed.
138 assertTrue(Reflect.setPrototypeOf(object, Object.getPrototypeOf(object)));
139 // Setting any other must fail.
140 assertFalse(Reflect.setPrototypeOf(object, proto));
141 }
142}
143TestSetPrototypeOfNonExtensibleObject();
144
145
146function TestSetPrototypeCyclic() {
147 var objects = [
148 Object.prototype, {},
149 Array.prototype, [],
150 Error.prototype, new TypeError,
151 // etc ...
152 ];
153 for (var i = 0; i < objects.length; i += 2) {
154 var object = objects[i];
155 var value = objects[i + 1];
156 assertFalse(Reflect.setPrototypeOf(object, value));
157 }
158}
159TestSetPrototypeCyclic();
160
161
162function TestLookup() {
163 var object = {};
164 assertFalse('x' in object);
165 assertFalse('y' in object);
166
167 var oldProto = {
168 x: 'old x',
169 y: 'old y'
170 };
171 assertTrue(Reflect.setPrototypeOf(object, oldProto));
172 assertEquals(object.x, 'old x');
173 assertEquals(object.y, 'old y');
174
175 var newProto = {
176 x: 'new x'
177 };
178 assertTrue(Reflect.setPrototypeOf(object, newProto));
179 assertEquals(object.x, 'new x');
180 assertFalse('y' in object);
181}
182TestLookup();