initial import
[vuplus_webkit] / Source / JavaScriptCore / runtime / MathObject.cpp
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
33 namespace JSC {
34
35 ASSERT_CLASS_FITS_IN_CELL(MathObject);
36
37 static EncodedJSValue JSC_HOST_CALL mathProtoFuncAbs(ExecState*);
38 static EncodedJSValue JSC_HOST_CALL mathProtoFuncACos(ExecState*);
39 static EncodedJSValue JSC_HOST_CALL mathProtoFuncASin(ExecState*);
40 static EncodedJSValue JSC_HOST_CALL mathProtoFuncATan(ExecState*);
41 static EncodedJSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState*);
42 static EncodedJSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState*);
43 static EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState*);
44 static EncodedJSValue JSC_HOST_CALL mathProtoFuncExp(ExecState*);
45 static EncodedJSValue JSC_HOST_CALL mathProtoFuncFloor(ExecState*);
46 static EncodedJSValue JSC_HOST_CALL mathProtoFuncLog(ExecState*);
47 static EncodedJSValue JSC_HOST_CALL mathProtoFuncMax(ExecState*);
48 static EncodedJSValue JSC_HOST_CALL mathProtoFuncMin(ExecState*);
49 static EncodedJSValue JSC_HOST_CALL mathProtoFuncPow(ExecState*);
50 static EncodedJSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState*);
51 static EncodedJSValue JSC_HOST_CALL mathProtoFuncRound(ExecState*);
52 static EncodedJSValue JSC_HOST_CALL mathProtoFuncSin(ExecState*);
53 static EncodedJSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState*);
54 static EncodedJSValue JSC_HOST_CALL mathProtoFuncTan(ExecState*);
55
56 }
57
58 #include "MathObject.lut.h"
59
60 namespace JSC {
61
62 const ClassInfo MathObject::s_info = { "Math", &JSNonFinalObject::s_info, 0, ExecState::mathTable };
63
64 /* Source for MathObject.lut.h
65 @begin mathTable
66   abs           mathProtoFuncAbs               DontEnum|Function 1
67   acos          mathProtoFuncACos              DontEnum|Function 1
68   asin          mathProtoFuncASin              DontEnum|Function 1
69   atan          mathProtoFuncATan              DontEnum|Function 1
70   atan2         mathProtoFuncATan2             DontEnum|Function 2
71   ceil          mathProtoFuncCeil              DontEnum|Function 1
72   cos           mathProtoFuncCos               DontEnum|Function 1
73   exp           mathProtoFuncExp               DontEnum|Function 1
74   floor         mathProtoFuncFloor             DontEnum|Function 1
75   log           mathProtoFuncLog               DontEnum|Function 1
76   max           mathProtoFuncMax               DontEnum|Function 2
77   min           mathProtoFuncMin               DontEnum|Function 2
78   pow           mathProtoFuncPow               DontEnum|Function 2
79   random        mathProtoFuncRandom            DontEnum|Function 0 
80   round         mathProtoFuncRound             DontEnum|Function 1
81   sin           mathProtoFuncSin               DontEnum|Function 1
82   sqrt          mathProtoFuncSqrt              DontEnum|Function 1
83   tan           mathProtoFuncTan               DontEnum|Function 1
84 @end
85 */
86
87 MathObject::MathObject(JSGlobalObject* globalObject, Structure* structure)
88     : JSNonFinalObject(globalObject->globalData(), structure)
89 {
90 }
91
92 void MathObject::finishCreation(ExecState* exec, JSGlobalObject* globalObject)
93 {
94     Base::finishCreation(globalObject->globalData());
95     ASSERT(inherits(&s_info));
96
97     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "E"), jsNumber(exp(1.0)), DontDelete | DontEnum | ReadOnly);
98     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "LN2"), jsNumber(log(2.0)), DontDelete | DontEnum | ReadOnly);
99     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "LN10"), jsNumber(log(10.0)), DontDelete | DontEnum | ReadOnly);
100     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "LOG2E"), jsNumber(1.0 / log(2.0)), DontDelete | DontEnum | ReadOnly);
101     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "LOG10E"), jsNumber(0.4342944819032518), DontDelete | DontEnum | ReadOnly); // See ECMA-262 15.8.1.5
102     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "PI"), jsNumber(piDouble), DontDelete | DontEnum | ReadOnly);
103     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "SQRT1_2"), jsNumber(sqrt(0.5)), DontDelete | DontEnum | ReadOnly);
104     putDirectWithoutTransition(exec->globalData(), Identifier(exec, "SQRT2"), jsNumber(sqrt(2.0)), DontDelete | DontEnum | ReadOnly);
105 }
106
107 bool MathObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
108 {
109     return getStaticFunctionSlot<JSObject>(exec, ExecState::mathTable(exec), this, propertyName, slot);
110 }
111
112 bool MathObject::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
113 {
114     return getStaticFunctionDescriptor<JSObject>(exec, ExecState::mathTable(exec), this, propertyName, descriptor);
115 }
116
117 // ------------------------------ Functions --------------------------------
118
119 EncodedJSValue JSC_HOST_CALL mathProtoFuncAbs(ExecState* exec)
120 {
121     return JSValue::encode(jsNumber(fabs(exec->argument(0).toNumber(exec))));
122 }
123
124 EncodedJSValue JSC_HOST_CALL mathProtoFuncACos(ExecState* exec)
125 {
126     return JSValue::encode(jsDoubleNumber(acos(exec->argument(0).toNumber(exec))));
127 }
128
129 EncodedJSValue JSC_HOST_CALL mathProtoFuncASin(ExecState* exec)
130 {
131     return JSValue::encode(jsDoubleNumber(asin(exec->argument(0).toNumber(exec))));
132 }
133
134 EncodedJSValue JSC_HOST_CALL mathProtoFuncATan(ExecState* exec)
135 {
136     return JSValue::encode(jsDoubleNumber(atan(exec->argument(0).toNumber(exec))));
137 }
138
139 EncodedJSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState* exec)
140 {
141     double arg0 = exec->argument(0).toNumber(exec);
142     double arg1 = exec->argument(1).toNumber(exec);
143     return JSValue::encode(jsDoubleNumber(atan2(arg0, arg1)));
144 }
145
146 EncodedJSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState* exec)
147 {
148     return JSValue::encode(jsNumber(ceil(exec->argument(0).toNumber(exec))));
149 }
150
151 EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState* exec)
152 {
153     return JSValue::encode(jsDoubleNumber(cos(exec->argument(0).toNumber(exec))));
154 }
155
156 EncodedJSValue JSC_HOST_CALL mathProtoFuncExp(ExecState* exec)
157 {
158     return JSValue::encode(jsDoubleNumber(exp(exec->argument(0).toNumber(exec))));
159 }
160
161 EncodedJSValue JSC_HOST_CALL mathProtoFuncFloor(ExecState* exec)
162 {
163     return JSValue::encode(jsNumber(floor(exec->argument(0).toNumber(exec))));
164 }
165
166 EncodedJSValue JSC_HOST_CALL mathProtoFuncLog(ExecState* exec)
167 {
168     return JSValue::encode(jsDoubleNumber(log(exec->argument(0).toNumber(exec))));
169 }
170
171 EncodedJSValue JSC_HOST_CALL mathProtoFuncMax(ExecState* exec)
172 {
173     unsigned argsCount = exec->argumentCount();
174     double result = -std::numeric_limits<double>::infinity();
175     for (unsigned k = 0; k < argsCount; ++k) {
176         double val = exec->argument(k).toNumber(exec);
177         if (isnan(val)) {
178             result = std::numeric_limits<double>::quiet_NaN();
179             break;
180         }
181         if (val > result || (val == 0 && result == 0 && !signbit(val)))
182             result = val;
183     }
184     return JSValue::encode(jsNumber(result));
185 }
186
187 EncodedJSValue JSC_HOST_CALL mathProtoFuncMin(ExecState* exec)
188 {
189     unsigned argsCount = exec->argumentCount();
190     double result = +std::numeric_limits<double>::infinity();
191     for (unsigned k = 0; k < argsCount; ++k) {
192         double val = exec->argument(k).toNumber(exec);
193         if (isnan(val)) {
194             result = std::numeric_limits<double>::quiet_NaN();
195             break;
196         }
197         if (val < result || (val == 0 && result == 0 && signbit(val)))
198             result = val;
199     }
200     return JSValue::encode(jsNumber(result));
201 }
202
203 EncodedJSValue JSC_HOST_CALL mathProtoFuncPow(ExecState* exec)
204 {
205     // ECMA 15.8.2.1.13
206
207     double arg = exec->argument(0).toNumber(exec);
208     double arg2 = exec->argument(1).toNumber(exec);
209
210     if (isnan(arg2))
211         return JSValue::encode(jsNaN());
212     if (isinf(arg2) && fabs(arg) == 1)
213         return JSValue::encode(jsNaN());
214     return JSValue::encode(jsNumber(pow(arg, arg2)));
215 }
216
217 EncodedJSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState* exec)
218 {
219     return JSValue::encode(jsDoubleNumber(exec->lexicalGlobalObject()->weakRandomNumber()));
220 }
221
222 EncodedJSValue JSC_HOST_CALL mathProtoFuncRound(ExecState* exec)
223 {
224     double arg = exec->argument(0).toNumber(exec);
225     double integer = ceil(arg);
226     return JSValue::encode(jsNumber(integer - (integer - arg > 0.5)));
227 }
228
229 EncodedJSValue JSC_HOST_CALL mathProtoFuncSin(ExecState* exec)
230 {
231     return JSValue::encode(exec->globalData().cachedSin(exec->argument(0).toNumber(exec)));
232 }
233
234 EncodedJSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState* exec)
235 {
236     return JSValue::encode(jsDoubleNumber(sqrt(exec->argument(0).toNumber(exec))));
237 }
238
239 EncodedJSValue JSC_HOST_CALL mathProtoFuncTan(ExecState* exec)
240 {
241     return JSValue::encode(jsDoubleNumber(tan(exec->argument(0).toNumber(exec))));
242 }
243
244 } // namespace JSC