| 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 // purpose for MathConstructor. | 37 // purpose for MathConstructor. |
| 38 function MathConstructor() {} | 38 function MathConstructor() {} |
| 39 %FunctionSetInstanceClassName(MathConstructor, 'Math'); | 39 %FunctionSetInstanceClassName(MathConstructor, 'Math'); |
| 40 const $Math = new MathConstructor(); | 40 const $Math = new MathConstructor(); |
| 41 $Math.__proto__ = global.Object.prototype; | 41 $Math.__proto__ = global.Object.prototype; |
| 42 %SetProperty(global, "Math", $Math, DONT_ENUM); | 42 %SetProperty(global, "Math", $Math, DONT_ENUM); |
| 43 | 43 |
| 44 // ECMA 262 - 15.8.2.1 | 44 // ECMA 262 - 15.8.2.1 |
| 45 function MathAbs(x) { | 45 function MathAbs(x) { |
| 46 if (%_IsSmi(x)) return x >= 0 ? x : -x; | 46 if (%_IsSmi(x)) return x >= 0 ? x : -x; |
| 47 if (!IS_NUMBER(x)) x = ToNumber(x); | 47 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 48 if (x === 0) return 0; // To handle -0. | 48 if (x === 0) return 0; // To handle -0. |
| 49 return x > 0 ? x : -x; | 49 return x > 0 ? x : -x; |
| 50 } | 50 } |
| 51 | 51 |
| 52 // ECMA 262 - 15.8.2.2 | 52 // ECMA 262 - 15.8.2.2 |
| 53 function MathAcos(x) { | 53 function MathAcos(x) { |
| 54 if (!IS_NUMBER(x)) x = ToNumber(x); | 54 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 55 return %Math_acos(x); | 55 return %Math_acos(x); |
| 56 } | 56 } |
| 57 | 57 |
| 58 // ECMA 262 - 15.8.2.3 | 58 // ECMA 262 - 15.8.2.3 |
| 59 function MathAsin(x) { | 59 function MathAsin(x) { |
| 60 if (!IS_NUMBER(x)) x = ToNumber(x); | 60 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 61 return %Math_asin(x); | 61 return %Math_asin(x); |
| 62 } | 62 } |
| 63 | 63 |
| 64 // ECMA 262 - 15.8.2.4 | 64 // ECMA 262 - 15.8.2.4 |
| 65 function MathAtan(x) { | 65 function MathAtan(x) { |
| 66 if (!IS_NUMBER(x)) x = ToNumber(x); | 66 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 67 return %Math_atan(x); | 67 return %Math_atan(x); |
| 68 } | 68 } |
| 69 | 69 |
| 70 // ECMA 262 - 15.8.2.5 | 70 // ECMA 262 - 15.8.2.5 |
| 71 // The naming of y and x matches the spec, as does the order in which | 71 // The naming of y and x matches the spec, as does the order in which |
| 72 // ToNumber (valueOf) is called. | 72 // ToNumber (valueOf) is called. |
| 73 function MathAtan2(y, x) { | 73 function MathAtan2(y, x) { |
| 74 if (!IS_NUMBER(y)) y = ToNumber(y); | 74 if (!IS_NUMBER(y)) y = NonNumberToNumber(y); |
| 75 if (!IS_NUMBER(x)) x = ToNumber(x); | 75 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 76 return %Math_atan2(y, x); | 76 return %Math_atan2(y, x); |
| 77 } | 77 } |
| 78 | 78 |
| 79 // ECMA 262 - 15.8.2.6 | 79 // ECMA 262 - 15.8.2.6 |
| 80 function MathCeil(x) { | 80 function MathCeil(x) { |
| 81 if (!IS_NUMBER(x)) x = ToNumber(x); | 81 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 82 return %Math_ceil(x); | 82 return %Math_ceil(x); |
| 83 } | 83 } |
| 84 | 84 |
| 85 // ECMA 262 - 15.8.2.7 | 85 // ECMA 262 - 15.8.2.7 |
| 86 function MathCos(x) { | 86 function MathCos(x) { |
| 87 if (!IS_NUMBER(x)) x = ToNumber(x); | 87 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 88 return %_MathCos(x); | 88 return %_MathCos(x); |
| 89 } | 89 } |
| 90 | 90 |
| 91 // ECMA 262 - 15.8.2.8 | 91 // ECMA 262 - 15.8.2.8 |
| 92 function MathExp(x) { | 92 function MathExp(x) { |
| 93 if (!IS_NUMBER(x)) x = ToNumber(x); | 93 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 94 return %Math_exp(x); | 94 return %Math_exp(x); |
| 95 } | 95 } |
| 96 | 96 |
| 97 // ECMA 262 - 15.8.2.9 | 97 // ECMA 262 - 15.8.2.9 |
| 98 function MathFloor(x) { | 98 function MathFloor(x) { |
| 99 if (!IS_NUMBER(x)) x = ToNumber(x); | 99 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 100 // It's more common to call this with a positive number that's out | 100 // It's more common to call this with a positive number that's out |
| 101 // of range than negative numbers; check the upper bound first. | 101 // of range than negative numbers; check the upper bound first. |
| 102 if (x < 0x80000000 && x > 0) { | 102 if (x < 0x80000000 && x > 0) { |
| 103 // Numbers in the range [0, 2^31) can be floored by converting | 103 // Numbers in the range [0, 2^31) can be floored by converting |
| 104 // them to an unsigned 32-bit value using the shift operator. | 104 // them to an unsigned 32-bit value using the shift operator. |
| 105 // We avoid doing so for -0, because the result of Math.floor(-0) | 105 // We avoid doing so for -0, because the result of Math.floor(-0) |
| 106 // has to be -0, which wouldn't be the case with the shift. | 106 // has to be -0, which wouldn't be the case with the shift. |
| 107 return TO_UINT32(x); | 107 return TO_UINT32(x); |
| 108 } else { | 108 } else { |
| 109 return %Math_floor(x); | 109 return %Math_floor(x); |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 // ECMA 262 - 15.8.2.10 | 113 // ECMA 262 - 15.8.2.10 |
| 114 function MathLog(x) { | 114 function MathLog(x) { |
| 115 if (!IS_NUMBER(x)) x = ToNumber(x); | 115 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 116 return %_MathLog(x); | 116 return %_MathLog(x); |
| 117 } | 117 } |
| 118 | 118 |
| 119 // ECMA 262 - 15.8.2.11 | 119 // ECMA 262 - 15.8.2.11 |
| 120 function MathMax(arg1, arg2) { // length == 2 | 120 function MathMax(arg1, arg2) { // length == 2 |
| 121 var length = %_ArgumentsLength(); | 121 var length = %_ArgumentsLength(); |
| 122 if (length == 0) { | 122 if (length == 0) { |
| 123 return -1/0; // Compiler constant-folds this to -Infinity. | 123 return -1/0; // Compiler constant-folds this to -Infinity. |
| 124 } | 124 } |
| 125 var r = arg1; | 125 var r = arg1; |
| 126 if (!IS_NUMBER(r)) r = ToNumber(r); | 126 if (!IS_NUMBER(r)) r = NonNumberToNumber(r); |
| 127 if (NUMBER_IS_NAN(r)) return r; | 127 if (NUMBER_IS_NAN(r)) return r; |
| 128 for (var i = 1; i < length; i++) { | 128 for (var i = 1; i < length; i++) { |
| 129 var n = %_Arguments(i); | 129 var n = %_Arguments(i); |
| 130 if (!IS_NUMBER(n)) n = ToNumber(n); | 130 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); |
| 131 if (NUMBER_IS_NAN(n)) return n; | 131 if (NUMBER_IS_NAN(n)) return n; |
| 132 // Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be | 132 // Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be |
| 133 // a Smi or heap number. | 133 // a Smi or heap number. |
| 134 if (n > r || (r === 0 && n === 0 && !%_IsSmi(r) && 1 / r < 0)) r = n; | 134 if (n > r || (r === 0 && n === 0 && !%_IsSmi(r) && 1 / r < 0)) r = n; |
| 135 } | 135 } |
| 136 return r; | 136 return r; |
| 137 } | 137 } |
| 138 | 138 |
| 139 // ECMA 262 - 15.8.2.12 | 139 // ECMA 262 - 15.8.2.12 |
| 140 function MathMin(arg1, arg2) { // length == 2 | 140 function MathMin(arg1, arg2) { // length == 2 |
| 141 var length = %_ArgumentsLength(); | 141 var length = %_ArgumentsLength(); |
| 142 if (length == 0) { | 142 if (length == 0) { |
| 143 return 1/0; // Compiler constant-folds this to Infinity. | 143 return 1/0; // Compiler constant-folds this to Infinity. |
| 144 } | 144 } |
| 145 var r = arg1; | 145 var r = arg1; |
| 146 if (!IS_NUMBER(r)) r = ToNumber(r); | 146 if (!IS_NUMBER(r)) r = NonNumberToNumber(r); |
| 147 if (NUMBER_IS_NAN(r)) return r; | 147 if (NUMBER_IS_NAN(r)) return r; |
| 148 for (var i = 1; i < length; i++) { | 148 for (var i = 1; i < length; i++) { |
| 149 var n = %_Arguments(i); | 149 var n = %_Arguments(i); |
| 150 if (!IS_NUMBER(n)) n = ToNumber(n); | 150 if (!IS_NUMBER(n)) n = NonNumberToNumber(n); |
| 151 if (NUMBER_IS_NAN(n)) return n; | 151 if (NUMBER_IS_NAN(n)) return n; |
| 152 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can b a | 152 // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can b a |
| 153 // Smi or a heap number. | 153 // Smi or a heap number. |
| 154 if (n < r || (r === 0 && n === 0 && !%_IsSmi(n) && 1 / n < 0)) r = n; | 154 if (n < r || (r === 0 && n === 0 && !%_IsSmi(n) && 1 / n < 0)) r = n; |
| 155 } | 155 } |
| 156 return r; | 156 return r; |
| 157 } | 157 } |
| 158 | 158 |
| 159 // ECMA 262 - 15.8.2.13 | 159 // ECMA 262 - 15.8.2.13 |
| 160 function MathPow(x, y) { | 160 function MathPow(x, y) { |
| 161 if (!IS_NUMBER(x)) x = ToNumber(x); | 161 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 162 if (!IS_NUMBER(y)) y = ToNumber(y); | 162 if (!IS_NUMBER(y)) y = NonNumberToNumber(y); |
| 163 return %_MathPow(x, y); | 163 return %_MathPow(x, y); |
| 164 } | 164 } |
| 165 | 165 |
| 166 // ECMA 262 - 15.8.2.14 | 166 // ECMA 262 - 15.8.2.14 |
| 167 function MathRandom() { | 167 function MathRandom() { |
| 168 return %_RandomHeapNumber(); | 168 return %_RandomHeapNumber(); |
| 169 } | 169 } |
| 170 | 170 |
| 171 // ECMA 262 - 15.8.2.15 | 171 // ECMA 262 - 15.8.2.15 |
| 172 function MathRound(x) { | 172 function MathRound(x) { |
| 173 if (!IS_NUMBER(x)) x = ToNumber(x); | 173 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 174 return %RoundNumber(x); | 174 return %RoundNumber(x); |
| 175 } | 175 } |
| 176 | 176 |
| 177 // ECMA 262 - 15.8.2.16 | 177 // ECMA 262 - 15.8.2.16 |
| 178 function MathSin(x) { | 178 function MathSin(x) { |
| 179 if (!IS_NUMBER(x)) x = ToNumber(x); | 179 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 180 return %_MathSin(x); | 180 return %_MathSin(x); |
| 181 } | 181 } |
| 182 | 182 |
| 183 // ECMA 262 - 15.8.2.17 | 183 // ECMA 262 - 15.8.2.17 |
| 184 function MathSqrt(x) { | 184 function MathSqrt(x) { |
| 185 if (!IS_NUMBER(x)) x = ToNumber(x); | 185 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 186 return %_MathSqrt(x); | 186 return %_MathSqrt(x); |
| 187 } | 187 } |
| 188 | 188 |
| 189 // ECMA 262 - 15.8.2.18 | 189 // ECMA 262 - 15.8.2.18 |
| 190 function MathTan(x) { | 190 function MathTan(x) { |
| 191 if (!IS_NUMBER(x)) x = ToNumber(x); | 191 if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 192 return %Math_tan(x); | 192 return %Math_tan(x); |
| 193 } | 193 } |
| 194 | 194 |
| 195 | 195 |
| 196 // ------------------------------------------------------------------- | 196 // ------------------------------------------------------------------- |
| 197 | 197 |
| 198 function SetupMath() { | 198 function SetupMath() { |
| 199 // Setup math constants. | 199 // Setup math constants. |
| 200 // ECMA-262, section 15.8.1.1. | 200 // ECMA-262, section 15.8.1.1. |
| 201 %OptimizeObjectForAddingMultipleProperties($Math, 8); | 201 %OptimizeObjectForAddingMultipleProperties($Math, 8); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 "tan", MathTan, | 255 "tan", MathTan, |
| 256 "atan2", MathAtan2, | 256 "atan2", MathAtan2, |
| 257 "pow", MathPow, | 257 "pow", MathPow, |
| 258 "max", MathMax, | 258 "max", MathMax, |
| 259 "min", MathMin | 259 "min", MathMin |
| 260 )); | 260 )); |
| 261 }; | 261 }; |
| 262 | 262 |
| 263 | 263 |
| 264 SetupMath(); | 264 SetupMath(); |
| OLD | NEW |