rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1 | // Copyright 2012 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. |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 32 | var $floor = MathFloor; |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 33 | var $abs = MathAbs; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 34 | |
| 35 | // Instance class name can only be set on functions. That is the only |
| 36 | // purpose for MathConstructor. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 37 | function MathConstructor() {} |
| 38 | %FunctionSetInstanceClassName(MathConstructor, 'Math'); |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 39 | var $Math = new MathConstructor(); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 40 | $Math.__proto__ = $Object.prototype; |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 41 | %SetProperty(global, "Math", $Math, DONT_ENUM); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 43 | // ECMA 262 - 15.8.2.1 |
| 44 | function MathAbs(x) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 45 | if (%_IsSmi(x)) return x >= 0 ? x : -x; |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 46 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 47 | if (x === 0) return 0; // To handle -0. |
| 48 | return x > 0 ? x : -x; |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 49 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 51 | // ECMA 262 - 15.8.2.2 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 52 | function MathAcos(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 53 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 54 | return %Math_acos(x); |
| 55 | } |
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 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 58 | function MathAsin(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 59 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 60 | return %Math_asin(x); |
| 61 | } |
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.4 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 64 | function MathAtan(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 65 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 66 | return %Math_atan(x); |
| 67 | } |
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.5 |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 70 | // The naming of y and x matches the spec, as does the order in which |
| 71 | // ToNumber (valueOf) is called. |
| 72 | function MathAtan2(y, x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 73 | if (!IS_NUMBER(y)) y = NonNumberToNumber(y); |
| 74 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 75 | return %Math_atan2(y, x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 76 | } |
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.6 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 79 | function MathCeil(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 80 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 81 | return %Math_ceil(x); |
| 82 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 83 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 84 | // ECMA 262 - 15.8.2.7 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 85 | function MathCos(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 86 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 87 | return %_MathCos(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 88 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 89 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 90 | // ECMA 262 - 15.8.2.8 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 91 | function MathExp(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 92 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 93 | return %Math_exp(x); |
| 94 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 95 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 96 | // ECMA 262 - 15.8.2.9 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 97 | function MathFloor(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 98 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
kasperl@chromium.org | 86f77b7 | 2009-07-06 08:21:57 +0000 | [diff] [blame] | 99 | // It's more common to call this with a positive number that's out |
| 100 | // of range than negative numbers; check the upper bound first. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 101 | if (x < 0x80000000 && x > 0) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 102 | // Numbers in the range [0, 2^31) can be floored by converting |
| 103 | // them to an unsigned 32-bit value using the shift operator. |
| 104 | // We avoid doing so for -0, because the result of Math.floor(-0) |
| 105 | // has to be -0, which wouldn't be the case with the shift. |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 106 | return TO_UINT32(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 107 | } else { |
| 108 | return %Math_floor(x); |
| 109 | } |
| 110 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 111 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 112 | // ECMA 262 - 15.8.2.10 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 113 | function MathLog(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 114 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 115 | return %_MathLog(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 116 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 117 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 118 | // ECMA 262 - 15.8.2.11 |
| 119 | function MathMax(arg1, arg2) { // length == 2 |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 120 | var length = %_ArgumentsLength(); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 121 | if (length == 2) { |
| 122 | if (!IS_NUMBER(arg1)) arg1 = NonNumberToNumber(arg1); |
| 123 | if (!IS_NUMBER(arg2)) arg2 = NonNumberToNumber(arg2); |
| 124 | if (arg2 > arg1) return arg2; |
| 125 | if (arg1 > arg2) return arg1; |
| 126 | if (arg1 == arg2) { |
| 127 | // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be |
| 128 | // a Smi or a heap number. |
| 129 | return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg2 : arg1; |
| 130 | } |
| 131 | // All comparisons failed, one of the arguments must be NaN. |
| 132 | return 0/0; // Compiler constant-folds this to NaN. |
| 133 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 134 | var r = -1/0; // Compiler constant-folds this to -Infinity. |
| 135 | for (var i = 0; i < length; i++) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 136 | var n = %_Arguments(i); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 137 | if (!IS_NUMBER(n)) n = NonNumberToNumber(n); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 138 | // Make sure +0 is considered greater than -0. -0 is never a Smi, +0 can be |
| 139 | // a Smi or heap number. |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 140 | if (NUMBER_IS_NAN(n) || n > r || |
| 141 | (r == 0 && n == 0 && !%_IsSmi(r) && 1 / r < 0)) { |
| 142 | r = n; |
| 143 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | } |
| 145 | return r; |
| 146 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 147 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 148 | // ECMA 262 - 15.8.2.12 |
| 149 | function MathMin(arg1, arg2) { // length == 2 |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 150 | var length = %_ArgumentsLength(); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 151 | if (length == 2) { |
| 152 | if (!IS_NUMBER(arg1)) arg1 = NonNumberToNumber(arg1); |
| 153 | if (!IS_NUMBER(arg2)) arg2 = NonNumberToNumber(arg2); |
| 154 | if (arg2 > arg1) return arg1; |
| 155 | if (arg1 > arg2) return arg2; |
| 156 | if (arg1 == arg2) { |
| 157 | // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be |
| 158 | // a Smi or a heap number. |
| 159 | return (arg1 == 0 && !%_IsSmi(arg1) && 1 / arg1 < 0) ? arg1 : arg2; |
| 160 | } |
| 161 | // All comparisons failed, one of the arguments must be NaN. |
| 162 | return 0/0; // Compiler constant-folds this to NaN. |
| 163 | } |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 164 | var r = 1/0; // Compiler constant-folds this to Infinity. |
| 165 | for (var i = 0; i < length; i++) { |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 166 | var n = %_Arguments(i); |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 167 | if (!IS_NUMBER(n)) n = NonNumberToNumber(n); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 168 | // Make sure -0 is considered less than +0. -0 is never a Smi, +0 can be a |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 169 | // Smi or a heap number. |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 170 | if (NUMBER_IS_NAN(n) || n < r || |
| 171 | (r == 0 && n == 0 && !%_IsSmi(n) && 1 / n < 0)) { |
| 172 | r = n; |
| 173 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | } |
| 175 | return r; |
| 176 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 177 | |
| 178 | // ECMA 262 - 15.8.2.13 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 179 | function MathPow(x, y) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 180 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
| 181 | if (!IS_NUMBER(y)) y = NonNumberToNumber(y); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 182 | return %_MathPow(x, y); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 183 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 184 | |
| 185 | // ECMA 262 - 15.8.2.14 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 186 | function MathRandom() { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 187 | return %_RandomHeapNumber(); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 188 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 189 | |
| 190 | // ECMA 262 - 15.8.2.15 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 191 | function MathRound(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 192 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 193 | return %RoundNumber(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 194 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 195 | |
| 196 | // ECMA 262 - 15.8.2.16 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 197 | function MathSin(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 198 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 199 | return %_MathSin(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 200 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 201 | |
| 202 | // ECMA 262 - 15.8.2.17 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 203 | function MathSqrt(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 204 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 205 | return %_MathSqrt(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 206 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 207 | |
| 208 | // ECMA 262 - 15.8.2.18 |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 209 | function MathTan(x) { |
kmillikin@chromium.org | d2c22f0 | 2011-01-10 08:15:37 +0000 | [diff] [blame] | 210 | if (!IS_NUMBER(x)) x = NonNumberToNumber(x); |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 211 | return %_MathTan(x); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 212 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 213 | |
| 214 | |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 215 | // ------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 216 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 217 | function SetUpMath() { |
| 218 | %CheckIsBootstrapping(); |
| 219 | // Set up math constants. |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 220 | // ECMA-262, section 15.8.1.1. |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 221 | %OptimizeObjectForAddingMultipleProperties($Math, 8); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 222 | %SetProperty($Math, |
| 223 | "E", |
| 224 | 2.7182818284590452354, |
| 225 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 226 | // ECMA-262, section 15.8.1.2. |
| 227 | %SetProperty($Math, |
| 228 | "LN10", |
| 229 | 2.302585092994046, |
| 230 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 231 | // ECMA-262, section 15.8.1.3. |
| 232 | %SetProperty($Math, |
| 233 | "LN2", |
| 234 | 0.6931471805599453, |
| 235 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 236 | // ECMA-262, section 15.8.1.4. |
| 237 | %SetProperty($Math, |
| 238 | "LOG2E", |
| 239 | 1.4426950408889634, |
| 240 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 241 | %SetProperty($Math, |
| 242 | "LOG10E", |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 243 | 0.4342944819032518, |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 244 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 245 | %SetProperty($Math, |
| 246 | "PI", |
| 247 | 3.1415926535897932, |
| 248 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 249 | %SetProperty($Math, |
| 250 | "SQRT1_2", |
| 251 | 0.7071067811865476, |
| 252 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 253 | %SetProperty($Math, |
| 254 | "SQRT2", |
| 255 | 1.4142135623730951, |
| 256 | DONT_ENUM | DONT_DELETE | READ_ONLY); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 257 | %ToFastProperties($Math); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 258 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 259 | // Set up non-enumerable functions of the Math object and |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 260 | // set their names. |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 261 | InstallFunctions($Math, DONT_ENUM, $Array( |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 262 | "random", MathRandom, |
| 263 | "abs", MathAbs, |
| 264 | "acos", MathAcos, |
| 265 | "asin", MathAsin, |
| 266 | "atan", MathAtan, |
| 267 | "ceil", MathCeil, |
| 268 | "cos", MathCos, |
| 269 | "exp", MathExp, |
| 270 | "floor", MathFloor, |
| 271 | "log", MathLog, |
| 272 | "round", MathRound, |
| 273 | "sin", MathSin, |
| 274 | "sqrt", MathSqrt, |
| 275 | "tan", MathTan, |
| 276 | "atan2", MathAtan2, |
| 277 | "pow", MathPow, |
| 278 | "max", MathMax, |
| 279 | "min", MathMin |
| 280 | )); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 281 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 282 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 283 | SetUpMath(); |