js/src/builtin/Utilities.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/builtin/Utilities.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,194 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*jshint bitwise: true, camelcase: false, curly: false, eqeqeq: true,
     1.9 +         es5: true, forin: true, immed: true, indent: 4, latedef: false,
    1.10 +         newcap: false, noarg: true, noempty: true, nonew: true,
    1.11 +         plusplus: false, quotmark: false, regexp: true, undef: true,
    1.12 +         unused: false, strict: false, trailing: true,
    1.13 +*/
    1.14 +
    1.15 +/*global ToObject: false, ToInteger: false, IsCallable: false,
    1.16 +         ThrowError: false, AssertionFailed: false, SetScriptHints: false,
    1.17 +         MakeConstructible: false, DecompileArg: false,
    1.18 +         RuntimeDefaultLocale: false,
    1.19 +         ParallelDo: false, ParallelSlices: false, NewDenseArray: false,
    1.20 +         UnsafePutElements: false, ShouldForceSequential: false,
    1.21 +         ParallelTestsShouldPass: false,
    1.22 +         Dump: false,
    1.23 +         callFunction: false,
    1.24 +         TO_UINT32: false,
    1.25 +         JSMSG_NOT_FUNCTION: false, JSMSG_MISSING_FUN_ARG: false,
    1.26 +         JSMSG_EMPTY_ARRAY_REDUCE: false, JSMSG_CANT_CONVERT_TO: false,
    1.27 +*/
    1.28 +
    1.29 +#include "SelfHostingDefines.h"
    1.30 +
    1.31 +// Remove unsafe builtin functions.
    1.32 +Object.defineProperty = null; // See bug 988416.
    1.33 +
    1.34 +// Cache builtin functions so using them doesn't require cloning the whole object they're 
    1.35 +// installed on.
    1.36 +var std_isFinite = isFinite;
    1.37 +var std_isNaN = isNaN;
    1.38 +var std_Array_indexOf = ArrayIndexOf;
    1.39 +var std_Array_iterator = Array.prototype.iterator;
    1.40 +var std_Array_join = Array.prototype.join;
    1.41 +var std_Array_push = Array.prototype.push;
    1.42 +var std_Array_pop = Array.prototype.pop;
    1.43 +var std_Array_shift = Array.prototype.shift;
    1.44 +var std_Array_slice = Array.prototype.slice;
    1.45 +var std_Array_sort = Array.prototype.sort;
    1.46 +var std_Array_unshift = Array.prototype.unshift;
    1.47 +var std_Boolean_toString = Boolean.prototype.toString;
    1.48 +var Std_Date = Date;
    1.49 +var std_Date_now = Date.now;
    1.50 +var std_Date_valueOf = Date.prototype.valueOf;
    1.51 +var std_Function_bind = Function.prototype.bind;
    1.52 +var std_Function_apply = Function.prototype.apply;
    1.53 +var std_Math_floor = Math.floor;
    1.54 +var std_Math_max = Math.max;
    1.55 +var std_Math_min = Math.min;
    1.56 +var std_Math_imul = Math.imul;
    1.57 +var std_Math_log2 = Math.log2;
    1.58 +var std_Number_valueOf = Number.prototype.valueOf;
    1.59 +var std_Number_POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
    1.60 +var std_Object_create = Object.create;
    1.61 +var std_Object_getOwnPropertyNames = Object.getOwnPropertyNames;
    1.62 +var std_Object_hasOwnProperty = Object.prototype.hasOwnProperty;
    1.63 +var std_RegExp_test = RegExp.prototype.test;
    1.64 +var Std_String = String;
    1.65 +var std_String_fromCharCode = String.fromCharCode;
    1.66 +var std_String_charCodeAt = String.prototype.charCodeAt;
    1.67 +var std_String_indexOf = String.prototype.indexOf;
    1.68 +var std_String_lastIndexOf = String.prototype.lastIndexOf;
    1.69 +var std_String_match = String.prototype.match;
    1.70 +var std_String_replace = String.prototype.replace;
    1.71 +var std_String_split = String.prototype.split;
    1.72 +var std_String_startsWith = String.prototype.startsWith;
    1.73 +var std_String_substring = String.prototype.substring;
    1.74 +var std_String_toLowerCase = String.prototype.toLowerCase;
    1.75 +var std_String_toUpperCase = String.prototype.toUpperCase;
    1.76 +var std_WeakMap = WeakMap;
    1.77 +var std_WeakMap_get = WeakMap.prototype.get;
    1.78 +var std_WeakMap_has = WeakMap.prototype.has;
    1.79 +var std_WeakMap_set = WeakMap.prototype.set;
    1.80 +var std_Map_has = Map.prototype.has;
    1.81 +var std_Set_has = Set.prototype.has;
    1.82 +var std_iterator = '@@iterator'; // FIXME: Change to be a symbol.
    1.83 +var std_StopIteration = StopIteration;
    1.84 +var std_Map_iterator = Map.prototype[std_iterator];
    1.85 +var std_Set_iterator = Set.prototype[std_iterator];
    1.86 +var std_Map_iterator_next = Object.getPrototypeOf(Map()[std_iterator]()).next;
    1.87 +var std_Set_iterator_next = Object.getPrototypeOf(Set()[std_iterator]()).next;
    1.88 +
    1.89 +
    1.90 +
    1.91 +/********** List specification type **********/
    1.92 +
    1.93 +
    1.94 +/* Spec: ECMAScript Language Specification, 5.1 edition, 8.8 */
    1.95 +function List() {}
    1.96 +{
    1.97 +  let ListProto = std_Object_create(null);
    1.98 +  ListProto.indexOf = std_Array_indexOf;
    1.99 +  ListProto.join = std_Array_join;
   1.100 +  ListProto.push = std_Array_push;
   1.101 +  ListProto.slice = std_Array_slice;
   1.102 +  ListProto.sort = std_Array_sort;
   1.103 +  MakeConstructible(List, ListProto);
   1.104 +}
   1.105 +
   1.106 +
   1.107 +/********** Record specification type **********/
   1.108 +
   1.109 +
   1.110 +/* Spec: ECMAScript Internationalization API Specification, draft, 5 */
   1.111 +function Record() {
   1.112 +    return std_Object_create(null);
   1.113 +}
   1.114 +MakeConstructible(Record, {});
   1.115 +
   1.116 +
   1.117 +/********** Abstract operations defined in ECMAScript Language Specification **********/
   1.118 +
   1.119 +
   1.120 +/* Spec: ECMAScript Language Specification, 5.1 edition, 8.12.6 and 11.8.7 */
   1.121 +function HasProperty(o, p) {
   1.122 +    return p in o;
   1.123 +}
   1.124 +
   1.125 +
   1.126 +/* Spec: ECMAScript Language Specification, 5.1 edition, 9.2 and 11.4.9 */
   1.127 +function ToBoolean(v) {
   1.128 +    return !!v;
   1.129 +}
   1.130 +
   1.131 +
   1.132 +/* Spec: ECMAScript Language Specification, 5.1 edition, 9.3 and 11.4.6 */
   1.133 +function ToNumber(v) {
   1.134 +    return +v;
   1.135 +}
   1.136 +
   1.137 +
   1.138 +/* Spec: ECMAScript Language Specification, 5.1 edition, 9.8 and 15.2.1.1 */
   1.139 +function ToString(v) {
   1.140 +    assert(arguments.length > 0, "__toString");
   1.141 +    return Std_String(v);
   1.142 +}
   1.143 +
   1.144 +
   1.145 +/* Spec: ECMAScript Language Specification, 5.1 edition, 9.10 */
   1.146 +function CheckObjectCoercible(v) {
   1.147 +    if (v === undefined || v === null)
   1.148 +        ThrowError(JSMSG_CANT_CONVERT_TO, ToString(v), "object");
   1.149 +}
   1.150 +
   1.151 +
   1.152 +/********** Various utility functions **********/
   1.153 +
   1.154 +
   1.155 +/** Returns true iff Type(v) is Object; see ES5 8.6. */
   1.156 +function IsObject(v) {
   1.157 +    // Watch out for |typeof null === "object"| as the most obvious pitfall.
   1.158 +    // But also be careful of SpiderMonkey's objects that emulate undefined
   1.159 +    // (i.e. |document.all|), which have bogus |typeof| behavior.  Detect
   1.160 +    // these objects using strict equality, which said bogosity doesn't affect.
   1.161 +    return (typeof v === "object" && v !== null) ||
   1.162 +           typeof v === "function" ||
   1.163 +           (typeof v === "undefined" && v !== undefined);
   1.164 +}
   1.165 +
   1.166 +
   1.167 +/********** Testing code **********/
   1.168 +
   1.169 +#ifdef ENABLE_PARALLEL_JS
   1.170 +
   1.171 +/**
   1.172 + * Internal debugging tool: checks that the given `mode` permits
   1.173 + * sequential execution
   1.174 + */
   1.175 +function AssertSequentialIsOK(mode) {
   1.176 +  if (mode && mode.mode && mode.mode !== "seq" && ParallelTestsShouldPass())
   1.177 +    ThrowError(JSMSG_WRONG_VALUE, "parallel execution", "sequential was forced");
   1.178 +}
   1.179 +
   1.180 +function ForkJoinMode(mode) {
   1.181 +  // WARNING: this must match the enum ForkJoinMode in ForkJoin.cpp
   1.182 +  if (!mode || !mode.mode) {
   1.183 +    return 0;
   1.184 +  } else if (mode.mode === "compile") {
   1.185 +    return 1;
   1.186 +  } else if (mode.mode === "par") {
   1.187 +    return 2;
   1.188 +  } else if (mode.mode === "recover") {
   1.189 +    return 3;
   1.190 +  } else if (mode.mode === "bailout") {
   1.191 +    return 4;
   1.192 +  }
   1.193 +  ThrowError(JSMSG_PAR_ARRAY_BAD_ARG);
   1.194 +  return undefined;
   1.195 +}
   1.196 +
   1.197 +#endif

mercurial