source: webkit/trunk/JavaScriptCore/runtime/MathObject.cpp@ 39337

Last change on this file since 39337 was 39337, checked in by Nikolas Zimmermann, 16 years ago

Reviewed by Darin Adler.

Fixes: https://bugs.webkit.org/show_bug.cgi?id=22876

Unify random number generation in JavaScriptCore & WebCore, by introducing
wtf/RandomNumber.h and moving wtf_random/wtf_random_init out of MathExtras.h.

wtf_random_init() has been renamed to initializeRandomNumberGenerator() and
lives in it's own private header: wtf/RandomNumberSeed.h, only intended to
be used from within JavaScriptCore.

wtf_random() has been renamed to randomNumber() and lives in a public header
wtf/RandomNumber.h, usable from within JavaScriptCore & WebCore. It encapsulates
the code taking care of initializing the random number generator (only when
building without ENABLE(JSC_MULTIPLE_THREADS), otherwhise initializeThreading()
already took care of that).

Functional change on darwin: Use random() instead of rand(), as it got a larger
period (more randomness). HTMLFormElement already contains this implementation
and I just moved it in randomNumber(), as special case for PLATFORM(DARWIN).

  • Property svn:eol-style set to native
File size: 9.3 KB
Line 
1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
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 "ObjectPrototype.h"
25#include "Operations.h"
26#include <time.h>
27#include <wtf/Assertions.h>
28#include <wtf/MathExtras.h>
29#include <wtf/RandomNumber.h>
30
31namespace JSC {
32
33ASSERT_CLASS_FITS_IN_CELL(MathObject);
34
35static JSValue* mathProtoFuncAbs(ExecState*, JSObject*, JSValue*, const ArgList&);
36static JSValue* mathProtoFuncACos(ExecState*, JSObject*, JSValue*, const ArgList&);
37static JSValue* mathProtoFuncASin(ExecState*, JSObject*, JSValue*, const ArgList&);
38static JSValue* mathProtoFuncATan(ExecState*, JSObject*, JSValue*, const ArgList&);
39static JSValue* mathProtoFuncATan2(ExecState*, JSObject*, JSValue*, const ArgList&);
40static JSValue* mathProtoFuncCeil(ExecState*, JSObject*, JSValue*, const ArgList&);
41static JSValue* mathProtoFuncCos(ExecState*, JSObject*, JSValue*, const ArgList&);
42static JSValue* mathProtoFuncExp(ExecState*, JSObject*, JSValue*, const ArgList&);
43static JSValue* mathProtoFuncFloor(ExecState*, JSObject*, JSValue*, const ArgList&);
44static JSValue* mathProtoFuncLog(ExecState*, JSObject*, JSValue*, const ArgList&);
45static JSValue* mathProtoFuncMax(ExecState*, JSObject*, JSValue*, const ArgList&);
46static JSValue* mathProtoFuncMin(ExecState*, JSObject*, JSValue*, const ArgList&);
47static JSValue* mathProtoFuncPow(ExecState*, JSObject*, JSValue*, const ArgList&);
48static JSValue* mathProtoFuncRandom(ExecState*, JSObject*, JSValue*, const ArgList&);
49static JSValue* mathProtoFuncRound(ExecState*, JSObject*, JSValue*, const ArgList&);
50static JSValue* mathProtoFuncSin(ExecState*, JSObject*, JSValue*, const ArgList&);
51static JSValue* mathProtoFuncSqrt(ExecState*, JSObject*, JSValue*, const ArgList&);
52static JSValue* mathProtoFuncTan(ExecState*, JSObject*, JSValue*, const ArgList&);
53
54}
55
56#include "MathObject.lut.h"
57
58namespace JSC {
59
60// ------------------------------ MathObject --------------------------------
61
62const ClassInfo MathObject::info = { "Math", 0, 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
87MathObject::MathObject(ExecState* exec, PassRefPtr<Structure> structure)
88 : JSObject(structure)
89{
90 putDirectWithoutTransition(Identifier(exec, "E"), jsNumber(exec, exp(1.0)), DontDelete | DontEnum | ReadOnly);
91 putDirectWithoutTransition(Identifier(exec, "LN2"), jsNumber(exec, log(2.0)), DontDelete | DontEnum | ReadOnly);
92 putDirectWithoutTransition(Identifier(exec, "LN10"), jsNumber(exec, log(10.0)), DontDelete | DontEnum | ReadOnly);
93 putDirectWithoutTransition(Identifier(exec, "LOG2E"), jsNumber(exec, 1.0 / log(2.0)), DontDelete | DontEnum | ReadOnly);
94 putDirectWithoutTransition(Identifier(exec, "LOG10E"), jsNumber(exec, 1.0 / log(10.0)), DontDelete | DontEnum | ReadOnly);
95 putDirectWithoutTransition(Identifier(exec, "PI"), jsNumber(exec, piDouble), DontDelete | DontEnum | ReadOnly);
96 putDirectWithoutTransition(Identifier(exec, "SQRT1_2"), jsNumber(exec, sqrt(0.5)), DontDelete | DontEnum | ReadOnly);
97 putDirectWithoutTransition(Identifier(exec, "SQRT2"), jsNumber(exec, sqrt(2.0)), DontDelete | DontEnum | ReadOnly);
98}
99
100// ECMA 15.8
101
102bool MathObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
103{
104 const HashEntry* entry = ExecState::mathTable(exec)->entry(exec, propertyName);
105
106 if (!entry)
107 return JSObject::getOwnPropertySlot(exec, propertyName, slot);
108
109 ASSERT(entry->attributes() & Function);
110 setUpStaticFunctionSlot(exec, entry, this, propertyName, slot);
111 return true;
112}
113
114// ------------------------------ Functions --------------------------------
115
116JSValue* mathProtoFuncAbs(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
117{
118 return jsNumber(exec, fabs(args.at(exec, 0)->toNumber(exec)));
119}
120
121JSValue* mathProtoFuncACos(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
122{
123 return jsNumber(exec, acos(args.at(exec, 0)->toNumber(exec)));
124}
125
126JSValue* mathProtoFuncASin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
127{
128 return jsNumber(exec, asin(args.at(exec, 0)->toNumber(exec)));
129}
130
131JSValue* mathProtoFuncATan(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
132{
133 return jsNumber(exec, atan(args.at(exec, 0)->toNumber(exec)));
134}
135
136JSValue* mathProtoFuncATan2(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
137{
138 return jsNumber(exec, atan2(args.at(exec, 0)->toNumber(exec), args.at(exec, 1)->toNumber(exec)));
139}
140
141JSValue* mathProtoFuncCeil(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
142{
143 return jsNumber(exec, ceil(args.at(exec, 0)->toNumber(exec)));
144}
145
146JSValue* mathProtoFuncCos(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
147{
148 return jsNumber(exec, cos(args.at(exec, 0)->toNumber(exec)));
149}
150
151JSValue* mathProtoFuncExp(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
152{
153 return jsNumber(exec, exp(args.at(exec, 0)->toNumber(exec)));
154}
155
156JSValue* mathProtoFuncFloor(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
157{
158 return jsNumber(exec, floor(args.at(exec, 0)->toNumber(exec)));
159}
160
161JSValue* mathProtoFuncLog(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
162{
163 return jsNumber(exec, log(args.at(exec, 0)->toNumber(exec)));
164}
165
166JSValue* mathProtoFuncMax(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
167{
168 unsigned argsCount = args.size();
169 double result = -Inf;
170 for (unsigned k = 0; k < argsCount; ++k) {
171 double val = args.at(exec, k)->toNumber(exec);
172 if (isnan(val)) {
173 result = NaN;
174 break;
175 }
176 if (val > result || (val == 0 && result == 0 && !signbit(val)))
177 result = val;
178 }
179 return jsNumber(exec, result);
180}
181
182JSValue* mathProtoFuncMin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
183{
184 unsigned argsCount = args.size();
185 double result = +Inf;
186 for (unsigned k = 0; k < argsCount; ++k) {
187 double val = args.at(exec, k)->toNumber(exec);
188 if (isnan(val)) {
189 result = NaN;
190 break;
191 }
192 if (val < result || (val == 0 && result == 0 && signbit(val)))
193 result = val;
194 }
195 return jsNumber(exec, result);
196}
197
198JSValue* mathProtoFuncPow(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
199{
200 // ECMA 15.8.2.1.13
201
202 double arg = args.at(exec, 0)->toNumber(exec);
203 double arg2 = args.at(exec, 1)->toNumber(exec);
204
205 if (isnan(arg2))
206 return jsNaN(exec);
207 if (isinf(arg2) && fabs(arg) == 1)
208 return jsNaN(exec);
209 return jsNumber(exec, pow(arg, arg2));
210}
211
212JSValue* mathProtoFuncRandom(ExecState* exec, JSObject*, JSValue*, const ArgList&)
213{
214 return jsNumber(exec, randomNumber());
215}
216
217JSValue* mathProtoFuncRound(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
218{
219 double arg = args.at(exec, 0)->toNumber(exec);
220 if (signbit(arg) && arg >= -0.5)
221 return jsNumber(exec, -0.0);
222 return jsNumber(exec, floor(arg + 0.5));
223}
224
225JSValue* mathProtoFuncSin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
226{
227 return jsNumber(exec, sin(args.at(exec, 0)->toNumber(exec)));
228}
229
230JSValue* mathProtoFuncSqrt(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
231{
232 return jsNumber(exec, sqrt(args.at(exec, 0)->toNumber(exec)));
233}
234
235JSValue* mathProtoFuncTan(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
236{
237 return jsNumber(exec, tan(args.at(exec, 0)->toNumber(exec)));
238}
239
240} // namespace JSC
Note: See TracBrowser for help on using the repository browser.