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