blob: 635ea6fa9aca8752c280559ed193e9216766a2b3 [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 Murdoch3ef787d2012-04-12 10:51:47 +010028// Test that we match ECMAScript in making most builtin functions
29// deletable and only specific ones undeletable or read-only.
Steve Blocka7e24c12009-10-30 11:49:00 +000030
31var array;
32
33array = [
34 "toString", "toLocaleString", "join", "pop", "push", "concat", "reverse",
35 "shift", "unshift", "slice", "splice", "sort", "filter", "forEach", "some",
36 "every", "map", "indexOf", "lastIndexOf", "reduce", "reduceRight"];
Ben Murdoch3ef787d2012-04-12 10:51:47 +010037CheckEcmaSemantics(Array.prototype, array, "Array prototype");
Steve Blocka7e24c12009-10-30 11:49:00 +000038
Steve Block6ded16b2010-05-10 14:33:55 +010039var old_Array_prototype = Array.prototype;
40var new_Array_prototype = {};
41for (var i = 0; i < 7; i++) {
42 Array.prototype = new_Array_prototype;
43 assertEquals(old_Array_prototype, Array.prototype);
44}
45
Steve Blocka7e24c12009-10-30 11:49:00 +000046array = [
47 "toString", "toDateString", "toTimeString", "toLocaleString",
48 "toLocaleDateString", "toLocaleTimeString", "valueOf", "getTime",
49 "getFullYear", "getUTCFullYear", "getMonth", "getUTCMonth", "getDate",
50 "getUTCDate", "getDay", "getUTCDay", "getHours", "getUTCHours", "getMinutes",
51 "getUTCMinutes", "getSeconds", "getUTCSeconds", "getMilliseconds",
52 "getUTCMilliseconds", "getTimezoneOffset", "setTime", "setMilliseconds",
53 "setUTCMilliseconds", "setSeconds", "setUTCSeconds", "setMinutes",
54 "setUTCMinutes", "setHours", "setUTCHours", "setDate", "setUTCDate",
55 "setMonth", "setUTCMonth", "setFullYear", "setUTCFullYear", "toGMTString",
56 "toUTCString", "getYear", "setYear", "toISOString", "toJSON"];
Ben Murdoch3ef787d2012-04-12 10:51:47 +010057CheckEcmaSemantics(Date.prototype, array, "Date prototype");
Steve Blocka7e24c12009-10-30 11:49:00 +000058
59array = [
60 "random", "abs", "acos", "asin", "atan", "ceil", "cos", "exp", "floor", "log",
61 "round", "sin", "sqrt", "tan", "atan2", "pow", "max", "min"];
Ben Murdoch3ef787d2012-04-12 10:51:47 +010062CheckEcmaSemantics(Math, array, "Math1");
Steve Blocka7e24c12009-10-30 11:49:00 +000063
64CheckEcmaSemantics(Date, ["UTC", "parse", "now"], "Date");
65
66array = [
67 "E", "LN10", "LN2", "LOG2E", "LOG10E", "PI", "SQRT1_2", "SQRT2"];
68CheckDontDelete(Math, array, "Math2");
69
70array = [
71 "escape", "unescape", "decodeURI", "decodeURIComponent", "encodeURI",
72 "encodeURIComponent", "isNaN", "isFinite", "parseInt", "parseFloat", "eval",
73 "execScript"];
74CheckEcmaSemantics(this, array, "Global");
75CheckReadOnlyAttr(this, "Infinity");
Ben Murdoch3ef787d2012-04-12 10:51:47 +010076CheckReadOnlyAttr(this, "NaN");
77CheckReadOnlyAttr(this, "undefined");
Steve Blocka7e24c12009-10-30 11:49:00 +000078
79array = ["exec", "test", "toString", "compile"];
80CheckEcmaSemantics(RegExp.prototype, array, "RegExp prototype");
81
82array = [
83 "toString", "toLocaleString", "valueOf", "hasOwnProperty",
84 "isPrototypeOf", "propertyIsEnumerable", "__defineGetter__",
85 "__lookupGetter__", "__defineSetter__", "__lookupSetter__"];
86CheckEcmaSemantics(Object.prototype, array, "Object prototype");
87
Steve Block6ded16b2010-05-10 14:33:55 +010088var old_Object_prototype = Object.prototype;
89var new_Object_prototype = {};
90for (var i = 0; i < 7; i++) {
91 Object.prototype = new_Object_prototype;
92 assertEquals(old_Object_prototype, Object.prototype);
93}
94
Steve Blocka7e24c12009-10-30 11:49:00 +000095array = [
96 "toString", "valueOf", "toJSON"];
97CheckEcmaSemantics(Boolean.prototype, array, "Boolean prototype");
98
99array = [
100 "toString", "toLocaleString", "valueOf", "toFixed", "toExponential",
101 "toPrecision", "toJSON"];
102CheckEcmaSemantics(Number.prototype, array, "Number prototype");
103
104CheckEcmaSemantics(Function.prototype, ["toString"], "Function prototype");
105CheckEcmaSemantics(Date.prototype, ["constructor"], "Date prototype constructor");
106
107array = [
108 "charAt", "charCodeAt", "concat", "indexOf",
109 "lastIndexOf", "localeCompare", "match", "replace", "search", "slice",
110 "split", "substring", "substr", "toLowerCase", "toLocaleLowerCase",
111 "toUpperCase", "toLocaleUpperCase", "link", "anchor", "fontcolor", "fontsize",
112 "big", "blink", "bold", "fixed", "italics", "small", "strike", "sub", "sup",
113 "toJSON", "toString", "valueOf"];
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100114CheckEcmaSemantics(String.prototype, array, "String prototype");
Steve Blocka7e24c12009-10-30 11:49:00 +0000115CheckEcmaSemantics(String, ["fromCharCode"], "String");
116
117
118function CheckEcmaSemantics(type, props, name) {
119 print(name);
120 for (var i = 0; i < props.length; i++) {
121 CheckDeletable(type, props[i]);
122 }
123}
124
125
Steve Blocka7e24c12009-10-30 11:49:00 +0000126function CheckDontDelete(type, props, name) {
127 print(name);
128 for (var i = 0; i < props.length; i++) {
129 CheckDontDeleteAttr(type, props[i]);
130 }
131}
132
133
134function CheckDeletable(type, prop) {
135 var old = type[prop];
136 var hasOwnProperty = Object.prototype.hasOwnProperty;
137 if (!type[prop]) return;
138 assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
139 var deleted = delete type[prop];
140 assertTrue(deleted, "delete operator returned false: " + prop);
141 assertFalse(hasOwnProperty.call(type, prop), "still there after delete: " + prop);
142 type[prop] = "foo";
143 assertEquals("foo", type[prop], "not overwritable: " + prop);
144 type[prop] = old;
145}
146
147
Steve Blocka7e24c12009-10-30 11:49:00 +0000148function CheckDontDeleteAttr(type, prop) {
149 var old = type[prop];
150 if (!type[prop]) return;
151 assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
152 var deleted = delete type[prop];
153 assertFalse(deleted, "delete operator returned true: " + prop);
154 assertTrue(type.hasOwnProperty(prop), "not there after delete: " + prop);
155 type[prop] = "foo";
156 assertFalse("foo" == type[prop], "overwritable: " + prop);
157}
158
159
160function CheckReadOnlyAttr(type, prop) {
161 var old = type[prop];
162 if (!type[prop]) return;
163 assertTrue(type.hasOwnProperty(prop), "inherited: " + prop);
164 var deleted = delete type[prop];
165 assertFalse(deleted, "delete operator returned true: " + prop);
166 assertTrue(type.hasOwnProperty(prop), "not there after delete: " + prop);
167 type[prop] = "foo";
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100168 assertEquals(old, type[prop], "overwritable: " + prop);
Steve Blocka7e24c12009-10-30 11:49:00 +0000169}
170
171print("OK");