]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
1 | /* |
2 | * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) | |
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 | ||
4e4e5a6f | 24 | #include "Lookup.h" |
9dae56ea A |
25 | #include "ObjectPrototype.h" |
26 | #include "Operations.h" | |
27 | #include <time.h> | |
28 | #include <wtf/Assertions.h> | |
29 | #include <wtf/MathExtras.h> | |
30 | #include <wtf/RandomNumber.h> | |
31 | #include <wtf/RandomNumberSeed.h> | |
32 | ||
33 | namespace JSC { | |
34 | ||
35 | ASSERT_CLASS_FITS_IN_CELL(MathObject); | |
36 | ||
ba379fdc A |
37 | static JSValue JSC_HOST_CALL mathProtoFuncAbs(ExecState*, JSObject*, JSValue, const ArgList&); |
38 | static JSValue JSC_HOST_CALL mathProtoFuncACos(ExecState*, JSObject*, JSValue, const ArgList&); | |
39 | static JSValue JSC_HOST_CALL mathProtoFuncASin(ExecState*, JSObject*, JSValue, const ArgList&); | |
40 | static JSValue JSC_HOST_CALL mathProtoFuncATan(ExecState*, JSObject*, JSValue, const ArgList&); | |
41 | static JSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState*, JSObject*, JSValue, const ArgList&); | |
42 | static JSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState*, JSObject*, JSValue, const ArgList&); | |
43 | static JSValue JSC_HOST_CALL mathProtoFuncCos(ExecState*, JSObject*, JSValue, const ArgList&); | |
44 | static JSValue JSC_HOST_CALL mathProtoFuncExp(ExecState*, JSObject*, JSValue, const ArgList&); | |
45 | static JSValue JSC_HOST_CALL mathProtoFuncFloor(ExecState*, JSObject*, JSValue, const ArgList&); | |
46 | static JSValue JSC_HOST_CALL mathProtoFuncLog(ExecState*, JSObject*, JSValue, const ArgList&); | |
47 | static JSValue JSC_HOST_CALL mathProtoFuncMax(ExecState*, JSObject*, JSValue, const ArgList&); | |
48 | static JSValue JSC_HOST_CALL mathProtoFuncMin(ExecState*, JSObject*, JSValue, const ArgList&); | |
49 | static JSValue JSC_HOST_CALL mathProtoFuncPow(ExecState*, JSObject*, JSValue, const ArgList&); | |
50 | static JSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState*, JSObject*, JSValue, const ArgList&); | |
51 | static JSValue JSC_HOST_CALL mathProtoFuncRound(ExecState*, JSObject*, JSValue, const ArgList&); | |
52 | static JSValue JSC_HOST_CALL mathProtoFuncSin(ExecState*, JSObject*, JSValue, const ArgList&); | |
53 | static JSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState*, JSObject*, JSValue, const ArgList&); | |
54 | static JSValue JSC_HOST_CALL mathProtoFuncTan(ExecState*, JSObject*, JSValue, const ArgList&); | |
9dae56ea A |
55 | |
56 | } | |
57 | ||
58 | #include "MathObject.lut.h" | |
59 | ||
60 | namespace JSC { | |
61 | ||
62 | // ------------------------------ MathObject -------------------------------- | |
63 | ||
64 | const ClassInfo MathObject::info = { "Math", 0, 0, ExecState::mathTable }; | |
65 | ||
66 | /* Source for MathObject.lut.h | |
67 | @begin mathTable | |
68 | abs mathProtoFuncAbs DontEnum|Function 1 | |
69 | acos mathProtoFuncACos DontEnum|Function 1 | |
70 | asin mathProtoFuncASin DontEnum|Function 1 | |
71 | atan mathProtoFuncATan DontEnum|Function 1 | |
72 | atan2 mathProtoFuncATan2 DontEnum|Function 2 | |
73 | ceil mathProtoFuncCeil DontEnum|Function 1 | |
74 | cos mathProtoFuncCos DontEnum|Function 1 | |
75 | exp mathProtoFuncExp DontEnum|Function 1 | |
76 | floor mathProtoFuncFloor DontEnum|Function 1 | |
77 | log mathProtoFuncLog DontEnum|Function 1 | |
78 | max mathProtoFuncMax DontEnum|Function 2 | |
79 | min mathProtoFuncMin DontEnum|Function 2 | |
80 | pow mathProtoFuncPow DontEnum|Function 2 | |
81 | random mathProtoFuncRandom DontEnum|Function 0 | |
82 | round mathProtoFuncRound DontEnum|Function 1 | |
83 | sin mathProtoFuncSin DontEnum|Function 1 | |
84 | sqrt mathProtoFuncSqrt DontEnum|Function 1 | |
85 | tan mathProtoFuncTan DontEnum|Function 1 | |
86 | @end | |
87 | */ | |
88 | ||
f9bf01c6 | 89 | MathObject::MathObject(ExecState* exec, NonNullPassRefPtr<Structure> structure) |
9dae56ea A |
90 | : JSObject(structure) |
91 | { | |
92 | putDirectWithoutTransition(Identifier(exec, "E"), jsNumber(exec, exp(1.0)), DontDelete | DontEnum | ReadOnly); | |
93 | putDirectWithoutTransition(Identifier(exec, "LN2"), jsNumber(exec, log(2.0)), DontDelete | DontEnum | ReadOnly); | |
94 | putDirectWithoutTransition(Identifier(exec, "LN10"), jsNumber(exec, log(10.0)), DontDelete | DontEnum | ReadOnly); | |
95 | putDirectWithoutTransition(Identifier(exec, "LOG2E"), jsNumber(exec, 1.0 / log(2.0)), DontDelete | DontEnum | ReadOnly); | |
96 | putDirectWithoutTransition(Identifier(exec, "LOG10E"), jsNumber(exec, 1.0 / log(10.0)), DontDelete | DontEnum | ReadOnly); | |
97 | putDirectWithoutTransition(Identifier(exec, "PI"), jsNumber(exec, piDouble), DontDelete | DontEnum | ReadOnly); | |
98 | putDirectWithoutTransition(Identifier(exec, "SQRT1_2"), jsNumber(exec, sqrt(0.5)), DontDelete | DontEnum | ReadOnly); | |
99 | putDirectWithoutTransition(Identifier(exec, "SQRT2"), jsNumber(exec, sqrt(2.0)), DontDelete | DontEnum | ReadOnly); | |
9dae56ea A |
100 | } |
101 | ||
102 | // ECMA 15.8 | |
103 | ||
104 | bool MathObject::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot &slot) | |
105 | { | |
f9bf01c6 A |
106 | return getStaticFunctionSlot<JSObject>(exec, ExecState::mathTable(exec), this, propertyName, slot); |
107 | } | |
9dae56ea | 108 | |
f9bf01c6 A |
109 | bool MathObject::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor) |
110 | { | |
111 | return getStaticFunctionDescriptor<JSObject>(exec, ExecState::mathTable(exec), this, propertyName, descriptor); | |
9dae56ea A |
112 | } |
113 | ||
114 | // ------------------------------ Functions -------------------------------- | |
115 | ||
ba379fdc | 116 | JSValue JSC_HOST_CALL mathProtoFuncAbs(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 117 | { |
ba379fdc | 118 | return jsNumber(exec, fabs(args.at(0).toNumber(exec))); |
9dae56ea A |
119 | } |
120 | ||
ba379fdc | 121 | JSValue JSC_HOST_CALL mathProtoFuncACos(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 122 | { |
f9bf01c6 | 123 | return jsDoubleNumber(exec, acos(args.at(0).toNumber(exec))); |
9dae56ea A |
124 | } |
125 | ||
ba379fdc | 126 | JSValue JSC_HOST_CALL mathProtoFuncASin(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 127 | { |
f9bf01c6 | 128 | return jsDoubleNumber(exec, asin(args.at(0).toNumber(exec))); |
9dae56ea A |
129 | } |
130 | ||
ba379fdc | 131 | JSValue JSC_HOST_CALL mathProtoFuncATan(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 132 | { |
f9bf01c6 | 133 | return jsDoubleNumber(exec, atan(args.at(0).toNumber(exec))); |
9dae56ea A |
134 | } |
135 | ||
ba379fdc | 136 | JSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 137 | { |
f9bf01c6 | 138 | return jsDoubleNumber(exec, atan2(args.at(0).toNumber(exec), args.at(1).toNumber(exec))); |
9dae56ea A |
139 | } |
140 | ||
ba379fdc | 141 | JSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 142 | { |
ba379fdc | 143 | return jsNumber(exec, ceil(args.at(0).toNumber(exec))); |
9dae56ea A |
144 | } |
145 | ||
ba379fdc | 146 | JSValue JSC_HOST_CALL mathProtoFuncCos(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 147 | { |
f9bf01c6 | 148 | return jsDoubleNumber(exec, cos(args.at(0).toNumber(exec))); |
9dae56ea A |
149 | } |
150 | ||
ba379fdc | 151 | JSValue JSC_HOST_CALL mathProtoFuncExp(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 152 | { |
f9bf01c6 | 153 | return jsDoubleNumber(exec, exp(args.at(0).toNumber(exec))); |
9dae56ea A |
154 | } |
155 | ||
ba379fdc | 156 | JSValue JSC_HOST_CALL mathProtoFuncFloor(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 157 | { |
ba379fdc | 158 | return jsNumber(exec, floor(args.at(0).toNumber(exec))); |
9dae56ea A |
159 | } |
160 | ||
ba379fdc | 161 | JSValue JSC_HOST_CALL mathProtoFuncLog(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 162 | { |
f9bf01c6 | 163 | return jsDoubleNumber(exec, log(args.at(0).toNumber(exec))); |
9dae56ea A |
164 | } |
165 | ||
ba379fdc | 166 | JSValue JSC_HOST_CALL mathProtoFuncMax(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea A |
167 | { |
168 | unsigned argsCount = args.size(); | |
169 | double result = -Inf; | |
170 | for (unsigned k = 0; k < argsCount; ++k) { | |
ba379fdc | 171 | double val = args.at(k).toNumber(exec); |
9dae56ea A |
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 | ||
ba379fdc | 182 | JSValue JSC_HOST_CALL mathProtoFuncMin(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea A |
183 | { |
184 | unsigned argsCount = args.size(); | |
185 | double result = +Inf; | |
186 | for (unsigned k = 0; k < argsCount; ++k) { | |
ba379fdc | 187 | double val = args.at(k).toNumber(exec); |
9dae56ea A |
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 | ||
ba379fdc | 198 | JSValue JSC_HOST_CALL mathProtoFuncPow(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea A |
199 | { |
200 | // ECMA 15.8.2.1.13 | |
201 | ||
ba379fdc A |
202 | double arg = args.at(0).toNumber(exec); |
203 | double arg2 = args.at(1).toNumber(exec); | |
9dae56ea A |
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 | ||
ba379fdc | 212 | JSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState* exec, JSObject*, JSValue, const ArgList&) |
9dae56ea | 213 | { |
4e4e5a6f | 214 | return jsDoubleNumber(exec, exec->lexicalGlobalObject()->weakRandomNumber()); |
9dae56ea A |
215 | } |
216 | ||
ba379fdc | 217 | JSValue JSC_HOST_CALL mathProtoFuncRound(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 218 | { |
ba379fdc | 219 | double arg = args.at(0).toNumber(exec); |
4e4e5a6f A |
220 | double integer = ceil(arg); |
221 | return jsNumber(exec, integer - (integer - arg > 0.5)); | |
9dae56ea A |
222 | } |
223 | ||
ba379fdc | 224 | JSValue JSC_HOST_CALL mathProtoFuncSin(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 225 | { |
4e4e5a6f | 226 | return exec->globalData().cachedSin(exec, args.at(0).toNumber(exec)); |
9dae56ea A |
227 | } |
228 | ||
ba379fdc | 229 | JSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 230 | { |
f9bf01c6 | 231 | return jsDoubleNumber(exec, sqrt(args.at(0).toNumber(exec))); |
9dae56ea A |
232 | } |
233 | ||
ba379fdc | 234 | JSValue JSC_HOST_CALL mathProtoFuncTan(ExecState* exec, JSObject*, JSValue, const ArgList& args) |
9dae56ea | 235 | { |
f9bf01c6 | 236 | return jsDoubleNumber(exec, tan(args.at(0).toNumber(exec))); |
9dae56ea A |
237 | } |
238 | ||
239 | } // namespace JSC |