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 26 matching lines...) Expand all Loading... |
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 %SetProperty(global, "Math", $Math, DONT_ENUM); | 43 %SetProperty(global, "Math", $Math, DONT_ENUM); |
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)) { | 47 if (%_IsSmi(x)) return x >= 0 ? x : -x; |
48 return x >= 0 ? x : -x; | 48 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 49 return %Math_abs(x); |
| 50 } |
| 51 |
| 52 // ECMA 262 - 15.8.2.2 |
| 53 function MathAcos(x) { |
| 54 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 55 return %Math_acos(x); |
| 56 } |
| 57 |
| 58 // ECMA 262 - 15.8.2.3 |
| 59 function MathAsin(x) { |
| 60 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 61 return %Math_asin(x); |
| 62 } |
| 63 |
| 64 // ECMA 262 - 15.8.2.4 |
| 65 function MathAtan(x) { |
| 66 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 67 return %Math_atan(x); |
| 68 } |
| 69 |
| 70 // ECMA 262 - 15.8.2.5 |
| 71 function MathAtan2(x, y) { |
| 72 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 73 if (!IS_NUMBER(y)) y = ToNumber(y); |
| 74 return %Math_atan2(x, y); |
| 75 } |
| 76 |
| 77 // ECMA 262 - 15.8.2.6 |
| 78 function MathCeil(x) { |
| 79 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 80 return %Math_ceil(x); |
| 81 } |
| 82 |
| 83 // ECMA 262 - 15.8.2.7 |
| 84 function MathCos(x) { |
| 85 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 86 return %Math_cos(x); |
| 87 } |
| 88 |
| 89 // ECMA 262 - 15.8.2.8 |
| 90 function MathExp(x) { |
| 91 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 92 return %Math_exp(x); |
| 93 } |
| 94 |
| 95 // ECMA 262 - 15.8.2.9 |
| 96 function MathFloor(x) { |
| 97 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 98 if (0 < x && x <= 0xFFFFFFFF) { |
| 99 // Numbers in the range [0, 2^32) can be floored by converting |
| 100 // them to an unsigned 32-bit value using the shift operator. |
| 101 // We avoid doing so for -0, because the result of Math.floor(-0) |
| 102 // has to be -0, which wouldn't be the case with the shift. |
| 103 return x << 0; |
49 } else { | 104 } else { |
50 return %Math_abs(ToNumber(x)); | 105 return %Math_floor(x); |
51 } | 106 } |
52 } | 107 } |
53 | 108 |
54 // ECMA 262 - 15.8.2.2 | |
55 function MathAcos(x) { return %Math_acos(ToNumber(x)); } | |
56 | |
57 // ECMA 262 - 15.8.2.3 | |
58 function MathAsin(x) { return %Math_asin(ToNumber(x)); } | |
59 | |
60 // ECMA 262 - 15.8.2.4 | |
61 function MathAtan(x) { return %Math_atan(ToNumber(x)); } | |
62 | |
63 // ECMA 262 - 15.8.2.5 | |
64 function MathAtan2(x, y) { return %Math_atan2(ToNumber(x), ToNumber(y)); } | |
65 | |
66 // ECMA 262 - 15.8.2.6 | |
67 function MathCeil(x) { return %Math_ceil(ToNumber(x)); } | |
68 | |
69 // ECMA 262 - 15.8.2.7 | |
70 function MathCos(x) { return %Math_cos(ToNumber(x)); } | |
71 | |
72 // ECMA 262 - 15.8.2.8 | |
73 function MathExp(x) { return %Math_exp(ToNumber(x)); } | |
74 | |
75 // ECMA 262 - 15.8.2.9 | |
76 function MathFloor(x) { return %Math_floor(ToNumber(x)); } | |
77 | |
78 // ECMA 262 - 15.8.2.10 | 109 // ECMA 262 - 15.8.2.10 |
79 function MathLog(x) { return %Math_log(ToNumber(x)); } | 110 function MathLog(x) { |
| 111 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 112 return %Math_log(x); |
| 113 } |
80 | 114 |
81 // ECMA 262 - 15.8.2.11 | 115 // ECMA 262 - 15.8.2.11 |
82 function MathMax(arg1, arg2) { // length == 2 | 116 function MathMax(arg1, arg2) { // length == 2 |
83 var r = -$Infinity; | 117 var r = -$Infinity; |
84 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { | 118 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { |
85 var n = ToNumber(%_Arguments(i)); | 119 var n = ToNumber(%_Arguments(i)); |
86 if (NUMBER_IS_NAN(n)) return n; | 120 if (NUMBER_IS_NAN(n)) return n; |
87 // Make sure +0 is consider greater than -0. | 121 // Make sure +0 is consider greater than -0. |
88 if (n > r || (n === 0 && r === 0 && (1 / n) > (1 / r))) r = n; | 122 if (n > r || (n === 0 && r === 0 && (1 / n) > (1 / r))) r = n; |
89 } | 123 } |
90 return r; | 124 return r; |
91 } | 125 } |
92 | 126 |
93 // ECMA 262 - 15.8.2.12 | 127 // ECMA 262 - 15.8.2.12 |
94 function MathMin(arg1, arg2) { // length == 2 | 128 function MathMin(arg1, arg2) { // length == 2 |
95 var r = $Infinity; | 129 var r = $Infinity; |
96 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { | 130 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { |
97 var n = ToNumber(%_Arguments(i)); | 131 var n = ToNumber(%_Arguments(i)); |
98 if (NUMBER_IS_NAN(n)) return n; | 132 if (NUMBER_IS_NAN(n)) return n; |
99 // Make sure -0 is consider less than +0. | 133 // Make sure -0 is consider less than +0. |
100 if (n < r || (n === 0 && r === 0 && (1 / n) < (1 / r))) r = n; | 134 if (n < r || (n === 0 && r === 0 && (1 / n) < (1 / r))) r = n; |
101 } | 135 } |
102 return r; | 136 return r; |
103 } | 137 } |
104 | 138 |
105 // ECMA 262 - 15.8.2.13 | 139 // ECMA 262 - 15.8.2.13 |
106 function MathPow(x, y) { return %Math_pow(ToNumber(x), ToNumber(y)); } | 140 function MathPow(x, y) { |
| 141 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 142 if (!IS_NUMBER(y)) y = ToNumber(y); |
| 143 return %Math_pow(x, y); |
| 144 } |
107 | 145 |
108 // ECMA 262 - 15.8.2.14 | 146 // ECMA 262 - 15.8.2.14 |
109 function MathRandom() { return %Math_random(); } | 147 function MathRandom() { |
| 148 return %_RandomPositiveSmi() / 0x40000000; |
| 149 } |
110 | 150 |
111 // ECMA 262 - 15.8.2.15 | 151 // ECMA 262 - 15.8.2.15 |
112 function MathRound(x) { return %Math_round(ToNumber(x)); } | 152 function MathRound(x) { |
| 153 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 154 return %Math_round(x); |
| 155 } |
113 | 156 |
114 // ECMA 262 - 15.8.2.16 | 157 // ECMA 262 - 15.8.2.16 |
115 function MathSin(x) { return %Math_sin(ToNumber(x)); } | 158 function MathSin(x) { |
| 159 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 160 return %Math_sin(x); |
| 161 } |
116 | 162 |
117 // ECMA 262 - 15.8.2.17 | 163 // ECMA 262 - 15.8.2.17 |
118 function MathSqrt(x) { return %Math_sqrt(ToNumber(x)); } | 164 function MathSqrt(x) { |
| 165 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 166 return %Math_sqrt(x); |
| 167 } |
119 | 168 |
120 // ECMA 262 - 15.8.2.18 | 169 // ECMA 262 - 15.8.2.18 |
121 function MathTan(x) { return %Math_tan(ToNumber(x)); } | 170 function MathTan(x) { |
| 171 if (!IS_NUMBER(x)) x = ToNumber(x); |
| 172 return %Math_tan(x); |
| 173 } |
122 | 174 |
123 | 175 |
124 // ------------------------------------------------------------------- | 176 // ------------------------------------------------------------------- |
125 | 177 |
126 function SetupMath() { | 178 function SetupMath() { |
127 // Setup math constants. | 179 // Setup math constants. |
128 // ECMA-262, section 15.8.1.1. | 180 // ECMA-262, section 15.8.1.1. |
129 %SetProperty($Math, | 181 %SetProperty($Math, |
130 "E", | 182 "E", |
131 2.7182818284590452354, | 183 2.7182818284590452354, |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 "tan", MathTan, | 233 "tan", MathTan, |
182 "atan2", MathAtan2, | 234 "atan2", MathAtan2, |
183 "pow", MathPow, | 235 "pow", MathPow, |
184 "max", MathMax, | 236 "max", MathMax, |
185 "min", MathMin | 237 "min", MathMin |
186 )); | 238 )); |
187 }; | 239 }; |
188 | 240 |
189 | 241 |
190 SetupMath(); | 242 SetupMath(); |
OLD | NEW |