2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
4 * Copyright (C) 2003, 2004, 2005, 2007, 2008 Apple Inc. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #ifndef JSNumberCell_h
24 #define JSNumberCell_h
26 #include "CallFrame.h"
28 #include "JSImmediate.h"
29 #include "Collector.h"
31 #include <stddef.h> // for size_t
44 class JSNumberCell : public JSCell {
46 friend JSValue* jsNumberCell(JSGlobalData*, double);
47 friend JSValue* jsNaN(JSGlobalData*);
48 friend JSValue* jsNumberCell(ExecState*, double);
49 friend JSValue* jsNaN(ExecState*);
51 double value() const { return m_value; }
53 virtual JSValue* toPrimitive(ExecState*, PreferredPrimitiveType) const;
54 virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
55 virtual bool toBoolean(ExecState*) const;
56 virtual double toNumber(ExecState*) const;
57 virtual UString toString(ExecState*) const;
58 virtual JSObject* toObject(ExecState*) const;
60 virtual UString toThisString(ExecState*) const;
61 virtual JSObject* toThisObject(ExecState*) const;
62 virtual JSValue* getJSNumber();
64 int32_t toInt32() const;
65 uint32_t toUInt32() const;
67 void* operator new(size_t size, ExecState* exec)
69 #ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
70 return exec->heap()->inlineAllocateNumber(size);
72 return exec->heap()->allocateNumber(size);
76 void* operator new(size_t size, JSGlobalData* globalData)
78 #ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
79 return globalData->heap.inlineAllocateNumber(size);
81 return globalData->heap.allocateNumber(size);
85 static PassRefPtr<Structure> createStructure(JSValue* proto) { return Structure::create(proto, TypeInfo(NumberType, NeedsThisConversion)); }
87 JSNumberCell(JSGlobalData* globalData)
88 : JSCell(globalData->numberStructure.get())
93 JSNumberCell(JSGlobalData* globalData, double value)
94 : JSCell(globalData->numberStructure.get())
99 JSNumberCell(ExecState* exec, double value)
100 : JSCell(exec->globalData().numberStructure.get())
105 virtual bool getUInt32(uint32_t&) const;
106 virtual bool getTruncatedInt32(int32_t&) const;
107 virtual bool getTruncatedUInt32(uint32_t&) const;
112 extern const double NaN;
113 extern const double Inf;
115 JSNumberCell* asNumberCell(JSValue*);
117 JSValue* jsNumberCell(JSGlobalData*, double);
118 JSValue* jsNaN(JSGlobalData*);
119 JSValue* jsNumberCell(ExecState*, double);
120 JSValue* jsNaN(ExecState*);
122 inline JSNumberCell* asNumberCell(JSValue* value)
124 ASSERT(asCell(value)->isNumber());
125 return static_cast<JSNumberCell*>(asCell(value));
128 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, double d)
130 JSValue* v = JSImmediate::from(d);
131 return v ? v : jsNumberCell(exec, d);
134 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, short i)
136 JSValue* v = JSImmediate::from(i);
137 return v ? v : jsNumberCell(exec, i);
140 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned short i)
142 JSValue* v = JSImmediate::from(i);
143 return v ? v : jsNumberCell(exec, i);
146 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, int i)
148 JSValue* v = JSImmediate::from(i);
149 return v ? v : jsNumberCell(exec, i);
152 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned i)
154 JSValue* v = JSImmediate::from(i);
155 return v ? v : jsNumberCell(exec, i);
158 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, long i)
160 JSValue* v = JSImmediate::from(i);
161 return v ? v : jsNumberCell(exec, i);
164 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned long i)
166 JSValue* v = JSImmediate::from(i);
167 return v ? v : jsNumberCell(exec, i);
170 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, long long i)
172 JSValue* v = JSImmediate::from(i);
173 return v ? v : jsNumberCell(exec, static_cast<double>(i));
176 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned long long i)
178 JSValue* v = JSImmediate::from(i);
179 return v ? v : jsNumberCell(exec, static_cast<double>(i));
182 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, double d)
184 JSValue* v = JSImmediate::from(d);
185 return v ? v : jsNumberCell(globalData, d);
188 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, short i)
190 JSValue* v = JSImmediate::from(i);
191 return v ? v : jsNumberCell(globalData, i);
194 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, unsigned short i)
196 JSValue* v = JSImmediate::from(i);
197 return v ? v : jsNumberCell(globalData, i);
200 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, int i)
202 JSValue* v = JSImmediate::from(i);
203 return v ? v : jsNumberCell(globalData, i);
206 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, unsigned i)
208 JSValue* v = JSImmediate::from(i);
209 return v ? v : jsNumberCell(globalData, i);
212 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, long i)
214 JSValue* v = JSImmediate::from(i);
215 return v ? v : jsNumberCell(globalData, i);
218 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, unsigned long i)
220 JSValue* v = JSImmediate::from(i);
221 return v ? v : jsNumberCell(globalData, i);
224 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, long long i)
226 JSValue* v = JSImmediate::from(i);
227 return v ? v : jsNumberCell(globalData, static_cast<double>(i));
230 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, unsigned long long i)
232 JSValue* v = JSImmediate::from(i);
233 return v ? v : jsNumberCell(globalData, static_cast<double>(i));
236 // --- JSValue inlines ----------------------------
238 inline double JSValue::uncheckedGetNumber() const
240 ASSERT(JSImmediate::isImmediate(asValue()) || asCell()->isNumber());
241 return JSImmediate::isImmediate(asValue()) ? JSImmediate::toDouble(asValue()) : asNumberCell(asValue())->value();
244 inline int32_t JSNumberCell::toInt32() const
246 if (m_value >= -2147483648.0 && m_value < 2147483648.0)
247 return static_cast<int32_t>(m_value);
249 return JSC::toInt32SlowCase(m_value, scratch);
252 inline uint32_t JSNumberCell::toUInt32() const
254 if (m_value >= 0.0 && m_value < 4294967296.0)
255 return static_cast<uint32_t>(m_value);
257 return JSC::toUInt32SlowCase(m_value, scratch);
260 ALWAYS_INLINE JSValue* JSValue::toJSNumber(ExecState* exec) const
262 return JSImmediate::isNumber(asValue()) ? asValue() : jsNumber(exec, this->toNumber(exec));
267 #endif // JSNumberCell_h