OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 27 matching lines...) Expand all Loading... |
38 // Instance class name can only be set on functions. That is the only | 38 // Instance class name can only be set on functions. That is the only |
39 // purpose for MathConstructor. | 39 // purpose for MathConstructor. |
40 function MathConstructor() {} | 40 function MathConstructor() {} |
41 var $Math = new MathConstructor(); | 41 var $Math = new MathConstructor(); |
42 | 42 |
43 // ------------------------------------------------------------------- | 43 // ------------------------------------------------------------------- |
44 | 44 |
45 // ECMA 262 - 15.8.2.1 | 45 // ECMA 262 - 15.8.2.1 |
46 function MathAbs(x) { | 46 function MathAbs(x) { |
47 if (%_IsSmi(x)) return x >= 0 ? x : -x; | 47 if (%_IsSmi(x)) return x >= 0 ? x : -x; |
48 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 48 x = TO_NUMBER_INLINE(x); |
49 if (x === 0) return 0; // To handle -0. | 49 if (x === 0) return 0; // To handle -0. |
50 return x > 0 ? x : -x; | 50 return x > 0 ? x : -x; |
51 } | 51 } |
52 | 52 |
53 // ECMA 262 - 15.8.2.2 | 53 // ECMA 262 - 15.8.2.2 |
54 function MathAcos(x) { | 54 function MathAcos(x) { |
55 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 55 return %Math_acos(TO_NUMBER_INLINE(x)); |
56 return %Math_acos(x); | |
57 } | 56 } |
58 | 57 |
59 // ECMA 262 - 15.8.2.3 | 58 // ECMA 262 - 15.8.2.3 |
60 function MathAsin(x) { | 59 function MathAsin(x) { |
61 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 60 return %Math_asin(TO_NUMBER_INLINE(x)); |
62 return %Math_asin(x); | |
63 } | 61 } |
64 | 62 |
65 // ECMA 262 - 15.8.2.4 | 63 // ECMA 262 - 15.8.2.4 |
66 function MathAtan(x) { | 64 function MathAtan(x) { |
67 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 65 return %Math_atan(TO_NUMBER_INLINE(x)); |
68 return %Math_atan(x); | |
69 } | 66 } |
70 | 67 |
71 // ECMA 262 - 15.8.2.5 | 68 // ECMA 262 - 15.8.2.5 |
72 // The naming of y and x matches the spec, as does the order in which | 69 // The naming of y and x matches the spec, as does the order in which |
73 // ToNumber (valueOf) is called. | 70 // ToNumber (valueOf) is called. |
74 function MathAtan2(y, x) { | 71 function MathAtan2(y, x) { |
75 if (!IS_NUMBER(y)) y = NonNumberToNumber(y); | 72 return %Math_atan2(TO_NUMBER_INLINE(y), TO_NUMBER_INLINE(x)); |
76 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | |
77 return %Math_atan2(y, x); | |
78 } | 73 } |
79 | 74 |
80 // ECMA 262 - 15.8.2.6 | 75 // ECMA 262 - 15.8.2.6 |
81 function MathCeil(x) { | 76 function MathCeil(x) { |
82 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 77 return %Math_ceil(TO_NUMBER_INLINE(x)); |
83 return %Math_ceil(x); | |
84 } | 78 } |
85 | 79 |
86 // ECMA 262 - 15.8.2.7 | 80 // ECMA 262 - 15.8.2.7 |
87 function MathCos(x) { | 81 function MathCos(x) { |
88 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 82 return %_MathCos(TO_NUMBER_INLINE(x)); |
89 return %_MathCos(x); | |
90 } | 83 } |
91 | 84 |
92 // ECMA 262 - 15.8.2.8 | 85 // ECMA 262 - 15.8.2.8 |
93 function MathExp(x) { | 86 function MathExp(x) { |
94 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 87 return %Math_exp(TO_NUMBER_INLINE(x)); |
95 return %Math_exp(x); | |
96 } | 88 } |
97 | 89 |
98 // ECMA 262 - 15.8.2.9 | 90 // ECMA 262 - 15.8.2.9 |
99 function MathFloor(x) { | 91 function MathFloor(x) { |
100 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 92 x = TO_NUMBER_INLINE(x); |
101 // It's more common to call this with a positive number that's out | 93 // It's more common to call this with a positive number that's out |
102 // of range than negative numbers; check the upper bound first. | 94 // of range than negative numbers; check the upper bound first. |
103 if (x < 0x80000000 && x > 0) { | 95 if (x < 0x80000000 && x > 0) { |
104 // Numbers in the range [0, 2^31) can be floored by converting | 96 // Numbers in the range [0, 2^31) can be floored by converting |
105 // them to an unsigned 32-bit value using the shift operator. | 97 // them to an unsigned 32-bit value using the shift operator. |
106 // We avoid doing so for -0, because the result of Math.floor(-0) | 98 // We avoid doing so for -0, because the result of Math.floor(-0) |
107 // has to be -0, which wouldn't be the case with the shift. | 99 // has to be -0, which wouldn't be the case with the shift. |
108 return TO_UINT32(x); | 100 return TO_UINT32(x); |
109 } else { | 101 } else { |
110 return %Math_floor(x); | 102 return %Math_floor(x); |
111 } | 103 } |
112 } | 104 } |
113 | 105 |
114 // ECMA 262 - 15.8.2.10 | 106 // ECMA 262 - 15.8.2.10 |
115 function MathLog(x) { | 107 function MathLog(x) { |
116 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 108 return %_MathLog(TO_NUMBER_INLINE(x)); |
117 return %_MathLog(x); | |
118 } | 109 } |
119 | 110 |
120 // ECMA 262 - 15.8.2.11 | 111 // ECMA 262 - 15.8.2.11 |
121 function MathMax(arg1, arg2) { // length == 2 | 112 function MathMax(arg1, arg2) { // length == 2 |
122 var length = %_ArgumentsLength(); | 113 var length = %_ArgumentsLength(); |
123 if (length == 2) { | 114 if (length == 2) { |
124 if (!IS_NUMBER(arg1)) arg1 = NonNumberToNumber(arg1); | 115 arg1 = TO_NUMBER_INLINE(arg1); |
125 if (!IS_NUMBER(arg2)) arg2 = NonNumberToNumber(arg2); | 116 arg2 = TO_NUMBER_INLINE(arg2); |
126 if (arg2 > arg1) return arg2; | 117 if (arg2 > arg1) return arg2; |
127 if (arg1 > arg2) return arg1; | 118 if (arg1 > arg2) return arg1; |
128 if (arg1 == arg2) { | 119 if (arg1 == arg2) { |
129 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be | 120 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be |
130 // a Smi or a heap number. | 121 // a Smi or a heap number. |
131 return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg2 : arg1; | 122 return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg2 : arg1; |
132 } | 123 } |
133 // All comparisons failed, one of the arguments must be NaN. | 124 // All comparisons failed, one of the arguments must be NaN. |
134 return NAN; | 125 return NAN; |
135 } | 126 } |
136 var r = -INFINITY; | 127 var r = -INFINITY; |
137 for (var i = 0; i < length; i++) { | 128 for (var i = 0; i < length; i++) { |
138 var n = %_Arguments(i); | 129 var n = %_Arguments(i); |
139 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); | 130 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); |
140 // Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be | 131 // Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be |
141 // a Smi or heap number. | 132 // a Smi or heap number. |
142 if (NUMBER_IS_NAN(n) || n > r || | 133 if (NUMBER_IS_NAN(n) || n > r || |
143 (r == 0 && n == 0 && !%_IsSmi(r) && 1 / r < 0)) { | 134 (r == 0 && n == 0 && !%_IsSmi(r) && 1 / r < 0)) { |
144 r = n; | 135 r = n; |
145 } | 136 } |
146 } | 137 } |
147 return r; | 138 return r; |
148 } | 139 } |
149 | 140 |
150 // ECMA 262 - 15.8.2.12 | 141 // ECMA 262 - 15.8.2.12 |
151 function MathMin(arg1, arg2) { // length == 2 | 142 function MathMin(arg1, arg2) { // length == 2 |
152 var length = %_ArgumentsLength(); | 143 var length = %_ArgumentsLength(); |
153 if (length == 2) { | 144 if (length == 2) { |
154 if (!IS_NUMBER(arg1)) arg1 = NonNumberToNumber(arg1); | 145 arg1 = TO_NUMBER_INLINE(arg1); |
155 if (!IS_NUMBER(arg2)) arg2 = NonNumberToNumber(arg2); | 146 arg2 = TO_NUMBER_INLINE(arg2); |
156 if (arg2 > arg1) return arg1; | 147 if (arg2 > arg1) return arg1; |
157 if (arg1 > arg2) return arg2; | 148 if (arg1 > arg2) return arg2; |
158 if (arg1 == arg2) { | 149 if (arg1 == arg2) { |
159 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be | 150 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be |
160 // a Smi or a heap number. | 151 // a Smi or a heap number. |
161 return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg1 : arg2; | 152 return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg1 : arg2; |
162 } | 153 } |
163 // All comparisons failed, one of the arguments must be NaN. | 154 // All comparisons failed, one of the arguments must be NaN. |
164 return NAN; | 155 return NAN; |
165 } | 156 } |
166 var r = INFINITY; | 157 var r = INFINITY; |
167 for (var i = 0; i < length; i++) { | 158 for (var i = 0; i < length; i++) { |
168 var n = %_Arguments(i); | 159 var n = %_Arguments(i); |
169 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); | 160 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); |
170 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be a | 161 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be a |
171 // Smi or a heap number. | 162 // Smi or a heap number. |
172 if (NUMBER_IS_NAN(n) || n < r || | 163 if (NUMBER_IS_NAN(n) || n < r || |
173 (r == 0 && n == 0 && !%_IsSmi(n) && 1 / n < 0)) { | 164 (r == 0 && n == 0 && !%_IsSmi(n) && 1 / n < 0)) { |
174 r = n; | 165 r = n; |
175 } | 166 } |
176 } | 167 } |
177 return r; | 168 return r; |
178 } | 169 } |
179 | 170 |
180 // ECMA 262 - 15.8.2.13 | 171 // ECMA 262 - 15.8.2.13 |
181 function MathPow(x, y) { | 172 function MathPow(x, y) { |
182 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 173 return %_MathPow(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y)); |
183 if (!IS_NUMBER(y)) y = NonNumberToNumber(y); | |
184 return %_MathPow(x, y); | |
185 } | 174 } |
186 | 175 |
187 // ECMA 262 - 15.8.2.14 | 176 // ECMA 262 - 15.8.2.14 |
188 function MathRandom() { | 177 function MathRandom() { |
189 return %_RandomHeapNumber(); | 178 return %_RandomHeapNumber(); |
190 } | 179 } |
191 | 180 |
192 // ECMA 262 - 15.8.2.15 | 181 // ECMA 262 - 15.8.2.15 |
193 function MathRound(x) { | 182 function MathRound(x) { |
194 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 183 return %RoundNumber(TO_NUMBER_INLINE(x)); |
195 return %RoundNumber(x); | |
196 } | 184 } |
197 | 185 |
198 // ECMA 262 - 15.8.2.16 | 186 // ECMA 262 - 15.8.2.16 |
199 function MathSin(x) { | 187 function MathSin(x) { |
200 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 188 return %_MathSin(TO_NUMBER_INLINE(x)); |
201 return %_MathSin(x); | |
202 } | 189 } |
203 | 190 |
204 // ECMA 262 - 15.8.2.17 | 191 // ECMA 262 - 15.8.2.17 |
205 function MathSqrt(x) { | 192 function MathSqrt(x) { |
206 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 193 return %_MathSqrt(TO_NUMBER_INLINE(x)); |
207 return %_MathSqrt(x); | |
208 } | 194 } |
209 | 195 |
210 // ECMA 262 - 15.8.2.18 | 196 // ECMA 262 - 15.8.2.18 |
211 function MathTan(x) { | 197 function MathTan(x) { |
212 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 198 return %_MathTan(TO_NUMBER_INLINE(x)); |
213 return %_MathTan(x); | |
214 } | 199 } |
215 | 200 |
216 // Non-standard extension. | 201 // Non-standard extension. |
217 function MathImul(x, y) { | 202 function MathImul(x, y) { |
218 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); | 203 return %NumberImul(TO_NUMBER_INLINE(x), TO_NUMBER_INLINE(y)); |
219 if (!IS_NUMBER(y)) y = NonNumberToNumber(y); | |
220 return %NumberImul(x, y); | |
221 } | 204 } |
222 | 205 |
223 | 206 |
224 // ------------------------------------------------------------------- | 207 // ------------------------------------------------------------------- |
225 | 208 |
226 function SetUpMath() { | 209 function SetUpMath() { |
227 %CheckIsBootstrapping(); | 210 %CheckIsBootstrapping(); |
228 | 211 |
229 %SetPrototype($Math, $Object.prototype); | 212 %SetPrototype($Math, $Object.prototype); |
230 %SetProperty(global, "Math", $Math, DONT_ENUM); | 213 %SetProperty(global, "Math", $Math, DONT_ENUM); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 "tan", MathTan, | 272 "tan", MathTan, |
290 "atan2", MathAtan2, | 273 "atan2", MathAtan2, |
291 "pow", MathPow, | 274 "pow", MathPow, |
292 "max", MathMax, | 275 "max", MathMax, |
293 "min", MathMin, | 276 "min", MathMin, |
294 "imul", MathImul | 277 "imul", MathImul |
295 )); | 278 )); |
296 } | 279 } |
297 | 280 |
298 SetUpMath(); | 281 SetUpMath(); |
OLD | NEW |