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 "use strict"; | |
6 | |
7 // This file relies on the fact that the following declarations have been made | 5 // This file relies on the fact that the following declarations have been made |
8 // in runtime.js: | 6 // in runtime.js: |
9 // var $Object = global.Object; | 7 // var $Object = global.Object; |
10 | 8 |
11 // Instance class name can only be set on functions. That is the only | 9 // Instance class name can only be set on functions. That is the only |
12 // purpose for MathConstructor. | 10 // purpose for MathConstructor. |
13 function MathConstructor() {} | |
14 var $Math = new MathConstructor(); | |
15 | 11 |
16 var rngstate; // Initialized to a Uint32Array during genesis. | 12 var rngstate; // Initialized to a Uint32Array during genesis. |
17 | 13 |
18 var $abs; | 14 var $abs; |
19 var $exp; | 15 var $exp; |
20 var $floor; | 16 var $floor; |
21 var $max; | 17 var $max; |
22 var $min; | 18 var $min; |
23 | 19 |
24 // ------------------------------------------------------------------- | 20 // ------------------------------------------------------------------- |
25 | 21 |
26 (function() { | 22 (function() { |
27 | 23 |
| 24 "use strict"; |
| 25 |
28 // ECMA 262 - 15.8.2.1 | 26 // ECMA 262 - 15.8.2.1 |
29 function MathAbs(x) { | 27 function MathAbs(x) { |
30 if (%_IsSmi(x)) return x >= 0 ? x : -x; | 28 if (%_IsSmi(x)) return x >= 0 ? x : -x; |
31 x = TO_NUMBER_INLINE(x); | 29 x = TO_NUMBER_INLINE(x); |
32 if (x === 0) return 0; // To handle -0. | 30 if (x === 0) return 0; // To handle -0. |
33 return x > 0 ? x : -x; | 31 return x > 0 ? x : -x; |
34 } | 32 } |
35 | 33 |
36 // ECMA 262 - 15.8.2.2 | 34 // ECMA 262 - 15.8.2.2 |
37 function MathAcosJS(x) { | 35 function MathAcosJS(x) { |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 approx = NEWTON_ITERATION_CBRT(x, approx); | 288 approx = NEWTON_ITERATION_CBRT(x, approx); |
291 approx = NEWTON_ITERATION_CBRT(x, approx); | 289 approx = NEWTON_ITERATION_CBRT(x, approx); |
292 approx = NEWTON_ITERATION_CBRT(x, approx); | 290 approx = NEWTON_ITERATION_CBRT(x, approx); |
293 return NEWTON_ITERATION_CBRT(x, approx); | 291 return NEWTON_ITERATION_CBRT(x, approx); |
294 } | 292 } |
295 | 293 |
296 // ------------------------------------------------------------------- | 294 // ------------------------------------------------------------------- |
297 | 295 |
298 %CheckIsBootstrapping(); | 296 %CheckIsBootstrapping(); |
299 | 297 |
300 %InternalSetPrototype($Math, $Object.prototype); | 298 function MathConstructor() {} |
301 %AddNamedProperty(global, "Math", $Math, DONT_ENUM); | 299 |
| 300 var Math = new MathConstructor(); |
| 301 |
| 302 %InternalSetPrototype(Math, $Object.prototype); |
| 303 %AddNamedProperty(global, "Math", Math, DONT_ENUM); |
302 %FunctionSetInstanceClassName(MathConstructor, 'Math'); | 304 %FunctionSetInstanceClassName(MathConstructor, 'Math'); |
303 | 305 |
304 %AddNamedProperty($Math, symbolToStringTag, "Math", READ_ONLY | DONT_ENUM); | 306 %AddNamedProperty(Math, symbolToStringTag, "Math", READ_ONLY | DONT_ENUM); |
305 | 307 |
306 // Set up math constants. | 308 // Set up math constants. |
307 InstallConstants($Math, $Array( | 309 InstallConstants(Math, $Array( |
308 // ECMA-262, section 15.8.1.1. | 310 // ECMA-262, section 15.8.1.1. |
309 "E", 2.7182818284590452354, | 311 "E", 2.7182818284590452354, |
310 // ECMA-262, section 15.8.1.2. | 312 // ECMA-262, section 15.8.1.2. |
311 "LN10", 2.302585092994046, | 313 "LN10", 2.302585092994046, |
312 // ECMA-262, section 15.8.1.3. | 314 // ECMA-262, section 15.8.1.3. |
313 "LN2", 0.6931471805599453, | 315 "LN2", 0.6931471805599453, |
314 // ECMA-262, section 15.8.1.4. | 316 // ECMA-262, section 15.8.1.4. |
315 "LOG2E", 1.4426950408889634, | 317 "LOG2E", 1.4426950408889634, |
316 "LOG10E", 0.4342944819032518, | 318 "LOG10E", 0.4342944819032518, |
317 "PI", 3.1415926535897932, | 319 "PI", 3.1415926535897932, |
318 "SQRT1_2", 0.7071067811865476, | 320 "SQRT1_2", 0.7071067811865476, |
319 "SQRT2", 1.4142135623730951 | 321 "SQRT2", 1.4142135623730951 |
320 )); | 322 )); |
321 | 323 |
322 // Set up non-enumerable functions of the Math object and | 324 // Set up non-enumerable functions of the Math object and |
323 // set their names. | 325 // set their names. |
324 InstallFunctions($Math, DONT_ENUM, $Array( | 326 InstallFunctions(Math, DONT_ENUM, $Array( |
325 "random", MathRandom, | 327 "random", MathRandom, |
326 "abs", MathAbs, | 328 "abs", MathAbs, |
327 "acos", MathAcosJS, | 329 "acos", MathAcosJS, |
328 "asin", MathAsinJS, | 330 "asin", MathAsinJS, |
329 "atan", MathAtanJS, | 331 "atan", MathAtanJS, |
330 "ceil", MathCeil, | 332 "ceil", MathCeil, |
331 "exp", MathExp, | 333 "exp", MathExp, |
332 "floor", MathFloor, | 334 "floor", MathFloor, |
333 "log", MathLog, | 335 "log", MathLog, |
334 "round", MathRound, | 336 "round", MathRound, |
(...skipping 12 matching lines...) Expand all Loading... |
347 "hypot", MathHypot, | 349 "hypot", MathHypot, |
348 "fround", MathFroundJS, | 350 "fround", MathFroundJS, |
349 "clz32", MathClz32, | 351 "clz32", MathClz32, |
350 "cbrt", MathCbrt | 352 "cbrt", MathCbrt |
351 )); | 353 )); |
352 | 354 |
353 %SetInlineBuiltinFlag(MathCeil); | 355 %SetInlineBuiltinFlag(MathCeil); |
354 %SetInlineBuiltinFlag(MathFloor); | 356 %SetInlineBuiltinFlag(MathFloor); |
355 %SetInlineBuiltinFlag(MathRandom); | 357 %SetInlineBuiltinFlag(MathRandom); |
356 | 358 |
357 // Keep reference to original values of some global properties. This | 359 // Expose to the global scope. |
358 // has the added benefit that the code in this file is isolated from | |
359 // changes to these properties. | |
360 $abs = MathAbs; | 360 $abs = MathAbs; |
361 $exp = MathExp; | 361 $exp = MathExp; |
362 $floor = MathFloor; | 362 $floor = MathFloor; |
363 $max = MathMax; | 363 $max = MathMax; |
364 $min = MathMin; | 364 $min = MathMin; |
365 | 365 |
366 })(); | 366 })(); |
OLD | NEW |