Home | History | Annotate | Download | only in mjsunit
      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 ECMAScript in making most builtin functions
     29 // deletable and only specific ones undeletable or read-only.
     30 
     31 var array;
     32 
     33 array = [
     34   "toString", "toLocaleString", "join", "pop", "push", "concat", "reverse",
     35   "shift", "unshift", "slice", "splice", "sort", "filter", "forEach", "some",
     36   "every", "map", "indexOf", "lastIndexOf", "reduce", "reduceRight"];
     37 CheckEcmaSemantics(Array.prototype, array, "Array prototype");
     38 
     39 var old_Array_prototype = Array.prototype;
     40 var new_Array_prototype = {};
     41 for (var i = 0; i < 7; i++) {
     42   Array.prototype = new_Array_prototype;
     43   assertEquals(old_Array_prototype, Array.prototype);
     44 }
     45 
     46 array = [
     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"];
     57 CheckEcmaSemantics(Date.prototype, array, "Date prototype");
     58 
     59 array = [
     60   "random", "abs", "acos", "asin", "atan", "ceil", "cos", "exp", "floor", "log",
     61   "round", "sin", "sqrt", "tan", "atan2", "pow", "max", "min"];
     62 CheckEcmaSemantics(Math, array, "Math1");
     63 
     64 CheckEcmaSemantics(Date, ["UTC", "parse", "now"], "Date");
     65 
     66 array = [
     67   "E", "LN10", "LN2", "LOG2E", "LOG10E", "PI", "SQRT1_2", "SQRT2"];
     68 CheckDontDelete(Math, array, "Math2");
     69 
     70 array = [
     71   "escape", "unescape", "decodeURI", "decodeURIComponent", "encodeURI",
     72   "encodeURIComponent", "isNaN", "isFinite", "parseInt", "parseFloat", "eval",
     73   "execScript"];
     74 CheckEcmaSemantics(this, array, "Global");
     75 CheckReadOnlyAttr(this, "Infinity");
     76 CheckReadOnlyAttr(this, "NaN");
     77 CheckReadOnlyAttr(this, "undefined");
     78 
     79 array = ["exec", "test", "toString", "compile"];
     80 CheckEcmaSemantics(RegExp.prototype, array, "RegExp prototype");
     81 
     82 array = [
     83   "toString", "toLocaleString", "valueOf", "hasOwnProperty",
     84   "isPrototypeOf", "propertyIsEnumerable", "__defineGetter__",
     85   "__lookupGetter__", "__defineSetter__", "__lookupSetter__"];
     86 CheckEcmaSemantics(Object.prototype, array, "Object prototype");
     87 
     88 var old_Object_prototype = Object.prototype;
     89 var new_Object_prototype = {};
     90 for (var i = 0; i < 7; i++) {
     91   Object.prototype = new_Object_prototype;
     92   assertEquals(old_Object_prototype, Object.prototype);
     93 }
     94 
     95 array = [
     96   "toString", "valueOf", "toJSON"];
     97 CheckEcmaSemantics(Boolean.prototype, array, "Boolean prototype");
     98 
     99 array = [
    100   "toString", "toLocaleString", "valueOf", "toFixed", "toExponential",
    101   "toPrecision", "toJSON"];
    102 CheckEcmaSemantics(Number.prototype, array, "Number prototype");
    103 
    104 CheckEcmaSemantics(Function.prototype, ["toString"], "Function prototype");
    105 CheckEcmaSemantics(Date.prototype, ["constructor"], "Date prototype constructor");
    106 
    107 array = [
    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"];
    114 CheckEcmaSemantics(String.prototype, array, "String prototype");
    115 CheckEcmaSemantics(String, ["fromCharCode"], "String");
    116 
    117 
    118 function 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 
    126 function 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 
    134 function 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 
    148 function 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 
    160 function 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";
    168   assertEquals(old, type[prop], "overwritable: " + prop);
    169 }
    170 
    171 print("OK");
    172