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
35 extern const double NaN
;
36 extern const double Inf
;
39 JSValue
jsNumberCell(ExecState
*, double);
50 class JSNumberCell
: public JSCell
{
52 friend JSValue
jsNumberCell(JSGlobalData
*, double);
53 friend JSValue
jsNumberCell(ExecState
*, double);
56 double value() const { return m_value
; }
58 virtual JSValue
toPrimitive(ExecState
*, PreferredPrimitiveType
) const;
59 virtual bool getPrimitiveNumber(ExecState
*, double& number
, JSValue
& value
);
60 virtual bool toBoolean(ExecState
*) const;
61 virtual double toNumber(ExecState
*) const;
62 virtual UString
toString(ExecState
*) const;
63 virtual JSObject
* toObject(ExecState
*) const;
65 virtual JSObject
* toThisObject(ExecState
*) const;
66 virtual JSValue
getJSNumber();
68 void* operator new(size_t size
, ExecState
* exec
)
70 return exec
->heap()->allocateNumber(size
);
73 void* operator new(size_t size
, JSGlobalData
* globalData
)
75 return globalData
->heap
.allocateNumber(size
);
78 static PassRefPtr
<Structure
> createStructure(JSValue proto
) { return Structure::create(proto
, TypeInfo(NumberType
, OverridesGetOwnPropertySlot
| NeedsThisConversion
), AnonymousSlotCount
); }
81 JSNumberCell(JSGlobalData
* globalData
, double value
)
82 : JSCell(globalData
->numberStructure
.get())
87 JSNumberCell(ExecState
* exec
, double value
)
88 : JSCell(exec
->globalData().numberStructure
.get())
93 virtual bool getUInt32(uint32_t&) const;
98 JSValue
jsNumberCell(JSGlobalData
*, double);
100 inline bool isNumberCell(JSValue v
)
102 return v
.isCell() && v
.asCell()->isNumber();
105 inline JSNumberCell
* asNumberCell(JSValue v
)
107 ASSERT(isNumberCell(v
));
108 return static_cast<JSNumberCell
*>(v
.asCell());
111 ALWAYS_INLINE
JSValue::JSValue(EncodeAsDoubleTag
, ExecState
* exec
, double d
)
113 *this = jsNumberCell(exec
, d
);
116 inline JSValue::JSValue(ExecState
* exec
, double d
)
118 JSValue v
= JSImmediate::from(d
);
119 *this = v
? v
: jsNumberCell(exec
, d
);
122 inline JSValue::JSValue(ExecState
* exec
, int i
)
124 JSValue v
= JSImmediate::from(i
);
125 *this = v
? v
: jsNumberCell(exec
, i
);
128 inline JSValue::JSValue(ExecState
* exec
, unsigned i
)
130 JSValue v
= JSImmediate::from(i
);
131 *this = v
? v
: jsNumberCell(exec
, i
);
134 inline JSValue::JSValue(ExecState
* exec
, long i
)
136 JSValue v
= JSImmediate::from(i
);
137 *this = v
? v
: jsNumberCell(exec
, i
);
140 inline JSValue::JSValue(ExecState
* exec
, unsigned long i
)
142 JSValue v
= JSImmediate::from(i
);
143 *this = v
? v
: jsNumberCell(exec
, i
);
146 inline JSValue::JSValue(ExecState
* exec
, long long i
)
148 JSValue v
= JSImmediate::from(i
);
149 *this = v
? v
: jsNumberCell(exec
, static_cast<double>(i
));
152 inline JSValue::JSValue(ExecState
* exec
, unsigned long long i
)
154 JSValue v
= JSImmediate::from(i
);
155 *this = v
? v
: jsNumberCell(exec
, static_cast<double>(i
));
158 inline JSValue::JSValue(JSGlobalData
* globalData
, double d
)
160 JSValue v
= JSImmediate::from(d
);
161 *this = v
? v
: jsNumberCell(globalData
, d
);
164 inline JSValue::JSValue(JSGlobalData
* globalData
, int i
)
166 JSValue v
= JSImmediate::from(i
);
167 *this = v
? v
: jsNumberCell(globalData
, i
);
170 inline JSValue::JSValue(JSGlobalData
* globalData
, unsigned i
)
172 JSValue v
= JSImmediate::from(i
);
173 *this = v
? v
: jsNumberCell(globalData
, i
);
176 inline bool JSValue::isDouble() const
178 return isNumberCell(asValue());
181 inline double JSValue::asDouble() const
183 return asNumberCell(asValue())->value();
186 inline bool JSValue::isNumber() const
188 return JSImmediate::isNumber(asValue()) || isDouble();
191 inline double JSValue::uncheckedGetNumber() const
194 return JSImmediate::isImmediate(asValue()) ? JSImmediate::toDouble(asValue()) : asDouble();
197 #endif // USE(JSVALUE32)
200 ALWAYS_INLINE
JSValue::JSValue(EncodeAsDoubleTag
, ExecState
*, double d
)
202 *this = JSImmediate::fromNumberOutsideIntegerRange(d
);
205 inline JSValue::JSValue(ExecState
*, double d
)
207 JSValue v
= JSImmediate::from(d
);
212 inline JSValue::JSValue(ExecState
*, int i
)
214 JSValue v
= JSImmediate::from(i
);
219 inline JSValue::JSValue(ExecState
*, unsigned i
)
221 JSValue v
= JSImmediate::from(i
);
226 inline JSValue::JSValue(ExecState
*, long i
)
228 JSValue v
= JSImmediate::from(i
);
233 inline JSValue::JSValue(ExecState
*, unsigned long i
)
235 JSValue v
= JSImmediate::from(i
);
240 inline JSValue::JSValue(ExecState
*, long long i
)
242 JSValue v
= JSImmediate::from(static_cast<double>(i
));
247 inline JSValue::JSValue(ExecState
*, unsigned long long i
)
249 JSValue v
= JSImmediate::from(static_cast<double>(i
));
254 inline JSValue::JSValue(JSGlobalData
*, double d
)
256 JSValue v
= JSImmediate::from(d
);
261 inline JSValue::JSValue(JSGlobalData
*, int i
)
263 JSValue v
= JSImmediate::from(i
);
268 inline JSValue::JSValue(JSGlobalData
*, unsigned i
)
270 JSValue v
= JSImmediate::from(i
);
275 inline bool JSValue::isDouble() const
277 return JSImmediate::isDouble(asValue());
280 inline double JSValue::asDouble() const
282 return JSImmediate::doubleValue(asValue());
285 inline bool JSValue::isNumber() const
287 return JSImmediate::isNumber(asValue());
290 inline double JSValue::uncheckedGetNumber() const
293 return JSImmediate::toDouble(asValue());
296 #endif // USE(JSVALUE64)
298 #if USE(JSVALUE32) || USE(JSVALUE64)
300 inline JSValue::JSValue(ExecState
*, char i
)
302 ASSERT(JSImmediate::from(i
));
303 *this = JSImmediate::from(i
);
306 inline JSValue::JSValue(ExecState
*, unsigned char i
)
308 ASSERT(JSImmediate::from(i
));
309 *this = JSImmediate::from(i
);
312 inline JSValue::JSValue(ExecState
*, short i
)
314 ASSERT(JSImmediate::from(i
));
315 *this = JSImmediate::from(i
);
318 inline JSValue::JSValue(ExecState
*, unsigned short i
)
320 ASSERT(JSImmediate::from(i
));
321 *this = JSImmediate::from(i
);
324 inline JSValue
jsNaN(ExecState
* exec
)
326 return jsNumber(exec
, NaN
);
329 inline JSValue
jsNaN(JSGlobalData
* globalData
)
331 return jsNumber(globalData
, NaN
);
334 // --- JSValue inlines ----------------------------
336 ALWAYS_INLINE JSValue
JSValue::toJSNumber(ExecState
* exec
) const
338 return isNumber() ? asValue() : jsNumber(exec
, this->toNumber(exec
));
341 inline bool JSValue::getNumber(double &result
) const
345 else if (LIKELY(isDouble()))
354 #endif // USE(JSVALUE32) || USE(JSVALUE64)
358 #endif // JSNumberCell_h