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