1/*
2 *  Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 *  Copyright (C) 2007, 2008 Apple Inc. All Rights Reserved.
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 *
19 */
20
21#include "config.h"
22#include "MathObject.h"
23
24#include "Lookup.h"
25#include "ObjectPrototype.h"
26#include "Operations.h"
27#include <time.h>
28#include <wtf/Assertions.h>
29#include <wtf/MathExtras.h>
30#include <wtf/RandomNumber.h>
31#include <wtf/RandomNumberSeed.h>
32
33namespace JSC {
34
35ASSERT_CLASS_FITS_IN_CELL(MathObject);
36
37static EncodedJSValue JSC_HOST_CALL mathProtoFuncAbs(ExecState*);
38static EncodedJSValue JSC_HOST_CALL mathProtoFuncACos(ExecState*);
39static EncodedJSValue JSC_HOST_CALL mathProtoFuncASin(ExecState*);
40static EncodedJSValue JSC_HOST_CALL mathProtoFuncATan(ExecState*);
41static EncodedJSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState*);
42static EncodedJSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState*);
43static EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState*);
44static EncodedJSValue JSC_HOST_CALL mathProtoFuncExp(ExecState*);
45static EncodedJSValue JSC_HOST_CALL mathProtoFuncFloor(ExecState*);
46static EncodedJSValue JSC_HOST_CALL mathProtoFuncLog(ExecState*);
47static EncodedJSValue JSC_HOST_CALL mathProtoFuncMax(ExecState*);
48static EncodedJSValue JSC_HOST_CALL mathProtoFuncMin(ExecState*);
49static EncodedJSValue JSC_HOST_CALL mathProtoFuncPow(ExecState*);
50static EncodedJSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState*);
51static EncodedJSValue JSC_HOST_CALL mathProtoFuncRound(ExecState*);
52static EncodedJSValue JSC_HOST_CALL mathProtoFuncSin(ExecState*);
53static EncodedJSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState*);
54static EncodedJSValue JSC_HOST_CALL mathProtoFuncTan(ExecState*);
55
56}
57
58#include "MathObject.lut.h"
59
60namespace JSC {
61
62// ------------------------------ MathObject --------------------------------
63
64const ClassInfo MathObject::s_info = { "Math", &JSObjectWithGlobalObject::s_info, 0, ExecState::mathTable };
65
66/* Source for MathObject.lut.h
67@begin mathTable
68  abs           mathProtoFuncAbs               DontEnum|Function 1
69  acos          mathProtoFuncACos              DontEnum|Function 1
70  asin          mathProtoFuncASin              DontEnum|Function 1
71  atan          mathProtoFuncATan              DontEnum|Function 1
72  atan2         mathProtoFuncATan2             DontEnum|Function 2
73  ceil          mathProtoFuncCeil              DontEnum|Function 1
74  cos           mathProtoFuncCos               DontEnum|Function 1
75  exp           mathProtoFuncExp               DontEnum|Function 1
76  floor         mathProtoFuncFloor             DontEnum|Function 1
77  log           mathProtoFuncLog               DontEnum|Function 1
78  max           mathProtoFuncMax               DontEnum|Function 2
79  min           mathProtoFuncMin               DontEnum|Function 2
80  pow           mathProtoFuncPow               DontEnum|Function 2
81  random        mathProtoFuncRandom            DontEnum|Function 0
82  round         mathProtoFuncRound             DontEnum|Function 1
83  sin           mathProtoFuncSin               DontEnum|Function 1
84  sqrt          mathProtoFuncSqrt              DontEnum|Function 1
85  tan           mathProtoFuncTan               DontEnum|Function 1
86@end
87*/
88
89MathObject::MathObject(ExecState* exec, JSGlobalObject* globalObject, Structure* structure)
90    : JSObjectWithGlobalObject(globalObject, structure)
91{
92    ASSERT(inherits(&s_info));
93
94    putDirectWithoutTransition(exec->globalData(), Identifier(exec, "E"), jsNumber(exp(1.0)), DontDelete | DontEnum | ReadOnly);
95    putDirectWithoutTransition(exec->globalData(), Identifier(exec, "LN2"), jsNumber(log(2.0)), DontDelete | DontEnum | ReadOnly);
96    putDirectWithoutTransition(exec->globalData(), Identifier(exec, "LN10"), jsNumber(log(10.0)), DontDelete | DontEnum | ReadOnly);
97    putDirectWithoutTransition(exec->globalData(), Identifier(exec, "LOG2E"), jsNumber(1.0 / log(2.0)), DontDelete | DontEnum | ReadOnly);
98    putDirectWithoutTransition(exec->globalData(), Identifier(exec, "LOG10E"), jsNumber(0.4342944819032518), DontDelete | DontEnum | ReadOnly); // See ECMA-262 15.8.1.5
99    putDirectWithoutTransition(exec->globalData(), Identifier(exec, "PI"), jsNumber(piDouble), DontDelete | DontEnum | ReadOnly);
100    putDirectWithoutTransition(exec->globalData(), Identifier(exec, "SQRT1_2"), jsNumber(sqrt(0.5)), DontDelete | DontEnum | ReadOnly);
101    putDirectWithoutTransition(exec->globalData(), Identifier(exec, "SQRT2"), jsNumber(sqrt(2.0)), DontDelete | DontEnum | ReadOnly);
102}
103
104// ECMA 15.8
105
106bool MathObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
107{
108    return getStaticFunctionSlot<JSObject>(exec, ExecState::mathTable(exec), this, propertyName, slot);
109}
110
111bool MathObject::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
112{
113    return getStaticFunctionDescriptor<JSObject>(exec, ExecState::mathTable(exec), this, propertyName, descriptor);
114}
115
116// ------------------------------ Functions --------------------------------
117
118EncodedJSValue JSC_HOST_CALL mathProtoFuncAbs(ExecState* exec)
119{
120    return JSValue::encode(jsNumber(fabs(exec->argument(0).toNumber(exec))));
121}
122
123EncodedJSValue JSC_HOST_CALL mathProtoFuncACos(ExecState* exec)
124{
125    return JSValue::encode(jsDoubleNumber(acos(exec->argument(0).toNumber(exec))));
126}
127
128EncodedJSValue JSC_HOST_CALL mathProtoFuncASin(ExecState* exec)
129{
130    return JSValue::encode(jsDoubleNumber(asin(exec->argument(0).toNumber(exec))));
131}
132
133EncodedJSValue JSC_HOST_CALL mathProtoFuncATan(ExecState* exec)
134{
135    return JSValue::encode(jsDoubleNumber(atan(exec->argument(0).toNumber(exec))));
136}
137
138EncodedJSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState* exec)
139{
140    double arg0 = exec->argument(0).toNumber(exec);
141    double arg1 = exec->argument(1).toNumber(exec);
142    return JSValue::encode(jsDoubleNumber(atan2(arg0, arg1)));
143}
144
145EncodedJSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState* exec)
146{
147    return JSValue::encode(jsNumber(ceil(exec->argument(0).toNumber(exec))));
148}
149
150EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState* exec)
151{
152    return JSValue::encode(jsDoubleNumber(cos(exec->argument(0).toNumber(exec))));
153}
154
155EncodedJSValue JSC_HOST_CALL mathProtoFuncExp(ExecState* exec)
156{
157    return JSValue::encode(jsDoubleNumber(exp(exec->argument(0).toNumber(exec))));
158}
159
160EncodedJSValue JSC_HOST_CALL mathProtoFuncFloor(ExecState* exec)
161{
162    return JSValue::encode(jsNumber(floor(exec->argument(0).toNumber(exec))));
163}
164
165EncodedJSValue JSC_HOST_CALL mathProtoFuncLog(ExecState* exec)
166{
167    return JSValue::encode(jsDoubleNumber(log(exec->argument(0).toNumber(exec))));
168}
169
170EncodedJSValue JSC_HOST_CALL mathProtoFuncMax(ExecState* exec)
171{
172    unsigned argsCount = exec->argumentCount();
173    double result = -Inf;
174    for (unsigned k = 0; k < argsCount; ++k) {
175        double val = exec->argument(k).toNumber(exec);
176        if (isnan(val)) {
177            result = NaN;
178            break;
179        }
180        if (val > result || (val == 0 && result == 0 && !signbit(val)))
181            result = val;
182    }
183    return JSValue::encode(jsNumber(result));
184}
185
186EncodedJSValue JSC_HOST_CALL mathProtoFuncMin(ExecState* exec)
187{
188    unsigned argsCount = exec->argumentCount();
189    double result = +Inf;
190    for (unsigned k = 0; k < argsCount; ++k) {
191        double val = exec->argument(k).toNumber(exec);
192        if (isnan(val)) {
193            result = NaN;
194            break;
195        }
196        if (val < result || (val == 0 && result == 0 && signbit(val)))
197            result = val;
198    }
199    return JSValue::encode(jsNumber(result));
200}
201
202EncodedJSValue JSC_HOST_CALL mathProtoFuncPow(ExecState* exec)
203{
204    // ECMA 15.8.2.1.13
205
206    double arg = exec->argument(0).toNumber(exec);
207    double arg2 = exec->argument(1).toNumber(exec);
208
209    if (isnan(arg2))
210        return JSValue::encode(jsNaN());
211    if (isinf(arg2) && fabs(arg) == 1)
212        return JSValue::encode(jsNaN());
213    return JSValue::encode(jsNumber(pow(arg, arg2)));
214}
215
216EncodedJSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState* exec)
217{
218    return JSValue::encode(jsDoubleNumber(exec->lexicalGlobalObject()->weakRandomNumber()));
219}
220
221EncodedJSValue JSC_HOST_CALL mathProtoFuncRound(ExecState* exec)
222{
223    double arg = exec->argument(0).toNumber(exec);
224    double integer = ceil(arg);
225    return JSValue::encode(jsNumber(integer - (integer - arg > 0.5)));
226}
227
228EncodedJSValue JSC_HOST_CALL mathProtoFuncSin(ExecState* exec)
229{
230    return JSValue::encode(exec->globalData().cachedSin(exec->argument(0).toNumber(exec)));
231}
232
233EncodedJSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState* exec)
234{
235    return JSValue::encode(jsDoubleNumber(sqrt(exec->argument(0).toNumber(exec))));
236}
237
238EncodedJSValue JSC_HOST_CALL mathProtoFuncTan(ExecState* exec)
239{
240    return JSValue::encode(jsDoubleNumber(tan(exec->argument(0).toNumber(exec))));
241}
242
243} // namespace JSC
244