1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/*
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
* Copyright (C) 2007, 2008, 2013, 2015 Apple Inc. All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "config.h"
#include "MathObject.h"
#include "Lookup.h"
#include "MathCommon.h"
#include "ObjectPrototype.h"
#include "JSCInlines.h"
#include <time.h>
#include <wtf/Assertions.h>
#include <wtf/MathExtras.h>
#include <wtf/RandomNumber.h>
#include <wtf/RandomNumberSeed.h>
#include <wtf/Vector.h>
namespace JSC {
STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(MathObject);
EncodedJSValue JSC_HOST_CALL mathProtoFuncACos(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncACosh(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncASin(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncASinh(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncATan(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncATanh(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncCbrt(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncClz32(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncCosh(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncExp(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncExpm1(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncFround(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncHypot(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncLog(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncLog1p(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncLog10(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncLog2(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncMax(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncMin(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncPow(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncRound(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncSign(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncSin(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncSinh(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncTan(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncTanh(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncTrunc(ExecState*);
EncodedJSValue JSC_HOST_CALL mathProtoFuncIMul(ExecState*);
}
namespace JSC {
const ClassInfo MathObject::s_info = { "Math", &Base::s_info, 0, CREATE_METHOD_TABLE(MathObject) };
MathObject::MathObject(VM& vm, Structure* structure)
: JSNonFinalObject(vm, structure)
{
}
void MathObject::finishCreation(VM& vm, JSGlobalObject* globalObject)
{
Base::finishCreation(vm);
ASSERT(inherits(info()));
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "E"), jsNumber(exp(1.0)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "LN2"), jsNumber(log(2.0)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "LN10"), jsNumber(log(10.0)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "LOG2E"), jsNumber(1.0 / log(2.0)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "LOG10E"), jsNumber(0.4342944819032518), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "PI"), jsNumber(piDouble), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "SQRT1_2"), jsNumber(sqrt(0.5)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(vm, Identifier::fromString(&vm, "SQRT2"), jsNumber(sqrt(2.0)), DontDelete | DontEnum | ReadOnly);
putDirectWithoutTransition(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "Math"), DontEnum | ReadOnly);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "abs"), 1, mathProtoFuncAbs, AbsIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "acos"), 1, mathProtoFuncACos, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "asin"), 1, mathProtoFuncASin, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "atan"), 1, mathProtoFuncATan, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "acosh"), 1, mathProtoFuncACosh, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "asinh"), 1, mathProtoFuncASinh, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "atanh"), 1, mathProtoFuncATanh, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "atan2"), 2, mathProtoFuncATan2, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "cbrt"), 1, mathProtoFuncCbrt, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "ceil"), 1, mathProtoFuncCeil, CeilIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "clz32"), 1, mathProtoFuncClz32, Clz32Intrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "cos"), 1, mathProtoFuncCos, CosIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "cosh"), 1, mathProtoFuncCosh, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "exp"), 1, mathProtoFuncExp, ExpIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "expm1"), 1, mathProtoFuncExpm1, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "floor"), 1, mathProtoFuncFloor, FloorIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "fround"), 1, mathProtoFuncFround, FRoundIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "hypot"), 2, mathProtoFuncHypot, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "log"), 1, mathProtoFuncLog, LogIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "log10"), 1, mathProtoFuncLog10, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "log1p"), 1, mathProtoFuncLog1p, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "log2"), 1, mathProtoFuncLog2, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "max"), 2, mathProtoFuncMax, MaxIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "min"), 2, mathProtoFuncMin, MinIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "pow"), 2, mathProtoFuncPow, PowIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "random"), 0, mathProtoFuncRandom, RandomIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "round"), 1, mathProtoFuncRound, RoundIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "sign"), 1, mathProtoFuncSign, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "sin"), 1, mathProtoFuncSin, SinIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "sinh"), 1, mathProtoFuncSinh, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "sqrt"), 1, mathProtoFuncSqrt, SqrtIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "tan"), 1, mathProtoFuncTan, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "tanh"), 1, mathProtoFuncTanh, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "trunc"), 1, mathProtoFuncTrunc, NoIntrinsic, DontEnum);
putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "imul"), 2, mathProtoFuncIMul, IMulIntrinsic, DontEnum);
}
// ------------------------------ Functions --------------------------------
EncodedJSValue JSC_HOST_CALL mathProtoFuncAbs(ExecState* exec)
{
return JSValue::encode(jsNumber(fabs(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncACos(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(acos(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncASin(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(asin(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncATan(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(atan(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState* exec)
{
double arg0 = exec->argument(0).toNumber(exec);
double arg1 = exec->argument(1).toNumber(exec);
return JSValue::encode(jsDoubleNumber(atan2(arg0, arg1)));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState* exec)
{
return JSValue::encode(jsNumber(ceil(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncClz32(ExecState* exec)
{
uint32_t value = exec->argument(0).toUInt32(exec);
if (exec->hadException())
return JSValue::encode(jsNull());
return JSValue::encode(JSValue(clz32(value)));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(cos(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncExp(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(exp(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncFloor(ExecState* exec)
{
return JSValue::encode(jsNumber(floor(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncHypot(ExecState* exec)
{
unsigned argsCount = exec->argumentCount();
double max = 0;
Vector<double, 8> args;
args.reserveInitialCapacity(argsCount);
for (unsigned i = 0; i < argsCount; ++i) {
args.uncheckedAppend(exec->uncheckedArgument(i).toNumber(exec));
if (exec->hadException())
return JSValue::encode(jsNull());
if (std::isinf(args[i]))
return JSValue::encode(jsDoubleNumber(+std::numeric_limits<double>::infinity()));
max = std::max(fabs(args[i]), max);
}
if (!max)
max = 1;
// Kahan summation algorithm significantly reduces the numerical error in the total obtained.
double sum = 0;
double compensation = 0;
for (double argument : args) {
double scaledArgument = argument / max;
double summand = scaledArgument * scaledArgument - compensation;
double preliminary = sum + summand;
compensation = (preliminary - sum) - summand;
sum = preliminary;
}
return JSValue::encode(jsDoubleNumber(sqrt(sum) * max));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncLog(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(log(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncMax(ExecState* exec)
{
unsigned argsCount = exec->argumentCount();
double result = -std::numeric_limits<double>::infinity();
for (unsigned k = 0; k < argsCount; ++k) {
double val = exec->uncheckedArgument(k).toNumber(exec);
if (std::isnan(val)) {
result = PNaN;
} else if (val > result || (!val && !result && !std::signbit(val)))
result = val;
}
return JSValue::encode(jsNumber(result));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncMin(ExecState* exec)
{
unsigned argsCount = exec->argumentCount();
double result = +std::numeric_limits<double>::infinity();
for (unsigned k = 0; k < argsCount; ++k) {
double val = exec->uncheckedArgument(k).toNumber(exec);
if (std::isnan(val)) {
result = PNaN;
} else if (val < result || (!val && !result && std::signbit(val)))
result = val;
}
return JSValue::encode(jsNumber(result));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncPow(ExecState* exec)
{
// ECMA 15.8.2.1.13
double arg = exec->argument(0).toNumber(exec);
double arg2 = exec->argument(1).toNumber(exec);
return JSValue::encode(JSValue(operationMathPow(arg, arg2)));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(exec->lexicalGlobalObject()->weakRandomNumber()));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncRound(ExecState* exec)
{
return JSValue::encode(jsNumber(jsRound(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncSign(ExecState* exec)
{
double arg = exec->argument(0).toNumber(exec);
if (std::isnan(arg))
return JSValue::encode(jsNaN());
if (!arg)
return JSValue::encode(std::signbit(arg) ? jsNumber(-0.0) : jsNumber(0));
return JSValue::encode(jsNumber(std::signbit(arg) ? -1 : 1));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncSin(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(sin(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(sqrt(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncTan(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(tan(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncIMul(ExecState* exec)
{
int32_t left = exec->argument(0).toInt32(exec);
if (exec->hadException())
return JSValue::encode(jsNull());
int32_t right = exec->argument(1).toInt32(exec);
return JSValue::encode(jsNumber(left * right));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncACosh(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(acosh(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncASinh(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(asinh(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncATanh(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(atanh(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncCbrt(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(cbrt(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncCosh(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(cosh(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncExpm1(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(expm1(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncFround(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(static_cast<float>(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncLog1p(ExecState* exec)
{
double value = exec->argument(0).toNumber(exec);
if (value == 0)
return JSValue::encode(jsDoubleNumber(value));
return JSValue::encode(jsDoubleNumber(log1p(value)));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncLog10(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(log10(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncLog2(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(log2(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncSinh(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(sinh(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncTanh(ExecState* exec)
{
return JSValue::encode(jsDoubleNumber(tanh(exec->argument(0).toNumber(exec))));
}
EncodedJSValue JSC_HOST_CALL mathProtoFuncTrunc(ExecState*exec)
{
return JSValue::encode(jsNumber(exec->argument(0).toIntegerPreserveNaN(exec)));
}
} // namespace JSC
|