]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/NumberConstructor.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / NumberConstructor.cpp
1 /*
2 * Copyright (C) 1999-2000,2003 Harri Porten (porten@kde.org)
3 * Copyright (C) 2007, 2008, 2011 Apple Inc. All rights reserved.
4 *
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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18 * USA
19 *
20 */
21
22 #include "config.h"
23 #include "NumberConstructor.h"
24
25 #include "Lookup.h"
26 #include "NumberObject.h"
27 #include "NumberPrototype.h"
28 #include "JSCInlines.h"
29 #include "JSGlobalObjectFunctions.h"
30
31 namespace JSC {
32
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*);
37
38 } // namespace JSC
39
40 namespace JSC {
41
42 STATIC_ASSERT_IS_TRIVIALLY_DESTRUCTIBLE(NumberConstructor);
43
44 const ClassInfo NumberConstructor::s_info = { "Function", &InternalFunction::s_info, 0, CREATE_METHOD_TABLE(NumberConstructor) };
45
46 NumberConstructor::NumberConstructor(VM& vm, Structure* structure)
47 : InternalFunction(vm, structure)
48 {
49 }
50
51 void NumberConstructor::finishCreation(VM& vm, NumberPrototype* numberPrototype)
52 {
53 Base::finishCreation(vm, NumberPrototype::info()->className);
54 ASSERT(inherits(info()));
55
56 // Number.Prototype
57 putDirectWithoutTransition(vm, vm.propertyNames->prototype, numberPrototype, DontEnum | DontDelete | ReadOnly);
58
59 // no. of arguments for constructor
60 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
61
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);
70
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);
77 }
78
79 // ECMA 15.7.1
80 static EncodedJSValue JSC_HOST_CALL constructWithNumberConstructor(ExecState* exec)
81 {
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);
86 }
87
88 ConstructType NumberConstructor::getConstructData(JSCell*, ConstructData& constructData)
89 {
90 constructData.native.function = constructWithNumberConstructor;
91 return ConstructTypeHost;
92 }
93
94 // ECMA 15.7.2
95 static EncodedJSValue JSC_HOST_CALL callNumberConstructor(ExecState* exec)
96 {
97 return JSValue::encode(jsNumber(!exec->argumentCount() ? 0 : exec->uncheckedArgument(0).toNumber(exec)));
98 }
99
100 CallType NumberConstructor::getCallData(JSCell*, CallData& callData)
101 {
102 callData.native.function = callNumberConstructor;
103 return CallTypeHost;
104 }
105
106 // ECMA-262 20.1.2.2
107 static EncodedJSValue JSC_HOST_CALL numberConstructorFuncIsFinite(ExecState* exec)
108 {
109 JSValue argument = exec->argument(0);
110 return JSValue::encode(jsBoolean(argument.isNumber() && (argument.isInt32() || std::isfinite(argument.asDouble()))));
111 }
112
113 // ECMA-262 20.1.2.3
114 static EncodedJSValue JSC_HOST_CALL numberConstructorFuncIsInteger(ExecState* exec)
115 {
116 JSValue argument = exec->argument(0);
117 bool isInteger;
118 if (argument.isInt32())
119 isInteger = true;
120 else if (!argument.isDouble())
121 isInteger = false;
122 else {
123 double number = argument.asDouble();
124 isInteger = std::isfinite(number) && trunc(number) == number;
125 }
126 return JSValue::encode(jsBoolean(isInteger));
127 }
128
129 // ECMA-262 20.1.2.4
130 static EncodedJSValue JSC_HOST_CALL numberConstructorFuncIsNaN(ExecState* exec)
131 {
132 JSValue argument = exec->argument(0);
133 return JSValue::encode(jsBoolean(argument.isDouble() && std::isnan(argument.asDouble())));
134 }
135
136 // ECMA-262 20.1.2.5
137 static EncodedJSValue JSC_HOST_CALL numberConstructorFuncIsSafeInteger(ExecState* exec)
138 {
139 JSValue argument = exec->argument(0);
140 bool isInteger;
141 if (argument.isInt32())
142 isInteger = true;
143 else if (!argument.isDouble())
144 isInteger = false;
145 else {
146 double number = argument.asDouble();
147 isInteger = trunc(number) == number && std::abs(number) <= 9007199254740991.0;
148 }
149 return JSValue::encode(jsBoolean(isInteger));
150 }
151
152 } // namespace JSC