1 // Copyright 2014 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
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.
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.
29 function getObjects() {
47 var coercibleValues = [
55 var nonCoercibleValues = [
61 var valuesWithoutNull = coercibleValues.concat(undefined);
64 function TestSetPrototypeOfCoercibleValues() {
65 for (var i = 0; i < coercibleValues.length; i++) {
66 var value = coercibleValues[i];
67 assertThrows(function() {
68 Object.getPrototypeOf(value);
71 assertEquals(Object.setPrototypeOf(value, {}), value);
73 assertThrows(function() {
74 Object.getPrototypeOf(value);
78 TestSetPrototypeOfCoercibleValues();
81 function TestSetPrototypeOfNonCoercibleValues() {
82 for (var i = 0; i < nonCoercibleValues.length; i++) {
83 var value = nonCoercibleValues[i];
84 assertThrows(function() {
85 Object.setPrototypeOf(value, {});
89 TestSetPrototypeOfNonCoercibleValues();
92 function TestSetPrototypeToNonObject(proto) {
93 var objects = getObjects();
94 for (var i = 0; i < objects.length; i++) {
95 var object = objects[i];
96 for (var j = 0; j < valuesWithoutNull.length; j++) {
97 var proto = valuesWithoutNull[j];
98 assertThrows(function() {
99 Object.setPrototypeOf(object, proto);
104 TestSetPrototypeToNonObject();
107 function TestSetPrototypeOf(object, proto) {
108 assertEquals(Object.setPrototypeOf(object, proto), object);
109 assertEquals(Object.getPrototypeOf(object), proto);
113 function TestSetPrototypeOfForObjects() {
114 var objects1 = getObjects();
115 var objects2 = getObjects();
116 for (var i = 0; i < objects1.length; i++) {
117 for (var j = 0; j < objects2.length; j++) {
118 TestSetPrototypeOf(objects1[i], objects2[j]);
122 TestSetPrototypeOfForObjects();
125 function TestSetPrototypeToNull() {
126 var objects = getObjects();
127 for (var i = 0; i < objects.length; i++) {
128 TestSetPrototypeOf(objects[i], null);
131 TestSetPrototypeToNull();
134 function TestSetPrototypeOfNonExtensibleObject() {
135 var objects = getObjects();
137 for (var i = 0; i < objects.length; i++) {
138 var object = objects[i];
139 Object.preventExtensions(object);
140 assertThrows(function() {
141 Object.setPrototypeOf(object, proto);
145 TestSetPrototypeOfNonExtensibleObject();
148 function TestLookup() {
150 assertFalse('x' in object);
151 assertFalse('y' in object);
157 Object.setPrototypeOf(object, oldProto);
158 assertEquals(object.x, 'old x');
159 assertEquals(object.y, 'old y');
164 Object.setPrototypeOf(object, newProto);
165 assertEquals(object.x, 'new x');
166 assertFalse('y' in object);