christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame^] | 1 | // Copyright 2006-2007 Google Inc. All Rights Reserved. |
| 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; |
| 33 | |
| 34 | |
| 35 | // Instance class name can only be set on functions. That is the only |
| 36 | // purpose for MathConstructor. |
| 37 | function MathConstructor() {}; |
| 38 | %FunctionSetInstanceClassName(MathConstructor, 'Math'); |
| 39 | const $Math = new MathConstructor(); |
| 40 | $Math.__proto__ = global.Object.prototype; |
| 41 | %AddProperty(global, "Math", $Math, DONT_ENUM); |
| 42 | |
| 43 | |
| 44 | // TODO(1240985): Make this work without arguments to the runtime |
| 45 | // call. |
| 46 | function $Math_random() { return %Math_random(0); } |
| 47 | %AddProperty($Math, "random", $Math_random, DONT_ENUM); |
| 48 | |
| 49 | function $Math_abs(x) { return %Math_abs(ToNumber(x)); } |
| 50 | %AddProperty($Math, "abs", $Math_abs, DONT_ENUM); |
| 51 | |
| 52 | function $Math_acos(x) { return %Math_acos(ToNumber(x)); } |
| 53 | %AddProperty($Math, "acos", $Math_acos, DONT_ENUM); |
| 54 | |
| 55 | function $Math_asin(x) { return %Math_asin(ToNumber(x)); } |
| 56 | %AddProperty($Math, "asin", $Math_asin, DONT_ENUM); |
| 57 | |
| 58 | function $Math_atan(x) { return %Math_atan(ToNumber(x)); } |
| 59 | %AddProperty($Math, "atan", $Math_atan, DONT_ENUM); |
| 60 | |
| 61 | function $Math_ceil(x) { return %Math_ceil(ToNumber(x)); } |
| 62 | %AddProperty($Math, "ceil", $Math_ceil, DONT_ENUM); |
| 63 | |
| 64 | function $Math_cos(x) { return %Math_cos(ToNumber(x)); } |
| 65 | %AddProperty($Math, "cos", $Math_cos, DONT_ENUM); |
| 66 | |
| 67 | function $Math_exp(x) { return %Math_exp(ToNumber(x)); } |
| 68 | %AddProperty($Math, "exp", $Math_exp, DONT_ENUM); |
| 69 | |
| 70 | function $Math_floor(x) { return %Math_floor(ToNumber(x)); } |
| 71 | %AddProperty($Math, "floor", $Math_floor, DONT_ENUM); |
| 72 | |
| 73 | function $Math_log(x) { return %Math_log(ToNumber(x)); } |
| 74 | %AddProperty($Math, "log", $Math_log, DONT_ENUM); |
| 75 | |
| 76 | function $Math_round(x) { return %Math_round(ToNumber(x)); } |
| 77 | %AddProperty($Math, "round", $Math_round, DONT_ENUM); |
| 78 | |
| 79 | function $Math_sin(x) { return %Math_sin(ToNumber(x)); } |
| 80 | %AddProperty($Math, "sin", $Math_sin, DONT_ENUM); |
| 81 | |
| 82 | function $Math_sqrt(x) { return %Math_sqrt(ToNumber(x)); } |
| 83 | %AddProperty($Math, "sqrt", $Math_sqrt, DONT_ENUM); |
| 84 | |
| 85 | function $Math_tan(x) { return %Math_tan(ToNumber(x)); } |
| 86 | %AddProperty($Math, "tan", $Math_tan, DONT_ENUM); |
| 87 | |
| 88 | function $Math_atan2(x, y) { return %Math_atan2(ToNumber(x), ToNumber(y)); } |
| 89 | %AddProperty($Math, "atan2", $Math_atan2, DONT_ENUM); |
| 90 | |
| 91 | function $Math_pow(x, y) { return %Math_pow(ToNumber(x), ToNumber(y)); } |
| 92 | %AddProperty($Math, "pow", $Math_pow, DONT_ENUM); |
| 93 | |
| 94 | function $Math_max(arg1, arg2) { // length == 2 |
| 95 | var r = -$Infinity; |
| 96 | for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { |
| 97 | var n = ToNumber(%_Arguments(i)); |
| 98 | if (%NumberIsNaN(n)) return n; |
| 99 | // Make sure +0 is consider greater than -0. |
| 100 | if (n > r || (n === 0 && r === 0 && (1 / n) > (1 / r))) r = n; |
| 101 | } |
| 102 | return r; |
| 103 | } |
| 104 | %AddProperty($Math, "max", $Math_max, DONT_ENUM); |
| 105 | |
| 106 | function $Math_min(arg1, arg2) { // length == 2 |
| 107 | var r = $Infinity; |
| 108 | for (var i = %_ArgumentsLength() - 1; i >= 0; --i) { |
| 109 | var n = ToNumber(%_Arguments(i)); |
| 110 | if (%NumberIsNaN(n)) return n; |
| 111 | // Make sure -0 is consider less than +0. |
| 112 | if (n < r || (n === 0 && r === 0 && (1 / n) < (1 / r))) r = n; |
| 113 | } |
| 114 | return r; |
| 115 | } |
| 116 | %AddProperty($Math, "min", $Math_min, DONT_ENUM); |
| 117 | |
| 118 | |
| 119 | // ECMA-262, section 15.8.1.1. |
| 120 | %AddProperty($Math, "E", 2.7182818284590452354, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 121 | |
| 122 | // ECMA-262, section 15.8.1.2. |
| 123 | %AddProperty($Math, "LN10", 2.302585092994046, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 124 | |
| 125 | // ECMA-262, section 15.8.1.3. |
| 126 | %AddProperty($Math, "LN2", 0.6931471805599453, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 127 | |
| 128 | // ECMA-262, section 15.8.1.4. |
| 129 | %AddProperty($Math, "LOG2E", 1.4426950408889634, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 130 | %AddProperty($Math, "LOG10E", 0.43429448190325176, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 131 | %AddProperty($Math, "PI", 3.1415926535897932, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 132 | %AddProperty($Math, "SQRT1_2", 0.7071067811865476, DONT_ENUM | DONT_DELETE | READ_ONLY); |
| 133 | %AddProperty($Math, "SQRT2", 1.4142135623730951, DONT_ENUM | DONT_DELETE | READ_ONLY); |