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) { |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 47 | if (%_IsSmi(x)) { |
| 48 | return x >= 0 ? x : -x; |
| 49 | } else { |
| 50 | return %Math_abs(ToNumber(x)); |
| 51 | } |
| 52 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 53 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 54 | // ECMA 262 - 15.8.2.2 |
| 55 | function MathAcos(x) { return %Math_acos(ToNumber(x)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 57 | // ECMA 262 - 15.8.2.3 |
| 58 | function MathAsin(x) { return %Math_asin(ToNumber(x)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 59 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 60 | // ECMA 262 - 15.8.2.4 |
| 61 | function MathAtan(x) { return %Math_atan(ToNumber(x)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 62 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 63 | // ECMA 262 - 15.8.2.5 |
| 64 | function MathAtan2(x, y) { return %Math_atan2(ToNumber(x), ToNumber(y)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 65 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 66 | // ECMA 262 - 15.8.2.6 |
| 67 | function MathCeil(x) { return %Math_ceil(ToNumber(x)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 68 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 69 | // ECMA 262 - 15.8.2.7 |
| 70 | function MathCos(x) { return %Math_cos(ToNumber(x)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 71 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 72 | // ECMA 262 - 15.8.2.8 |
| 73 | function MathExp(x) { return %Math_exp(ToNumber(x)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 74 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 75 | // ECMA 262 - 15.8.2.9 |
| 76 | function MathFloor(x) { return %Math_floor(ToNumber(x)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 77 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 78 | // ECMA 262 - 15.8.2.10 |
| 79 | function MathLog(x) { return %Math_log(ToNumber(x)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 80 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 81 | // ECMA 262 - 15.8.2.11 |
| 82 | function MathMax(arg1, arg2) { // length == 2 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | var r = -$Infinity; |
| 84 | for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { |
| 85 | var n = ToNumber(%_Arguments(i)); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 86 | if (NUMBER_IS_NAN(n)) return n; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 87 | // Make sure +0 is consider greater than -0. |
| 88 | if (n > r || (n === 0 && r === 0 && (1 / n) > (1 / r))) r = n; |
| 89 | } |
| 90 | return r; |
| 91 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 92 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 93 | // ECMA 262 - 15.8.2.12 |
| 94 | function MathMin(arg1, arg2) { // length == 2 |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | var r = $Infinity; |
| 96 | for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { |
| 97 | var n = ToNumber(%_Arguments(i)); |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 98 | if (NUMBER_IS_NAN(n)) return n; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 99 | // Make sure -0 is consider less than +0. |
| 100 | if (n < r || (n === 0 && r === 0 && (1 / n) < (1 / r))) r = n; |
| 101 | } |
| 102 | return r; |
| 103 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 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)); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 122 | |
| 123 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 124 | // ------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 126 | function SetupMath() { |
| 127 | // Setup math constants. |
| 128 | // ECMA-262, section 15.8.1.1. |
| 129 | %SetProperty($Math, |
| 130 | "E", |
| 131 | 2.7182818284590452354, |
| 132 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 133 | // ECMA-262, section 15.8.1.2. |
| 134 | %SetProperty($Math, |
| 135 | "LN10", |
| 136 | 2.302585092994046, |
| 137 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 138 | // ECMA-262, section 15.8.1.3. |
| 139 | %SetProperty($Math, |
| 140 | "LN2", |
| 141 | 0.6931471805599453, |
| 142 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 143 | // ECMA-262, section 15.8.1.4. |
| 144 | %SetProperty($Math, |
| 145 | "LOG2E", |
| 146 | 1.4426950408889634, |
| 147 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 148 | %SetProperty($Math, |
| 149 | "LOG10E", |
| 150 | 0.43429448190325176, |
| 151 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 152 | %SetProperty($Math, |
| 153 | "PI", |
| 154 | 3.1415926535897932, |
| 155 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 156 | %SetProperty($Math, |
| 157 | "SQRT1_2", |
| 158 | 0.7071067811865476, |
| 159 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 160 | %SetProperty($Math, |
| 161 | "SQRT2", |
| 162 | 1.4142135623730951, |
| 163 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 165 | // Setup non-enumerable functions of the Math object and |
| 166 | // set their names. |
| 167 | InstallFunctions($Math, DONT_ENUM, $Array( |
| 168 | "random", MathRandom, |
| 169 | "abs", MathAbs, |
| 170 | "acos", MathAcos, |
| 171 | "asin", MathAsin, |
| 172 | "atan", MathAtan, |
| 173 | "ceil", MathCeil, |
| 174 | "cos", MathCos, |
| 175 | "exp", MathExp, |
| 176 | "floor", MathFloor, |
| 177 | "log", MathLog, |
| 178 | "round", MathRound, |
| 179 | "sin", MathSin, |
| 180 | "sqrt", MathSqrt, |
| 181 | "tan", MathTan, |
| 182 | "atan2", MathAtan2, |
| 183 | "pow", MathPow, |
| 184 | "max", MathMax, |
| 185 | "min", MathMin |
| 186 | )); |
| 187 | }; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame^] | 189 | |
| 190 | SetupMath(); |