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)); }
88 JSNumberCell(JSGlobalData* globalData, double value)
89 : JSCell(globalData->numberStructure.get())
94 JSNumberCell(ExecState* exec, double value)
95 : JSCell(exec->globalData().numberStructure.get())
100 virtual bool getUInt32(uint32_t&) const;
101 virtual bool getTruncatedInt32(int32_t&) const;
102 virtual bool getTruncatedUInt32(uint32_t&) const;
107 extern const double NaN;
108 extern const double Inf;
110 JSNumberCell* asNumberCell(JSValue*);
112 JSValue* jsNumberCell(JSGlobalData*, double);
113 JSValue* jsNaN(JSGlobalData*);
114 JSValue* jsNumberCell(ExecState*, double);
115 JSValue* jsNaN(ExecState*);
117 inline JSNumberCell* asNumberCell(JSValue* value)
119 ASSERT(asCell(value)->isNumber());
120 return static_cast<JSNumberCell*>(asCell(value));
123 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, double d)
125 JSValue* v = JSImmediate::from(d);
126 return v ? v : jsNumberCell(exec, d);
129 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, short i)
131 JSValue* v = JSImmediate::from(i);
132 return v ? v : jsNumberCell(exec, i);
135 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned short i)
137 JSValue* v = JSImmediate::from(i);
138 return v ? v : jsNumberCell(exec, i);
141 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, int i)
143 JSValue* v = JSImmediate::from(i);
144 return v ? v : jsNumberCell(exec, i);
147 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned i)
149 JSValue* v = JSImmediate::from(i);
150 return v ? v : jsNumberCell(exec, i);
153 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, long i)
155 JSValue* v = JSImmediate::from(i);
156 return v ? v : jsNumberCell(exec, i);
159 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned long i)
161 JSValue* v = JSImmediate::from(i);
162 return v ? v : jsNumberCell(exec, i);
165 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, long long i)
167 JSValue* v = JSImmediate::from(i);
168 return v ? v : jsNumberCell(exec, static_cast<double>(i));
171 ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned long long i)
173 JSValue* v = JSImmediate::from(i);
174 return v ? v : jsNumberCell(exec, static_cast<double>(i));
177 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, double d)
179 JSValue* v = JSImmediate::from(d);
180 return v ? v : jsNumberCell(globalData, d);
183 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, short i)
185 JSValue* v = JSImmediate::from(i);
186 return v ? v : jsNumberCell(globalData, i);
189 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, unsigned short i)
191 JSValue* v = JSImmediate::from(i);
192 return v ? v : jsNumberCell(globalData, i);
195 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, int i)
197 JSValue* v = JSImmediate::from(i);
198 return v ? v : jsNumberCell(globalData, i);
201 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, unsigned i)
203 JSValue* v = JSImmediate::from(i);
204 return v ? v : jsNumberCell(globalData, i);
207 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, long i)
209 JSValue* v = JSImmediate::from(i);
210 return v ? v : jsNumberCell(globalData, i);
213 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, unsigned long i)
215 JSValue* v = JSImmediate::from(i);
216 return v ? v : jsNumberCell(globalData, i);
219 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, long long i)
221 JSValue* v = JSImmediate::from(i);
222 return v ? v : jsNumberCell(globalData, static_cast<double>(i));
225 ALWAYS_INLINE JSValue* jsNumber(JSGlobalData* globalData, unsigned long long i)
227 JSValue* v = JSImmediate::from(i);
228 return v ? v : jsNumberCell(globalData, static_cast<double>(i));
231 // --- JSValue inlines ----------------------------
233 inline double JSValue::uncheckedGetNumber() const
235 ASSERT(JSImmediate::isImmediate(asValue()) || asCell()->isNumber());
236 return JSImmediate::isImmediate(asValue()) ? JSImmediate::toDouble(asValue()) : asNumberCell(asValue())->value();
239 inline int32_t JSNumberCell::toInt32() const
241 if (m_value >= -2147483648.0 && m_value < 2147483648.0)
242 return static_cast<int32_t>(m_value);
244 return JSC::toInt32SlowCase(m_value, scratch);
247 inline uint32_t JSNumberCell::toUInt32() const
249 if (m_value >= 0.0 && m_value < 4294967296.0)
250 return static_cast<uint32_t>(m_value);
252 return JSC::toUInt32SlowCase(m_value, scratch);
255 ALWAYS_INLINE JSValue* JSValue::toJSNumber(ExecState* exec) const
257 return JSImmediate::isNumber(asValue()) ? asValue() : jsNumber(exec, this->toNumber(exec));
262 #endif // JSNumberCell_h