barraclough@apple.com | dd99310 | 2011-03-04 22:34:48 +0000 | [diff] [blame] | 1 | description( |
| 2 | "This test checks whether various seal/freeze/preventExtentions work on a regular object." |
| 3 | ); |
| 4 | |
| 5 | function obj() |
| 6 | { |
barraclough@apple.com | e2ea07b | 2012-02-16 19:06:03 +0000 | [diff] [blame] | 7 | // Add an accessor property to check 'isFrozen' returns the correct result for objects with accessors. |
| 8 | return Object.defineProperty({ a: 1, b: 2 }, 'g', { get: function() { return "getter"; } }); |
barraclough@apple.com | dd99310 | 2011-03-04 22:34:48 +0000 | [diff] [blame] | 9 | } |
| 10 | |
| 11 | function test(obj) |
| 12 | { |
| 13 | obj.c =3; |
| 14 | obj.b =4; |
| 15 | delete obj.a; |
| 16 | |
| 17 | var result = ""; |
| 18 | for (key in obj) |
| 19 | result += ("(" + key + ":" + obj[key] + ")"); |
| 20 | if (Object.isSealed(obj)) |
| 21 | result += "S"; |
| 22 | if (Object.isFrozen(obj)) |
| 23 | result += "F"; |
| 24 | if (Object.isExtensible(obj)) |
| 25 | result += "E"; |
| 26 | return result; |
| 27 | } |
| 28 | |
| 29 | function seal(obj) |
| 30 | { |
| 31 | Object.seal(obj); |
| 32 | return obj; |
| 33 | } |
| 34 | |
| 35 | function freeze(obj) |
| 36 | { |
| 37 | Object.freeze(obj); |
| 38 | return obj; |
| 39 | } |
| 40 | |
| 41 | function preventExtensions(obj) |
| 42 | { |
| 43 | Object.preventExtensions(obj); |
| 44 | return obj; |
| 45 | } |
| 46 | |
oliver@apple.com | 6f34f97 | 2011-06-01 18:08:07 +0000 | [diff] [blame] | 47 | function inextensible(){} |
| 48 | function sealed(){} |
| 49 | function frozen(){}; |
| 50 | preventExtensions(inextensible); |
| 51 | seal(sealed); |
| 52 | freeze(frozen); |
| 53 | new inextensible; |
| 54 | new sealed; |
| 55 | new frozen; |
| 56 | inextensible.prototype.prototypeExists = true; |
| 57 | sealed.prototype.prototypeExists = true; |
| 58 | frozen.prototype.prototypeExists = true; |
| 59 | |
| 60 | shouldBeTrue("(new inextensible).prototypeExists"); |
| 61 | shouldBeTrue("(new sealed).prototypeExists"); |
| 62 | shouldBeTrue("(new frozen).prototypeExists"); |
| 63 | |
barraclough@apple.com | dd99310 | 2011-03-04 22:34:48 +0000 | [diff] [blame] | 64 | shouldBe('test(obj())', '"(b:4)(c:3)E"'); // extensible, can delete a, can modify b, and can add c |
| 65 | shouldBe('test(preventExtensions(obj()))', '"(b:4)"'); // <nothing>, can delete a, can modify b, and CANNOT add c |
| 66 | shouldBe('test(seal(obj()))', '"(a:1)(b:4)S"'); // sealed, CANNOT delete a, can modify b, and CANNOT add c |
| 67 | shouldBe('test(freeze(obj()))', '"(a:1)(b:2)SF"'); // sealed and frozen, CANNOT delete a, CANNOT modify b, and CANNOT add c |
| 68 | |
barraclough@apple.com | 7fccc52 | 2011-07-05 19:02:44 +0000 | [diff] [blame] | 69 | // check that we can preventExtensions on a host function. |
| 70 | shouldBe('Object.preventExtensions(Math.sin)', 'Math.sin'); |
| 71 | |
barraclough@apple.com | 4f5c0c0 | 2012-02-20 21:14:48 +0000 | [diff] [blame] | 72 | shouldThrow('var o = {}; Object.preventExtensions(o); o.__proto__ = { newProp: "Should not see this" }; o.newProp;'); |
| 73 | shouldThrow('"use strict"; var o = {}; Object.preventExtensions(o); o.__proto__ = { newProp: "Should not see this" }; o.newProp;'); |
oliver@apple.com | d5bf7d0 | 2011-08-08 19:09:51 +0000 | [diff] [blame] | 74 | |
barraclough@apple.com | d55cc59 | 2011-08-15 18:51:42 +0000 | [diff] [blame] | 75 | // check that we can still access static properties on an object after calling preventExtensions. |
| 76 | shouldBe('Object.preventExtensions(Math); Math.sqrt(4)', '2'); |
barraclough@apple.com | e2ea07b | 2012-02-16 19:06:03 +0000 | [diff] [blame] | 77 | |
| 78 | // Should not be able to add properties to a preventExtensions array. |
| 79 | shouldBeUndefined('var arr = Object.preventExtensions([]); arr[0] = 42; arr[0]'); |
| 80 | shouldBe('var arr = Object.preventExtensions([]); arr[0] = 42; arr.length', '0'); |
barraclough@apple.com | b1db28d8 | 2012-03-06 07:23:21 +0000 | [diff] [blame] | 81 | // In strict mode, this throws. |
| 82 | shouldThrow('"use strict"; var arr = Object.preventExtensions([]); arr[0] = 42; arr[0]'); |
barraclough@apple.com | 2668db9 | 2012-02-22 23:46:48 +0000 | [diff] [blame] | 83 | |
| 84 | // A read-only property on the prototype should prevent a [[Put]] . |
| 85 | function Constructor() {} |
| 86 | Constructor.prototype.foo = 1; |
| 87 | Object.freeze(Constructor.prototype); |
| 88 | var obj = new Constructor(); |
| 89 | obj.foo = 2; |
| 90 | shouldBe('obj.foo', '1'); |
| 91 | |
barraclough@apple.com | 9ce855c | 2012-03-11 18:58:04 +0000 | [diff] [blame] | 92 | // Check that freezing a function works correctly. |
| 93 | var func = freeze(function foo(){}); |
| 94 | shouldBeTrue('Object.isFrozen(func)') |
| 95 | func.prototype = 42; |
| 96 | shouldBeFalse('func.prototype === 42'); |
| 97 | shouldBeFalse('Object.getOwnPropertyDescriptor(func, "prototype").writable') |
| 98 | |
barraclough@apple.com | 7ef4152 | 2012-03-19 21:34:25 +0000 | [diff] [blame] | 99 | // Check that freezing a strict function works correctly. |
| 100 | var strictFunc = freeze(function foo(){ "use strict"; }); |
| 101 | shouldBeTrue('Object.isFrozen(strictFunc)') |
| 102 | strictFunc.prototype = 42; |
| 103 | shouldBeFalse('strictFunc.prototype === 42'); |
| 104 | shouldBeFalse('Object.getOwnPropertyDescriptor(strictFunc, "prototype").writable') |
| 105 | |
barraclough@apple.com | 2668db9 | 2012-02-22 23:46:48 +0000 | [diff] [blame] | 106 | // Check that freezing array objects works correctly. |
| 107 | var array = freeze([0,1,2]); |
barraclough@apple.com | 9ce855c | 2012-03-11 18:58:04 +0000 | [diff] [blame] | 108 | shouldBeTrue('Object.isFrozen(array)') |
barraclough@apple.com | 2668db9 | 2012-02-22 23:46:48 +0000 | [diff] [blame] | 109 | array[0] = 3; |
| 110 | shouldBe('array[0]', '0'); |
barraclough@apple.com | 4e34894 | 2012-02-23 19:43:07 +0000 | [diff] [blame] | 111 | shouldBeFalse('Object.getOwnPropertyDescriptor(array, "length").writable') |
barraclough@apple.com | 2668db9 | 2012-02-22 23:46:48 +0000 | [diff] [blame] | 112 | |
| 113 | // Check that freezing arguments objects works correctly. |
| 114 | var args = freeze((function(){ return arguments; })(0,1,2)); |
barraclough@apple.com | 9ce855c | 2012-03-11 18:58:04 +0000 | [diff] [blame] | 115 | shouldBeTrue('Object.isFrozen(args)') |
barraclough@apple.com | 2668db9 | 2012-02-22 23:46:48 +0000 | [diff] [blame] | 116 | args[0] = 3; |
| 117 | shouldBe('args[0]', '0'); |
barraclough@apple.com | 4e34894 | 2012-02-23 19:43:07 +0000 | [diff] [blame] | 118 | shouldBeFalse('Object.getOwnPropertyDescriptor(args, "length").writable') |
| 119 | shouldBeFalse('Object.getOwnPropertyDescriptor(args, "callee").writable') |
barraclough@apple.com | 7ef4152 | 2012-03-19 21:34:25 +0000 | [diff] [blame] | 120 | |
| 121 | // Check that freeze still works if preventExtensions has been called on the object. |
| 122 | function preventExtensionsFreezeIsFrozen(x) |
| 123 | { |
| 124 | Object.preventExtensions(x); |
| 125 | Object.freeze(x); |
| 126 | return Object.isFrozen(x); |
| 127 | } |
| 128 | shouldBeTrue('preventExtensionsFreezeIsFrozen(function foo(){})') |
| 129 | shouldBeTrue('preventExtensionsFreezeIsFrozen(function foo(){ "use strict"; })') |
| 130 | shouldBeTrue('preventExtensionsFreezeIsFrozen([0,1,2])') |
| 131 | shouldBeTrue('preventExtensionsFreezeIsFrozen((function(){ return arguments; })(0,1,2))') |
| 132 | |
barraclough@apple.com | fb498f4 | 2012-09-25 06:48:35 +0000 | [diff] [blame] | 133 | shouldBeFalse('Object.getOwnPropertyDescriptor(freeze({0:0}), 0).configurable'); |
| 134 | shouldBeFalse('Object.getOwnPropertyDescriptor(freeze({10000001:0}), 10000001).configurable'); |