]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/NumberConstructor.cpp
JavaScriptCore-1218.34.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 "Operations.h"
29
30 namespace JSC {
31
32 static JSValue numberConstructorNaNValue(ExecState*, JSValue, PropertyName);
33 static JSValue numberConstructorNegInfinity(ExecState*, JSValue, PropertyName);
34 static JSValue numberConstructorPosInfinity(ExecState*, JSValue, PropertyName);
35 static JSValue numberConstructorMaxValue(ExecState*, JSValue, PropertyName);
36 static JSValue numberConstructorMinValue(ExecState*, JSValue, PropertyName);
37
38 } // namespace JSC
39
40 #include "NumberConstructor.lut.h"
41
42 namespace JSC {
43
44 ASSERT_HAS_TRIVIAL_DESTRUCTOR(NumberConstructor);
45
46 const ClassInfo NumberConstructor::s_info = { "Function", &InternalFunction::s_info, 0, ExecState::numberConstructorTable, CREATE_METHOD_TABLE(NumberConstructor) };
47
48 /* Source for NumberConstructor.lut.h
49 @begin numberConstructorTable
50 NaN numberConstructorNaNValue DontEnum|DontDelete|ReadOnly
51 NEGATIVE_INFINITY numberConstructorNegInfinity DontEnum|DontDelete|ReadOnly
52 POSITIVE_INFINITY numberConstructorPosInfinity DontEnum|DontDelete|ReadOnly
53 MAX_VALUE numberConstructorMaxValue DontEnum|DontDelete|ReadOnly
54 MIN_VALUE numberConstructorMinValue DontEnum|DontDelete|ReadOnly
55 @end
56 */
57
58 NumberConstructor::NumberConstructor(JSGlobalObject* globalObject, Structure* structure)
59 : InternalFunction(globalObject, structure)
60 {
61 }
62
63 void NumberConstructor::finishCreation(ExecState* exec, NumberPrototype* numberPrototype)
64 {
65 Base::finishCreation(exec->vm(), numberPrototype->s_info.className);
66 ASSERT(inherits(&s_info));
67
68 // Number.Prototype
69 putDirectWithoutTransition(exec->vm(), exec->propertyNames().prototype, numberPrototype, DontEnum | DontDelete | ReadOnly);
70
71 // no. of arguments for constructor
72 putDirectWithoutTransition(exec->vm(), exec->propertyNames().length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
73 }
74
75 bool NumberConstructor::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
76 {
77 return getStaticValueSlot<NumberConstructor, InternalFunction>(exec, ExecState::numberConstructorTable(exec), jsCast<NumberConstructor*>(cell), propertyName, slot);
78 }
79
80 bool NumberConstructor::getOwnPropertyDescriptor(JSObject* object, ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
81 {
82 return getStaticValueDescriptor<NumberConstructor, InternalFunction>(exec, ExecState::numberConstructorTable(exec), jsCast<NumberConstructor*>(object), propertyName, descriptor);
83 }
84
85 void NumberConstructor::put(JSCell* cell, ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot)
86 {
87 lookupPut<NumberConstructor, InternalFunction>(exec, propertyName, value, ExecState::numberConstructorTable(exec), jsCast<NumberConstructor*>(cell), slot);
88 }
89
90 static JSValue numberConstructorNaNValue(ExecState*, JSValue, PropertyName)
91 {
92 return jsNaN();
93 }
94
95 static JSValue numberConstructorNegInfinity(ExecState*, JSValue, PropertyName)
96 {
97 return jsNumber(-std::numeric_limits<double>::infinity());
98 }
99
100 static JSValue numberConstructorPosInfinity(ExecState*, JSValue, PropertyName)
101 {
102 return jsNumber(std::numeric_limits<double>::infinity());
103 }
104
105 static JSValue numberConstructorMaxValue(ExecState*, JSValue, PropertyName)
106 {
107 return jsNumber(1.7976931348623157E+308);
108 }
109
110 static JSValue numberConstructorMinValue(ExecState*, JSValue, PropertyName)
111 {
112 return jsNumber(5E-324);
113 }
114
115 // ECMA 15.7.1
116 static EncodedJSValue JSC_HOST_CALL constructWithNumberConstructor(ExecState* exec)
117 {
118 NumberObject* object = NumberObject::create(exec->vm(), asInternalFunction(exec->callee())->globalObject()->numberObjectStructure());
119 double n = exec->argumentCount() ? exec->argument(0).toNumber(exec) : 0;
120 object->setInternalValue(exec->vm(), jsNumber(n));
121 return JSValue::encode(object);
122 }
123
124 ConstructType NumberConstructor::getConstructData(JSCell*, ConstructData& constructData)
125 {
126 constructData.native.function = constructWithNumberConstructor;
127 return ConstructTypeHost;
128 }
129
130 // ECMA 15.7.2
131 static EncodedJSValue JSC_HOST_CALL callNumberConstructor(ExecState* exec)
132 {
133 return JSValue::encode(jsNumber(!exec->argumentCount() ? 0 : exec->argument(0).toNumber(exec)));
134 }
135
136 CallType NumberConstructor::getCallData(JSCell*, CallData& callData)
137 {
138 callData.native.function = callNumberConstructor;
139 return CallTypeHost;
140 }
141
142 } // namespace JSC