source: webkit/trunk/JavaScriptCore/kjs/MathObject.cpp@ 35291

Last change on this file since 35291 was 35291, checked in by [email protected], 17 years ago

JavaScriptCore:

2008-07-22 Geoffrey Garen <[email protected]>

Reviewed by Oliver Hunt and Sam Weinig.

Next step toward putting doubles in registers: Prepare the Register class
and its clients for registers that don't contain JSValue*s.


This means a few things:


  1. Register::jsValue() clients, including ArgList clients, must now supply an ExecState* when accessing an entry in an ArgList, in case the entry will need to create a JSValue* on the fly.


  1. Register clients that definitely don't want to create a JSValue* on the fly now use different APIs: getJSValue() for clients that know the register contains a JSValue*, and v() for clients who just want a void*.


  1. I had to change some headers around in order to resolve dependency problems created by using a Register in the ArgList header.


SunSpider reports no change.

JavaScriptGlue:

2008-07-22 Geoffrey Garen <[email protected]>

Reviewed by Oliver Hunt and Sam Weinig.

Next step toward putting doubles in registers: Prepare the Register class
and its clients for registers that don't contain JSValue*s.

WebCore:

2008-07-22 Geoffrey Garen <[email protected]>

Reviewed by Oliver Hunt and Sam Weinig.

Next step toward putting doubles in registers: Prepare the Register class
and its clients for registers that don't contain JSValue*s.

  • Property svn:eol-style set to native
File size: 9.7 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
30namespace KJS {
31
32static JSValue* mathProtoFuncAbs(ExecState*, JSObject*, JSValue*, const ArgList&);
33static JSValue* mathProtoFuncACos(ExecState*, JSObject*, JSValue*, const ArgList&);
34static JSValue* mathProtoFuncASin(ExecState*, JSObject*, JSValue*, const ArgList&);
35static JSValue* mathProtoFuncATan(ExecState*, JSObject*, JSValue*, const ArgList&);
36static JSValue* mathProtoFuncATan2(ExecState*, JSObject*, JSValue*, const ArgList&);
37static JSValue* mathProtoFuncCeil(ExecState*, JSObject*, JSValue*, const ArgList&);
38static JSValue* mathProtoFuncCos(ExecState*, JSObject*, JSValue*, const ArgList&);
39static JSValue* mathProtoFuncExp(ExecState*, JSObject*, JSValue*, const ArgList&);
40static JSValue* mathProtoFuncFloor(ExecState*, JSObject*, JSValue*, const ArgList&);
41static JSValue* mathProtoFuncLog(ExecState*, JSObject*, JSValue*, const ArgList&);
42static JSValue* mathProtoFuncMax(ExecState*, JSObject*, JSValue*, const ArgList&);
43static JSValue* mathProtoFuncMin(ExecState*, JSObject*, JSValue*, const ArgList&);
44static JSValue* mathProtoFuncPow(ExecState*, JSObject*, JSValue*, const ArgList&);
45static JSValue* mathProtoFuncRandom(ExecState*, JSObject*, JSValue*, const ArgList&);
46static JSValue* mathProtoFuncRound(ExecState*, JSObject*, JSValue*, const ArgList&);
47static JSValue* mathProtoFuncSin(ExecState*, JSObject*, JSValue*, const ArgList&);
48static JSValue* mathProtoFuncSqrt(ExecState*, JSObject*, JSValue*, const ArgList&);
49static JSValue* mathProtoFuncTan(ExecState*, JSObject*, JSValue*, const ArgList&);
50
51}
52
53#include "MathObject.lut.h"
54
55namespace KJS {
56
57// ------------------------------ MathObject --------------------------------
58
59const ClassInfo MathObject::info = { "Math", 0, 0, ExecState::mathTable };
60
61/* Source for MathObject.lut.h
62@begin mathTable
63 E MathObject::Euler DontEnum|DontDelete|ReadOnly
64 LN2 MathObject::Ln2 DontEnum|DontDelete|ReadOnly
65 LN10 MathObject::Ln10 DontEnum|DontDelete|ReadOnly
66 LOG2E MathObject::Log2E DontEnum|DontDelete|ReadOnly
67 LOG10E MathObject::Log10E DontEnum|DontDelete|ReadOnly
68 PI MathObject::Pi DontEnum|DontDelete|ReadOnly
69 SQRT1_2 MathObject::Sqrt1_2 DontEnum|DontDelete|ReadOnly
70 SQRT2 MathObject::Sqrt2 DontEnum|DontDelete|ReadOnly
71 abs mathProtoFuncAbs DontEnum|Function 1
72 acos mathProtoFuncACos DontEnum|Function 1
73 asin mathProtoFuncASin DontEnum|Function 1
74 atan mathProtoFuncATan DontEnum|Function 1
75 atan2 mathProtoFuncATan2 DontEnum|Function 2
76 ceil mathProtoFuncCeil DontEnum|Function 1
77 cos mathProtoFuncCos DontEnum|Function 1
78 exp mathProtoFuncExp DontEnum|Function 1
79 floor mathProtoFuncFloor DontEnum|Function 1
80 log mathProtoFuncLog DontEnum|Function 1
81 max mathProtoFuncMax DontEnum|Function 2
82 min mathProtoFuncMin DontEnum|Function 2
83 pow mathProtoFuncPow DontEnum|Function 2
84 random mathProtoFuncRandom DontEnum|Function 0
85 round mathProtoFuncRound DontEnum|Function 1
86 sin mathProtoFuncSin DontEnum|Function 1
87 sqrt mathProtoFuncSqrt DontEnum|Function 1
88 tan mathProtoFuncTan DontEnum|Function 1
89@end
90*/
91
92MathObject::MathObject(ExecState*, ObjectPrototype* objectPrototype)
93 : JSObject(objectPrototype)
94{
95}
96
97// ECMA 15.8
98
99bool MathObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
100{
101 return getStaticPropertySlot<MathObject, JSObject>(exec, ExecState::mathTable(exec), this, propertyName, slot);
102}
103
104JSValue* MathObject::getValueProperty(ExecState* exec, int token) const
105{
106 switch (token) {
107 case Euler:
108 return jsNumber(exec, exp(1.0));
109 case Ln2:
110 return jsNumber(exec, log(2.0));
111 case Ln10:
112 return jsNumber(exec, log(10.0));
113 case Log2E:
114 return jsNumber(exec, 1.0 / log(2.0));
115 case Log10E:
116 return jsNumber(exec, 1.0 / log(10.0));
117 case Pi:
118 return jsNumber(exec, piDouble);
119 case Sqrt1_2:
120 return jsNumber(exec, sqrt(0.5));
121 case Sqrt2:
122 return jsNumber(exec, sqrt(2.0));
123 }
124
125 ASSERT_NOT_REACHED();
126 return 0;
127}
128
129// ------------------------------ Functions --------------------------------
130
131JSValue* mathProtoFuncAbs(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
132{
133 double arg = args.at(exec, 0)->toNumber(exec);
134 return signbit(arg) ? jsNumber(exec, -arg) : jsNumber(exec, arg);
135}
136
137JSValue* mathProtoFuncACos(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
138{
139 return jsNumber(exec, acos(args.at(exec, 0)->toNumber(exec)));
140}
141
142JSValue* mathProtoFuncASin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
143{
144 return jsNumber(exec, asin(args.at(exec, 0)->toNumber(exec)));
145}
146
147JSValue* mathProtoFuncATan(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
148{
149 return jsNumber(exec, atan(args.at(exec, 0)->toNumber(exec)));
150}
151
152JSValue* mathProtoFuncATan2(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
153{
154 return jsNumber(exec, atan2(args.at(exec, 0)->toNumber(exec), args.at(exec, 1)->toNumber(exec)));
155}
156
157JSValue* mathProtoFuncCeil(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
158{
159 double arg = args.at(exec, 0)->toNumber(exec);
160 if (signbit(arg) && arg > -1.0)
161 return jsNumber(exec, -0.0);
162 return jsNumber(exec, ceil(arg));
163}
164
165JSValue* mathProtoFuncCos(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
166{
167 return jsNumber(exec, cos(args.at(exec, 0)->toNumber(exec)));
168}
169
170JSValue* mathProtoFuncExp(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
171{
172 return jsNumber(exec, exp(args.at(exec, 0)->toNumber(exec)));
173}
174
175JSValue* mathProtoFuncFloor(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
176{
177 double arg = args.at(exec, 0)->toNumber(exec);
178 if (signbit(arg) && arg == 0.0)
179 return jsNumber(exec, -0.0);
180 return jsNumber(exec, floor(arg));
181}
182
183JSValue* mathProtoFuncLog(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
184{
185 return jsNumber(exec, log(args.at(exec, 0)->toNumber(exec)));
186}
187
188JSValue* mathProtoFuncMax(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
189{
190 unsigned argsCount = args.size();
191 double result = -Inf;
192 for (unsigned k = 0; k < argsCount; ++k) {
193 double val = args.at(exec, k)->toNumber(exec);
194 if (isnan(val)) {
195 result = NaN;
196 break;
197 }
198 if (val > result || (val == 0 && result == 0 && !signbit(val)))
199 result = val;
200 }
201 return jsNumber(exec, result);
202}
203
204JSValue* mathProtoFuncMin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
205{
206 unsigned argsCount = args.size();
207 double result = +Inf;
208 for (unsigned k = 0; k < argsCount; ++k) {
209 double val = args.at(exec, k)->toNumber(exec);
210 if (isnan(val)) {
211 result = NaN;
212 break;
213 }
214 if (val < result || (val == 0 && result == 0 && signbit(val)))
215 result = val;
216 }
217 return jsNumber(exec, result);
218}
219
220JSValue* mathProtoFuncPow(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
221{
222 // ECMA 15.8.2.1.13
223
224 double arg = args.at(exec, 0)->toNumber(exec);
225 double arg2 = args.at(exec, 1)->toNumber(exec);
226
227 if (isnan(arg2))
228 return jsNaN(exec);
229 if (isinf(arg2) && fabs(arg) == 1)
230 return jsNaN(exec);
231 return jsNumber(exec, pow(arg, arg2));
232}
233
234JSValue* mathProtoFuncRandom(ExecState* exec, JSObject*, JSValue*, const ArgList&)
235{
236#if !USE(MULTIPLE_THREADS)
237 static bool didInitRandom;
238 if (!didInitRandom) {
239 wtf_random_init();
240 didInitRandom = true;
241 }
242#endif
243
244 return jsNumber(exec, wtf_random());
245}
246
247JSValue* mathProtoFuncRound(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
248{
249 double arg = args.at(exec, 0)->toNumber(exec);
250 if (signbit(arg) && arg >= -0.5)
251 return jsNumber(exec, -0.0);
252 return jsNumber(exec, floor(arg + 0.5));
253}
254
255JSValue* mathProtoFuncSin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
256{
257 return jsNumber(exec, sin(args.at(exec, 0)->toNumber(exec)));
258}
259
260JSValue* mathProtoFuncSqrt(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
261{
262 return jsNumber(exec, sqrt(args.at(exec, 0)->toNumber(exec)));
263}
264
265JSValue* mathProtoFuncTan(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
266{
267 return jsNumber(exec, tan(args.at(exec, 0)->toNumber(exec)));
268}
269
270} // namespace KJS
Note: See TracBrowser for help on using the repository browser.