blob: 04fd06068d0b7b7616070b143ec4aa4b9c015a36 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 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
Ben Murdoch85b71792012-04-11 18:30:58 +010028// Test that we match JSC in making some functions undeletable.
29// See http://code.google.com/p/chromium/issues/detail?id=1717
30// The functions on these prototypes are not just undeletable. It is
31// possible to override them with new definitions, then get the old
32// version back by deleting the new definition.
Steve Blocka7e24c12009-10-30 11:49:00 +000033
34var array;
35
36array = [
37 "toString", "toLocaleString", "join", "pop", "push", "concat", "reverse",
38 "shift", "unshift", "slice", "splice", "sort", "filter", "forEach", "some",
39 "every", "map", "indexOf", "lastIndexOf", "reduce", "reduceRight"];
Ben Murdoch85b71792012-04-11 18:30:58 +010040CheckJSCSemantics(Array.prototype, array, "Array prototype");
Steve Blocka7e24c12009-10-30 11:49:00 +000041
Steve Block6ded16b2010-05-10 14:33:55 +010042var old_Array_prototype = Array.prototype;
43var new_Array_prototype = {};
44for (var i = 0; i < 7; i++) {
45 Array.prototype = new_Array_prototype;
46 assertEquals(old_Array_prototype, Array.prototype);
47}
48
Steve Blocka7e24c12009-10-30 11:49:00 +000049array = [
50 "toString", "toDateString", "toTimeString", "toLocaleString",
51 "toLocaleDateString", "toLocaleTimeString", "valueOf", "getTime",
52 "getFullYear", "getUTCFullYear", "getMonth", "getUTCMonth", "getDate",
53 "getUTCDate", "getDay", "getUTCDay", "getHours", "getUTCHours", "getMinutes",
54 "getUTCMinutes", "getSeconds", "getUTCSeconds", "getMilliseconds",
55 "getUTCMilliseconds", "getTimezoneOffset", "setTime", "setMilliseconds",
56 "setUTCMilliseconds", "setSeconds", "setUTCSeconds", "setMinutes",
57 "setUTCMinutes", "setHours", "setUTCHours", "setDate", "setUTCDate",
58 "setMonth", "setUTCMonth", "setFullYear", "setUTCFullYear", "toGMTString",
59 "toUTCString", "getYear", "setYear", "toISOString", "toJSON"];
Ben Murdoch85b71792012-04-11 18:30:58 +010060CheckJSCSemantics(Date.prototype, array, "Date prototype");
Steve Blocka7e24c12009-10-30 11:49:00 +000061
62array = [
63 "random", "abs", "acos", "asin", "atan", "ceil", "cos", "exp", "floor", "log",
64 "round", "sin", "sqrt", "tan", "atan2", "pow", "max", "min"];
Ben Murdoch85b71792012-04-11 18:30:58 +010065CheckJSCSemantics(Math, array, "Math1");
Steve Blocka7e24c12009-10-30 11:49:00 +000066
67CheckEcmaSemantics(Date, ["UTC", "parse", "now"], "Date");
68
69array = [
70 "E", "LN10", "LN2", "LOG2E", "LOG10E", "PI", "SQRT1_2", "SQRT2"];
71CheckDontDelete(Math, array, "Math2");
72
73array = [
74 "escape", "unescape", "decodeURI", "decodeURIComponent", "encodeURI",
75 "encodeURIComponent", "isNaN", "isFinite", "parseInt", "parseFloat", "eval",
76 "execScript"];
77CheckEcmaSemantics(this, array, "Global");
78CheckReadOnlyAttr(this, "Infinity");
79
80array = ["exec", "test", "toString", "compile"];
81CheckEcmaSemantics(RegExp.prototype, array, "RegExp prototype");
82
83array = [
84 "toString", "toLocaleString", "valueOf", "hasOwnProperty",
85 "isPrototypeOf", "propertyIsEnumerable", "__defineGetter__",
86 "__lookupGetter__", "__defineSetter__", "__lookupSetter__"];
87CheckEcmaSemantics(Object.prototype, array, "Object prototype");
88
Steve Block6ded16b2010-05-10 14:33:55 +010089var old_Object_prototype = Object.prototype;
90var new_Object_prototype = {};
91for (var i = 0; i < 7; i++) {
92 Object.prototype = new_Object_prototype;
93 assertEquals(old_Object_prototype, Object.prototype);
94}
95
Steve Blocka7e24c12009-10-30 11:49:00 +000096array = [
97 "toString", "valueOf", "toJSON"];
98CheckEcmaSemantics(Boolean.prototype, array, "Boolean prototype");
99
100array = [
101 "toString", "toLocaleString", "valueOf", "toFixed", "toExponential",
102 "toPrecision", "toJSON"];
103CheckEcmaSemantics(Number.prototype, array, "Number prototype");
104
105CheckEcmaSemantics(Function.prototype, ["toString"], "Function prototype");
106CheckEcmaSemantics(Date.prototype, ["constructor"], "Date prototype constructor");
107
108array = [
109 "charAt", "charCodeAt", "concat", "indexOf",
110 "lastIndexOf", "localeCompare", "match", "replace", "search", "slice",
111 "split", "substring", "substr", "toLowerCase", "toLocaleLowerCase",
112 "toUpperCase", "toLocaleUpperCase", "link", "anchor", "fontcolor", "fontsize",
113 "big", "blink", "bold", "fixed", "italics", "small", "strike", "sub", "sup",
114 "toJSON", "toString", "valueOf"];
Ben Murdoch85b71792012-04-11 18:30:58 +0100115CheckJSCSemantics(String.prototype, array, "String prototype");
Steve Blocka7e24c12009-10-30 11:49:00 +0000116CheckEcmaSemantics(String, ["fromCharCode"], "String");
117
118
119function CheckEcmaSemantics(type, props, name) {
120 print(name);
121 for (var i = 0; i < props.length; i++) {
122 CheckDeletable(type, props[i]);
123 }
124}
125
126
Ben Murdoch85b71792012-04-11 18:30:58 +0100127function CheckJSCSemantics(type, props, name) {
128 print(name);
129 for (var i = 0; i < props.length; i++) {
130 CheckNotDeletable(type, props[i]);
131 }
132}
133
134
Steve Blocka7e24c12009-10-30 11:49:00 +0000135function CheckDontDelete(type, props, name) {
136 print(name);
137 for (var i = 0; i < props.length; i++) {
138 CheckDontDeleteAttr(type, props[i]);
139 }
140}
141
142
143function CheckDeletable(type, prop) {
144 var old = type[prop];
145 var hasOwnProperty = Object.prototype.hasOwnProperty;
146 if (!type[prop]) return;
147 assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
148 var deleted = delete type[prop];
149 assertTrue(deleted, "delete operator returned false: " + prop);
150 assertFalse(hasOwnProperty.call(type, prop), "still there after delete: " + prop);
151 type[prop] = "foo";
152 assertEquals("foo", type[prop], "not overwritable: " + prop);
153 type[prop] = old;
154}
155
156
Ben Murdoch85b71792012-04-11 18:30:58 +0100157function CheckNotDeletable(type, prop) {
158 var old = type[prop];
159 if (!type[prop]) return;
160 assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
161 var deleted = delete type[prop];
162 assertTrue(deleted, "delete operator returned false: " + prop);
163 assertTrue(type.hasOwnProperty(prop), "not there after delete: " + prop);
164 type[prop] = "foo";
165 assertEquals("foo", type[prop], "not overwritable: " + prop);
166 deleted = delete type[prop];
167 assertTrue(deleted, "delete operator returned false 2nd time: " + prop);
168 assertEquals(old.toString(), type[prop].toString(), "delete didn't restore the old value: " + prop);
169}
170
171
Steve Blocka7e24c12009-10-30 11:49:00 +0000172function CheckDontDeleteAttr(type, prop) {
173 var old = type[prop];
174 if (!type[prop]) return;
175 assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
176 var deleted = delete type[prop];
177 assertFalse(deleted, "delete operator returned true: " + prop);
178 assertTrue(type.hasOwnProperty(prop), "not there after delete: " + prop);
179 type[prop] = "foo";
180 assertFalse("foo" == type[prop], "overwritable: " + prop);
181}
182
183
184function CheckReadOnlyAttr(type, prop) {
185 var old = type[prop];
186 if (!type[prop]) return;
187 assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
188 var deleted = delete type[prop];
189 assertFalse(deleted, "delete operator returned true: " + prop);
190 assertTrue(type.hasOwnProperty(prop), "not there after delete: " + prop);
191 type[prop] = "foo";
Ben Murdoch85b71792012-04-11 18:30:58 +0100192 assertEquals("foo", type[prop], "overwritable: " + prop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000193}
194
195print("OK");