| 1 |
load(libdir + 'asserts.js'); |
| 2 |
|
| 3 |
function getObjects() { |
| 4 |
function func(){} |
| 5 |
return [func, |
| 6 |
new func(), |
| 7 |
{x: 5}, |
| 8 |
/regexp/, |
| 9 |
[1, 2, 3], |
| 10 |
new Date(), |
| 11 |
new Number(1), |
| 12 |
new Boolean(true), |
| 13 |
new String('str'), |
| 14 |
Object.create(null)]; |
| 15 |
} |
| 16 |
|
| 17 |
var coercibleValues = [1, |
| 18 |
true, |
| 19 |
'string']; |
| 20 |
|
| 21 |
var nonCoercibleValues = [undefined, |
| 22 |
null]; |
| 23 |
|
| 24 |
var valuesWithoutNull = coercibleValues.concat(undefined); |
| 25 |
|
| 26 |
function TestSetPrototypeOf(object, proto) { |
| 27 |
assertEq(Object.setPrototypeOf(object, proto), object); |
| 28 |
assertEq(Object.getPrototypeOf(object), proto); |
| 29 |
} |
| 30 |
|
| 31 |
// check if Object.setPrototypeOf works with coercible values |
| 32 |
for(var value of coercibleValues) { |
| 33 |
assertEq(Object.setPrototypeOf(value, {}), value); |
| 34 |
} |
| 35 |
|
| 36 |
// check if Object.setPrototypeOf fails on non-coercible values |
| 37 |
for (var value of nonCoercibleValues) { |
| 38 |
assertThrowsInstanceOf(() => Object.setPrototypeOf(value, {}), |
| 39 |
TypeError, "Object.setPrototypeOf shouldn't work on non-coercible values"); |
| 40 |
} |
| 41 |
|
| 42 |
// check if Object.setPrototypeOf works when prototype is set to non-objects |
| 43 |
var objects = getObjects(); |
| 44 |
for (var object of objects) { |
| 45 |
for (var proto of valuesWithoutNull) { |
| 46 |
assertThrowsInstanceOf(() => Object.setPrototypeOf(object, proto), |
| 47 |
TypeError, "Object.setPrototypeOf fails when the prototype is set to non-objects"); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// check if Object.setPrototypeOf works when prototype is set to objects |
| 52 |
var objects1 = getObjects(); |
| 53 |
var objects2 = getObjects(); |
| 54 |
for (var object1 of objects1) { |
| 55 |
for (var object2 of objects2) { |
| 56 |
TestSetPrototypeOf(object1, object2); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// check if Object.setPrototypeOf works when prototype is set to null |
| 61 |
objects = getObjects(); |
| 62 |
for (var object of objects) { |
| 63 |
TestSetPrototypeOf(object, null); |
| 64 |
} |
| 65 |
|
| 66 |
// check if Object.setPrototypeOf fails when object is not extensible |
| 67 |
var objects = getObjects(); |
| 68 |
var proto = {}; |
| 69 |
for (var object of objects) { |
| 70 |
Object.preventExtensions(object); |
| 71 |
assertThrowsInstanceOf(() => Object.setPrototypeOf(object, proto), |
| 72 |
TypeError, "Object.setPrototypeOf should fail when the object is not extensible"); |
| 73 |
} |
| 74 |
|
| 75 |
// check if Object.setPrototypeof(A, B) succeeds on not extensible object A if |
| 76 |
// prototype of A == B already |
| 77 |
var objectProto = {}; |
| 78 |
var nonExtensibleObject = Object.create(objectProto); |
| 79 |
Object.preventExtensions(nonExtensibleObject); |
| 80 |
assertEq(Object.setPrototypeOf(nonExtensibleObject, objectProto), nonExtensibleObject); |
| 81 |
|
| 82 |
// check if Object.setPrototypeOf works with prototype lookup |
| 83 |
var object = {}; |
| 84 |
assertEq('x' in object, false); |
| 85 |
assertEq('y' in object, false); |
| 86 |
|
| 87 |
var oldProto = { |
| 88 |
x: 'old x', |
| 89 |
y: 'old y' |
| 90 |
}; |
| 91 |
Object.setPrototypeOf(object, oldProto); |
| 92 |
assertEq(object.x, 'old x'); |
| 93 |
assertEq(object.y, 'old y'); |
| 94 |
|
| 95 |
var newProto = { |
| 96 |
x: 'new x' |
| 97 |
}; |
| 98 |
Object.setPrototypeOf(object, newProto); |
| 99 |
assertEq(object.x, 'new x'); |
| 100 |
assertEq('y' in object, false); |
| 101 |
|
| 102 |
// check if Object.setPrototypeOf throws TypeError on fewer arguments |
| 103 |
assertThrowsInstanceOf(() => Object.setPrototypeOf(), |
| 104 |
TypeError, "Object.setPrototypeOf throws TypeError when called without any parameters"); |
| 105 |
assertThrowsInstanceOf(() => Object.setPrototypeOf({}), |
| 106 |
TypeError, "Object.setPrototypeOf throws TypeError when called with 1 parameter"); |