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