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