#include "config.h"
#include "math_object.h"
#include "math_object.lut.h"
#include "wtf/MathExtras.h"
#include "operations.h"
#include <math.h>
#include <time.h>
using namespace KJS;
const ClassInfo MathObjectImp::info = { "Math", 0, &mathTable, 0 };
MathObjectImp::MathObjectImp(ExecState * ,
ObjectPrototype *objProto)
: JSObject(objProto)
{
}
bool MathObjectImp::getOwnPropertySlot(ExecState *exec, const Identifier& propertyName, PropertySlot &slot)
{
return getStaticPropertySlot<MathFuncImp, MathObjectImp, JSObject>(exec, &mathTable, this, propertyName, slot);
}
JSValue *MathObjectImp::getValueProperty(ExecState *, int token) const
{
double d = -42; switch (token) {
case Euler:
d = exp(1.0);
break;
case Ln2:
d = log(2.0);
break;
case Ln10:
d = log(10.0);
break;
case Log2E:
d = 1.0/log(2.0);
break;
case Log10E:
d = 1.0/log(10.0);
break;
case Pi:
d = piDouble;
break;
case Sqrt1_2:
d = sqrt(0.5);
break;
case Sqrt2:
d = sqrt(2.0);
break;
default:
assert(0);
}
return jsNumber(d);
}
static bool didInitRandom;
MathFuncImp::MathFuncImp(ExecState* exec, int i, int l, const Identifier& name)
: InternalFunctionImp(static_cast<FunctionPrototype*>(exec->lexicalInterpreter()->builtinFunctionPrototype()), name)
, id(i)
{
putDirect(exec->propertyNames().length, l, DontDelete|ReadOnly|DontEnum);
}
JSValue *MathFuncImp::callAsFunction(ExecState *exec, JSObject* , const List &args)
{
double arg = args[0]->toNumber(exec);
double arg2 = args[1]->toNumber(exec);
double result;
switch (id) {
case MathObjectImp::Abs:
result = ( arg < 0 || arg == -0) ? (-arg) : arg;
break;
case MathObjectImp::ACos:
result = acos(arg);
break;
case MathObjectImp::ASin:
result = asin(arg);
break;
case MathObjectImp::ATan:
result = atan(arg);
break;
case MathObjectImp::ATan2:
result = atan2(arg, arg2);
break;
case MathObjectImp::Ceil:
result = ceil(arg);
break;
case MathObjectImp::Cos:
result = cos(arg);
break;
case MathObjectImp::Exp:
result = exp(arg);
break;
case MathObjectImp::Floor:
result = floor(arg);
break;
case MathObjectImp::Log:
result = log(arg);
break;
case MathObjectImp::Max: {
unsigned int argsCount = args.size();
result = -Inf;
for ( unsigned int k = 0 ; k < argsCount ; ++k ) {
double val = args[k]->toNumber(exec);
if ( isNaN( val ) )
{
result = NaN;
break;
}
if ( val > result || (val == 0 && result == 0 && !signbit(val)) )
result = val;
}
break;
}
case MathObjectImp::Min: {
unsigned int argsCount = args.size();
result = +Inf;
for ( unsigned int k = 0 ; k < argsCount ; ++k ) {
double val = args[k]->toNumber(exec);
if ( isNaN( val ) )
{
result = NaN;
break;
}
if ( val < result || (val == 0 && result == 0 && signbit(val)) )
result = val;
}
break;
}
case MathObjectImp::Pow:
if (isNaN(arg2))
result = NaN;
else if (isNaN(arg) && arg2 != 0)
result = NaN;
else if (fabs(arg) == 1 && isInf(arg2))
result = NaN;
else if (arg2 == 0 && arg != 0)
result = 1;
else
result = ::pow(arg, arg2);
break;
case MathObjectImp::Random:
if (!didInitRandom) {
wtf_random_init();
didInitRandom = true;
}
result = wtf_random();
break;
case MathObjectImp::Round:
if (signbit(arg) && arg >= -0.5)
result = -0.0;
else
result = floor(arg + 0.5);
break;
case MathObjectImp::Sin:
result = sin(arg);
break;
case MathObjectImp::Sqrt:
result = sqrt(arg);
break;
case MathObjectImp::Tan:
result = tan(arg);
break;
default:
result = 0.0;
assert(0);
}
return jsNumber(result);
}