1 | /*
|
---|
2 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
4 | * Copyright (C) 2003, 2004, 2005, 2007, 2008 Apple Inc. All rights reserved.
|
---|
5 | *
|
---|
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.
|
---|
10 | *
|
---|
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.
|
---|
15 | *
|
---|
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.
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef JSNumberCell_h
|
---|
24 | #define JSNumberCell_h
|
---|
25 |
|
---|
26 | #include "CallFrame.h"
|
---|
27 | #include "JSCell.h"
|
---|
28 | #include "JSImmediate.h"
|
---|
29 | #include "Collector.h"
|
---|
30 | #include "UString.h"
|
---|
31 | #include <stddef.h> // for size_t
|
---|
32 |
|
---|
33 | namespace JSC {
|
---|
34 |
|
---|
35 | class Identifier;
|
---|
36 | class JSCell;
|
---|
37 | class JSObject;
|
---|
38 | class JSString;
|
---|
39 | class PropertySlot;
|
---|
40 |
|
---|
41 | struct ClassInfo;
|
---|
42 | struct Instruction;
|
---|
43 |
|
---|
44 | class JSNumberCell : public JSCell {
|
---|
45 | friend class JIT;
|
---|
46 | friend JSValuePtr jsNumberCell(JSGlobalData*, double);
|
---|
47 | friend JSValuePtr jsNaN(JSGlobalData*);
|
---|
48 | friend JSValuePtr jsNumberCell(ExecState*, double);
|
---|
49 | friend JSValuePtr jsNaN(ExecState*);
|
---|
50 | public:
|
---|
51 | double value() const { return m_value; }
|
---|
52 |
|
---|
53 | virtual JSValuePtr toPrimitive(ExecState*, PreferredPrimitiveType) const;
|
---|
54 | virtual bool getPrimitiveNumber(ExecState*, double& number, JSValuePtr& 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;
|
---|
59 |
|
---|
60 | virtual UString toThisString(ExecState*) const;
|
---|
61 | virtual JSObject* toThisObject(ExecState*) const;
|
---|
62 | virtual JSValuePtr getJSNumber();
|
---|
63 |
|
---|
64 | int32_t toInt32() const;
|
---|
65 | uint32_t toUInt32() const;
|
---|
66 |
|
---|
67 | void* operator new(size_t size, ExecState* exec)
|
---|
68 | {
|
---|
69 | #ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
|
---|
70 | return exec->heap()->inlineAllocateNumber(size);
|
---|
71 | #else
|
---|
72 | return exec->heap()->allocateNumber(size);
|
---|
73 | #endif
|
---|
74 | }
|
---|
75 |
|
---|
76 | void* operator new(size_t size, JSGlobalData* globalData)
|
---|
77 | {
|
---|
78 | #ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
|
---|
79 | return globalData->heap.inlineAllocateNumber(size);
|
---|
80 | #else
|
---|
81 | return globalData->heap.allocateNumber(size);
|
---|
82 | #endif
|
---|
83 | }
|
---|
84 |
|
---|
85 | static PassRefPtr<Structure> createStructure(JSValuePtr proto) { return Structure::create(proto, TypeInfo(NumberType, NeedsThisConversion)); }
|
---|
86 |
|
---|
87 | private:
|
---|
88 | JSNumberCell(JSGlobalData* globalData, double value)
|
---|
89 | : JSCell(globalData->numberStructure.get())
|
---|
90 | , m_value(value)
|
---|
91 | {
|
---|
92 | }
|
---|
93 |
|
---|
94 | JSNumberCell(ExecState* exec, double value)
|
---|
95 | : JSCell(exec->globalData().numberStructure.get())
|
---|
96 | , m_value(value)
|
---|
97 | {
|
---|
98 | }
|
---|
99 |
|
---|
100 | virtual bool getUInt32(uint32_t&) const;
|
---|
101 | virtual bool getTruncatedInt32(int32_t&) const;
|
---|
102 | virtual bool getTruncatedUInt32(uint32_t&) const;
|
---|
103 |
|
---|
104 | double m_value;
|
---|
105 | };
|
---|
106 |
|
---|
107 | extern const double NaN;
|
---|
108 | extern const double Inf;
|
---|
109 |
|
---|
110 | JSValuePtr jsNumberCell(JSGlobalData*, double);
|
---|
111 | JSValuePtr jsNaN(JSGlobalData*);
|
---|
112 | JSValuePtr jsNumberCell(ExecState*, double);
|
---|
113 | JSValuePtr jsNaN(ExecState*);
|
---|
114 |
|
---|
115 | inline JSNumberCell* JSValuePtr::asNumberCell() const
|
---|
116 | {
|
---|
117 | ASSERT(isNumberCell());
|
---|
118 | return static_cast<JSNumberCell*>(asCell());
|
---|
119 | }
|
---|
120 |
|
---|
121 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState* exec, double d)
|
---|
122 | {
|
---|
123 | JSValuePtr v = JSImmediate::from(d);
|
---|
124 | return v ? v : jsNumberCell(exec, d);
|
---|
125 | }
|
---|
126 |
|
---|
127 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState*, char i)
|
---|
128 | {
|
---|
129 | ASSERT(JSImmediate::from(i));
|
---|
130 | return JSImmediate::from(i);
|
---|
131 | }
|
---|
132 |
|
---|
133 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState*, unsigned char i)
|
---|
134 | {
|
---|
135 | ASSERT(JSImmediate::from(i));
|
---|
136 | return JSImmediate::from(i);
|
---|
137 | }
|
---|
138 |
|
---|
139 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState*, short i)
|
---|
140 | {
|
---|
141 | ASSERT(JSImmediate::from(i));
|
---|
142 | return JSImmediate::from(i);
|
---|
143 | }
|
---|
144 |
|
---|
145 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState*, unsigned short i)
|
---|
146 | {
|
---|
147 | ASSERT(JSImmediate::from(i));
|
---|
148 | return JSImmediate::from(i);
|
---|
149 | }
|
---|
150 |
|
---|
151 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState* exec, int i)
|
---|
152 | {
|
---|
153 | JSValuePtr v = JSImmediate::from(i);
|
---|
154 | return v ? v : jsNumberCell(exec, i);
|
---|
155 | }
|
---|
156 |
|
---|
157 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState* exec, unsigned i)
|
---|
158 | {
|
---|
159 | JSValuePtr v = JSImmediate::from(i);
|
---|
160 | return v ? v : jsNumberCell(exec, i);
|
---|
161 | }
|
---|
162 |
|
---|
163 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState* exec, long i)
|
---|
164 | {
|
---|
165 | JSValuePtr v = JSImmediate::from(i);
|
---|
166 | return v ? v : jsNumberCell(exec, i);
|
---|
167 | }
|
---|
168 |
|
---|
169 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState* exec, unsigned long i)
|
---|
170 | {
|
---|
171 | JSValuePtr v = JSImmediate::from(i);
|
---|
172 | return v ? v : jsNumberCell(exec, i);
|
---|
173 | }
|
---|
174 |
|
---|
175 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState* exec, long long i)
|
---|
176 | {
|
---|
177 | JSValuePtr v = JSImmediate::from(i);
|
---|
178 | return v ? v : jsNumberCell(exec, static_cast<double>(i));
|
---|
179 | }
|
---|
180 |
|
---|
181 | ALWAYS_INLINE JSValuePtr jsNumber(ExecState* exec, unsigned long long i)
|
---|
182 | {
|
---|
183 | JSValuePtr v = JSImmediate::from(i);
|
---|
184 | return v ? v : jsNumberCell(exec, static_cast<double>(i));
|
---|
185 | }
|
---|
186 |
|
---|
187 | ALWAYS_INLINE JSValuePtr jsNumber(JSGlobalData* globalData, double d)
|
---|
188 | {
|
---|
189 | JSValuePtr v = JSImmediate::from(d);
|
---|
190 | return v ? v : jsNumberCell(globalData, d);
|
---|
191 | }
|
---|
192 |
|
---|
193 | ALWAYS_INLINE JSValuePtr jsNumber(JSGlobalData* globalData, short i)
|
---|
194 | {
|
---|
195 | JSValuePtr v = JSImmediate::from(i);
|
---|
196 | return v ? v : jsNumberCell(globalData, i);
|
---|
197 | }
|
---|
198 |
|
---|
199 | ALWAYS_INLINE JSValuePtr jsNumber(JSGlobalData* globalData, unsigned short i)
|
---|
200 | {
|
---|
201 | JSValuePtr v = JSImmediate::from(i);
|
---|
202 | return v ? v : jsNumberCell(globalData, i);
|
---|
203 | }
|
---|
204 |
|
---|
205 | ALWAYS_INLINE JSValuePtr jsNumber(JSGlobalData* globalData, int i)
|
---|
206 | {
|
---|
207 | JSValuePtr v = JSImmediate::from(i);
|
---|
208 | return v ? v : jsNumberCell(globalData, i);
|
---|
209 | }
|
---|
210 |
|
---|
211 | ALWAYS_INLINE JSValuePtr jsNumber(JSGlobalData* globalData, unsigned i)
|
---|
212 | {
|
---|
213 | JSValuePtr v = JSImmediate::from(i);
|
---|
214 | return v ? v : jsNumberCell(globalData, i);
|
---|
215 | }
|
---|
216 |
|
---|
217 | ALWAYS_INLINE JSValuePtr jsNumber(JSGlobalData* globalData, long i)
|
---|
218 | {
|
---|
219 | JSValuePtr v = JSImmediate::from(i);
|
---|
220 | return v ? v : jsNumberCell(globalData, i);
|
---|
221 | }
|
---|
222 |
|
---|
223 | ALWAYS_INLINE JSValuePtr jsNumber(JSGlobalData* globalData, unsigned long i)
|
---|
224 | {
|
---|
225 | JSValuePtr v = JSImmediate::from(i);
|
---|
226 | return v ? v : jsNumberCell(globalData, i);
|
---|
227 | }
|
---|
228 |
|
---|
229 | ALWAYS_INLINE JSValuePtr jsNumber(JSGlobalData* globalData, long long i)
|
---|
230 | {
|
---|
231 | JSValuePtr v = JSImmediate::from(i);
|
---|
232 | return v ? v : jsNumberCell(globalData, static_cast<double>(i));
|
---|
233 | }
|
---|
234 |
|
---|
235 | ALWAYS_INLINE JSValuePtr jsNumber(JSGlobalData* globalData, unsigned long long i)
|
---|
236 | {
|
---|
237 | JSValuePtr v = JSImmediate::from(i);
|
---|
238 | return v ? v : jsNumberCell(globalData, static_cast<double>(i));
|
---|
239 | }
|
---|
240 |
|
---|
241 | // --- JSValue inlines ----------------------------
|
---|
242 |
|
---|
243 | inline double JSValuePtr::uncheckedGetNumber() const
|
---|
244 | {
|
---|
245 | ASSERT(isNumber());
|
---|
246 | return JSImmediate::isImmediate(asValue()) ? JSImmediate::toDouble(asValue()) : asNumberCell()->value();
|
---|
247 | }
|
---|
248 |
|
---|
249 | inline int32_t JSNumberCell::toInt32() const
|
---|
250 | {
|
---|
251 | if (m_value >= -2147483648.0 && m_value < 2147483648.0)
|
---|
252 | return static_cast<int32_t>(m_value);
|
---|
253 | bool ignored;
|
---|
254 | return toInt32SlowCase(m_value, ignored);
|
---|
255 | }
|
---|
256 |
|
---|
257 | inline uint32_t JSNumberCell::toUInt32() const
|
---|
258 | {
|
---|
259 | if (m_value >= 0.0 && m_value < 4294967296.0)
|
---|
260 | return static_cast<uint32_t>(m_value);
|
---|
261 | bool ignored;
|
---|
262 | return toUInt32SlowCase(m_value, ignored);
|
---|
263 | }
|
---|
264 |
|
---|
265 | ALWAYS_INLINE JSValuePtr JSValuePtr::toJSNumber(ExecState* exec) const
|
---|
266 | {
|
---|
267 | return isNumber() ? asValue() : jsNumber(exec, this->toNumber(exec));
|
---|
268 | }
|
---|
269 |
|
---|
270 | inline bool JSValuePtr::getNumber(double &result) const
|
---|
271 | {
|
---|
272 | if (isInt32Fast())
|
---|
273 | result = getInt32Fast();
|
---|
274 | else if (LIKELY(isNumberCell()))
|
---|
275 | result = asNumberCell()->value();
|
---|
276 | else {
|
---|
277 | ASSERT(!isNumber());
|
---|
278 | return false;
|
---|
279 | }
|
---|
280 | return true;
|
---|
281 | }
|
---|
282 |
|
---|
283 | inline bool JSValuePtr::numberToInt32(int32_t& arg)
|
---|
284 | {
|
---|
285 | if (isInt32Fast())
|
---|
286 | arg = getInt32Fast();
|
---|
287 | else if (LIKELY(isNumberCell()))
|
---|
288 | arg = asNumberCell()->toInt32();
|
---|
289 | else {
|
---|
290 | ASSERT(!isNumber());
|
---|
291 | return false;
|
---|
292 | }
|
---|
293 | return true;
|
---|
294 | }
|
---|
295 |
|
---|
296 | inline bool JSValuePtr::numberToUInt32(uint32_t& arg)
|
---|
297 | {
|
---|
298 | if (isUInt32Fast())
|
---|
299 | arg = getUInt32Fast();
|
---|
300 | else if (LIKELY(isNumberCell()))
|
---|
301 | arg = asNumberCell()->toUInt32();
|
---|
302 | else if (isInt32Fast()) {
|
---|
303 | // FIXME: I think this case can be merged with the uint case; toUInt32SlowCase
|
---|
304 | // on a negative value is equivalent to simple static_casting.
|
---|
305 | bool ignored;
|
---|
306 | arg = toUInt32SlowCase(getInt32Fast(), ignored);
|
---|
307 | } else {
|
---|
308 | ASSERT(!isNumber());
|
---|
309 | return false;
|
---|
310 | }
|
---|
311 | return true;
|
---|
312 | }
|
---|
313 |
|
---|
314 | } // namespace JSC
|
---|
315 |
|
---|
316 | #endif // JSNumberCell_h
|
---|