OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 (function(global, utils) { | 5 (function(global, utils) { |
6 | 6 |
7 %CheckIsBootstrapping(); | 7 %CheckIsBootstrapping(); |
8 | 8 |
9 // ---------------------------------------------------------------------------- | 9 // ---------------------------------------------------------------------------- |
10 // Imports | 10 // Imports |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 "setPrototypeOf", ObjectSetPrototypeOf, | 215 "setPrototypeOf", ObjectSetPrototypeOf, |
216 // getOwnPropertySymbols is added in symbol.js. | 216 // getOwnPropertySymbols is added in symbol.js. |
217 // Others are added in bootstrapper.cc. | 217 // Others are added in bootstrapper.cc. |
218 ]); | 218 ]); |
219 | 219 |
220 | 220 |
221 | 221 |
222 // ---------------------------------------------------------------------------- | 222 // ---------------------------------------------------------------------------- |
223 // Number | 223 // Number |
224 | 224 |
| 225 // ES6 Number.prototype.toString([ radix ]) |
| 226 function NumberToStringJS(radix) { |
| 227 // NOTE: Both Number objects and values can enter here as |
| 228 // 'this'. This is not as dictated by ECMA-262. |
| 229 var number = this; |
| 230 if (!IS_NUMBER(this)) { |
| 231 if (!IS_NUMBER_WRAPPER(this)) { |
| 232 throw MakeTypeError(kNotGeneric, 'Number.prototype.toString'); |
| 233 } |
| 234 // Get the value of this number in case it's an object. |
| 235 number = %_ValueOf(this); |
| 236 } |
| 237 // Fast case: Convert number in radix 10. |
| 238 if (IS_UNDEFINED(radix) || radix === 10) { |
| 239 return %_NumberToString(number); |
| 240 } |
| 241 |
| 242 // Convert the radix to an integer and check the range. |
| 243 radix = TO_INTEGER(radix); |
| 244 if (radix < 2 || radix > 36) throw MakeRangeError(kToRadixFormatRange); |
| 245 // Convert the number to a string in the given radix. |
| 246 return %NumberToRadixString(number, radix); |
| 247 } |
| 248 |
| 249 |
| 250 // ES6 20.1.3.4 Number.prototype.toLocaleString([reserved1 [, reserved2]]) |
| 251 function NumberToLocaleString() { |
| 252 return %_Call(NumberToStringJS, this); |
| 253 } |
| 254 |
| 255 |
| 256 // ES6 20.1.3.7 Number.prototype.valueOf() |
| 257 function NumberValueOf() { |
| 258 // NOTE: Both Number objects and values can enter here as |
| 259 // 'this'. This is not as dictated by ECMA-262. |
| 260 if (!IS_NUMBER(this) && !IS_NUMBER_WRAPPER(this)) { |
| 261 throw MakeTypeError(kNotGeneric, 'Number.prototype.valueOf'); |
| 262 } |
| 263 return %_ValueOf(this); |
| 264 } |
| 265 |
| 266 |
| 267 // ES6 20.1.3.3 Number.prototype.toFixed(fractionDigits) |
| 268 function NumberToFixedJS(fractionDigits) { |
| 269 var x = this; |
| 270 if (!IS_NUMBER(this)) { |
| 271 if (!IS_NUMBER_WRAPPER(this)) { |
| 272 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 273 "Number.prototype.toFixed", this); |
| 274 } |
| 275 // Get the value of this number in case it's an object. |
| 276 x = %_ValueOf(this); |
| 277 } |
| 278 var f = TO_INTEGER(fractionDigits); |
| 279 |
| 280 if (f < 0 || f > 20) { |
| 281 throw MakeRangeError(kNumberFormatRange, "toFixed() digits"); |
| 282 } |
| 283 |
| 284 if (NUMBER_IS_NAN(x)) return "NaN"; |
| 285 if (x == INFINITY) return "Infinity"; |
| 286 if (x == -INFINITY) return "-Infinity"; |
| 287 |
| 288 return %NumberToFixed(x, f); |
| 289 } |
| 290 |
| 291 |
| 292 // ES6 20.1.3.2 Number.prototype.toExponential(fractionDigits) |
| 293 function NumberToExponentialJS(fractionDigits) { |
| 294 var x = this; |
| 295 if (!IS_NUMBER(this)) { |
| 296 if (!IS_NUMBER_WRAPPER(this)) { |
| 297 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 298 "Number.prototype.toExponential", this); |
| 299 } |
| 300 // Get the value of this number in case it's an object. |
| 301 x = %_ValueOf(this); |
| 302 } |
| 303 var f = IS_UNDEFINED(fractionDigits) ? UNDEFINED : TO_INTEGER(fractionDigits); |
| 304 |
| 305 if (NUMBER_IS_NAN(x)) return "NaN"; |
| 306 if (x == INFINITY) return "Infinity"; |
| 307 if (x == -INFINITY) return "-Infinity"; |
| 308 |
| 309 if (IS_UNDEFINED(f)) { |
| 310 f = -1; // Signal for runtime function that f is not defined. |
| 311 } else if (f < 0 || f > 20) { |
| 312 throw MakeRangeError(kNumberFormatRange, "toExponential()"); |
| 313 } |
| 314 return %NumberToExponential(x, f); |
| 315 } |
| 316 |
| 317 |
| 318 // ES6 20.1.3.5 Number.prototype.toPrecision(precision) |
| 319 function NumberToPrecisionJS(precision) { |
| 320 var x = this; |
| 321 if (!IS_NUMBER(this)) { |
| 322 if (!IS_NUMBER_WRAPPER(this)) { |
| 323 throw MakeTypeError(kIncompatibleMethodReceiver, |
| 324 "Number.prototype.toPrecision", this); |
| 325 } |
| 326 // Get the value of this number in case it's an object. |
| 327 x = %_ValueOf(this); |
| 328 } |
| 329 if (IS_UNDEFINED(precision)) return TO_STRING(x); |
| 330 var p = TO_INTEGER(precision); |
| 331 |
| 332 if (NUMBER_IS_NAN(x)) return "NaN"; |
| 333 if (x == INFINITY) return "Infinity"; |
| 334 if (x == -INFINITY) return "-Infinity"; |
| 335 |
| 336 if (p < 1 || p > 21) { |
| 337 throw MakeRangeError(kToPrecisionFormatRange); |
| 338 } |
| 339 return %NumberToPrecision(x, p); |
| 340 } |
| 341 |
| 342 |
225 // Harmony isFinite. | 343 // Harmony isFinite. |
226 function NumberIsFinite(number) { | 344 function NumberIsFinite(number) { |
227 return IS_NUMBER(number) && NUMBER_IS_FINITE(number); | 345 return IS_NUMBER(number) && NUMBER_IS_FINITE(number); |
228 } | 346 } |
229 | 347 |
230 | 348 |
231 // Harmony isInteger | 349 // Harmony isInteger |
232 function NumberIsInteger(number) { | 350 function NumberIsInteger(number) { |
233 return NumberIsFinite(number) && TO_INTEGER(number) == number; | 351 return NumberIsFinite(number) && TO_INTEGER(number) == number; |
234 } | 352 } |
(...skipping 12 matching lines...) Expand all Loading... |
247 if (integral == number) { | 365 if (integral == number) { |
248 return -kMaxSafeInteger <= integral && integral <= kMaxSafeInteger; | 366 return -kMaxSafeInteger <= integral && integral <= kMaxSafeInteger; |
249 } | 367 } |
250 } | 368 } |
251 return false; | 369 return false; |
252 } | 370 } |
253 | 371 |
254 | 372 |
255 // ---------------------------------------------------------------------------- | 373 // ---------------------------------------------------------------------------- |
256 | 374 |
| 375 %FunctionSetPrototype(GlobalNumber, new GlobalNumber(0)); |
| 376 |
| 377 %OptimizeObjectForAddingMultipleProperties(GlobalNumber.prototype, 8); |
| 378 // Set up the constructor property on the Number prototype object. |
| 379 %AddNamedProperty(GlobalNumber.prototype, "constructor", GlobalNumber, |
| 380 DONT_ENUM); |
| 381 |
257 utils.InstallConstants(GlobalNumber, [ | 382 utils.InstallConstants(GlobalNumber, [ |
258 // ECMA-262 section 15.7.3.1. | 383 // ECMA-262 section 15.7.3.1. |
259 "MAX_VALUE", 1.7976931348623157e+308, | 384 "MAX_VALUE", 1.7976931348623157e+308, |
260 // ECMA-262 section 15.7.3.2. | 385 // ECMA-262 section 15.7.3.2. |
261 "MIN_VALUE", 5e-324, | 386 "MIN_VALUE", 5e-324, |
262 // ECMA-262 section 15.7.3.3. | 387 // ECMA-262 section 15.7.3.3. |
263 "NaN", NaN, | 388 "NaN", NaN, |
264 // ECMA-262 section 15.7.3.4. | 389 // ECMA-262 section 15.7.3.4. |
265 "NEGATIVE_INFINITY", -INFINITY, | 390 "NEGATIVE_INFINITY", -INFINITY, |
266 // ECMA-262 section 15.7.3.5. | 391 // ECMA-262 section 15.7.3.5. |
267 "POSITIVE_INFINITY", INFINITY, | 392 "POSITIVE_INFINITY", INFINITY, |
268 | 393 |
269 // --- Harmony constants (no spec refs until settled.) | 394 // --- Harmony constants (no spec refs until settled.) |
270 | 395 |
271 "MAX_SAFE_INTEGER", 9007199254740991, | 396 "MAX_SAFE_INTEGER", 9007199254740991, |
272 "MIN_SAFE_INTEGER", -9007199254740991, | 397 "MIN_SAFE_INTEGER", -9007199254740991, |
273 "EPSILON", 2.220446049250313e-16, | 398 "EPSILON", 2.220446049250313e-16, |
274 ]); | 399 ]); |
275 | 400 |
| 401 // Set up non-enumerable functions on the Number prototype object. |
| 402 utils.InstallFunctions(GlobalNumber.prototype, DONT_ENUM, [ |
| 403 "toString", NumberToStringJS, |
| 404 "toLocaleString", NumberToLocaleString, |
| 405 "valueOf", NumberValueOf, |
| 406 "toFixed", NumberToFixedJS, |
| 407 "toExponential", NumberToExponentialJS, |
| 408 "toPrecision", NumberToPrecisionJS |
| 409 ]); |
| 410 |
276 // Harmony Number constructor additions | 411 // Harmony Number constructor additions |
277 utils.InstallFunctions(GlobalNumber, DONT_ENUM, [ | 412 utils.InstallFunctions(GlobalNumber, DONT_ENUM, [ |
278 "isFinite", NumberIsFinite, | 413 "isFinite", NumberIsFinite, |
279 "isInteger", NumberIsInteger, | 414 "isInteger", NumberIsInteger, |
280 "isNaN", NumberIsNaN, | 415 "isNaN", NumberIsNaN, |
281 "isSafeInteger", NumberIsSafeInteger, | 416 "isSafeInteger", NumberIsSafeInteger, |
282 "parseInt", GlobalParseInt, | 417 "parseInt", GlobalParseInt, |
283 "parseFloat", GlobalParseFloat | 418 "parseFloat", GlobalParseFloat |
284 ]); | 419 ]); |
285 | 420 |
(...skipping 28 matching lines...) Expand all Loading... |
314 to.NumberIsNaN = NumberIsNaN; | 449 to.NumberIsNaN = NumberIsNaN; |
315 to.NumberIsInteger = NumberIsInteger; | 450 to.NumberIsInteger = NumberIsInteger; |
316 to.ObjectHasOwnProperty = GlobalObject.prototype.hasOwnProperty; | 451 to.ObjectHasOwnProperty = GlobalObject.prototype.hasOwnProperty; |
317 }); | 452 }); |
318 | 453 |
319 %InstallToContext([ | 454 %InstallToContext([ |
320 "object_value_of", ObjectValueOf, | 455 "object_value_of", ObjectValueOf, |
321 ]); | 456 ]); |
322 | 457 |
323 }) | 458 }) |
OLD | NEW |