31 #include "interpreter.h"
32 #include "operations.h"
33 #include "math_object.h"
35 #include "math_object.lut.h"
38 #define M_PI 3.14159265358979323846
42 #define signbit(x) ((x) < 0.0 || IS_NEGATIVE_ZERO(x))
49 const ClassInfo MathObjectImp::info = {
"Math", 0, &mathTable, 0 };
82 MathObjectImp::MathObjectImp(
ExecState * ,
83 ObjectPrototypeImp *objProto)
86 unsigned int seed = time(NULL);
93 return lookupGet<MathFuncImp, MathObjectImp, ObjectImp>( exec, propertyName, &mathTable,
this );
96 Value MathObjectImp::getValueProperty(
ExecState *,
int token)
const
125 fprintf( stderr,
"[math_object] Internal error in MathObjectImp: unhandled token %d\n", token );
134 MathFuncImp::MathFuncImp(
ExecState *exec,
int i,
int l)
136 static_cast<
FunctionPrototypeImp*>(exec->lexicalInterpreter()->builtinFunctionPrototype().imp())
140 putDirect(lengthPropertyName, l, DontDelete|ReadOnly|DontEnum);
143 bool MathFuncImp::implementsCall()
const
150 double arg = args[0].toNumber(exec);
151 double arg2 = args[1].toNumber(exec);
155 case MathObjectImp::Abs:
156 result = ( arg < 0 || arg == -0) ? (-arg) : arg;
158 case MathObjectImp::ACos:
159 result = ::acos(arg);
161 case MathObjectImp::ASin:
162 result = ::asin(arg);
164 case MathObjectImp::ATan:
165 result = ::atan(arg);
167 case MathObjectImp::ATan2:
168 result = ::atan2(arg, arg2);
170 case MathObjectImp::Ceil:
171 result = ::ceil(arg);
173 case MathObjectImp::Cos:
176 case MathObjectImp::Exp:
179 case MathObjectImp::Floor:
180 result = ::floor(arg);
182 case MathObjectImp::Log:
185 case MathObjectImp::Max: {
186 unsigned int argsCount = args.
size();
188 for (
unsigned int k = 0 ; k < argsCount ; ++k ) {
189 double val = args[k].toNumber(exec);
195 if ( val > result || (val == 0 && result == 0 && !signbit(val)) )
200 case MathObjectImp::Min: {
201 unsigned int argsCount = args.
size();
203 for (
unsigned int k = 0 ; k < argsCount ; ++k ) {
204 double val = args[k].toNumber(exec);
210 if ( val < result || (val == 0 && result == 0 && signbit(val)) )
215 case MathObjectImp::Pow:
217 if (KJS::isNaN(arg2))
219 #ifndef APPLE_CHANGES
223 else if (KJS::isNaN(arg) && arg2 != 0)
225 #ifndef APPLE_CHANGES
226 else if (::fabs(arg) > 1 && KJS::isPosInf(arg2))
228 else if (::fabs(arg) > 1 && KJS::isNegInf(arg2))
231 else if (::fabs(arg) == 1 && KJS::isInf(arg2))
233 #ifndef APPLE_CHANGES
234 else if (::fabs(arg) < 1 && KJS::isPosInf(arg2))
236 else if (::fabs(arg) < 1 && KJS::isNegInf(arg2))
240 result = ::pow(arg, arg2);
242 case MathObjectImp::Random:
244 result = result / RAND_MAX;
246 case MathObjectImp::Round:
247 if (signbit(arg) && arg >= -0.5)
250 result = ::floor(arg + 0.5);
252 case MathObjectImp::Sin:
255 case MathObjectImp::Sqrt:
256 result = ::sqrt(arg);
258 case MathObjectImp::Tan:
Represents the current state of script execution.
The initial value of Function.prototype (and thus all objects created with the Function constructor)
Represents an Identifier for a Javascript object.
Base class for all function objects.
Represents an primitive Number value.
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.