OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 12 matching lines...) Expand all Loading... |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 | 28 |
29 // Keep reference to original values of some global properties. This | 29 // Keep reference to original values of some global properties. This |
30 // has the added benefit that the code in this file is isolated from | 30 // has the added benefit that the code in this file is isolated from |
31 // changes to these properties. | 31 // changes to these properties. |
32 const $Infinity = global.Infinity; | 32 const $Infinity = global.Infinity; |
33 const $floor = $Math_floor; | 33 const $floor = MathFloor; |
34 const $random = $Math_random; | 34 const $random = MathRandom; |
35 const $abs = $Math_abs; | 35 const $abs = MathAbs; |
36 | 36 |
37 // Instance class name can only be set on functions. That is the only | 37 // Instance class name can only be set on functions. That is the only |
38 // purpose for MathConstructor. | 38 // purpose for MathConstructor. |
39 function MathConstructor() {}; | 39 function MathConstructor() {} |
40 %FunctionSetInstanceClassName(MathConstructor, 'Math'); | 40 %FunctionSetInstanceClassName(MathConstructor, 'Math'); |
41 const $Math = new MathConstructor(); | 41 const $Math = new MathConstructor(); |
42 $Math.__proto__ = global.Object.prototype; | 42 $Math.__proto__ = global.Object.prototype; |
43 %AddProperty(global, "Math", $Math, DONT_ENUM); | 43 %AddProperty(global, "Math", $Math, DONT_ENUM); |
44 | 44 |
45 | 45 // ECMA 262 - 15.8.2.1 |
46 function $Math_random() { return %Math_random(); } | 46 function MathAbs(x) { |
47 %AddProperty($Math, "random", $Math_random, DONT_ENUM); | |
48 | |
49 function $Math_abs(x) { | |
50 if (%_IsSmi(x)) { | 47 if (%_IsSmi(x)) { |
51 return x >= 0 ? x : -x; | 48 return x >= 0 ? x : -x; |
52 } else { | 49 } else { |
53 return %Math_abs(ToNumber(x)); | 50 return %Math_abs(ToNumber(x)); |
54 } | 51 } |
55 } | 52 } |
56 %AddProperty($Math, "abs", $Math_abs, DONT_ENUM); | |
57 | 53 |
58 function $Math_acos(x) { return %Math_acos(ToNumber(x)); } | 54 // ECMA 262 - 15.8.2.2 |
59 %AddProperty($Math, "acos", $Math_acos, DONT_ENUM); | 55 function MathAcos(x) { return %Math_acos(ToNumber(x)); } |
60 | 56 |
61 function $Math_asin(x) { return %Math_asin(ToNumber(x)); } | 57 // ECMA 262 - 15.8.2.3 |
62 %AddProperty($Math, "asin", $Math_asin, DONT_ENUM); | 58 function MathAsin(x) { return %Math_asin(ToNumber(x)); } |
63 | 59 |
64 function $Math_atan(x) { return %Math_atan(ToNumber(x)); } | 60 // ECMA 262 - 15.8.2.4 |
65 %AddProperty($Math, "atan", $Math_atan, DONT_ENUM); | 61 function MathAtan(x) { return %Math_atan(ToNumber(x)); } |
66 | 62 |
67 function $Math_ceil(x) { return %Math_ceil(ToNumber(x)); } | 63 // ECMA 262 - 15.8.2.5 |
68 %AddProperty($Math, "ceil", $Math_ceil, DONT_ENUM); | 64 function MathAtan2(x, y) { return %Math_atan2(ToNumber(x), ToNumber(y)); } |
69 | 65 |
70 function $Math_cos(x) { return %Math_cos(ToNumber(x)); } | 66 // ECMA 262 - 15.8.2.6 |
71 %AddProperty($Math, "cos", $Math_cos, DONT_ENUM); | 67 function MathCeil(x) { return %Math_ceil(ToNumber(x)); } |
72 | 68 |
73 function $Math_exp(x) { return %Math_exp(ToNumber(x)); } | 69 // ECMA 262 - 15.8.2.7 |
74 %AddProperty($Math, "exp", $Math_exp, DONT_ENUM); | 70 function MathCos(x) { return %Math_cos(ToNumber(x)); } |
75 | 71 |
76 function $Math_floor(x) { return %Math_floor(ToNumber(x)); } | 72 // ECMA 262 - 15.8.2.8 |
77 %AddProperty($Math, "floor", $Math_floor, DONT_ENUM); | 73 function MathExp(x) { return %Math_exp(ToNumber(x)); } |
78 | 74 |
79 function $Math_log(x) { return %Math_log(ToNumber(x)); } | 75 // ECMA 262 - 15.8.2.9 |
80 %AddProperty($Math, "log", $Math_log, DONT_ENUM); | 76 function MathFloor(x) { return %Math_floor(ToNumber(x)); } |
81 | 77 |
82 function $Math_round(x) { return %Math_round(ToNumber(x)); } | 78 // ECMA 262 - 15.8.2.10 |
83 %AddProperty($Math, "round", $Math_round, DONT_ENUM); | 79 function MathLog(x) { return %Math_log(ToNumber(x)); } |
84 | 80 |
85 function $Math_sin(x) { return %Math_sin(ToNumber(x)); } | 81 // ECMA 262 - 15.8.2.11 |
86 %AddProperty($Math, "sin", $Math_sin, DONT_ENUM); | 82 function MathMax(arg1, arg2) { // length == 2 |
87 | |
88 function $Math_sqrt(x) { return %Math_sqrt(ToNumber(x)); } | |
89 %AddProperty($Math, "sqrt", $Math_sqrt, DONT_ENUM); | |
90 | |
91 function $Math_tan(x) { return %Math_tan(ToNumber(x)); } | |
92 %AddProperty($Math, "tan", $Math_tan, DONT_ENUM); | |
93 | |
94 function $Math_atan2(x, y) { return %Math_atan2(ToNumber(x), ToNumber(y)); } | |
95 %AddProperty($Math, "atan2", $Math_atan2, DONT_ENUM); | |
96 | |
97 function $Math_pow(x, y) { return %Math_pow(ToNumber(x), ToNumber(y)); } | |
98 %AddProperty($Math, "pow", $Math_pow, DONT_ENUM); | |
99 | |
100 function $Math_max(arg1, arg2) { // length == 2 | |
101 var r = -$Infinity; | 83 var r = -$Infinity; |
102 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { | 84 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { |
103 var n = ToNumber(%_Arguments(i)); | 85 var n = ToNumber(%_Arguments(i)); |
104 if (NUMBER_IS_NAN(n)) return n; | 86 if (NUMBER_IS_NAN(n)) return n; |
105 // Make sure +0 is consider greater than -0. | 87 // Make sure +0 is consider greater than -0. |
106 if (n > r || (n === 0 && r === 0 && (1 / n) > (1 / r))) r = n; | 88 if (n > r || (n === 0 && r === 0 && (1 / n) > (1 / r))) r = n; |
107 } | 89 } |
108 return r; | 90 return r; |
109 } | 91 } |
110 %AddProperty($Math, "max", $Math_max, DONT_ENUM); | |
111 | 92 |
112 function $Math_min(arg1, arg2) { // length == 2 | 93 // ECMA 262 - 15.8.2.12 |
| 94 function MathMin(arg1, arg2) { // length == 2 |
113 var r = $Infinity; | 95 var r = $Infinity; |
114 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { | 96 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { |
115 var n = ToNumber(%_Arguments(i)); | 97 var n = ToNumber(%_Arguments(i)); |
116 if (NUMBER_IS_NAN(n)) return n; | 98 if (NUMBER_IS_NAN(n)) return n; |
117 // Make sure -0 is consider less than +0. | 99 // Make sure -0 is consider less than +0. |
118 if (n < r || (n === 0 && r === 0 && (1 / n) < (1 / r))) r = n; | 100 if (n < r || (n === 0 && r === 0 && (1 / n) < (1 / r))) r = n; |
119 } | 101 } |
120 return r; | 102 return r; |
121 } | 103 } |
122 %AddProperty($Math, "min", $Math_min, DONT_ENUM); | 104 |
| 105 // ECMA 262 - 15.8.2.13 |
| 106 function MathPow(x, y) { return %Math_pow(ToNumber(x), ToNumber(y)); } |
| 107 |
| 108 // ECMA 262 - 15.8.2.14 |
| 109 function MathRandom() { return %Math_random(); } |
| 110 |
| 111 // ECMA 262 - 15.8.2.15 |
| 112 function MathRound(x) { return %Math_round(ToNumber(x)); } |
| 113 |
| 114 // ECMA 262 - 15.8.2.16 |
| 115 function MathSin(x) { return %Math_sin(ToNumber(x)); } |
| 116 |
| 117 // ECMA 262 - 15.8.2.17 |
| 118 function MathSqrt(x) { return %Math_sqrt(ToNumber(x)); } |
| 119 |
| 120 // ECMA 262 - 15.8.2.18 |
| 121 function MathTan(x) { return %Math_tan(ToNumber(x)); } |
123 | 122 |
124 | 123 |
125 // ECMA-262, section 15.8.1.1. | 124 // ------------------------------------------------------------------- |
126 %AddProperty($Math, "E", 2.7182818284590452354, DONT_ENUM | DONT_DELETE | READ_
ONLY); | |
127 | 125 |
128 // ECMA-262, section 15.8.1.2. | 126 function SetupMath() { |
129 %AddProperty($Math, "LN10", 2.302585092994046, DONT_ENUM | DONT_DELETE | READ_O
NLY); | 127 // Setup math constants. |
| 128 // ECMA-262, section 15.8.1.1. |
| 129 %AddProperty($Math, "E", 2.7182818284590452354, DONT_ENUM | DONT_DELETE | REA
D_ONLY); |
| 130 // ECMA-262, section 15.8.1.2. |
| 131 %AddProperty($Math, "LN10", 2.302585092994046, DONT_ENUM | DONT_DELETE | READ
_ONLY); |
| 132 // ECMA-262, section 15.8.1.3. |
| 133 %AddProperty($Math, "LN2", 0.6931471805599453, DONT_ENUM | DONT_DELETE | READ
_ONLY); |
| 134 // ECMA-262, section 15.8.1.4. |
| 135 %AddProperty($Math, "LOG2E", 1.4426950408889634, DONT_ENUM | DONT_DELETE | RE
AD_ONLY); |
| 136 %AddProperty($Math, "LOG10E", 0.43429448190325176, DONT_ENUM | DONT_DELETE |
READ_ONLY); |
| 137 %AddProperty($Math, "PI", 3.1415926535897932, DONT_ENUM | DONT_DELETE | READ_
ONLY); |
| 138 %AddProperty($Math, "SQRT1_2", 0.7071067811865476, DONT_ENUM | DONT_DELETE |
READ_ONLY); |
| 139 %AddProperty($Math, "SQRT2", 1.4142135623730951, DONT_ENUM | DONT_DELETE | RE
AD_ONLY); |
130 | 140 |
131 // ECMA-262, section 15.8.1.3. | 141 // Setup non-enumerable functions of the Math object and |
132 %AddProperty($Math, "LN2", 0.6931471805599453, DONT_ENUM | DONT_DELETE | READ_O
NLY); | 142 // set their names. |
| 143 InstallFunctions($Math, DONT_ENUM, $Array( |
| 144 "random", MathRandom, |
| 145 "abs", MathAbs, |
| 146 "acos", MathAcos, |
| 147 "asin", MathAsin, |
| 148 "atan", MathAtan, |
| 149 "ceil", MathCeil, |
| 150 "cos", MathCos, |
| 151 "exp", MathExp, |
| 152 "floor", MathFloor, |
| 153 "log", MathLog, |
| 154 "round", MathRound, |
| 155 "sin", MathSin, |
| 156 "sqrt", MathSqrt, |
| 157 "tan", MathTan, |
| 158 "atan2", MathAtan2, |
| 159 "pow", MathPow, |
| 160 "max", MathMax, |
| 161 "min", MathMin |
| 162 )); |
| 163 }; |
133 | 164 |
134 // ECMA-262, section 15.8.1.4. | 165 |
135 %AddProperty($Math, "LOG2E", 1.4426950408889634, DONT_ENUM | DONT_DELETE | READ
_ONLY); | 166 SetupMath(); |
136 %AddProperty($Math, "LOG10E", 0.43429448190325176, DONT_ENUM | DONT_DELETE | RE
AD_ONLY); | |
137 %AddProperty($Math, "PI", 3.1415926535897932, DONT_ENUM | DONT_DELETE | READ_ON
LY); | |
138 %AddProperty($Math, "SQRT1_2", 0.7071067811865476, DONT_ENUM | DONT_DELETE | RE
AD_ONLY); | |
139 %AddProperty($Math, "SQRT2", 1.4142135623730951, DONT_ENUM | DONT_DELETE | READ
_ONLY); | |
OLD | NEW |