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 |
|
---|
30 | namespace KJS {
|
---|
31 |
|
---|
32 | static JSValue* mathProtoFuncAbs(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
33 | static JSValue* mathProtoFuncACos(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
34 | static JSValue* mathProtoFuncASin(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
35 | static JSValue* mathProtoFuncATan(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
36 | static JSValue* mathProtoFuncATan2(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
37 | static JSValue* mathProtoFuncCeil(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
38 | static JSValue* mathProtoFuncCos(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
39 | static JSValue* mathProtoFuncExp(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
40 | static JSValue* mathProtoFuncFloor(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
41 | static JSValue* mathProtoFuncLog(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
42 | static JSValue* mathProtoFuncMax(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
43 | static JSValue* mathProtoFuncMin(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
44 | static JSValue* mathProtoFuncPow(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
45 | static JSValue* mathProtoFuncRandom(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
46 | static JSValue* mathProtoFuncRound(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
47 | static JSValue* mathProtoFuncSin(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
48 | static JSValue* mathProtoFuncSqrt(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
49 | static JSValue* mathProtoFuncTan(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
50 |
|
---|
51 | }
|
---|
52 |
|
---|
53 | #include "MathObject.lut.h"
|
---|
54 |
|
---|
55 | namespace KJS {
|
---|
56 |
|
---|
57 | // ------------------------------ MathObject --------------------------------
|
---|
58 |
|
---|
59 | const 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 |
|
---|
92 | MathObject::MathObject(ExecState*, ObjectPrototype* objectPrototype)
|
---|
93 | : JSObject(objectPrototype)
|
---|
94 | {
|
---|
95 | }
|
---|
96 |
|
---|
97 | // ECMA 15.8
|
---|
98 |
|
---|
99 | bool MathObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
|
---|
100 | {
|
---|
101 | return getStaticPropertySlot<MathObject, JSObject>(exec, ExecState::mathTable(exec), this, propertyName, slot);
|
---|
102 | }
|
---|
103 |
|
---|
104 | JSValue* 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 |
|
---|
131 | JSValue* 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 |
|
---|
137 | JSValue* mathProtoFuncACos(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
138 | {
|
---|
139 | return jsNumber(exec, acos(args.at(exec, 0)->toNumber(exec)));
|
---|
140 | }
|
---|
141 |
|
---|
142 | JSValue* mathProtoFuncASin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
143 | {
|
---|
144 | return jsNumber(exec, asin(args.at(exec, 0)->toNumber(exec)));
|
---|
145 | }
|
---|
146 |
|
---|
147 | JSValue* mathProtoFuncATan(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
148 | {
|
---|
149 | return jsNumber(exec, atan(args.at(exec, 0)->toNumber(exec)));
|
---|
150 | }
|
---|
151 |
|
---|
152 | JSValue* 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 |
|
---|
157 | JSValue* 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 |
|
---|
165 | JSValue* mathProtoFuncCos(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
166 | {
|
---|
167 | return jsNumber(exec, cos(args.at(exec, 0)->toNumber(exec)));
|
---|
168 | }
|
---|
169 |
|
---|
170 | JSValue* mathProtoFuncExp(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
171 | {
|
---|
172 | return jsNumber(exec, exp(args.at(exec, 0)->toNumber(exec)));
|
---|
173 | }
|
---|
174 |
|
---|
175 | JSValue* 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 |
|
---|
183 | JSValue* mathProtoFuncLog(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
184 | {
|
---|
185 | return jsNumber(exec, log(args.at(exec, 0)->toNumber(exec)));
|
---|
186 | }
|
---|
187 |
|
---|
188 | JSValue* 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 |
|
---|
204 | JSValue* 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 |
|
---|
220 | JSValue* 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 |
|
---|
234 | JSValue* 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 |
|
---|
247 | JSValue* 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 |
|
---|
255 | JSValue* mathProtoFuncSin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
256 | {
|
---|
257 | return jsNumber(exec, sin(args.at(exec, 0)->toNumber(exec)));
|
---|
258 | }
|
---|
259 |
|
---|
260 | JSValue* mathProtoFuncSqrt(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
261 | {
|
---|
262 | return jsNumber(exec, sqrt(args.at(exec, 0)->toNumber(exec)));
|
---|
263 | }
|
---|
264 |
|
---|
265 | JSValue* 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
|
---|