]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSNumberCell.h
04cccefecb794160ecbf8f3a2d7e4f36fad98204
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 UString
toThisString(ExecState
*) const;
66 virtual JSObject
* toThisObject(ExecState
*) const;
67 virtual JSValue
getJSNumber();
69 void* operator new(size_t size
, ExecState
* exec
)
71 #ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
72 return exec
->heap()->inlineAllocateNumber(size
);
74 return exec
->heap()->allocateNumber(size
);
78 void* operator new(size_t size
, JSGlobalData
* globalData
)
80 #ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
81 return globalData
->heap
.inlineAllocateNumber(size
);
83 return globalData
->heap
.allocateNumber(size
);
87 static PassRefPtr
<Structure
> createStructure(JSValue proto
) { return Structure::create(proto
, TypeInfo(NumberType
, NeedsThisConversion
)); }
90 JSNumberCell(JSGlobalData
* globalData
, double value
)
91 : JSCell(globalData
->numberStructure
.get())
96 JSNumberCell(ExecState
* exec
, double value
)
97 : JSCell(exec
->globalData().numberStructure
.get())
102 virtual bool getUInt32(uint32_t&) const;
107 JSValue
jsNumberCell(JSGlobalData
*, double);
109 inline bool isNumberCell(JSValue v
)
111 return v
.isCell() && v
.asCell()->isNumber();
114 inline JSNumberCell
* asNumberCell(JSValue v
)
116 ASSERT(isNumberCell(v
));
117 return static_cast<JSNumberCell
*>(v
.asCell());
120 inline JSValue::JSValue(ExecState
* exec
, double d
)
122 JSValue v
= JSImmediate::from(d
);
123 *this = v
? v
: jsNumberCell(exec
, d
);
126 inline JSValue::JSValue(ExecState
* exec
, int i
)
128 JSValue v
= JSImmediate::from(i
);
129 *this = v
? v
: jsNumberCell(exec
, i
);
132 inline JSValue::JSValue(ExecState
* exec
, unsigned i
)
134 JSValue v
= JSImmediate::from(i
);
135 *this = v
? v
: jsNumberCell(exec
, i
);
138 inline JSValue::JSValue(ExecState
* exec
, long i
)
140 JSValue v
= JSImmediate::from(i
);
141 *this = v
? v
: jsNumberCell(exec
, i
);
144 inline JSValue::JSValue(ExecState
* exec
, unsigned long i
)
146 JSValue v
= JSImmediate::from(i
);
147 *this = v
? v
: jsNumberCell(exec
, i
);
150 inline JSValue::JSValue(ExecState
* exec
, long long i
)
152 JSValue v
= JSImmediate::from(i
);
153 *this = v
? v
: jsNumberCell(exec
, static_cast<double>(i
));
156 inline JSValue::JSValue(ExecState
* exec
, unsigned long long i
)
158 JSValue v
= JSImmediate::from(i
);
159 *this = v
? v
: jsNumberCell(exec
, static_cast<double>(i
));
162 inline JSValue::JSValue(JSGlobalData
* globalData
, double d
)
164 JSValue v
= JSImmediate::from(d
);
165 *this = v
? v
: jsNumberCell(globalData
, d
);
168 inline JSValue::JSValue(JSGlobalData
* globalData
, int i
)
170 JSValue v
= JSImmediate::from(i
);
171 *this = v
? v
: jsNumberCell(globalData
, i
);
174 inline JSValue::JSValue(JSGlobalData
* globalData
, unsigned i
)
176 JSValue v
= JSImmediate::from(i
);
177 *this = v
? v
: jsNumberCell(globalData
, i
);
180 inline bool JSValue::isDouble() const
182 return isNumberCell(asValue());
185 inline double JSValue::asDouble() const
187 return asNumberCell(asValue())->value();
190 inline bool JSValue::isNumber() const
192 return JSImmediate::isNumber(asValue()) || isDouble();
195 inline double JSValue::uncheckedGetNumber() const
198 return JSImmediate::isImmediate(asValue()) ? JSImmediate::toDouble(asValue()) : asDouble();
201 #endif // USE(JSVALUE32)
204 inline JSValue::JSValue(ExecState
*, double d
)
206 JSValue v
= JSImmediate::from(d
);
211 inline JSValue::JSValue(ExecState
*, int i
)
213 JSValue v
= JSImmediate::from(i
);
218 inline JSValue::JSValue(ExecState
*, unsigned i
)
220 JSValue v
= JSImmediate::from(i
);
225 inline JSValue::JSValue(ExecState
*, long i
)
227 JSValue v
= JSImmediate::from(i
);
232 inline JSValue::JSValue(ExecState
*, unsigned long i
)
234 JSValue v
= JSImmediate::from(i
);
239 inline JSValue::JSValue(ExecState
*, long long i
)
241 JSValue v
= JSImmediate::from(static_cast<double>(i
));
246 inline JSValue::JSValue(ExecState
*, unsigned long long i
)
248 JSValue v
= JSImmediate::from(static_cast<double>(i
));
253 inline JSValue::JSValue(JSGlobalData
*, double d
)
255 JSValue v
= JSImmediate::from(d
);
260 inline JSValue::JSValue(JSGlobalData
*, int i
)
262 JSValue v
= JSImmediate::from(i
);
267 inline JSValue::JSValue(JSGlobalData
*, unsigned i
)
269 JSValue v
= JSImmediate::from(i
);
274 inline bool JSValue::isDouble() const
276 return JSImmediate::isDouble(asValue());
279 inline double JSValue::asDouble() const
281 return JSImmediate::doubleValue(asValue());
284 inline bool JSValue::isNumber() const
286 return JSImmediate::isNumber(asValue());
289 inline double JSValue::uncheckedGetNumber() const
292 return JSImmediate::toDouble(asValue());
295 #endif // USE(JSVALUE64)
297 #if USE(JSVALUE32) || USE(JSVALUE64)
299 inline JSValue::JSValue(ExecState
*, char i
)
301 ASSERT(JSImmediate::from(i
));
302 *this = JSImmediate::from(i
);
305 inline JSValue::JSValue(ExecState
*, unsigned char i
)
307 ASSERT(JSImmediate::from(i
));
308 *this = JSImmediate::from(i
);
311 inline JSValue::JSValue(ExecState
*, short i
)
313 ASSERT(JSImmediate::from(i
));
314 *this = JSImmediate::from(i
);
317 inline JSValue::JSValue(ExecState
*, unsigned short i
)
319 ASSERT(JSImmediate::from(i
));
320 *this = JSImmediate::from(i
);
323 inline JSValue
jsNaN(ExecState
* exec
)
325 return jsNumber(exec
, NaN
);
328 inline JSValue
jsNaN(JSGlobalData
* globalData
)
330 return jsNumber(globalData
, NaN
);
333 // --- JSValue inlines ----------------------------
335 ALWAYS_INLINE JSValue
JSValue::toJSNumber(ExecState
* exec
) const
337 return isNumber() ? asValue() : jsNumber(exec
, this->toNumber(exec
));
340 inline bool JSValue::getNumber(double &result
) const
344 else if (LIKELY(isDouble()))
353 #endif // USE(JSVALUE32) || USE(JSVALUE64)
357 #endif // JSNumberCell_h