ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | |
| 28 | |
| 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 |
| 31 | // changes to these properties. |
| 32 | const $Infinity = global.Infinity; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 33 | const $floor = MathFloor; |
| 34 | const $random = MathRandom; |
| 35 | const $abs = MathAbs; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | // Instance class name can only be set on functions. That is the only |
| 38 | // purpose for MathConstructor. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 39 | function MathConstructor() {} |
| 40 | %FunctionSetInstanceClassName(MathConstructor, 'Math'); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 41 | const $Math = new MathConstructor(); |
| 42 | $Math.__proto__ = global.Object.prototype; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 43 | %SetProperty(global, "Math", $Math, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 45 | // ECMA 262 - 15.8.2.1 |
| 46 | function MathAbs(x) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 47 | if (%_IsSmi(x)) return x >= 0 ? x : -x; |
| 48 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 49 | return %Math_abs(x); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 50 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 51 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 52 | // ECMA 262 - 15.8.2.2 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 53 | function MathAcos(x) { |
| 54 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 55 | return %Math_acos(x); |
| 56 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 57 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 58 | // ECMA 262 - 15.8.2.3 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 59 | function MathAsin(x) { |
| 60 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 61 | return %Math_asin(x); |
| 62 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 63 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 64 | // ECMA 262 - 15.8.2.4 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 65 | function MathAtan(x) { |
| 66 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 67 | return %Math_atan(x); |
| 68 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 69 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 70 | // ECMA 262 - 15.8.2.5 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 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 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 76 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 77 | // ECMA 262 - 15.8.2.6 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 78 | function MathCeil(x) { |
| 79 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 80 | return %Math_ceil(x); |
| 81 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 82 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 83 | // ECMA 262 - 15.8.2.7 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 84 | function MathCos(x) { |
| 85 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 86 | return %_Math_cos(x); |
| 87 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 89 | // ECMA 262 - 15.8.2.8 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 90 | function MathExp(x) { |
| 91 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 92 | return %Math_exp(x); |
| 93 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 94 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 95 | // ECMA 262 - 15.8.2.9 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 96 | function MathFloor(x) { |
| 97 | if (!IS_NUMBER(x)) x = ToNumber(x); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame^] | 98 | // It's more common to call this with a positive number that's out |
| 99 | // of range than negative numbers; check the upper bound first. |
| 100 | if (x <= 0x7FFFFFFF && x > 0) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 101 | // Numbers in the range [0, 2^31) can be floored by converting |
| 102 | // them to an unsigned 32-bit value using the shift operator. |
| 103 | // We avoid doing so for -0, because the result of Math.floor(-0) |
| 104 | // has to be -0, which wouldn't be the case with the shift. |
| 105 | return x << 0; |
| 106 | } else { |
| 107 | return %Math_floor(x); |
| 108 | } |
| 109 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 111 | // ECMA 262 - 15.8.2.10 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 112 | function MathLog(x) { |
| 113 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 114 | return %Math_log(x); |
| 115 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 117 | // ECMA 262 - 15.8.2.11 |
| 118 | function MathMax(arg1, arg2) { // length == 2 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 119 | var r = -$Infinity; |
| 120 | for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { |
| 121 | var n = ToNumber(%_Arguments(i)); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 122 | if (NUMBER_IS_NAN(n)) return n; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 123 | // Make sure +0 is consider greater than -0. |
| 124 | if (n > r || (n === 0 && r === 0 && (1 / n) > (1 / r))) r = n; |
| 125 | } |
| 126 | return r; |
| 127 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 128 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 129 | // ECMA 262 - 15.8.2.12 |
| 130 | function MathMin(arg1, arg2) { // length == 2 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 131 | var r = $Infinity; |
| 132 | for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { |
| 133 | var n = ToNumber(%_Arguments(i)); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 134 | if (NUMBER_IS_NAN(n)) return n; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 135 | // Make sure -0 is consider less than +0. |
| 136 | if (n < r || (n === 0 && r === 0 && (1 / n) < (1 / r))) r = n; |
| 137 | } |
| 138 | return r; |
| 139 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 140 | |
| 141 | // ECMA 262 - 15.8.2.13 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 142 | function MathPow(x, y) { |
| 143 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 144 | if (!IS_NUMBER(y)) y = ToNumber(y); |
| 145 | return %Math_pow(x, y); |
| 146 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 147 | |
| 148 | // ECMA 262 - 15.8.2.14 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 149 | function MathRandom() { |
| 150 | return %_RandomPositiveSmi() / 0x40000000; |
| 151 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 152 | |
| 153 | // ECMA 262 - 15.8.2.15 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 154 | function MathRound(x) { |
| 155 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 156 | return %Math_round(x); |
| 157 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 158 | |
| 159 | // ECMA 262 - 15.8.2.16 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 160 | function MathSin(x) { |
| 161 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 162 | return %_Math_sin(x); |
| 163 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 164 | |
| 165 | // ECMA 262 - 15.8.2.17 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 166 | function MathSqrt(x) { |
| 167 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 168 | return %Math_sqrt(x); |
| 169 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 170 | |
| 171 | // ECMA 262 - 15.8.2.18 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 172 | function MathTan(x) { |
| 173 | if (!IS_NUMBER(x)) x = ToNumber(x); |
| 174 | return %Math_tan(x); |
| 175 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 176 | |
| 177 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 178 | // ------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 179 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 180 | function SetupMath() { |
| 181 | // Setup math constants. |
| 182 | // ECMA-262, section 15.8.1.1. |
| 183 | %SetProperty($Math, |
| 184 | "E", |
| 185 | 2.7182818284590452354, |
| 186 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 187 | // ECMA-262, section 15.8.1.2. |
| 188 | %SetProperty($Math, |
| 189 | "LN10", |
| 190 | 2.302585092994046, |
| 191 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 192 | // ECMA-262, section 15.8.1.3. |
| 193 | %SetProperty($Math, |
| 194 | "LN2", |
| 195 | 0.6931471805599453, |
| 196 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 197 | // ECMA-262, section 15.8.1.4. |
| 198 | %SetProperty($Math, |
| 199 | "LOG2E", |
| 200 | 1.4426950408889634, |
| 201 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 202 | %SetProperty($Math, |
| 203 | "LOG10E", |
| 204 | 0.43429448190325176, |
| 205 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 206 | %SetProperty($Math, |
| 207 | "PI", |
| 208 | 3.1415926535897932, |
| 209 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 210 | %SetProperty($Math, |
| 211 | "SQRT1_2", |
| 212 | 0.7071067811865476, |
| 213 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 214 | %SetProperty($Math, |
| 215 | "SQRT2", |
| 216 | 1.4142135623730951, |
| 217 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 218 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 219 | // Setup non-enumerable functions of the Math object and |
| 220 | // set their names. |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 221 | InstallFunctionsOnHiddenPrototype($Math, DONT_ENUM, $Array( |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 222 | "random", MathRandom, |
| 223 | "abs", MathAbs, |
| 224 | "acos", MathAcos, |
| 225 | "asin", MathAsin, |
| 226 | "atan", MathAtan, |
| 227 | "ceil", MathCeil, |
| 228 | "cos", MathCos, |
| 229 | "exp", MathExp, |
| 230 | "floor", MathFloor, |
| 231 | "log", MathLog, |
| 232 | "round", MathRound, |
| 233 | "sin", MathSin, |
| 234 | "sqrt", MathSqrt, |
| 235 | "tan", MathTan, |
| 236 | "atan2", MathAtan2, |
| 237 | "pow", MathPow, |
| 238 | "max", MathMax, |
| 239 | "min", MathMin |
| 240 | )); |
| 241 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 242 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 243 | |
| 244 | SetupMath(); |