blob: 2bf8b099b42b5501fc687861faa31dd76e3513b6 [file] [log] [blame]
barraclough@apple.comdd993102011-03-04 22:34:48 +00001description(
2"This test checks whether various seal/freeze/preventExtentions work on a regular object."
3);
4
5function obj()
6{
barraclough@apple.come2ea07b2012-02-16 19:06:03 +00007 // 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.comdd993102011-03-04 22:34:48 +00009}
10
11function 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
29function seal(obj)
30{
31 Object.seal(obj);
32 return obj;
33}
34
35function freeze(obj)
36{
37 Object.freeze(obj);
38 return obj;
39}
40
41function preventExtensions(obj)
42{
43 Object.preventExtensions(obj);
44 return obj;
45}
46
oliver@apple.com6f34f972011-06-01 18:08:07 +000047function inextensible(){}
48function sealed(){}
49function frozen(){};
50preventExtensions(inextensible);
51seal(sealed);
52freeze(frozen);
53new inextensible;
54new sealed;
55new frozen;
56inextensible.prototype.prototypeExists = true;
57sealed.prototype.prototypeExists = true;
58frozen.prototype.prototypeExists = true;
59
60shouldBeTrue("(new inextensible).prototypeExists");
61shouldBeTrue("(new sealed).prototypeExists");
62shouldBeTrue("(new frozen).prototypeExists");
63
barraclough@apple.comdd993102011-03-04 22:34:48 +000064shouldBe('test(obj())', '"(b:4)(c:3)E"'); // extensible, can delete a, can modify b, and can add c
65shouldBe('test(preventExtensions(obj()))', '"(b:4)"'); // <nothing>, can delete a, can modify b, and CANNOT add c
66shouldBe('test(seal(obj()))', '"(a:1)(b:4)S"'); // sealed, CANNOT delete a, can modify b, and CANNOT add c
67shouldBe('test(freeze(obj()))', '"(a:1)(b:2)SF"'); // sealed and frozen, CANNOT delete a, CANNOT modify b, and CANNOT add c
68
barraclough@apple.com7fccc522011-07-05 19:02:44 +000069// check that we can preventExtensions on a host function.
70shouldBe('Object.preventExtensions(Math.sin)', 'Math.sin');
71
barraclough@apple.com4f5c0c02012-02-20 21:14:48 +000072shouldThrow('var o = {}; Object.preventExtensions(o); o.__proto__ = { newProp: "Should not see this" }; o.newProp;');
73shouldThrow('"use strict"; var o = {}; Object.preventExtensions(o); o.__proto__ = { newProp: "Should not see this" }; o.newProp;');
oliver@apple.comd5bf7d02011-08-08 19:09:51 +000074
barraclough@apple.comd55cc592011-08-15 18:51:42 +000075// check that we can still access static properties on an object after calling preventExtensions.
76shouldBe('Object.preventExtensions(Math); Math.sqrt(4)', '2');
barraclough@apple.come2ea07b2012-02-16 19:06:03 +000077
78// Should not be able to add properties to a preventExtensions array.
79shouldBeUndefined('var arr = Object.preventExtensions([]); arr[0] = 42; arr[0]');
80shouldBe('var arr = Object.preventExtensions([]); arr[0] = 42; arr.length', '0');
barraclough@apple.comb1db28d82012-03-06 07:23:21 +000081// In strict mode, this throws.
82shouldThrow('"use strict"; var arr = Object.preventExtensions([]); arr[0] = 42; arr[0]');
barraclough@apple.com2668db92012-02-22 23:46:48 +000083
84// A read-only property on the prototype should prevent a [[Put]] .
85function Constructor() {}
86Constructor.prototype.foo = 1;
87Object.freeze(Constructor.prototype);
88var obj = new Constructor();
89obj.foo = 2;
90shouldBe('obj.foo', '1');
91
barraclough@apple.com9ce855c2012-03-11 18:58:04 +000092// Check that freezing a function works correctly.
93var func = freeze(function foo(){});
94shouldBeTrue('Object.isFrozen(func)')
95func.prototype = 42;
96shouldBeFalse('func.prototype === 42');
97shouldBeFalse('Object.getOwnPropertyDescriptor(func, "prototype").writable')
98
barraclough@apple.com7ef41522012-03-19 21:34:25 +000099// Check that freezing a strict function works correctly.
100var strictFunc = freeze(function foo(){ "use strict"; });
101shouldBeTrue('Object.isFrozen(strictFunc)')
102strictFunc.prototype = 42;
103shouldBeFalse('strictFunc.prototype === 42');
104shouldBeFalse('Object.getOwnPropertyDescriptor(strictFunc, "prototype").writable')
105
barraclough@apple.com2668db92012-02-22 23:46:48 +0000106// Check that freezing array objects works correctly.
107var array = freeze([0,1,2]);
barraclough@apple.com9ce855c2012-03-11 18:58:04 +0000108shouldBeTrue('Object.isFrozen(array)')
barraclough@apple.com2668db92012-02-22 23:46:48 +0000109array[0] = 3;
110shouldBe('array[0]', '0');
barraclough@apple.com4e348942012-02-23 19:43:07 +0000111shouldBeFalse('Object.getOwnPropertyDescriptor(array, "length").writable')
barraclough@apple.com2668db92012-02-22 23:46:48 +0000112
113// Check that freezing arguments objects works correctly.
114var args = freeze((function(){ return arguments; })(0,1,2));
barraclough@apple.com9ce855c2012-03-11 18:58:04 +0000115shouldBeTrue('Object.isFrozen(args)')
barraclough@apple.com2668db92012-02-22 23:46:48 +0000116args[0] = 3;
117shouldBe('args[0]', '0');
barraclough@apple.com4e348942012-02-23 19:43:07 +0000118shouldBeFalse('Object.getOwnPropertyDescriptor(args, "length").writable')
119shouldBeFalse('Object.getOwnPropertyDescriptor(args, "callee").writable')
barraclough@apple.com7ef41522012-03-19 21:34:25 +0000120
121// Check that freeze still works if preventExtensions has been called on the object.
122function preventExtensionsFreezeIsFrozen(x)
123{
124 Object.preventExtensions(x);
125 Object.freeze(x);
126 return Object.isFrozen(x);
127}
128shouldBeTrue('preventExtensionsFreezeIsFrozen(function foo(){})')
129shouldBeTrue('preventExtensionsFreezeIsFrozen(function foo(){ "use strict"; })')
130shouldBeTrue('preventExtensionsFreezeIsFrozen([0,1,2])')
131shouldBeTrue('preventExtensionsFreezeIsFrozen((function(){ return arguments; })(0,1,2))')
132
barraclough@apple.comfb498f42012-09-25 06:48:35 +0000133shouldBeFalse('Object.getOwnPropertyDescriptor(freeze({0:0}), 0).configurable');
134shouldBeFalse('Object.getOwnPropertyDescriptor(freeze({10000001:0}), 10000001).configurable');