2 * Copyright (C) 1999-2000,2003 Harri Porten (porten@kde.org)
3 * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
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.
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.
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
23 #include "NumberConstructor.h"
26 #include "NumberObject.h"
27 #include "NumberPrototype.h"
28 #include "JSCInlines.h"
29 #include "JSGlobalObjectFunctions.h"
33 static EncodedJSValue JSC_HOST_CALL
numberConstructorFuncIsFinite(ExecState
*);
34 static EncodedJSValue JSC_HOST_CALL
numberConstructorFuncIsInteger(ExecState
*);
35 static EncodedJSValue JSC_HOST_CALL
numberConstructorFuncIsNaN(ExecState
*);
36 static EncodedJSValue JSC_HOST_CALL
numberConstructorFuncIsSafeInteger(ExecState
*);
42 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(NumberConstructor
);
44 const ClassInfo
NumberConstructor::s_info
= { "Function", &InternalFunction::s_info
, 0, CREATE_METHOD_TABLE(NumberConstructor
) };
46 NumberConstructor::NumberConstructor(VM
& vm
, Structure
* structure
)
47 : InternalFunction(vm
, structure
)
51 void NumberConstructor::finishCreation(VM
& vm
, NumberPrototype
* numberPrototype
)
53 Base::finishCreation(vm
, NumberPrototype::info()->className
);
54 ASSERT(inherits(info()));
57 putDirectWithoutTransition(vm
, vm
.propertyNames
->prototype
, numberPrototype
, DontEnum
| DontDelete
| ReadOnly
);
59 // no. of arguments for constructor
60 putDirectWithoutTransition(vm
, vm
.propertyNames
->length
, jsNumber(1), ReadOnly
| DontEnum
| DontDelete
);
62 putDirectWithoutTransition(vm
, Identifier::fromString(&vm
, "EPSILON"), jsDoubleNumber(std::numeric_limits
<double>::epsilon()), DontDelete
| DontEnum
| ReadOnly
);
63 putDirectWithoutTransition(vm
, Identifier::fromString(&vm
, "MAX_VALUE"), jsDoubleNumber(1.7976931348623157E+308), DontDelete
| DontEnum
| ReadOnly
);
64 putDirectWithoutTransition(vm
, Identifier::fromString(&vm
, "MIN_VALUE"), jsDoubleNumber(5E-324), DontDelete
| DontEnum
| ReadOnly
);
65 putDirectWithoutTransition(vm
, Identifier::fromString(&vm
, "MAX_SAFE_INTEGER"), jsDoubleNumber(9007199254740991.0), DontDelete
| DontEnum
| ReadOnly
);
66 putDirectWithoutTransition(vm
, Identifier::fromString(&vm
, "MIN_SAFE_INTEGER"), jsDoubleNumber(-9007199254740991.0), DontDelete
| DontEnum
| ReadOnly
);
67 putDirectWithoutTransition(vm
, Identifier::fromString(&vm
, "NEGATIVE_INFINITY"), jsDoubleNumber(-std::numeric_limits
<double>::infinity()), DontDelete
| DontEnum
| ReadOnly
);
68 putDirectWithoutTransition(vm
, Identifier::fromString(&vm
, "POSITIVE_INFINITY"), jsDoubleNumber(std::numeric_limits
<double>::infinity()), DontDelete
| DontEnum
| ReadOnly
);
69 putDirectWithoutTransition(vm
, Identifier::fromString(&vm
, "NaN"), jsNaN(), DontDelete
| DontEnum
| ReadOnly
);
71 putDirectNativeFunctionWithoutTransition(vm
, numberPrototype
->globalObject(), Identifier::fromString(&vm
, "isFinite"), 1, numberConstructorFuncIsFinite
, NoIntrinsic
, DontEnum
| Function
);
72 putDirectNativeFunctionWithoutTransition(vm
, numberPrototype
->globalObject(), Identifier::fromString(&vm
, "isInteger"), 1, numberConstructorFuncIsInteger
, NoIntrinsic
, DontEnum
| Function
);
73 putDirectNativeFunctionWithoutTransition(vm
, numberPrototype
->globalObject(), Identifier::fromString(&vm
, "isNaN"), 1, numberConstructorFuncIsNaN
, NoIntrinsic
, DontEnum
| Function
);
74 putDirectNativeFunctionWithoutTransition(vm
, numberPrototype
->globalObject(), Identifier::fromString(&vm
, "isSafeInteger"), 1, numberConstructorFuncIsSafeInteger
, NoIntrinsic
, DontEnum
| Function
);
75 putDirectNativeFunctionWithoutTransition(vm
, numberPrototype
->globalObject(), Identifier::fromString(&vm
, "parseFloat"), 1, globalFuncParseFloat
, NoIntrinsic
, DontEnum
| Function
);
76 putDirectWithoutTransition(vm
, Identifier::fromString(&vm
, "parseInt"), numberPrototype
->globalObject()->parseIntFunction(), DontEnum
| Function
);
80 static EncodedJSValue JSC_HOST_CALL
constructWithNumberConstructor(ExecState
* exec
)
82 NumberObject
* object
= NumberObject::create(exec
->vm(), asInternalFunction(exec
->callee())->globalObject()->numberObjectStructure());
83 double n
= exec
->argumentCount() ? exec
->uncheckedArgument(0).toNumber(exec
) : 0;
84 object
->setInternalValue(exec
->vm(), jsNumber(n
));
85 return JSValue::encode(object
);
88 ConstructType
NumberConstructor::getConstructData(JSCell
*, ConstructData
& constructData
)
90 constructData
.native
.function
= constructWithNumberConstructor
;
91 return ConstructTypeHost
;
95 static EncodedJSValue JSC_HOST_CALL
callNumberConstructor(ExecState
* exec
)
97 return JSValue::encode(jsNumber(!exec
->argumentCount() ? 0 : exec
->uncheckedArgument(0).toNumber(exec
)));
100 CallType
NumberConstructor::getCallData(JSCell
*, CallData
& callData
)
102 callData
.native
.function
= callNumberConstructor
;
107 static EncodedJSValue JSC_HOST_CALL
numberConstructorFuncIsFinite(ExecState
* exec
)
109 JSValue argument
= exec
->argument(0);
110 return JSValue::encode(jsBoolean(argument
.isNumber() && (argument
.isInt32() || std::isfinite(argument
.asDouble()))));
114 static EncodedJSValue JSC_HOST_CALL
numberConstructorFuncIsInteger(ExecState
* exec
)
116 JSValue argument
= exec
->argument(0);
118 if (argument
.isInt32())
120 else if (!argument
.isDouble())
123 double number
= argument
.asDouble();
124 isInteger
= std::isfinite(number
) && trunc(number
) == number
;
126 return JSValue::encode(jsBoolean(isInteger
));
130 static EncodedJSValue JSC_HOST_CALL
numberConstructorFuncIsNaN(ExecState
* exec
)
132 JSValue argument
= exec
->argument(0);
133 return JSValue::encode(jsBoolean(argument
.isDouble() && std::isnan(argument
.asDouble())));
137 static EncodedJSValue JSC_HOST_CALL
numberConstructorFuncIsSafeInteger(ExecState
* exec
)
139 JSValue argument
= exec
->argument(0);
141 if (argument
.isInt32())
143 else if (!argument
.isDouble())
146 double number
= argument
.asDouble();
147 isInteger
= trunc(number
) == number
&& std::abs(number
) <= 9007199254740991.0;
149 return JSValue::encode(jsBoolean(isInteger
));