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 JSValuePtr mathProtoFuncAbs(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
35 | static JSValuePtr mathProtoFuncACos(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
36 | static JSValuePtr mathProtoFuncASin(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
37 | static JSValuePtr mathProtoFuncATan(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
38 | static JSValuePtr mathProtoFuncATan2(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
39 | static JSValuePtr mathProtoFuncCeil(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
40 | static JSValuePtr mathProtoFuncCos(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
41 | static JSValuePtr mathProtoFuncExp(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
42 | static JSValuePtr mathProtoFuncFloor(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
43 | static JSValuePtr mathProtoFuncLog(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
44 | static JSValuePtr mathProtoFuncMax(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
45 | static JSValuePtr mathProtoFuncMin(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
46 | static JSValuePtr mathProtoFuncPow(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
47 | static JSValuePtr mathProtoFuncRandom(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
48 | static JSValuePtr mathProtoFuncRound(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
49 | static JSValuePtr mathProtoFuncSin(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
50 | static JSValuePtr mathProtoFuncSqrt(ExecState*, JSObject*, JSValuePtr, const ArgList&);
|
---|
51 | static JSValuePtr mathProtoFuncTan(ExecState*, JSObject*, JSValuePtr, 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 | putDirectWithoutTransition(Identifier(exec, "E"), jsNumber(exec, exp(1.0)), DontDelete | DontEnum | ReadOnly);
|
---|
90 | putDirectWithoutTransition(Identifier(exec, "LN2"), jsNumber(exec, log(2.0)), DontDelete | DontEnum | ReadOnly);
|
---|
91 | putDirectWithoutTransition(Identifier(exec, "LN10"), jsNumber(exec, log(10.0)), DontDelete | DontEnum | ReadOnly);
|
---|
92 | putDirectWithoutTransition(Identifier(exec, "LOG2E"), jsNumber(exec, 1.0 / log(2.0)), DontDelete | DontEnum | ReadOnly);
|
---|
93 | putDirectWithoutTransition(Identifier(exec, "LOG10E"), jsNumber(exec, 1.0 / log(10.0)), DontDelete | DontEnum | ReadOnly);
|
---|
94 | putDirectWithoutTransition(Identifier(exec, "PI"), jsNumber(exec, piDouble), DontDelete | DontEnum | ReadOnly);
|
---|
95 | putDirectWithoutTransition(Identifier(exec, "SQRT1_2"), jsNumber(exec, sqrt(0.5)), DontDelete | DontEnum | ReadOnly);
|
---|
96 | putDirectWithoutTransition(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 | JSValuePtr mathProtoFuncAbs(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
116 | {
|
---|
117 | return jsNumber(exec, fabs(args.at(exec, 0)->toNumber(exec)));
|
---|
118 | }
|
---|
119 |
|
---|
120 | JSValuePtr mathProtoFuncACos(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
121 | {
|
---|
122 | return jsNumber(exec, acos(args.at(exec, 0)->toNumber(exec)));
|
---|
123 | }
|
---|
124 |
|
---|
125 | JSValuePtr mathProtoFuncASin(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
126 | {
|
---|
127 | return jsNumber(exec, asin(args.at(exec, 0)->toNumber(exec)));
|
---|
128 | }
|
---|
129 |
|
---|
130 | JSValuePtr mathProtoFuncATan(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
131 | {
|
---|
132 | return jsNumber(exec, atan(args.at(exec, 0)->toNumber(exec)));
|
---|
133 | }
|
---|
134 |
|
---|
135 | JSValuePtr mathProtoFuncATan2(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
136 | {
|
---|
137 | return jsNumber(exec, atan2(args.at(exec, 0)->toNumber(exec), args.at(exec, 1)->toNumber(exec)));
|
---|
138 | }
|
---|
139 |
|
---|
140 | JSValuePtr mathProtoFuncCeil(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
141 | {
|
---|
142 | return jsNumber(exec, ceil(args.at(exec, 0)->toNumber(exec)));
|
---|
143 | }
|
---|
144 |
|
---|
145 | JSValuePtr mathProtoFuncCos(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
146 | {
|
---|
147 | return jsNumber(exec, cos(args.at(exec, 0)->toNumber(exec)));
|
---|
148 | }
|
---|
149 |
|
---|
150 | JSValuePtr mathProtoFuncExp(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
151 | {
|
---|
152 | return jsNumber(exec, exp(args.at(exec, 0)->toNumber(exec)));
|
---|
153 | }
|
---|
154 |
|
---|
155 | JSValuePtr mathProtoFuncFloor(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
156 | {
|
---|
157 | return jsNumber(exec, floor(args.at(exec, 0)->toNumber(exec)));
|
---|
158 | }
|
---|
159 |
|
---|
160 | JSValuePtr mathProtoFuncLog(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
161 | {
|
---|
162 | return jsNumber(exec, log(args.at(exec, 0)->toNumber(exec)));
|
---|
163 | }
|
---|
164 |
|
---|
165 | JSValuePtr mathProtoFuncMax(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
166 | {
|
---|
167 | unsigned argsCount = args.size();
|
---|
168 | double result = -Inf;
|
---|
169 | for (unsigned k = 0; k < argsCount; ++k) {
|
---|
170 | double val = args.at(exec, k)->toNumber(exec);
|
---|
171 | if (isnan(val)) {
|
---|
172 | result = NaN;
|
---|
173 | break;
|
---|
174 | }
|
---|
175 | if (val > result || (val == 0 && result == 0 && !signbit(val)))
|
---|
176 | result = val;
|
---|
177 | }
|
---|
178 | return jsNumber(exec, result);
|
---|
179 | }
|
---|
180 |
|
---|
181 | JSValuePtr mathProtoFuncMin(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
182 | {
|
---|
183 | unsigned argsCount = args.size();
|
---|
184 | double result = +Inf;
|
---|
185 | for (unsigned k = 0; k < argsCount; ++k) {
|
---|
186 | double val = args.at(exec, k)->toNumber(exec);
|
---|
187 | if (isnan(val)) {
|
---|
188 | result = NaN;
|
---|
189 | break;
|
---|
190 | }
|
---|
191 | if (val < result || (val == 0 && result == 0 && signbit(val)))
|
---|
192 | result = val;
|
---|
193 | }
|
---|
194 | return jsNumber(exec, result);
|
---|
195 | }
|
---|
196 |
|
---|
197 | JSValuePtr mathProtoFuncPow(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
198 | {
|
---|
199 | // ECMA 15.8.2.1.13
|
---|
200 |
|
---|
201 | double arg = args.at(exec, 0)->toNumber(exec);
|
---|
202 | double arg2 = args.at(exec, 1)->toNumber(exec);
|
---|
203 |
|
---|
204 | if (isnan(arg2))
|
---|
205 | return jsNaN(exec);
|
---|
206 | if (isinf(arg2) && fabs(arg) == 1)
|
---|
207 | return jsNaN(exec);
|
---|
208 | return jsNumber(exec, pow(arg, arg2));
|
---|
209 | }
|
---|
210 |
|
---|
211 | JSValuePtr mathProtoFuncRandom(ExecState* exec, JSObject*, JSValuePtr, const ArgList&)
|
---|
212 | {
|
---|
213 | #if !ENABLE(JSC_MULTIPLE_THREADS)
|
---|
214 | static bool didInitRandom;
|
---|
215 | if (!didInitRandom) {
|
---|
216 | wtf_random_init();
|
---|
217 | didInitRandom = true;
|
---|
218 | }
|
---|
219 | #endif
|
---|
220 |
|
---|
221 | return jsNumber(exec, wtf_random());
|
---|
222 | }
|
---|
223 |
|
---|
224 | JSValuePtr mathProtoFuncRound(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
225 | {
|
---|
226 | double arg = args.at(exec, 0)->toNumber(exec);
|
---|
227 | if (signbit(arg) && arg >= -0.5)
|
---|
228 | return jsNumber(exec, -0.0);
|
---|
229 | return jsNumber(exec, floor(arg + 0.5));
|
---|
230 | }
|
---|
231 |
|
---|
232 | JSValuePtr mathProtoFuncSin(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
233 | {
|
---|
234 | return jsNumber(exec, sin(args.at(exec, 0)->toNumber(exec)));
|
---|
235 | }
|
---|
236 |
|
---|
237 | JSValuePtr mathProtoFuncSqrt(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
238 | {
|
---|
239 | return jsNumber(exec, sqrt(args.at(exec, 0)->toNumber(exec)));
|
---|
240 | }
|
---|
241 |
|
---|
242 | JSValuePtr mathProtoFuncTan(ExecState* exec, JSObject*, JSValuePtr, const ArgList& args)
|
---|
243 | {
|
---|
244 | return jsNumber(exec, tan(args.at(exec, 0)->toNumber(exec)));
|
---|
245 | }
|
---|
246 |
|
---|
247 | } // namespace JSC
|
---|