js/src/builtin/Utilities.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /*jshint bitwise: true, camelcase: false, curly: false, eqeqeq: true,
michael@0 6 es5: true, forin: true, immed: true, indent: 4, latedef: false,
michael@0 7 newcap: false, noarg: true, noempty: true, nonew: true,
michael@0 8 plusplus: false, quotmark: false, regexp: true, undef: true,
michael@0 9 unused: false, strict: false, trailing: true,
michael@0 10 */
michael@0 11
michael@0 12 /*global ToObject: false, ToInteger: false, IsCallable: false,
michael@0 13 ThrowError: false, AssertionFailed: false, SetScriptHints: false,
michael@0 14 MakeConstructible: false, DecompileArg: false,
michael@0 15 RuntimeDefaultLocale: false,
michael@0 16 ParallelDo: false, ParallelSlices: false, NewDenseArray: false,
michael@0 17 UnsafePutElements: false, ShouldForceSequential: false,
michael@0 18 ParallelTestsShouldPass: false,
michael@0 19 Dump: false,
michael@0 20 callFunction: false,
michael@0 21 TO_UINT32: false,
michael@0 22 JSMSG_NOT_FUNCTION: false, JSMSG_MISSING_FUN_ARG: false,
michael@0 23 JSMSG_EMPTY_ARRAY_REDUCE: false, JSMSG_CANT_CONVERT_TO: false,
michael@0 24 */
michael@0 25
michael@0 26 #include "SelfHostingDefines.h"
michael@0 27
michael@0 28 // Remove unsafe builtin functions.
michael@0 29 Object.defineProperty = null; // See bug 988416.
michael@0 30
michael@0 31 // Cache builtin functions so using them doesn't require cloning the whole object they're
michael@0 32 // installed on.
michael@0 33 var std_isFinite = isFinite;
michael@0 34 var std_isNaN = isNaN;
michael@0 35 var std_Array_indexOf = ArrayIndexOf;
michael@0 36 var std_Array_iterator = Array.prototype.iterator;
michael@0 37 var std_Array_join = Array.prototype.join;
michael@0 38 var std_Array_push = Array.prototype.push;
michael@0 39 var std_Array_pop = Array.prototype.pop;
michael@0 40 var std_Array_shift = Array.prototype.shift;
michael@0 41 var std_Array_slice = Array.prototype.slice;
michael@0 42 var std_Array_sort = Array.prototype.sort;
michael@0 43 var std_Array_unshift = Array.prototype.unshift;
michael@0 44 var std_Boolean_toString = Boolean.prototype.toString;
michael@0 45 var Std_Date = Date;
michael@0 46 var std_Date_now = Date.now;
michael@0 47 var std_Date_valueOf = Date.prototype.valueOf;
michael@0 48 var std_Function_bind = Function.prototype.bind;
michael@0 49 var std_Function_apply = Function.prototype.apply;
michael@0 50 var std_Math_floor = Math.floor;
michael@0 51 var std_Math_max = Math.max;
michael@0 52 var std_Math_min = Math.min;
michael@0 53 var std_Math_imul = Math.imul;
michael@0 54 var std_Math_log2 = Math.log2;
michael@0 55 var std_Number_valueOf = Number.prototype.valueOf;
michael@0 56 var std_Number_POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
michael@0 57 var std_Object_create = Object.create;
michael@0 58 var std_Object_getOwnPropertyNames = Object.getOwnPropertyNames;
michael@0 59 var std_Object_hasOwnProperty = Object.prototype.hasOwnProperty;
michael@0 60 var std_RegExp_test = RegExp.prototype.test;
michael@0 61 var Std_String = String;
michael@0 62 var std_String_fromCharCode = String.fromCharCode;
michael@0 63 var std_String_charCodeAt = String.prototype.charCodeAt;
michael@0 64 var std_String_indexOf = String.prototype.indexOf;
michael@0 65 var std_String_lastIndexOf = String.prototype.lastIndexOf;
michael@0 66 var std_String_match = String.prototype.match;
michael@0 67 var std_String_replace = String.prototype.replace;
michael@0 68 var std_String_split = String.prototype.split;
michael@0 69 var std_String_startsWith = String.prototype.startsWith;
michael@0 70 var std_String_substring = String.prototype.substring;
michael@0 71 var std_String_toLowerCase = String.prototype.toLowerCase;
michael@0 72 var std_String_toUpperCase = String.prototype.toUpperCase;
michael@0 73 var std_WeakMap = WeakMap;
michael@0 74 var std_WeakMap_get = WeakMap.prototype.get;
michael@0 75 var std_WeakMap_has = WeakMap.prototype.has;
michael@0 76 var std_WeakMap_set = WeakMap.prototype.set;
michael@0 77 var std_Map_has = Map.prototype.has;
michael@0 78 var std_Set_has = Set.prototype.has;
michael@0 79 var std_iterator = '@@iterator'; // FIXME: Change to be a symbol.
michael@0 80 var std_StopIteration = StopIteration;
michael@0 81 var std_Map_iterator = Map.prototype[std_iterator];
michael@0 82 var std_Set_iterator = Set.prototype[std_iterator];
michael@0 83 var std_Map_iterator_next = Object.getPrototypeOf(Map()[std_iterator]()).next;
michael@0 84 var std_Set_iterator_next = Object.getPrototypeOf(Set()[std_iterator]()).next;
michael@0 85
michael@0 86
michael@0 87
michael@0 88 /********** List specification type **********/
michael@0 89
michael@0 90
michael@0 91 /* Spec: ECMAScript Language Specification, 5.1 edition, 8.8 */
michael@0 92 function List() {}
michael@0 93 {
michael@0 94 let ListProto = std_Object_create(null);
michael@0 95 ListProto.indexOf = std_Array_indexOf;
michael@0 96 ListProto.join = std_Array_join;
michael@0 97 ListProto.push = std_Array_push;
michael@0 98 ListProto.slice = std_Array_slice;
michael@0 99 ListProto.sort = std_Array_sort;
michael@0 100 MakeConstructible(List, ListProto);
michael@0 101 }
michael@0 102
michael@0 103
michael@0 104 /********** Record specification type **********/
michael@0 105
michael@0 106
michael@0 107 /* Spec: ECMAScript Internationalization API Specification, draft, 5 */
michael@0 108 function Record() {
michael@0 109 return std_Object_create(null);
michael@0 110 }
michael@0 111 MakeConstructible(Record, {});
michael@0 112
michael@0 113
michael@0 114 /********** Abstract operations defined in ECMAScript Language Specification **********/
michael@0 115
michael@0 116
michael@0 117 /* Spec: ECMAScript Language Specification, 5.1 edition, 8.12.6 and 11.8.7 */
michael@0 118 function HasProperty(o, p) {
michael@0 119 return p in o;
michael@0 120 }
michael@0 121
michael@0 122
michael@0 123 /* Spec: ECMAScript Language Specification, 5.1 edition, 9.2 and 11.4.9 */
michael@0 124 function ToBoolean(v) {
michael@0 125 return !!v;
michael@0 126 }
michael@0 127
michael@0 128
michael@0 129 /* Spec: ECMAScript Language Specification, 5.1 edition, 9.3 and 11.4.6 */
michael@0 130 function ToNumber(v) {
michael@0 131 return +v;
michael@0 132 }
michael@0 133
michael@0 134
michael@0 135 /* Spec: ECMAScript Language Specification, 5.1 edition, 9.8 and 15.2.1.1 */
michael@0 136 function ToString(v) {
michael@0 137 assert(arguments.length > 0, "__toString");
michael@0 138 return Std_String(v);
michael@0 139 }
michael@0 140
michael@0 141
michael@0 142 /* Spec: ECMAScript Language Specification, 5.1 edition, 9.10 */
michael@0 143 function CheckObjectCoercible(v) {
michael@0 144 if (v === undefined || v === null)
michael@0 145 ThrowError(JSMSG_CANT_CONVERT_TO, ToString(v), "object");
michael@0 146 }
michael@0 147
michael@0 148
michael@0 149 /********** Various utility functions **********/
michael@0 150
michael@0 151
michael@0 152 /** Returns true iff Type(v) is Object; see ES5 8.6. */
michael@0 153 function IsObject(v) {
michael@0 154 // Watch out for |typeof null === "object"| as the most obvious pitfall.
michael@0 155 // But also be careful of SpiderMonkey's objects that emulate undefined
michael@0 156 // (i.e. |document.all|), which have bogus |typeof| behavior. Detect
michael@0 157 // these objects using strict equality, which said bogosity doesn't affect.
michael@0 158 return (typeof v === "object" && v !== null) ||
michael@0 159 typeof v === "function" ||
michael@0 160 (typeof v === "undefined" && v !== undefined);
michael@0 161 }
michael@0 162
michael@0 163
michael@0 164 /********** Testing code **********/
michael@0 165
michael@0 166 #ifdef ENABLE_PARALLEL_JS
michael@0 167
michael@0 168 /**
michael@0 169 * Internal debugging tool: checks that the given `mode` permits
michael@0 170 * sequential execution
michael@0 171 */
michael@0 172 function AssertSequentialIsOK(mode) {
michael@0 173 if (mode && mode.mode && mode.mode !== "seq" && ParallelTestsShouldPass())
michael@0 174 ThrowError(JSMSG_WRONG_VALUE, "parallel execution", "sequential was forced");
michael@0 175 }
michael@0 176
michael@0 177 function ForkJoinMode(mode) {
michael@0 178 // WARNING: this must match the enum ForkJoinMode in ForkJoin.cpp
michael@0 179 if (!mode || !mode.mode) {
michael@0 180 return 0;
michael@0 181 } else if (mode.mode === "compile") {
michael@0 182 return 1;
michael@0 183 } else if (mode.mode === "par") {
michael@0 184 return 2;
michael@0 185 } else if (mode.mode === "recover") {
michael@0 186 return 3;
michael@0 187 } else if (mode.mode === "bailout") {
michael@0 188 return 4;
michael@0 189 }
michael@0 190 ThrowError(JSMSG_PAR_ARRAY_BAD_ARG);
michael@0 191 return undefined;
michael@0 192 }
michael@0 193
michael@0 194 #endif

mercurial