blob: db75cb28ae8a2cebf4c31eecda8baec23b913af6 [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) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +000047 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.com769cc962008-08-06 10:02:49 +000050}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000051
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000052// ECMA 262 - 15.8.2.2
ager@chromium.orgeadaf222009-06-16 09:43:10 +000053function MathAcos(x) {
54 if (!IS_NUMBER(x)) x = ToNumber(x);
55 return %Math_acos(x);
56}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000057
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000058// ECMA 262 - 15.8.2.3
ager@chromium.orgeadaf222009-06-16 09:43:10 +000059function MathAsin(x) {
60 if (!IS_NUMBER(x)) x = ToNumber(x);
61 return %Math_asin(x);
62}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000063
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000064// ECMA 262 - 15.8.2.4
ager@chromium.orgeadaf222009-06-16 09:43:10 +000065function MathAtan(x) {
66 if (!IS_NUMBER(x)) x = ToNumber(x);
67 return %Math_atan(x);
68}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000069
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000070// ECMA 262 - 15.8.2.5
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000071// The naming of y and x matches the spec, as does the order in which
72// ToNumber (valueOf) is called.
73function MathAtan2(y, x) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +000074 if (!IS_NUMBER(y)) y = ToNumber(y);
kasperl@chromium.org68ac0092009-07-09 06:00:35 +000075 if (!IS_NUMBER(x)) x = ToNumber(x);
76 return %Math_atan2(y, x);
ager@chromium.orgeadaf222009-06-16 09:43:10 +000077}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000078
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000079// ECMA 262 - 15.8.2.6
ager@chromium.orgeadaf222009-06-16 09:43:10 +000080function MathCeil(x) {
81 if (!IS_NUMBER(x)) x = ToNumber(x);
82 return %Math_ceil(x);
83}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000084
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000085// ECMA 262 - 15.8.2.7
ager@chromium.orgeadaf222009-06-16 09:43:10 +000086function MathCos(x) {
87 if (!IS_NUMBER(x)) x = ToNumber(x);
88 return %_Math_cos(x);
89}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000090
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000091// ECMA 262 - 15.8.2.8
ager@chromium.orgeadaf222009-06-16 09:43:10 +000092function MathExp(x) {
93 if (!IS_NUMBER(x)) x = ToNumber(x);
94 return %Math_exp(x);
95}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000096
kasperl@chromium.org41044eb2008-10-06 08:24:46 +000097// ECMA 262 - 15.8.2.9
ager@chromium.orgeadaf222009-06-16 09:43:10 +000098function MathFloor(x) {
99 if (!IS_NUMBER(x)) x = ToNumber(x);
kasperl@chromium.org86f77b72009-07-06 08:21:57 +0000100 // It's more common to call this with a positive number that's out
101 // of range than negative numbers; check the upper bound first.
102 if (x <= 0x7FFFFFFF && x > 0) {
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000103 // Numbers in the range [0, 2^31) can be floored by converting
104 // them to an unsigned 32-bit value using the shift operator.
105 // We avoid doing so for -0, because the result of Math.floor(-0)
106 // has to be -0, which wouldn't be the case with the shift.
107 return x << 0;
108 } else {
109 return %Math_floor(x);
110 }
111}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000112
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000113// ECMA 262 - 15.8.2.10
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000114function MathLog(x) {
115 if (!IS_NUMBER(x)) x = ToNumber(x);
116 return %Math_log(x);
117}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000118
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000119// ECMA 262 - 15.8.2.11
120function MathMax(arg1, arg2) { // length == 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000121 var r = -$Infinity;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000122 var length = %_ArgumentsLength();
123 for (var i = 0; i < length; i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000124 var n = ToNumber(%_Arguments(i));
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000125 if (NUMBER_IS_NAN(n)) return n;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000126 // Make sure +0 is considered greater than -0.
127 if (n > r || (r === 0 && n === 0 && !%_IsSmi(r))) r = n;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000128 }
129 return r;
130}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000131
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000132// ECMA 262 - 15.8.2.12
133function MathMin(arg1, arg2) { // length == 2
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000134 var r = $Infinity;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000135 var length = %_ArgumentsLength();
136 for (var i = 0; i < length; i++) {
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000137 var n = ToNumber(%_Arguments(i));
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000138 if (NUMBER_IS_NAN(n)) return n;
kasperl@chromium.org68ac0092009-07-09 06:00:35 +0000139 // Make sure -0 is considered less than +0.
140 if (n < r || (r === 0 && n === 0 && !%_IsSmi(n))) r = n;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000141 }
142 return r;
143}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000144
145// ECMA 262 - 15.8.2.13
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000146function MathPow(x, y) {
147 if (!IS_NUMBER(x)) x = ToNumber(x);
148 if (!IS_NUMBER(y)) y = ToNumber(y);
149 return %Math_pow(x, y);
150}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000151
152// ECMA 262 - 15.8.2.14
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000153function MathRandom() {
154 return %_RandomPositiveSmi() / 0x40000000;
155}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000156
157// ECMA 262 - 15.8.2.15
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000158function MathRound(x) {
159 if (!IS_NUMBER(x)) x = ToNumber(x);
160 return %Math_round(x);
161}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000162
163// ECMA 262 - 15.8.2.16
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000164function MathSin(x) {
165 if (!IS_NUMBER(x)) x = ToNumber(x);
166 return %_Math_sin(x);
167}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000168
169// ECMA 262 - 15.8.2.17
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000170function MathSqrt(x) {
171 if (!IS_NUMBER(x)) x = ToNumber(x);
172 return %Math_sqrt(x);
173}
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000174
175// ECMA 262 - 15.8.2.18
ager@chromium.orgeadaf222009-06-16 09:43:10 +0000176function MathTan(x) {
177 if (!IS_NUMBER(x)) x = ToNumber(x);
178 return %Math_tan(x);
179}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000180
181
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000182// -------------------------------------------------------------------
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000183
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000184function SetupMath() {
185 // Setup math constants.
186 // ECMA-262, section 15.8.1.1.
187 %SetProperty($Math,
188 "E",
189 2.7182818284590452354,
190 DONT_ENUM | DONT_DELETE | READ_ONLY);
191 // ECMA-262, section 15.8.1.2.
192 %SetProperty($Math,
193 "LN10",
194 2.302585092994046,
195 DONT_ENUM | DONT_DELETE | READ_ONLY);
196 // ECMA-262, section 15.8.1.3.
197 %SetProperty($Math,
198 "LN2",
199 0.6931471805599453,
200 DONT_ENUM | DONT_DELETE | READ_ONLY);
201 // ECMA-262, section 15.8.1.4.
202 %SetProperty($Math,
203 "LOG2E",
204 1.4426950408889634,
205 DONT_ENUM | DONT_DELETE | READ_ONLY);
206 %SetProperty($Math,
207 "LOG10E",
208 0.43429448190325176,
209 DONT_ENUM | DONT_DELETE | READ_ONLY);
210 %SetProperty($Math,
211 "PI",
212 3.1415926535897932,
213 DONT_ENUM | DONT_DELETE | READ_ONLY);
214 %SetProperty($Math,
215 "SQRT1_2",
216 0.7071067811865476,
217 DONT_ENUM | DONT_DELETE | READ_ONLY);
218 %SetProperty($Math,
219 "SQRT2",
220 1.4142135623730951,
221 DONT_ENUM | DONT_DELETE | READ_ONLY);
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000222
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000223 // Setup non-enumerable functions of the Math object and
224 // set their names.
ager@chromium.org9085a012009-05-11 19:22:57 +0000225 InstallFunctionsOnHiddenPrototype($Math, DONT_ENUM, $Array(
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000226 "random", MathRandom,
227 "abs", MathAbs,
228 "acos", MathAcos,
229 "asin", MathAsin,
230 "atan", MathAtan,
231 "ceil", MathCeil,
232 "cos", MathCos,
233 "exp", MathExp,
234 "floor", MathFloor,
235 "log", MathLog,
236 "round", MathRound,
237 "sin", MathSin,
238 "sqrt", MathSqrt,
239 "tan", MathTan,
240 "atan2", MathAtan2,
241 "pow", MathPow,
242 "max", MathMax,
243 "min", MathMin
244 ));
245};
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000246
kasperl@chromium.org41044eb2008-10-06 08:24:46 +0000247
248SetupMath();