Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: test/mjsunit/object-prevent-extensions.js

Issue 2819034: Add ES5 Object.isExtensible and Object.preventExtensions. (Closed) Base URL: http://v8.googlecode.com/svn/branches/bleeding_edge/
Patch Set: '' Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « test/es5conform/es5conform.status ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2010 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 // Tests the Object.preventExtensions method - ES 15.2.3.10
29
30
31 var obj1 = {};
32 // Extensible defaults to true.
33 assertTrue(Object.isExtensible(obj1));
34 Object.preventExtensions(obj1);
35
36 // Make sure the is_extensible flag is set.
37 assertFalse(Object.isExtensible(obj1));
38 // Try adding a new property.
39 try {
40 obj1.x = 42;
41 assertUnreachable();
42 } catch (e) {
43 assertTrue(/object is not extensible/.test(e));
44 }
45 assertEquals(undefined, obj1.x);
46
47 // Try adding a new element.
48 try {
49 obj1[1] = 42;
50 assertUnreachable();
51 } catch (e) {
52 assertTrue(/object is not extensible/.test(e));
53 }
54 assertEquals(undefined, obj1[1]);
55
56
57 // Try when the object has an existing property.
58 var obj2 = {};
59 assertTrue(Object.isExtensible(obj2));
60 obj2.x = 42;
61 assertEquals(42, obj2.x);
62 assertTrue(Object.isExtensible(obj2));
63
64 Object.preventExtensions(obj2);
65 assertEquals(42, obj2.x);
66
67 try {
68 obj2.y = 42;
69 assertUnreachable();
70 } catch (e) {
71 assertTrue(/object is not extensible/.test(e));
72 }
73
74 // obj2.y should still be undefined.
75 assertEquals(undefined, obj2.y);
76 // Make sure we can still write values to obj.x.
77 obj2.x = 43;
78 assertEquals(43, obj2.x)
79
80 try {
81 obj2.y = new function() { return 42; };
82 assertUnreachable();
83 } catch (e) {
84 assertTrue(/object is not extensible/.test(e));
85 }
86 // obj2.y should still be undefined.
87 assertEquals(undefined, obj2.y);
88 assertEquals(43, obj2.x)
89
90 try {
91 Object.defineProperty(obj2, "y", {value: 42});
92 } catch (e) {
93 assertTrue(/object is not extensible/.test(e));
94 }
95
96 // obj2.y should still be undefined.
97 assertEquals(undefined, obj2.y);
98 assertEquals(43, obj2.x);
99
100 try {
101 obj2[1] = 42;
102 } catch (e) {
103 assertTrue(/object is not extensible/.test(e));
104 }
105
106 assertEquals(undefined, obj2[1]);
107
108 var arr = new Array();
109 arr[1] = 10;
110
111 Object.preventExtensions(arr);
112
113 try {
114 arr[2] = 42;
115 assertUnreachable();
116 } catch (e) {
117 assertTrue(/object is not extensible/.test(e));
118 }
119 assertEquals(10, arr[1]);
120
121 // We should still be able to change exiting elements.
122 arr[1]= 42;
123 assertEquals(42, arr[1]);
124
125
126 // Test the the extensible flag is not inherited.
127 var parent = {};
128 parent.x = 42;
129 Object.preventExtensions(parent);
130
131 var child = Object.create(parent);
132
133 // We should be able to add new properties to the child object.
134 child.y = 42;
135
136 // This should have no influence on the parent class.
137 try {
138 parent.y = 29;
139 assertUnreachable();
140 } catch (e) {
141 assertTrue(/object is not extensible/.test(e));
142 }
143
144
145 // Test that attributes on functions are also handled correctly.
146 function foo() {
147 return 42;
148 }
149
150 Object.preventExtensions(foo);
151
152 try {
153 foo.x = 29;
154 assertUnreachable();
155 } catch (e) {
156 assertTrue(/object is not extensible/.test(e));
157 }
OLDNEW
« no previous file with comments | « test/es5conform/es5conform.status ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698