blob: b4f93c9e53766e90cea247589d108d85eb2473b3 [file] [log] [blame]
kocienda66a6d362001-08-24 14:24:45 +00001/*
kocienda66a6d362001-08-24 14:24:45 +00002 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
mark.lam@apple.come1ab17c2016-09-26 19:11:17 +00003 * Copyright (C) 2007-2008, 2013, 2015-2016 Apple Inc. All Rights Reserved.
kocienda66a6d362001-08-24 14:24:45 +00004 *
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
mjscdff33b2006-01-23 21:41:36 +000017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
mjs6f821c82002-03-22 00:31:57 +000018 *
kocienda66a6d362001-08-24 14:24:45 +000019 */
20
mjsb64c50a2005-10-03 21:13:12 +000021#include "config.h"
darin@apple.com5c0863d2008-06-16 04:17:44 +000022#include "MathObject.h"
kocienda66a6d362001-08-24 14:24:45 +000023
fpizlo@apple.comafa9d882016-11-01 03:10:00 +000024#include "JSCInlines.h"
benjamin@webkit.org28b4f2d2015-02-18 08:01:01 +000025#include "MathCommon.h"
weinig@apple.comee8c7802008-06-28 21:40:47 +000026#include "ObjectPrototype.h"
darin36d11362006-04-11 16:30:21 +000027#include <time.h>
ggaren7213ee02007-10-16 23:25:33 +000028#include <wtf/Assertions.h>
darindeaf0592007-10-27 19:10:40 +000029#include <wtf/MathExtras.h>
commit-queue@webkit.org4510bff2014-03-18 03:21:22 +000030#include <wtf/Vector.h>
darin36d11362006-04-11 16:30:21 +000031
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000032namespace JSC {
kocienda66a6d362001-08-24 14:24:45 +000033
andersca@apple.com7de5aae2013-09-05 20:12:23 +000034STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(MathObject);
ggaren@apple.comfea43532008-08-17 20:23:49 +000035
dbatyai.u-szeged@partner.samsung.comb107a6e2014-10-21 10:18:13 +000036EncodedJSValue JSC_HOST_CALL mathProtoFuncACos(ExecState*);
37EncodedJSValue JSC_HOST_CALL mathProtoFuncACosh(ExecState*);
38EncodedJSValue JSC_HOST_CALL mathProtoFuncASin(ExecState*);
39EncodedJSValue JSC_HOST_CALL mathProtoFuncASinh(ExecState*);
40EncodedJSValue JSC_HOST_CALL mathProtoFuncATan(ExecState*);
41EncodedJSValue JSC_HOST_CALL mathProtoFuncATanh(ExecState*);
42EncodedJSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState*);
43EncodedJSValue JSC_HOST_CALL mathProtoFuncCbrt(ExecState*);
44EncodedJSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState*);
benjamin@webkit.orge324d432015-04-26 19:55:18 +000045EncodedJSValue JSC_HOST_CALL mathProtoFuncClz32(ExecState*);
dbatyai.u-szeged@partner.samsung.comb107a6e2014-10-21 10:18:13 +000046EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState*);
47EncodedJSValue JSC_HOST_CALL mathProtoFuncCosh(ExecState*);
48EncodedJSValue JSC_HOST_CALL mathProtoFuncExp(ExecState*);
49EncodedJSValue JSC_HOST_CALL mathProtoFuncExpm1(ExecState*);
dbatyai.u-szeged@partner.samsung.comb107a6e2014-10-21 10:18:13 +000050EncodedJSValue JSC_HOST_CALL mathProtoFuncFround(ExecState*);
51EncodedJSValue JSC_HOST_CALL mathProtoFuncHypot(ExecState*);
52EncodedJSValue JSC_HOST_CALL mathProtoFuncLog(ExecState*);
53EncodedJSValue JSC_HOST_CALL mathProtoFuncLog1p(ExecState*);
54EncodedJSValue JSC_HOST_CALL mathProtoFuncLog10(ExecState*);
55EncodedJSValue JSC_HOST_CALL mathProtoFuncLog2(ExecState*);
56EncodedJSValue JSC_HOST_CALL mathProtoFuncMax(ExecState*);
57EncodedJSValue JSC_HOST_CALL mathProtoFuncMin(ExecState*);
58EncodedJSValue JSC_HOST_CALL mathProtoFuncPow(ExecState*);
59EncodedJSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState*);
60EncodedJSValue JSC_HOST_CALL mathProtoFuncRound(ExecState*);
61EncodedJSValue JSC_HOST_CALL mathProtoFuncSign(ExecState*);
62EncodedJSValue JSC_HOST_CALL mathProtoFuncSin(ExecState*);
63EncodedJSValue JSC_HOST_CALL mathProtoFuncSinh(ExecState*);
64EncodedJSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState*);
65EncodedJSValue JSC_HOST_CALL mathProtoFuncTan(ExecState*);
66EncodedJSValue JSC_HOST_CALL mathProtoFuncTanh(ExecState*);
dbatyai.u-szeged@partner.samsung.comb107a6e2014-10-21 10:18:13 +000067EncodedJSValue JSC_HOST_CALL mathProtoFuncIMul(ExecState*);
darin@apple.com1edff432008-06-24 05:23:17 +000068
utatane.tea@gmail.coma5544f12017-05-19 09:23:20 +000069const ClassInfo MathObject::s_info = { "Math", &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(MathObject) };
mjs6f821c82002-03-22 00:31:57 +000070
akling@apple.com3828a862013-09-30 21:41:04 +000071MathObject::MathObject(VM& vm, Structure* structure)
72 : JSNonFinalObject(vm, structure)
kocienda66a6d362001-08-24 14:24:45 +000073{
commit-queue@webkit.org6e5671b2011-09-01 23:49:23 +000074}
75
akling@apple.com3828a862013-09-30 21:41:04 +000076void MathObject::finishCreation(VM& vm, JSGlobalObject* globalObject)
commit-queue@webkit.org6e5671b2011-09-01 23:49:23 +000077{
akling@apple.com3828a862013-09-30 21:41:04 +000078 Base::finishCreation(vm);
keith_miller@apple.com45da7602017-01-27 01:47:52 +000079 ASSERT(inherits(vm, info()));
barraclough@apple.com737a1582011-02-21 19:31:42 +000080
achristensen@apple.com5c6da6e2017-09-25 22:37:03 +000081 putDirectWithoutTransition(vm, Identifier::fromString(&vm, "E"), jsNumber(Math::exp(1.0)), PropertyAttribute::DontDelete | PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
82 putDirectWithoutTransition(vm, Identifier::fromString(&vm, "LN2"), jsNumber(Math::log(2.0)), PropertyAttribute::DontDelete | PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
83 putDirectWithoutTransition(vm, Identifier::fromString(&vm, "LN10"), jsNumber(Math::log(10.0)), PropertyAttribute::DontDelete | PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
84 putDirectWithoutTransition(vm, Identifier::fromString(&vm, "LOG2E"), jsNumber(1.0 / Math::log(2.0)), PropertyAttribute::DontDelete | PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
85 putDirectWithoutTransition(vm, Identifier::fromString(&vm, "LOG10E"), jsNumber(0.4342944819032518), PropertyAttribute::DontDelete | PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
86 putDirectWithoutTransition(vm, Identifier::fromString(&vm, "PI"), jsNumber(piDouble), PropertyAttribute::DontDelete | PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
87 putDirectWithoutTransition(vm, Identifier::fromString(&vm, "SQRT1_2"), jsNumber(sqrt(0.5)), PropertyAttribute::DontDelete | PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
88 putDirectWithoutTransition(vm, Identifier::fromString(&vm, "SQRT2"), jsNumber(sqrt(2.0)), PropertyAttribute::DontDelete | PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
89 putDirectWithoutTransition(vm, vm.propertyNames->toStringTagSymbol, jsString(&vm, "Math"), PropertyAttribute::DontEnum | PropertyAttribute::ReadOnly);
kocienda66a6d362001-08-24 14:24:45 +000090
achristensen@apple.com5c6da6e2017-09-25 22:37:03 +000091 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "abs"), 1, mathProtoFuncAbs, AbsIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
92 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "acos"), 1, mathProtoFuncACos, ACosIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
93 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "asin"), 1, mathProtoFuncASin, ASinIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
94 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "atan"), 1, mathProtoFuncATan, ATanIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
95 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "acosh"), 1, mathProtoFuncACosh, ACoshIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
96 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "asinh"), 1, mathProtoFuncASinh, ASinhIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
97 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "atanh"), 1, mathProtoFuncATanh, ATanhIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
98 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "atan2"), 2, mathProtoFuncATan2, NoIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
99 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "cbrt"), 1, mathProtoFuncCbrt, CbrtIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
100 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "ceil"), 1, mathProtoFuncCeil, CeilIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
101 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "clz32"), 1, mathProtoFuncClz32, Clz32Intrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
102 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "cos"), 1, mathProtoFuncCos, CosIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
103 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "cosh"), 1, mathProtoFuncCosh, CoshIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
104 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "exp"), 1, mathProtoFuncExp, ExpIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
105 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "expm1"), 1, mathProtoFuncExpm1, Expm1Intrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
106 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "floor"), 1, mathProtoFuncFloor, FloorIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
107 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "fround"), 1, mathProtoFuncFround, FRoundIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
108 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "hypot"), 2, mathProtoFuncHypot, NoIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
109 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "log"), 1, mathProtoFuncLog, LogIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
110 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "log10"), 1, mathProtoFuncLog10, Log10Intrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
111 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "log1p"), 1, mathProtoFuncLog1p, Log1pIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
112 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "log2"), 1, mathProtoFuncLog2, Log2Intrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
113 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "max"), 2, mathProtoFuncMax, MaxIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
114 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "min"), 2, mathProtoFuncMin, MinIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
115 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "pow"), 2, mathProtoFuncPow, PowIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
116 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "random"), 0, mathProtoFuncRandom, RandomIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
117 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "round"), 1, mathProtoFuncRound, RoundIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
118 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "sign"), 1, mathProtoFuncSign, NoIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
119 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "sin"), 1, mathProtoFuncSin, SinIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
120 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "sinh"), 1, mathProtoFuncSinh, SinhIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
121 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "sqrt"), 1, mathProtoFuncSqrt, SqrtIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
122 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "tan"), 1, mathProtoFuncTan, TanIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
123 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "tanh"), 1, mathProtoFuncTanh, TanhIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
124 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "trunc"), 1, mathProtoFuncTrunc, TruncIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
125 putDirectNativeFunctionWithoutTransition(vm, globalObject, Identifier::fromString(&vm, "imul"), 2, mathProtoFuncIMul, IMulIntrinsic, static_cast<unsigned>(PropertyAttribute::DontEnum));
kocienda66a6d362001-08-24 14:24:45 +0000126}
127
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000128// ------------------------------ Functions --------------------------------
129
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000130EncodedJSValue JSC_HOST_CALL mathProtoFuncAbs(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000131{
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000132 return JSValue::encode(jsNumber(fabs(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000133}
134
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000135EncodedJSValue JSC_HOST_CALL mathProtoFuncACos(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000136{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000137 return JSValue::encode(jsDoubleNumber(Math::acos(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000138}
139
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000140EncodedJSValue JSC_HOST_CALL mathProtoFuncASin(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000141{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000142 return JSValue::encode(jsDoubleNumber(Math::asin(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000143}
144
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000145EncodedJSValue JSC_HOST_CALL mathProtoFuncATan(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000146{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000147 return JSValue::encode(jsDoubleNumber(Math::atan(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000148}
149
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000150EncodedJSValue JSC_HOST_CALL mathProtoFuncATan2(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000151{
jfbastien@apple.com8dd8e5c2017-12-02 05:44:04 +0000152 VM& vm = exec->vm();
153 auto scope = DECLARE_THROW_SCOPE(vm);
commit-queue@webkit.org4bf477e2010-09-26 21:38:31 +0000154 double arg0 = exec->argument(0).toNumber(exec);
jfbastien@apple.com8dd8e5c2017-12-02 05:44:04 +0000155 RETURN_IF_EXCEPTION(scope, encodedJSValue());
jfbastien@apple.com762a3702017-12-05 07:51:33 +0000156 scope.release();
commit-queue@webkit.org4bf477e2010-09-26 21:38:31 +0000157 double arg1 = exec->argument(1).toNumber(exec);
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000158 return JSValue::encode(jsDoubleNumber(atan2(arg0, arg1)));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000159}
160
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000161EncodedJSValue JSC_HOST_CALL mathProtoFuncCeil(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000162{
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000163 return JSValue::encode(jsNumber(ceil(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000164}
165
benjamin@webkit.orge324d432015-04-26 19:55:18 +0000166EncodedJSValue JSC_HOST_CALL mathProtoFuncClz32(ExecState* exec)
167{
mark.lam@apple.com451de992016-09-07 22:10:50 +0000168 VM& vm = exec->vm();
169 auto scope = DECLARE_THROW_SCOPE(vm);
benjamin@webkit.orge324d432015-04-26 19:55:18 +0000170 uint32_t value = exec->argument(0).toUInt32(exec);
mark.lam@apple.come1ab17c2016-09-26 19:11:17 +0000171 RETURN_IF_EXCEPTION(scope, encodedJSValue());
benjamin@webkit.orge324d432015-04-26 19:55:18 +0000172 return JSValue::encode(JSValue(clz32(value)));
173}
174
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000175EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000176{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000177 return JSValue::encode(jsDoubleNumber(Math::cos(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000178}
179
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000180EncodedJSValue JSC_HOST_CALL mathProtoFuncExp(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000181{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000182 return JSValue::encode(jsDoubleNumber(Math::exp(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000183}
184
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000185EncodedJSValue JSC_HOST_CALL mathProtoFuncFloor(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000186{
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000187 return JSValue::encode(jsNumber(floor(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000188}
189
commit-queue@webkit.org4510bff2014-03-18 03:21:22 +0000190EncodedJSValue JSC_HOST_CALL mathProtoFuncHypot(ExecState* exec)
191{
mark.lam@apple.com451de992016-09-07 22:10:50 +0000192 VM& vm = exec->vm();
193 auto scope = DECLARE_THROW_SCOPE(vm);
commit-queue@webkit.org4510bff2014-03-18 03:21:22 +0000194 unsigned argsCount = exec->argumentCount();
195 double max = 0;
196 Vector<double, 8> args;
197 args.reserveInitialCapacity(argsCount);
198 for (unsigned i = 0; i < argsCount; ++i) {
199 args.uncheckedAppend(exec->uncheckedArgument(i).toNumber(exec));
mark.lam@apple.come1ab17c2016-09-26 19:11:17 +0000200 RETURN_IF_EXCEPTION(scope, encodedJSValue());
commit-queue@webkit.org4510bff2014-03-18 03:21:22 +0000201 if (std::isinf(args[i]))
202 return JSValue::encode(jsDoubleNumber(+std::numeric_limits<double>::infinity()));
203 max = std::max(fabs(args[i]), max);
204 }
205 if (!max)
206 max = 1;
207 // Kahan summation algorithm significantly reduces the numerical error in the total obtained.
208 double sum = 0;
209 double compensation = 0;
210 for (double argument : args) {
211 double scaledArgument = argument / max;
212 double summand = scaledArgument * scaledArgument - compensation;
213 double preliminary = sum + summand;
214 compensation = (preliminary - sum) - summand;
215 sum = preliminary;
216 }
217 return JSValue::encode(jsDoubleNumber(sqrt(sum) * max));
218}
219
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000220EncodedJSValue JSC_HOST_CALL mathProtoFuncLog(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000221{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000222 return JSValue::encode(jsDoubleNumber(Math::log(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000223}
224
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000225EncodedJSValue JSC_HOST_CALL mathProtoFuncMax(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000226{
jfbastien@apple.com8dd8e5c2017-12-02 05:44:04 +0000227 VM& vm = exec->vm();
228 auto scope = DECLARE_THROW_SCOPE(vm);
ggaren@apple.comfea29f12010-05-29 06:33:05 +0000229 unsigned argsCount = exec->argumentCount();
barraclough@apple.comf1fa5792011-06-11 02:03:00 +0000230 double result = -std::numeric_limits<double>::infinity();
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000231 for (unsigned k = 0; k < argsCount; ++k) {
darin@apple.comd9b22132013-09-22 04:02:59 +0000232 double val = exec->uncheckedArgument(k).toNumber(exec);
jfbastien@apple.com8dd8e5c2017-12-02 05:44:04 +0000233 RETURN_IF_EXCEPTION(scope, encodedJSValue());
zandobersek@gmail.com9182d472013-02-13 23:01:21 +0000234 if (std::isnan(val)) {
fpizlo@apple.combeef4522014-04-16 22:44:00 +0000235 result = PNaN;
commit-queue@webkit.orgb471d142014-02-27 19:39:42 +0000236 } else if (val > result || (!val && !result && !std::signbit(val)))
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000237 result = val;
238 }
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000239 return JSValue::encode(jsNumber(result));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000240}
241
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000242EncodedJSValue JSC_HOST_CALL mathProtoFuncMin(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000243{
jfbastien@apple.com8dd8e5c2017-12-02 05:44:04 +0000244 VM& vm = exec->vm();
245 auto scope = DECLARE_THROW_SCOPE(vm);
ggaren@apple.comfea29f12010-05-29 06:33:05 +0000246 unsigned argsCount = exec->argumentCount();
barraclough@apple.comf1fa5792011-06-11 02:03:00 +0000247 double result = +std::numeric_limits<double>::infinity();
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000248 for (unsigned k = 0; k < argsCount; ++k) {
darin@apple.comd9b22132013-09-22 04:02:59 +0000249 double val = exec->uncheckedArgument(k).toNumber(exec);
jfbastien@apple.com8dd8e5c2017-12-02 05:44:04 +0000250 RETURN_IF_EXCEPTION(scope, encodedJSValue());
zandobersek@gmail.com9182d472013-02-13 23:01:21 +0000251 if (std::isnan(val)) {
fpizlo@apple.combeef4522014-04-16 22:44:00 +0000252 result = PNaN;
commit-queue@webkit.orgb471d142014-02-27 19:39:42 +0000253 } else if (val < result || (!val && !result && std::signbit(val)))
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000254 result = val;
255 }
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000256 return JSValue::encode(jsNumber(result));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000257}
258
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000259EncodedJSValue JSC_HOST_CALL mathProtoFuncPow(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000260{
261 // ECMA 15.8.2.1.13
262
jfbastien@apple.com8dd8e5c2017-12-02 05:44:04 +0000263 VM& vm = exec->vm();
264 auto scope = DECLARE_THROW_SCOPE(vm);
265
ggaren@apple.comfea29f12010-05-29 06:33:05 +0000266 double arg = exec->argument(0).toNumber(exec);
jfbastien@apple.com8dd8e5c2017-12-02 05:44:04 +0000267 RETURN_IF_EXCEPTION(scope, encodedJSValue());
jfbastien@apple.com762a3702017-12-05 07:51:33 +0000268 scope.release();
ggaren@apple.comfea29f12010-05-29 06:33:05 +0000269 double arg2 = exec->argument(1).toNumber(exec);
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000270
benjamin@webkit.org903025b2015-02-14 04:20:21 +0000271 return JSValue::encode(JSValue(operationMathPow(arg, arg2)));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000272}
mjs6f821c82002-03-22 00:31:57 +0000273
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000274EncodedJSValue JSC_HOST_CALL mathProtoFuncRandom(ExecState* exec)
kocienda66a6d362001-08-24 14:24:45 +0000275{
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000276 return JSValue::encode(jsDoubleNumber(exec->lexicalGlobalObject()->weakRandomNumber()));
kocienda66a6d362001-08-24 14:24:45 +0000277}
278
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000279EncodedJSValue JSC_HOST_CALL mathProtoFuncRound(ExecState* exec)
mjs6f821c82002-03-22 00:31:57 +0000280{
benjamin@webkit.orgcb584082015-05-08 00:23:32 +0000281 return JSValue::encode(jsNumber(jsRound(exec->argument(0).toNumber(exec))));
kocienda66a6d362001-08-24 14:24:45 +0000282}
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000283
commit-queue@webkit.org8cc45232014-07-20 16:16:57 +0000284EncodedJSValue JSC_HOST_CALL mathProtoFuncSign(ExecState* exec)
285{
286 double arg = exec->argument(0).toNumber(exec);
287 if (std::isnan(arg))
288 return JSValue::encode(jsNaN());
289 if (!arg)
290 return JSValue::encode(std::signbit(arg) ? jsNumber(-0.0) : jsNumber(0));
291 return JSValue::encode(jsNumber(std::signbit(arg) ? -1 : 1));
292}
293
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000294EncodedJSValue JSC_HOST_CALL mathProtoFuncSin(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000295{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000296 return JSValue::encode(jsDoubleNumber(Math::sin(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000297}
298
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000299EncodedJSValue JSC_HOST_CALL mathProtoFuncSqrt(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000300{
oliver@apple.com5b67d9e2010-10-25 22:40:53 +0000301 return JSValue::encode(jsDoubleNumber(sqrt(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000302}
303
barraclough@apple.com99ff3432010-06-03 20:00:18 +0000304EncodedJSValue JSC_HOST_CALL mathProtoFuncTan(ExecState* exec)
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000305{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000306 return JSValue::encode(jsDoubleNumber(Math::tan(exec->argument(0).toNumber(exec))));
weinig@apple.coma73c15f2007-11-08 20:31:26 +0000307}
308
oliver@apple.com64367322013-04-26 00:41:38 +0000309EncodedJSValue JSC_HOST_CALL mathProtoFuncIMul(ExecState* exec)
310{
mark.lam@apple.com451de992016-09-07 22:10:50 +0000311 VM& vm = exec->vm();
312 auto scope = DECLARE_THROW_SCOPE(vm);
oliver@apple.com64367322013-04-26 00:41:38 +0000313 int32_t left = exec->argument(0).toInt32(exec);
mark.lam@apple.come1ab17c2016-09-26 19:11:17 +0000314 RETURN_IF_EXCEPTION(scope, encodedJSValue());
mark.lam@apple.com223e1f82016-11-28 22:21:29 +0000315 scope.release();
oliver@apple.com64367322013-04-26 00:41:38 +0000316 int32_t right = exec->argument(1).toInt32(exec);
317 return JSValue::encode(jsNumber(left * right));
318}
319
oliver@apple.com975f2142013-10-31 22:38:05 +0000320EncodedJSValue JSC_HOST_CALL mathProtoFuncACosh(ExecState* exec)
321{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000322 return JSValue::encode(jsDoubleNumber(Math::acosh(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000323}
324
325EncodedJSValue JSC_HOST_CALL mathProtoFuncASinh(ExecState* exec)
326{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000327 return JSValue::encode(jsDoubleNumber(Math::asinh(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000328}
329
330EncodedJSValue JSC_HOST_CALL mathProtoFuncATanh(ExecState* exec)
331{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000332 return JSValue::encode(jsDoubleNumber(Math::atanh(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000333}
334
335EncodedJSValue JSC_HOST_CALL mathProtoFuncCbrt(ExecState* exec)
336{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000337 return JSValue::encode(jsDoubleNumber(Math::cbrt(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000338}
339
340EncodedJSValue JSC_HOST_CALL mathProtoFuncCosh(ExecState* exec)
341{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000342 return JSValue::encode(jsDoubleNumber(Math::cosh(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000343}
344
345EncodedJSValue JSC_HOST_CALL mathProtoFuncExpm1(ExecState* exec)
346{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000347 return JSValue::encode(jsDoubleNumber(Math::expm1(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000348}
349
350EncodedJSValue JSC_HOST_CALL mathProtoFuncFround(ExecState* exec)
351{
352 return JSValue::encode(jsDoubleNumber(static_cast<float>(exec->argument(0).toNumber(exec))));
353}
354
355EncodedJSValue JSC_HOST_CALL mathProtoFuncLog1p(ExecState* exec)
356{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000357 return JSValue::encode(jsDoubleNumber(Math::log1p(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000358}
359
360EncodedJSValue JSC_HOST_CALL mathProtoFuncLog10(ExecState* exec)
361{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000362 return JSValue::encode(jsDoubleNumber(Math::log10(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000363}
364
365EncodedJSValue JSC_HOST_CALL mathProtoFuncLog2(ExecState* exec)
366{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000367 return JSValue::encode(jsDoubleNumber(Math::log2(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000368}
369
370EncodedJSValue JSC_HOST_CALL mathProtoFuncSinh(ExecState* exec)
371{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000372 return JSValue::encode(jsDoubleNumber(Math::sinh(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000373}
374
375EncodedJSValue JSC_HOST_CALL mathProtoFuncTanh(ExecState* exec)
376{
utatane.tea@gmail.comccb74992017-05-04 11:40:46 +0000377 return JSValue::encode(jsDoubleNumber(Math::tanh(exec->argument(0).toNumber(exec))));
oliver@apple.com975f2142013-10-31 22:38:05 +0000378}
379
380EncodedJSValue JSC_HOST_CALL mathProtoFuncTrunc(ExecState*exec)
381{
382 return JSValue::encode(jsNumber(exec->argument(0).toIntegerPreserveNaN(exec)));
383}
384
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +0000385} // namespace JSC