Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | (function(global, utils) { |
| 6 | |
| 7 | %CheckIsBootstrapping(); |
| 8 | |
| 9 | // ---------------------------------------------------------------------------- |
| 10 | // Imports |
| 11 | |
| 12 | var GlobalArray = global.Array; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 13 | var GlobalNumber = global.Number; |
| 14 | var GlobalObject = global.Object; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15 | var iteratorSymbol = utils.ImportNow("iterator_symbol"); |
| 16 | var MakeRangeError; |
| 17 | var MakeSyntaxError; |
| 18 | var MakeTypeError; |
| 19 | var MathAbs; |
| 20 | var NaN = %GetRootNaN(); |
| 21 | var ObjectToString = utils.ImportNow("object_to_string"); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 22 | var toStringTagSymbol = utils.ImportNow("to_string_tag_symbol"); |
| 23 | |
| 24 | utils.Import(function(from) { |
| 25 | MakeRangeError = from.MakeRangeError; |
| 26 | MakeSyntaxError = from.MakeSyntaxError; |
| 27 | MakeTypeError = from.MakeTypeError; |
| 28 | MathAbs = from.MathAbs; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 29 | }); |
| 30 | |
| 31 | // ---------------------------------------------------------------------------- |
| 32 | |
| 33 | |
| 34 | // ES6 18.2.3 isNaN(number) |
| 35 | function GlobalIsNaN(number) { |
| 36 | number = TO_NUMBER(number); |
| 37 | return NUMBER_IS_NAN(number); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | // ES6 18.2.2 isFinite(number) |
| 42 | function GlobalIsFinite(number) { |
| 43 | number = TO_NUMBER(number); |
| 44 | return NUMBER_IS_FINITE(number); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | // ES6 18.2.5 parseInt(string, radix) |
| 49 | function GlobalParseInt(string, radix) { |
| 50 | if (IS_UNDEFINED(radix) || radix === 10 || radix === 0) { |
| 51 | // Some people use parseInt instead of Math.floor. This |
| 52 | // optimization makes parseInt on a Smi 12 times faster (60ns |
| 53 | // vs 800ns). The following optimization makes parseInt on a |
| 54 | // non-Smi number 9 times faster (230ns vs 2070ns). Together |
| 55 | // they make parseInt on a string 1.4% slower (274ns vs 270ns). |
| 56 | if (%_IsSmi(string)) return string; |
| 57 | if (IS_NUMBER(string) && |
| 58 | ((0.01 < string && string < 1e9) || |
| 59 | (-1e9 < string && string < -0.01))) { |
| 60 | // Truncate number. |
| 61 | return string | 0; |
| 62 | } |
| 63 | string = TO_STRING(string); |
| 64 | radix = radix | 0; |
| 65 | } else { |
| 66 | // The spec says ToString should be evaluated before ToInt32. |
| 67 | string = TO_STRING(string); |
| 68 | radix = TO_INT32(radix); |
| 69 | if (!(radix == 0 || (2 <= radix && radix <= 36))) { |
| 70 | return NaN; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (%_HasCachedArrayIndex(string) && |
| 75 | (radix == 0 || radix == 10)) { |
| 76 | return %_GetCachedArrayIndex(string); |
| 77 | } |
| 78 | return %StringParseInt(string, radix); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | // ES6 18.2.4 parseFloat(string) |
| 83 | function GlobalParseFloat(string) { |
| 84 | // 1. Let inputString be ? ToString(string). |
| 85 | string = TO_STRING(string); |
| 86 | if (%_HasCachedArrayIndex(string)) return %_GetCachedArrayIndex(string); |
| 87 | return %StringParseFloat(string); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | // ---------------------------------------------------------------------------- |
| 92 | |
| 93 | // Set up global object. |
| 94 | var attributes = DONT_ENUM | DONT_DELETE | READ_ONLY; |
| 95 | |
| 96 | utils.InstallConstants(global, [ |
| 97 | // ES6 18.1.1 |
| 98 | "Infinity", INFINITY, |
| 99 | // ES6 18.1.2 |
| 100 | "NaN", NaN, |
| 101 | // ES6 18.1.3 |
| 102 | "undefined", UNDEFINED, |
| 103 | ]); |
| 104 | |
| 105 | // Set up non-enumerable function on the global object. |
| 106 | utils.InstallFunctions(global, DONT_ENUM, [ |
| 107 | "isNaN", GlobalIsNaN, |
| 108 | "isFinite", GlobalIsFinite, |
| 109 | "parseInt", GlobalParseInt, |
| 110 | "parseFloat", GlobalParseFloat, |
| 111 | ]); |
| 112 | |
| 113 | |
| 114 | // ---------------------------------------------------------------------------- |
| 115 | // Object |
| 116 | |
| 117 | // ES6 19.1.3.5 Object.prototype.toLocaleString([reserved1 [,reserved2]]) |
| 118 | function ObjectToLocaleString() { |
| 119 | CHECK_OBJECT_COERCIBLE(this, "Object.prototype.toLocaleString"); |
| 120 | return this.toString(); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | // ES6 19.1.3.7 Object.prototype.valueOf() |
| 125 | function ObjectValueOf() { |
| 126 | return TO_OBJECT(this); |
| 127 | } |
| 128 | |
| 129 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 130 | // ES6 19.1.3.3 Object.prototype.isPrototypeOf(V) |
| 131 | function ObjectIsPrototypeOf(V) { |
| 132 | if (!IS_RECEIVER(V)) return false; |
| 133 | var O = TO_OBJECT(this); |
| 134 | return %HasInPrototypeChain(V, O); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | // ES6 19.1.3.4 |
| 139 | function ObjectPropertyIsEnumerable(V) { |
| 140 | var P = TO_NAME(V); |
| 141 | return %PropertyIsEnumerable(TO_OBJECT(this), P); |
| 142 | } |
| 143 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 144 | // ES6 7.3.9 |
| 145 | function GetMethod(obj, p) { |
| 146 | var func = obj[p]; |
| 147 | if (IS_NULL_OR_UNDEFINED(func)) return UNDEFINED; |
| 148 | if (IS_CALLABLE(func)) return func; |
| 149 | throw MakeTypeError(kCalledNonCallable, typeof func); |
| 150 | } |
| 151 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 152 | // ES6 section 19.1.2.18. |
| 153 | function ObjectSetPrototypeOf(obj, proto) { |
| 154 | CHECK_OBJECT_COERCIBLE(obj, "Object.setPrototypeOf"); |
| 155 | |
| 156 | if (proto !== null && !IS_RECEIVER(proto)) { |
| 157 | throw MakeTypeError(kProtoObjectOrNull, proto); |
| 158 | } |
| 159 | |
| 160 | if (IS_RECEIVER(obj)) { |
| 161 | %SetPrototype(obj, proto); |
| 162 | } |
| 163 | |
| 164 | return obj; |
| 165 | } |
| 166 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 167 | // ES6 B.2.2.1.1 |
| 168 | function ObjectGetProto() { |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 169 | return %object_get_prototype_of(this); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | |
| 173 | // ES6 B.2.2.1.2 |
| 174 | function ObjectSetProto(proto) { |
| 175 | CHECK_OBJECT_COERCIBLE(this, "Object.prototype.__proto__"); |
| 176 | |
| 177 | if ((IS_RECEIVER(proto) || IS_NULL(proto)) && IS_RECEIVER(this)) { |
| 178 | %SetPrototype(this, proto); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | |
| 183 | // ES6 19.1.1.1 |
| 184 | function ObjectConstructor(x) { |
| 185 | if (GlobalObject != new.target && !IS_UNDEFINED(new.target)) { |
| 186 | return this; |
| 187 | } |
| 188 | if (IS_NULL(x) || IS_UNDEFINED(x)) return {}; |
| 189 | return TO_OBJECT(x); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | // ---------------------------------------------------------------------------- |
| 194 | // Object |
| 195 | |
| 196 | %SetNativeFlag(GlobalObject); |
| 197 | %SetCode(GlobalObject, ObjectConstructor); |
| 198 | |
| 199 | %AddNamedProperty(GlobalObject.prototype, "constructor", GlobalObject, |
| 200 | DONT_ENUM); |
| 201 | |
| 202 | // Set up non-enumerable functions on the Object.prototype object. |
| 203 | utils.InstallFunctions(GlobalObject.prototype, DONT_ENUM, [ |
| 204 | "toString", ObjectToString, |
| 205 | "toLocaleString", ObjectToLocaleString, |
| 206 | "valueOf", ObjectValueOf, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 207 | "isPrototypeOf", ObjectIsPrototypeOf, |
| 208 | "propertyIsEnumerable", ObjectPropertyIsEnumerable, |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 209 | // __defineGetter__ is added in bootstrapper.cc. |
| 210 | // __lookupGetter__ is added in bootstrapper.cc. |
| 211 | // __defineSetter__ is added in bootstrapper.cc. |
| 212 | // __lookupSetter__ is added in bootstrapper.cc. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 213 | ]); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 214 | utils.InstallGetterSetter( |
| 215 | GlobalObject.prototype, "__proto__", ObjectGetProto, ObjectSetProto); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 216 | |
| 217 | // Set up non-enumerable functions in the Object object. |
| 218 | utils.InstallFunctions(GlobalObject, DONT_ENUM, [ |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 219 | "setPrototypeOf", ObjectSetPrototypeOf, |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 220 | // getOwnPropertySymbols is added in symbol.js. |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame] | 221 | // Others are added in bootstrapper.cc. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 222 | ]); |
| 223 | |
| 224 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 225 | |
| 226 | // ---------------------------------------------------------------------------- |
| 227 | // Number |
| 228 | |
| 229 | // ES6 Number.prototype.toString([ radix ]) |
| 230 | function NumberToStringJS(radix) { |
| 231 | // NOTE: Both Number objects and values can enter here as |
| 232 | // 'this'. This is not as dictated by ECMA-262. |
| 233 | var number = this; |
| 234 | if (!IS_NUMBER(this)) { |
| 235 | if (!IS_NUMBER_WRAPPER(this)) { |
| 236 | throw MakeTypeError(kNotGeneric, 'Number.prototype.toString'); |
| 237 | } |
| 238 | // Get the value of this number in case it's an object. |
| 239 | number = %_ValueOf(this); |
| 240 | } |
| 241 | // Fast case: Convert number in radix 10. |
| 242 | if (IS_UNDEFINED(radix) || radix === 10) { |
| 243 | return %_NumberToString(number); |
| 244 | } |
| 245 | |
| 246 | // Convert the radix to an integer and check the range. |
| 247 | radix = TO_INTEGER(radix); |
| 248 | if (radix < 2 || radix > 36) throw MakeRangeError(kToRadixFormatRange); |
| 249 | // Convert the number to a string in the given radix. |
| 250 | return %NumberToRadixString(number, radix); |
| 251 | } |
| 252 | |
| 253 | |
| 254 | // ES6 20.1.3.4 Number.prototype.toLocaleString([reserved1 [, reserved2]]) |
| 255 | function NumberToLocaleString() { |
| 256 | return %_Call(NumberToStringJS, this); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | // ES6 20.1.3.7 Number.prototype.valueOf() |
| 261 | function NumberValueOf() { |
| 262 | // NOTE: Both Number objects and values can enter here as |
| 263 | // 'this'. This is not as dictated by ECMA-262. |
| 264 | if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this)) { |
| 265 | throw MakeTypeError(kNotGeneric, 'Number.prototype.valueOf'); |
| 266 | } |
| 267 | return %_ValueOf(this); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | // ES6 20.1.3.3 Number.prototype.toFixed(fractionDigits) |
| 272 | function NumberToFixedJS(fractionDigits) { |
| 273 | var x = this; |
| 274 | if (!IS_NUMBER(this)) { |
| 275 | if (!IS_NUMBER_WRAPPER(this)) { |
| 276 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 277 | "Number.prototype.toFixed", this); |
| 278 | } |
| 279 | // Get the value of this number in case it's an object. |
| 280 | x = %_ValueOf(this); |
| 281 | } |
| 282 | var f = TO_INTEGER(fractionDigits); |
| 283 | |
| 284 | if (f < 0 || f > 20) { |
| 285 | throw MakeRangeError(kNumberFormatRange, "toFixed() digits"); |
| 286 | } |
| 287 | |
| 288 | if (NUMBER_IS_NAN(x)) return "NaN"; |
| 289 | if (x == INFINITY) return "Infinity"; |
| 290 | if (x == -INFINITY) return "-Infinity"; |
| 291 | |
| 292 | return %NumberToFixed(x, f); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | // ES6 20.1.3.2 Number.prototype.toExponential(fractionDigits) |
| 297 | function NumberToExponentialJS(fractionDigits) { |
| 298 | var x = this; |
| 299 | if (!IS_NUMBER(this)) { |
| 300 | if (!IS_NUMBER_WRAPPER(this)) { |
| 301 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 302 | "Number.prototype.toExponential", this); |
| 303 | } |
| 304 | // Get the value of this number in case it's an object. |
| 305 | x = %_ValueOf(this); |
| 306 | } |
| 307 | var f = IS_UNDEFINED(fractionDigits) ? UNDEFINED : TO_INTEGER(fractionDigits); |
| 308 | |
| 309 | if (NUMBER_IS_NAN(x)) return "NaN"; |
| 310 | if (x == INFINITY) return "Infinity"; |
| 311 | if (x == -INFINITY) return "-Infinity"; |
| 312 | |
| 313 | if (IS_UNDEFINED(f)) { |
| 314 | f = -1; // Signal for runtime function that f is not defined. |
| 315 | } else if (f < 0 || f > 20) { |
| 316 | throw MakeRangeError(kNumberFormatRange, "toExponential()"); |
| 317 | } |
| 318 | return %NumberToExponential(x, f); |
| 319 | } |
| 320 | |
| 321 | |
| 322 | // ES6 20.1.3.5 Number.prototype.toPrecision(precision) |
| 323 | function NumberToPrecisionJS(precision) { |
| 324 | var x = this; |
| 325 | if (!IS_NUMBER(this)) { |
| 326 | if (!IS_NUMBER_WRAPPER(this)) { |
| 327 | throw MakeTypeError(kIncompatibleMethodReceiver, |
| 328 | "Number.prototype.toPrecision", this); |
| 329 | } |
| 330 | // Get the value of this number in case it's an object. |
| 331 | x = %_ValueOf(this); |
| 332 | } |
| 333 | if (IS_UNDEFINED(precision)) return TO_STRING(x); |
| 334 | var p = TO_INTEGER(precision); |
| 335 | |
| 336 | if (NUMBER_IS_NAN(x)) return "NaN"; |
| 337 | if (x == INFINITY) return "Infinity"; |
| 338 | if (x == -INFINITY) return "-Infinity"; |
| 339 | |
| 340 | if (p < 1 || p > 21) { |
| 341 | throw MakeRangeError(kToPrecisionFormatRange); |
| 342 | } |
| 343 | return %NumberToPrecision(x, p); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | // Harmony isFinite. |
| 348 | function NumberIsFinite(number) { |
| 349 | return IS_NUMBER(number) && NUMBER_IS_FINITE(number); |
| 350 | } |
| 351 | |
| 352 | |
| 353 | // Harmony isInteger |
| 354 | function NumberIsInteger(number) { |
| 355 | return NumberIsFinite(number) && TO_INTEGER(number) == number; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | // Harmony isNaN. |
| 360 | function NumberIsNaN(number) { |
| 361 | return IS_NUMBER(number) && NUMBER_IS_NAN(number); |
| 362 | } |
| 363 | |
| 364 | |
| 365 | // Harmony isSafeInteger |
| 366 | function NumberIsSafeInteger(number) { |
| 367 | if (NumberIsFinite(number)) { |
| 368 | var integral = TO_INTEGER(number); |
| 369 | if (integral == number) { |
| 370 | return MathAbs(integral) <= kMaxSafeInteger; |
| 371 | } |
| 372 | } |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | // ---------------------------------------------------------------------------- |
| 378 | |
| 379 | %FunctionSetPrototype(GlobalNumber, new GlobalNumber(0)); |
| 380 | |
| 381 | %OptimizeObjectForAddingMultipleProperties(GlobalNumber.prototype, 8); |
| 382 | // Set up the constructor property on the Number prototype object. |
| 383 | %AddNamedProperty(GlobalNumber.prototype, "constructor", GlobalNumber, |
| 384 | DONT_ENUM); |
| 385 | |
| 386 | utils.InstallConstants(GlobalNumber, [ |
| 387 | // ECMA-262 section 15.7.3.1. |
| 388 | "MAX_VALUE", 1.7976931348623157e+308, |
| 389 | // ECMA-262 section 15.7.3.2. |
| 390 | "MIN_VALUE", 5e-324, |
| 391 | // ECMA-262 section 15.7.3.3. |
| 392 | "NaN", NaN, |
| 393 | // ECMA-262 section 15.7.3.4. |
| 394 | "NEGATIVE_INFINITY", -INFINITY, |
| 395 | // ECMA-262 section 15.7.3.5. |
| 396 | "POSITIVE_INFINITY", INFINITY, |
| 397 | |
| 398 | // --- Harmony constants (no spec refs until settled.) |
| 399 | |
| 400 | "MAX_SAFE_INTEGER", %_MathPow(2, 53) - 1, |
| 401 | "MIN_SAFE_INTEGER", -%_MathPow(2, 53) + 1, |
| 402 | "EPSILON", %_MathPow(2, -52) |
| 403 | ]); |
| 404 | |
| 405 | // Set up non-enumerable functions on the Number prototype object. |
| 406 | utils.InstallFunctions(GlobalNumber.prototype, DONT_ENUM, [ |
| 407 | "toString", NumberToStringJS, |
| 408 | "toLocaleString", NumberToLocaleString, |
| 409 | "valueOf", NumberValueOf, |
| 410 | "toFixed", NumberToFixedJS, |
| 411 | "toExponential", NumberToExponentialJS, |
| 412 | "toPrecision", NumberToPrecisionJS |
| 413 | ]); |
| 414 | |
| 415 | // Harmony Number constructor additions |
| 416 | utils.InstallFunctions(GlobalNumber, DONT_ENUM, [ |
| 417 | "isFinite", NumberIsFinite, |
| 418 | "isInteger", NumberIsInteger, |
| 419 | "isNaN", NumberIsNaN, |
| 420 | "isSafeInteger", NumberIsSafeInteger, |
| 421 | "parseInt", GlobalParseInt, |
| 422 | "parseFloat", GlobalParseFloat |
| 423 | ]); |
| 424 | |
| 425 | %SetForceInlineFlag(NumberIsNaN); |
| 426 | |
| 427 | |
| 428 | // ---------------------------------------------------------------------------- |
| 429 | // Iterator related spec functions. |
| 430 | |
| 431 | // ES6 7.4.1 GetIterator(obj, method) |
| 432 | function GetIterator(obj, method) { |
| 433 | if (IS_UNDEFINED(method)) { |
| 434 | method = obj[iteratorSymbol]; |
| 435 | } |
| 436 | if (!IS_CALLABLE(method)) { |
| 437 | throw MakeTypeError(kNotIterable, obj); |
| 438 | } |
| 439 | var iterator = %_Call(method, obj); |
| 440 | if (!IS_RECEIVER(iterator)) { |
| 441 | throw MakeTypeError(kNotAnIterator, iterator); |
| 442 | } |
| 443 | return iterator; |
| 444 | } |
| 445 | |
| 446 | // ---------------------------------------------------------------------------- |
| 447 | // Exports |
| 448 | |
| 449 | utils.Export(function(to) { |
| 450 | to.GetIterator = GetIterator; |
| 451 | to.GetMethod = GetMethod; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 452 | to.IsNaN = GlobalIsNaN; |
| 453 | to.NumberIsNaN = NumberIsNaN; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 454 | to.NumberIsInteger = NumberIsInteger; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 455 | to.ObjectHasOwnProperty = GlobalObject.prototype.hasOwnProperty; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 456 | }); |
| 457 | |
| 458 | %InstallToContext([ |
| 459 | "object_value_of", ObjectValueOf, |
| 460 | ]); |
| 461 | |
| 462 | }) |