Home | History | Annotate | Download | only in js
      1 // Copyright 2013 the V8 project authors. All rights reserved.
      2 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions
      6 // are met:
      7 // 1.  Redistributions of source code must retain the above copyright
      8 //     notice, this list of conditions and the following disclaimer.
      9 // 2.  Redistributions in binary form must reproduce the above copyright
     10 //     notice, this list of conditions and the following disclaimer in the
     11 //     documentation and/or other materials provided with the distribution.
     12 //
     13 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
     14 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     16 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
     20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     23 
     24 description("Test to ensure correct behaviour of Object.getOwnPropertyNames");
     25 
     26 function argumentsObject() { return arguments; };
     27 
     28 var expectedPropertyNamesSet = {
     29     "{}": "[]",
     30     "{a:null}": "['a']",
     31     "{a:null, b:null}": "['a', 'b']",
     32     "{b:null, a:null}": "['a', 'b']",
     33     "{__proto__:{a:null}}": "[]",
     34     "{__proto__:[1,2,3]}": "[]",
     35     "Object.create({}, { 'a': { 'value': 1, 'enumerable': false } })": "['a']",
     36     "Object.create([1,2,3], { 'a': { 'value': 1, 'enumerable': false } })": "['a']",
     37 // Function objects
     38     "new Function()": "['arguments', 'caller', 'length', 'name', 'prototype']",
     39     "(function(){var x=new Function();x.__proto__=[1,2,3];return x;})()": "['arguments', 'caller', 'length', 'name', 'prototype']",
     40 // String objects
     41     "new String('')": "['length']",
     42     "new String('a')": "['0', 'length']",
     43     "new String('abc')": "['0', '1', '2', 'length']",
     44     "(function(){var x=new String('');x.__proto__=[1,2,3];return x;})()": "['length']",
     45 // Array objects
     46     "[]": "['length']",
     47     "[null]": "['0', 'length']",
     48     "[null,null]": "['0','1', 'length']",
     49     "[null,null,,,,null]": "['0','1','5', 'length']",
     50     "(function(){var x=[];x.__proto__=[1,2,3];return x;})()": "['length']",
     51 // Date objects
     52     "new Date()": "[]",
     53     "(function(){var x=new Date();x.__proto__=[1,2,3];return x;})()": "[]",
     54 // RegExp objects
     55     "new RegExp('foo')": "['global', 'ignoreCase', 'lastIndex', 'multiline', 'source']",
     56     "(function(){var x=new RegExp();x.__proto__=[1,2,3];return x;})()": "['global', 'ignoreCase', 'lastIndex', 'multiline', 'source']",
     57 // Arguments objects
     58      "argumentsObject()": "['callee', 'length']",
     59      "argumentsObject(1)": "['0', 'callee', 'length']",
     60      "argumentsObject(1,2,3)": "['0', '1', '2', 'callee', 'length']",
     61     "(function(){arguments.__proto__=[1,2,3];return arguments;})()": "['callee', 'length']",
     62 // Built-in ECMA functions
     63     "parseInt": "['length', 'name']",
     64     "parseFloat": "['length', 'name']",
     65     "isNaN": "['length', 'name']",
     66     "isFinite": "['length', 'name']",
     67     "escape": "['length', 'name']",
     68     "unescape": "['length', 'name']",
     69     "decodeURI": "['length', 'name']",
     70     "decodeURIComponent": "['length', 'name']",
     71     "encodeURI": "['length', 'name']",
     72     "encodeURIComponent": "['length', 'name']",
     73 // Built-in ECMA objects
     74     "Object": "['create', 'defineProperties', 'defineProperty', 'freeze', 'getOwnPropertyDescriptor', 'getOwnPropertyNames', 'getPrototypeOf', 'isExtensible', 'isFrozen', 'isSealed', 'keys', 'length', 'name', 'preventExtensions', 'prototype', 'seal']",
     75     "Object.prototype": "['__defineGetter__', '__defineSetter__', '__lookupGetter__', '__lookupSetter__', '__proto__', 'constructor', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', 'toLocaleString', 'toString', 'valueOf']",
     76     "Function": "['length', 'name', 'prototype']",
     77     "Function.prototype": "['apply', 'bind', 'call', 'constructor', 'length', 'name', 'toString']",
     78     "Array": "['isArray', 'length', 'name', 'prototype']",
     79     "Array.prototype": "['concat', 'constructor', 'every', 'filter', 'forEach', 'indexOf', 'join', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift']",
     80     "String": "['fromCharCode', 'length', 'name', 'prototype']",
     81     "String.prototype": "['anchor', 'big', 'blink', 'bold', 'charAt', 'charCodeAt', 'concat', 'constructor', 'fixed', 'fontcolor', 'fontsize', 'indexOf', 'italics', 'lastIndexOf', 'length', 'link', 'localeCompare', 'match', 'replace', 'search', 'slice', 'small', 'split', 'strike', 'sub', 'substr', 'substring', 'sup', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'trimLeft', 'trimRight', 'valueOf']",
     82     "Boolean": "['length', 'name', 'prototype']",
     83     "Boolean.prototype": "['constructor', 'toString', 'valueOf']",
     84     "Number": "['MAX_VALUE', 'MIN_VALUE', 'NEGATIVE_INFINITY', 'NaN', 'POSITIVE_INFINITY', 'length', 'name', 'prototype']",
     85     "Number.prototype": "['constructor', 'toExponential', 'toFixed', 'toLocaleString', 'toPrecision', 'toString', 'valueOf']",
     86     "Date": "['UTC', 'length', 'name', 'now', 'parse', 'prototype']",
     87     "Date.prototype": "['constructor', 'getDate', 'getDay', 'getFullYear', 'getHours', 'getMilliseconds', 'getMinutes', 'getMonth', 'getSeconds', 'getTime', 'getTimezoneOffset', 'getUTCDate', 'getUTCDay', 'getUTCFullYear', 'getUTCHours', 'getUTCMilliseconds', 'getUTCMinutes', 'getUTCMonth', 'getUTCSeconds', 'getYear', 'setDate', 'setFullYear', 'setHours', 'setMilliseconds', 'setMinutes', 'setMonth', 'setSeconds', 'setTime', 'setUTCDate', 'setUTCFullYear', 'setUTCHours', 'setUTCMilliseconds', 'setUTCMinutes', 'setUTCMonth', 'setUTCSeconds', 'setYear', 'toDateString', 'toGMTString', 'toISOString', 'toJSON', 'toLocaleDateString', 'toLocaleString', 'toLocaleTimeString', 'toString', 'toTimeString', 'toUTCString', 'valueOf']",
     88     "RegExp": "['$&', \"$'\", '$*', '$+', '$1', '$2', '$3', '$4', '$5', '$6', '$7', '$8', '$9', '$_', '$`', 'input', 'lastMatch', 'lastParen', 'leftContext', 'length', 'multiline', 'name', 'prototype', 'rightContext']",
     89     "RegExp.prototype": "['compile', 'constructor', 'exec', 'global', 'ignoreCase', 'lastIndex', 'multiline', 'source', 'test', 'toString']",
     90     "Error": "['length', 'name', 'prototype']",
     91     "Error.prototype": "['constructor', 'message', 'name', 'toString']",
     92     "Math": "['E', 'LN10', 'LN2', 'LOG10E', 'LOG2E', 'PI', 'SQRT1_2', 'SQRT2', 'abs', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'exp', 'floor', 'log', 'max', 'min', 'pow', 'random', 'round', 'sin', 'sqrt', 'tan']",
     93     "JSON": "['parse', 'stringify']"
     94 };
     95 
     96 function getSortedOwnPropertyNames(obj)
     97 {
     98     return Object.getOwnPropertyNames(obj).sort();
     99 }
    100 
    101 for (var expr in expectedPropertyNamesSet)
    102     shouldBe("getSortedOwnPropertyNames(" + expr + ")", expectedPropertyNamesSet[expr]);
    103 
    104 // Global Object
    105 // Only check for ECMA properties here
    106 var globalPropertyNames = Object.getOwnPropertyNames(this);
    107 var expectedGlobalPropertyNames = [
    108     "NaN",
    109     "Infinity",
    110     "undefined",
    111     "parseInt",
    112     "parseFloat",
    113     "isNaN",
    114     "isFinite",
    115     "escape",
    116     "unescape",
    117     "decodeURI",
    118     "decodeURIComponent",
    119     "encodeURI",
    120     "encodeURIComponent",
    121     "Object",
    122     "Function",
    123     "Array",
    124     "String",
    125     "Boolean",
    126     "Number",
    127     "Date",
    128     "RegExp",
    129     "Error",
    130     "Math",
    131     "JSON"
    132 ];
    133 
    134 for (var i = 0; i < expectedGlobalPropertyNames.length; ++i)
    135     shouldBeTrue("globalPropertyNames.indexOf('" + expectedGlobalPropertyNames[i] + "') != -1");
    136