blob: 5b7c396cde6e5b15344f0b229e8c5fbe152fd4c7 [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00002// 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.
32const $Infinity = global.Infinity;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000033const $floor = MathFloor;
34const $random = MathRandom;
35const $abs = MathAbs;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000036
37// Instance class name can only be set on functions. That is the only
38// purpose for MathConstructor.
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000039function MathConstructor() {}
40%FunctionSetInstanceClassName(MathConstructor, 'Math');
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000041const $Math = new MathConstructor();
42$Math.__proto__ = global.Object.prototype;
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000043%SetProperty(global, "Math", $Math, DONT_ENUM);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000044
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000045// ECMA 262 - 15.8.2.1
46function MathAbs(x) {
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000047 if (%_IsSmi(x)) {
48 return x >= 0 ? x : -x;
49 } else {
50 return %Math_abs(ToNumber(x));
51 }
52}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000053
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000054// ECMA 262 - 15.8.2.2
55function MathAcos(x) { return %Math_acos(ToNumber(x)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000057// ECMA 262 - 15.8.2.3
58function MathAsin(x) { return %Math_asin(ToNumber(x)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000059
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000060// ECMA 262 - 15.8.2.4
61function MathAtan(x) { return %Math_atan(ToNumber(x)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000062
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000063// ECMA 262 - 15.8.2.5
64function MathAtan2(x, y) { return %Math_atan2(ToNumber(x), ToNumber(y)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000065
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000066// ECMA 262 - 15.8.2.6
67function MathCeil(x) { return %Math_ceil(ToNumber(x)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000068
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000069// ECMA 262 - 15.8.2.7
70function MathCos(x) { return %Math_cos(ToNumber(x)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000071
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000072// ECMA 262 - 15.8.2.8
73function MathExp(x) { return %Math_exp(ToNumber(x)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000074
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000075// ECMA 262 - 15.8.2.9
76function MathFloor(x) { return %Math_floor(ToNumber(x)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000077
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000078// ECMA 262 - 15.8.2.10
79function MathLog(x) { return %Math_log(ToNumber(x)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000080
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000081// ECMA 262 - 15.8.2.11
82function MathMax(arg1, arg2) { // length == 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000083 var r = -$Infinity;
84 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) {
85 var n = ToNumber(%_Arguments(i));
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000086 if (NUMBER_IS_NAN(n)) return n;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000087 // 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.hansen43d26ec2008-07-03 15:10:15 +000092
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000093// ECMA 262 - 15.8.2.12
94function MathMin(arg1, arg2) { // length == 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000095 var r = $Infinity;
96 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) {
97 var n = ToNumber(%_Arguments(i));
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000098 if (NUMBER_IS_NAN(n)) return n;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000099 // 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.org41044eb2008-10-06 08:24:46 +0000104
105// ECMA 262 - 15.8.2.13
106function MathPow(x, y) { return %Math_pow(ToNumber(x), ToNumber(y)); }
107
108// ECMA 262 - 15.8.2.14
109function MathRandom() { return %Math_random(); }
110
111// ECMA 262 - 15.8.2.15
112function MathRound(x) { return %Math_round(ToNumber(x)); }
113
114// ECMA 262 - 15.8.2.16
115function MathSin(x) { return %Math_sin(ToNumber(x)); }
116
117// ECMA 262 - 15.8.2.17
118function MathSqrt(x) { return %Math_sqrt(ToNumber(x)); }
119
120// ECMA 262 - 15.8.2.18
121function MathTan(x) { return %Math_tan(ToNumber(x)); }
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000122
123
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000124// -------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000125
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000126function 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.hansen43d26ec2008-07-03 15:10:15 +0000164
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000165 // 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.hansen43d26ec2008-07-03 15:10:15 +0000188
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000189
190SetupMath();