blob: eb4a4cc1acf953d4329a643d7b53546840df08a2 [file] [log] [blame]
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +00001// 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.
32const $Infinity = global.Infinity;
33
34
35// Instance class name can only be set on functions. That is the only
36// purpose for MathConstructor.
37function MathConstructor() {};
38%FunctionSetInstanceClassName(MathConstructor, 'Math');
39const $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.
46function $Math_random() { return %Math_random(0); }
47%AddProperty($Math, "random", $Math_random, DONT_ENUM);
48
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +000049function $Math_abs(x) {
50 if (%_IsSmi(x)) {
51 return x >= 0 ? x : -x;
52 } else {
53 return %Math_abs(ToNumber(x));
54 }
55}
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +000056%AddProperty($Math, "abs", $Math_abs, DONT_ENUM);
57
58function $Math_acos(x) { return %Math_acos(ToNumber(x)); }
59%AddProperty($Math, "acos", $Math_acos, DONT_ENUM);
60
61function $Math_asin(x) { return %Math_asin(ToNumber(x)); }
62%AddProperty($Math, "asin", $Math_asin, DONT_ENUM);
63
64function $Math_atan(x) { return %Math_atan(ToNumber(x)); }
65%AddProperty($Math, "atan", $Math_atan, DONT_ENUM);
66
67function $Math_ceil(x) { return %Math_ceil(ToNumber(x)); }
68%AddProperty($Math, "ceil", $Math_ceil, DONT_ENUM);
69
70function $Math_cos(x) { return %Math_cos(ToNumber(x)); }
71%AddProperty($Math, "cos", $Math_cos, DONT_ENUM);
72
73function $Math_exp(x) { return %Math_exp(ToNumber(x)); }
74%AddProperty($Math, "exp", $Math_exp, DONT_ENUM);
75
76function $Math_floor(x) { return %Math_floor(ToNumber(x)); }
77%AddProperty($Math, "floor", $Math_floor, DONT_ENUM);
78
79function $Math_log(x) { return %Math_log(ToNumber(x)); }
80%AddProperty($Math, "log", $Math_log, DONT_ENUM);
81
82function $Math_round(x) { return %Math_round(ToNumber(x)); }
83%AddProperty($Math, "round", $Math_round, DONT_ENUM);
84
85function $Math_sin(x) { return %Math_sin(ToNumber(x)); }
86%AddProperty($Math, "sin", $Math_sin, DONT_ENUM);
87
88function $Math_sqrt(x) { return %Math_sqrt(ToNumber(x)); }
89%AddProperty($Math, "sqrt", $Math_sqrt, DONT_ENUM);
90
91function $Math_tan(x) { return %Math_tan(ToNumber(x)); }
92%AddProperty($Math, "tan", $Math_tan, DONT_ENUM);
93
94function $Math_atan2(x, y) { return %Math_atan2(ToNumber(x), ToNumber(y)); }
95%AddProperty($Math, "atan2", $Math_atan2, DONT_ENUM);
96
97function $Math_pow(x, y) { return %Math_pow(ToNumber(x), ToNumber(y)); }
98%AddProperty($Math, "pow", $Math_pow, DONT_ENUM);
99
100function $Math_max(arg1, arg2) { // length == 2
101 var r = -$Infinity;
102 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) {
103 var n = ToNumber(%_Arguments(i));
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000104 if (NUMBER_IS_NAN(n)) return n;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000105 // Make sure +0 is consider greater than -0.
106 if (n > r || (n === 0 && r === 0 && (1 / n) > (1 / r))) r = n;
107 }
108 return r;
109}
110%AddProperty($Math, "max", $Math_max, DONT_ENUM);
111
112function $Math_min(arg1, arg2) { // length == 2
113 var r = $Infinity;
114 for (var i = %_ArgumentsLength() - 1; i >= 0; --i) {
115 var n = ToNumber(%_Arguments(i));
mads.s.ager@gmail.com769cc962008-08-06 10:02:49 +0000116 if (NUMBER_IS_NAN(n)) return n;
christian.plesner.hansen43d26ec2008-07-03 15:10:15 +0000117 // Make sure -0 is consider less than +0.
118 if (n < r || (n === 0 && r === 0 && (1 / n) < (1 / r))) r = n;
119 }
120 return r;
121}
122%AddProperty($Math, "min", $Math_min, DONT_ENUM);
123
124
125// ECMA-262, section 15.8.1.1.
126%AddProperty($Math, "E", 2.7182818284590452354, DONT_ENUM | DONT_DELETE | READ_ONLY);
127
128// ECMA-262, section 15.8.1.2.
129%AddProperty($Math, "LN10", 2.302585092994046, DONT_ENUM | DONT_DELETE | READ_ONLY);
130
131// ECMA-262, section 15.8.1.3.
132%AddProperty($Math, "LN2", 0.6931471805599453, DONT_ENUM | DONT_DELETE | READ_ONLY);
133
134// ECMA-262, section 15.8.1.4.
135%AddProperty($Math, "LOG2E", 1.4426950408889634, DONT_ENUM | DONT_DELETE | READ_ONLY);
136%AddProperty($Math, "LOG10E", 0.43429448190325176, DONT_ENUM | DONT_DELETE | READ_ONLY);
137%AddProperty($Math, "PI", 3.1415926535897932, DONT_ENUM | DONT_DELETE | READ_ONLY);
138%AddProperty($Math, "SQRT1_2", 0.7071067811865476, DONT_ENUM | DONT_DELETE | READ_ONLY);
139%AddProperty($Math, "SQRT2", 1.4142135623730951, DONT_ENUM | DONT_DELETE | READ_ONLY);