undeletable-functions.js revision a7e24c173cf37484693b9abb38e494fa7bd7baeb
1// 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
28// 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.
33
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"];
40CheckJSCSemantics(Array.prototype, array, "Array prototype");
41
42array = [
43  "toString", "toDateString", "toTimeString", "toLocaleString",
44  "toLocaleDateString", "toLocaleTimeString", "valueOf", "getTime",
45  "getFullYear", "getUTCFullYear", "getMonth", "getUTCMonth", "getDate",
46  "getUTCDate", "getDay", "getUTCDay", "getHours", "getUTCHours", "getMinutes",
47  "getUTCMinutes", "getSeconds", "getUTCSeconds", "getMilliseconds",
48  "getUTCMilliseconds", "getTimezoneOffset", "setTime", "setMilliseconds",
49  "setUTCMilliseconds", "setSeconds", "setUTCSeconds", "setMinutes",
50  "setUTCMinutes", "setHours", "setUTCHours", "setDate", "setUTCDate",
51  "setMonth", "setUTCMonth", "setFullYear", "setUTCFullYear", "toGMTString",
52  "toUTCString", "getYear", "setYear", "toISOString", "toJSON"];
53CheckJSCSemantics(Date.prototype, array, "Date prototype");
54
55array = [
56  "random", "abs", "acos", "asin", "atan", "ceil", "cos", "exp", "floor", "log",
57  "round", "sin", "sqrt", "tan", "atan2", "pow", "max", "min"];
58CheckJSCSemantics(Math, array, "Math1");
59
60CheckEcmaSemantics(Date, ["UTC", "parse", "now"], "Date");
61
62array = [
63  "E", "LN10", "LN2", "LOG2E", "LOG10E", "PI", "SQRT1_2", "SQRT2"];
64CheckDontDelete(Math, array, "Math2");
65
66array = [
67  "escape", "unescape", "decodeURI", "decodeURIComponent", "encodeURI",
68  "encodeURIComponent", "isNaN", "isFinite", "parseInt", "parseFloat", "eval",
69  "execScript"];
70CheckEcmaSemantics(this, array, "Global");
71CheckReadOnlyAttr(this, "Infinity");
72
73array = ["exec", "test", "toString", "compile"];
74CheckEcmaSemantics(RegExp.prototype, array, "RegExp prototype");
75
76array = [
77  "toString", "toLocaleString", "valueOf", "hasOwnProperty",
78  "isPrototypeOf", "propertyIsEnumerable", "__defineGetter__",
79  "__lookupGetter__", "__defineSetter__", "__lookupSetter__"];
80CheckEcmaSemantics(Object.prototype, array, "Object prototype");
81
82array = [
83  "toString", "valueOf", "toJSON"];
84CheckEcmaSemantics(Boolean.prototype, array, "Boolean prototype");
85
86array = [
87  "toString", "toLocaleString", "valueOf", "toFixed", "toExponential",
88  "toPrecision", "toJSON"];
89CheckEcmaSemantics(Number.prototype, array, "Number prototype");
90
91CheckEcmaSemantics(Function.prototype, ["toString"], "Function prototype");
92CheckEcmaSemantics(Date.prototype, ["constructor"], "Date prototype constructor");
93
94array = [
95  "charAt", "charCodeAt", "concat", "indexOf",
96  "lastIndexOf", "localeCompare", "match", "replace", "search", "slice",
97  "split", "substring", "substr", "toLowerCase", "toLocaleLowerCase",
98  "toUpperCase", "toLocaleUpperCase", "link", "anchor", "fontcolor", "fontsize",
99  "big", "blink", "bold", "fixed", "italics", "small", "strike", "sub", "sup",
100  "toJSON", "toString", "valueOf"];
101CheckJSCSemantics(String.prototype, array, "String prototype");
102CheckEcmaSemantics(String, ["fromCharCode"], "String");
103
104
105function CheckEcmaSemantics(type, props, name) {
106  print(name);
107  for (var i = 0; i < props.length; i++) {
108    CheckDeletable(type, props[i]);
109  }
110}
111
112
113function CheckJSCSemantics(type, props, name) {
114  print(name);
115  for (var i = 0; i < props.length; i++) {
116    CheckNotDeletable(type, props[i]);
117  }
118}
119
120
121function CheckDontDelete(type, props, name) {
122  print(name);
123  for (var i = 0; i < props.length; i++) {
124    CheckDontDeleteAttr(type, props[i]);
125  }
126}
127
128
129function CheckDeletable(type, prop) {
130  var old = type[prop];
131  var hasOwnProperty = Object.prototype.hasOwnProperty;
132  if (!type[prop]) return;
133  assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
134  var deleted = delete type[prop];
135  assertTrue(deleted, "delete operator returned false: " + prop);
136  assertFalse(hasOwnProperty.call(type, prop), "still there after delete: " + prop);
137  type[prop] = "foo";
138  assertEquals("foo", type[prop], "not overwritable: " + prop);
139  type[prop] = old;
140}
141
142
143function CheckNotDeletable(type, prop) {
144  var old = type[prop];
145  if (!type[prop]) return;
146  assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
147  var deleted = delete type[prop];
148  assertTrue(deleted, "delete operator returned false: " + prop);
149  assertTrue(type.hasOwnProperty(prop), "not there after delete: " + prop);
150  type[prop] = "foo";
151  assertEquals("foo", type[prop], "not overwritable: " + prop);
152  deleted = delete type[prop];
153  assertTrue(deleted, "delete operator returned false 2nd time: " + prop);
154  assertEquals(old.toString(), type[prop].toString(), "delete didn't restore the old value: " + prop);
155}
156
157
158function CheckDontDeleteAttr(type, prop) {
159  var old = type[prop];
160  if (!type[prop]) return;
161  assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
162  var deleted = delete type[prop];
163  assertFalse(deleted, "delete operator returned true: " + prop);
164  assertTrue(type.hasOwnProperty(prop), "not there after delete: " + prop);
165  type[prop] = "foo";
166  assertFalse("foo" == type[prop], "overwritable: " + prop);
167}
168
169
170function CheckReadOnlyAttr(type, prop) {
171  var old = type[prop];
172  if (!type[prop]) return;
173  assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
174  var deleted = delete type[prop];
175  assertFalse(deleted, "delete operator returned true: " + prop);
176  assertTrue(type.hasOwnProperty(prop), "not there after delete: " + prop);
177  type[prop] = "foo";
178  assertEquals("foo", type[prop], "overwritable: " + prop);
179}
180
181print("OK");
182