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 JSC {
|
---|
31 |
|
---|
32 | ASSERT_CLASS_FITS_IN_CELL(MathObject);
|
---|
33 |
|
---|
34 | static JSValue* mathProtoFuncAbs(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
35 | static JSValue* mathProtoFuncACos(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
36 | static JSValue* mathProtoFuncASin(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
37 | static JSValue* mathProtoFuncATan(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
38 | static JSValue* mathProtoFuncATan2(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
39 | static JSValue* mathProtoFuncCeil(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
40 | static JSValue* mathProtoFuncCos(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
41 | static JSValue* mathProtoFuncExp(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
42 | static JSValue* mathProtoFuncFloor(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
43 | static JSValue* mathProtoFuncLog(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
44 | static JSValue* mathProtoFuncMax(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
45 | static JSValue* mathProtoFuncMin(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
46 | static JSValue* mathProtoFuncPow(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
47 | static JSValue* mathProtoFuncRandom(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
48 | static JSValue* mathProtoFuncRound(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
49 | static JSValue* mathProtoFuncSin(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
50 | static JSValue* mathProtoFuncSqrt(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
51 | static JSValue* mathProtoFuncTan(ExecState*, JSObject*, JSValue*, const ArgList&);
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | #include "MathObject.lut.h"
|
---|
56 |
|
---|
57 | namespace JSC {
|
---|
58 |
|
---|
59 | // ------------------------------ MathObject --------------------------------
|
---|
60 |
|
---|
61 | const ClassInfo MathObject::info = { "Math", 0, 0, ExecState::mathTable };
|
---|
62 |
|
---|
63 | /* Source for MathObject.lut.h
|
---|
64 | @begin mathTable
|
---|
65 | abs mathProtoFuncAbs DontEnum|Function 1
|
---|
66 | acos mathProtoFuncACos DontEnum|Function 1
|
---|
67 | asin mathProtoFuncASin DontEnum|Function 1
|
---|
68 | atan mathProtoFuncATan DontEnum|Function 1
|
---|
69 | atan2 mathProtoFuncATan2 DontEnum|Function 2
|
---|
70 | ceil mathProtoFuncCeil DontEnum|Function 1
|
---|
71 | cos mathProtoFuncCos DontEnum|Function 1
|
---|
72 | exp mathProtoFuncExp DontEnum|Function 1
|
---|
73 | floor mathProtoFuncFloor DontEnum|Function 1
|
---|
74 | log mathProtoFuncLog DontEnum|Function 1
|
---|
75 | max mathProtoFuncMax DontEnum|Function 2
|
---|
76 | min mathProtoFuncMin DontEnum|Function 2
|
---|
77 | pow mathProtoFuncPow DontEnum|Function 2
|
---|
78 | random mathProtoFuncRandom DontEnum|Function 0
|
---|
79 | round mathProtoFuncRound DontEnum|Function 1
|
---|
80 | sin mathProtoFuncSin DontEnum|Function 1
|
---|
81 | sqrt mathProtoFuncSqrt DontEnum|Function 1
|
---|
82 | tan mathProtoFuncTan DontEnum|Function 1
|
---|
83 | @end
|
---|
84 | */
|
---|
85 |
|
---|
86 | MathObject::MathObject(ExecState* exec, PassRefPtr<StructureID> structure)
|
---|
87 | : JSObject(structure)
|
---|
88 | {
|
---|
89 | putDirect(Identifier(exec, "E"), jsNumber(exec, exp(1.0)), DontDelete | DontEnum | ReadOnly);
|
---|
90 | putDirect(Identifier(exec, "LN2"), jsNumber(exec, log(2.0)), DontDelete | DontEnum | ReadOnly);
|
---|
91 | putDirect(Identifier(exec, "LN10"), jsNumber(exec, log(10.0)), DontDelete | DontEnum | ReadOnly);
|
---|
92 | putDirect(Identifier(exec, "LOG2E"), jsNumber(exec, 1.0 / log(2.0)), DontDelete | DontEnum | ReadOnly);
|
---|
93 | putDirect(Identifier(exec, "LOG10E"), jsNumber(exec, 1.0 / log(10.0)), DontDelete | DontEnum | ReadOnly);
|
---|
94 | putDirect(Identifier(exec, "PI"), jsNumber(exec, piDouble), DontDelete | DontEnum | ReadOnly);
|
---|
95 | putDirect(Identifier(exec, "SQRT1_2"), jsNumber(exec, sqrt(0.5)), DontDelete | DontEnum | ReadOnly);
|
---|
96 | putDirect(Identifier(exec, "SQRT2"), jsNumber(exec, sqrt(2.0)), DontDelete | DontEnum | ReadOnly);
|
---|
97 | }
|
---|
98 |
|
---|
99 | // ECMA 15.8
|
---|
100 |
|
---|
101 | bool MathObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot)
|
---|
102 | {
|
---|
103 | const HashEntry* entry = ExecState::mathTable(exec)->entry(exec, propertyName);
|
---|
104 |
|
---|
105 | if (!entry)
|
---|
106 | return JSObject::getOwnPropertySlot(exec, propertyName, slot);
|
---|
107 |
|
---|
108 | ASSERT(entry->attributes() & Function);
|
---|
109 | setUpStaticFunctionSlot(exec, entry, this, propertyName, slot);
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 |
|
---|
113 | // ------------------------------ Functions --------------------------------
|
---|
114 |
|
---|
115 | JSValue* mathProtoFuncAbs(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
116 | {
|
---|
117 | double arg = args.at(exec, 0)->toNumber(exec);
|
---|
118 | return signbit(arg) ? jsNumber(exec, -arg) : jsNumber(exec, arg);
|
---|
119 | }
|
---|
120 |
|
---|
121 | JSValue* mathProtoFuncACos(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
122 | {
|
---|
123 | return jsNumber(exec, acos(args.at(exec, 0)->toNumber(exec)));
|
---|
124 | }
|
---|
125 |
|
---|
126 | JSValue* mathProtoFuncASin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
127 | {
|
---|
128 | return jsNumber(exec, asin(args.at(exec, 0)->toNumber(exec)));
|
---|
129 | }
|
---|
130 |
|
---|
131 | JSValue* mathProtoFuncATan(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
132 | {
|
---|
133 | return jsNumber(exec, atan(args.at(exec, 0)->toNumber(exec)));
|
---|
134 | }
|
---|
135 |
|
---|
136 | JSValue* 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 |
|
---|
141 | JSValue* mathProtoFuncCeil(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
142 | {
|
---|
143 | double arg = args.at(exec, 0)->toNumber(exec);
|
---|
144 | if (signbit(arg) && arg > -1.0)
|
---|
145 | return jsNumber(exec, -0.0);
|
---|
146 | return jsNumber(exec, ceil(arg));
|
---|
147 | }
|
---|
148 |
|
---|
149 | JSValue* mathProtoFuncCos(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
150 | {
|
---|
151 | return jsNumber(exec, cos(args.at(exec, 0)->toNumber(exec)));
|
---|
152 | }
|
---|
153 |
|
---|
154 | JSValue* mathProtoFuncExp(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
155 | {
|
---|
156 | return jsNumber(exec, exp(args.at(exec, 0)->toNumber(exec)));
|
---|
157 | }
|
---|
158 |
|
---|
159 | JSValue* mathProtoFuncFloor(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
160 | {
|
---|
161 | double arg = args.at(exec, 0)->toNumber(exec);
|
---|
162 | if (signbit(arg) && arg == 0.0)
|
---|
163 | return jsNumber(exec, -0.0);
|
---|
164 | return jsNumber(exec, floor(arg));
|
---|
165 | }
|
---|
166 |
|
---|
167 | JSValue* mathProtoFuncLog(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
168 | {
|
---|
169 | return jsNumber(exec, log(args.at(exec, 0)->toNumber(exec)));
|
---|
170 | }
|
---|
171 |
|
---|
172 | JSValue* mathProtoFuncMax(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
173 | {
|
---|
174 | unsigned argsCount = args.size();
|
---|
175 | double result = -Inf;
|
---|
176 | for (unsigned k = 0; k < argsCount; ++k) {
|
---|
177 | double val = args.at(exec, k)->toNumber(exec);
|
---|
178 | if (isnan(val)) {
|
---|
179 | result = NaN;
|
---|
180 | break;
|
---|
181 | }
|
---|
182 | if (val > result || (val == 0 && result == 0 && !signbit(val)))
|
---|
183 | result = val;
|
---|
184 | }
|
---|
185 | return jsNumber(exec, result);
|
---|
186 | }
|
---|
187 |
|
---|
188 | JSValue* mathProtoFuncMin(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* mathProtoFuncPow(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
205 | {
|
---|
206 | // ECMA 15.8.2.1.13
|
---|
207 |
|
---|
208 | double arg = args.at(exec, 0)->toNumber(exec);
|
---|
209 | double arg2 = args.at(exec, 1)->toNumber(exec);
|
---|
210 |
|
---|
211 | if (isnan(arg2))
|
---|
212 | return jsNaN(exec);
|
---|
213 | if (isinf(arg2) && fabs(arg) == 1)
|
---|
214 | return jsNaN(exec);
|
---|
215 | return jsNumber(exec, pow(arg, arg2));
|
---|
216 | }
|
---|
217 |
|
---|
218 | JSValue* mathProtoFuncRandom(ExecState* exec, JSObject*, JSValue*, const ArgList&)
|
---|
219 | {
|
---|
220 | #if !ENABLE(JSC_MULTIPLE_THREADS)
|
---|
221 | static bool didInitRandom;
|
---|
222 | if (!didInitRandom) {
|
---|
223 | wtf_random_init();
|
---|
224 | didInitRandom = true;
|
---|
225 | }
|
---|
226 | #endif
|
---|
227 |
|
---|
228 | return jsNumber(exec, wtf_random());
|
---|
229 | }
|
---|
230 |
|
---|
231 | JSValue* mathProtoFuncRound(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
232 | {
|
---|
233 | double arg = args.at(exec, 0)->toNumber(exec);
|
---|
234 | if (signbit(arg) && arg >= -0.5)
|
---|
235 | return jsNumber(exec, -0.0);
|
---|
236 | return jsNumber(exec, floor(arg + 0.5));
|
---|
237 | }
|
---|
238 |
|
---|
239 | JSValue* mathProtoFuncSin(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
240 | {
|
---|
241 | return jsNumber(exec, sin(args.at(exec, 0)->toNumber(exec)));
|
---|
242 | }
|
---|
243 |
|
---|
244 | JSValue* mathProtoFuncSqrt(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
245 | {
|
---|
246 | return jsNumber(exec, sqrt(args.at(exec, 0)->toNumber(exec)));
|
---|
247 | }
|
---|
248 |
|
---|
249 | JSValue* mathProtoFuncTan(ExecState* exec, JSObject*, JSValue*, const ArgList& args)
|
---|
250 | {
|
---|
251 | return jsNumber(exec, tan(args.at(exec, 0)->toNumber(exec)));
|
---|
252 | }
|
---|
253 |
|
---|
254 | } // namespace JSC
|
---|