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 "ExecState.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 KJS {
|
---|
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 JSValue* jsNumberCell(ExecState*, double);
|
---|
46 | public:
|
---|
47 | double value() const { return m_value; }
|
---|
48 |
|
---|
49 | virtual JSValue* toPrimitive(ExecState*, PreferredPrimitiveType) const;
|
---|
50 | virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
|
---|
51 | virtual bool toBoolean(ExecState*) const;
|
---|
52 | virtual double toNumber(ExecState*) const;
|
---|
53 | virtual UString toString(ExecState*) const;
|
---|
54 | virtual JSObject* toObject(ExecState*) const;
|
---|
55 |
|
---|
56 | virtual UString toThisString(ExecState*) const;
|
---|
57 | virtual JSObject* toThisObject(ExecState*) const;
|
---|
58 | virtual JSValue* getJSNumber();
|
---|
59 |
|
---|
60 | int32_t toInt32() const;
|
---|
61 | uint32_t toUInt32() const;
|
---|
62 |
|
---|
63 | void* operator new(size_t size, ExecState* exec)
|
---|
64 | {
|
---|
65 | #ifdef JAVASCRIPTCORE_BUILDING_ALL_IN_ONE_FILE
|
---|
66 | return exec->heap()->inlineAllocateNumber(size);
|
---|
67 | #else
|
---|
68 | return exec->heap()->allocateNumber(size);
|
---|
69 | #endif
|
---|
70 | }
|
---|
71 |
|
---|
72 | private:
|
---|
73 | JSNumberCell(double value)
|
---|
74 | : m_value(value)
|
---|
75 | {
|
---|
76 | }
|
---|
77 |
|
---|
78 | virtual bool getUInt32(uint32_t&) const;
|
---|
79 | virtual bool getTruncatedInt32(int32_t&) const;
|
---|
80 | virtual bool getTruncatedUInt32(uint32_t&) const;
|
---|
81 |
|
---|
82 | double m_value;
|
---|
83 | };
|
---|
84 |
|
---|
85 | extern const double NaN;
|
---|
86 | extern const double Inf;
|
---|
87 |
|
---|
88 | // Beware marking this function ALWAYS_INLINE: It takes a PIC branch, so
|
---|
89 | // inlining it may not always be a win.
|
---|
90 | inline JSValue* jsNumberCell(ExecState* exec, double d)
|
---|
91 | {
|
---|
92 | return new (exec) JSNumberCell(d);
|
---|
93 | }
|
---|
94 |
|
---|
95 | inline JSValue* jsNaN(ExecState* exec)
|
---|
96 | {
|
---|
97 | return jsNumberCell(exec, NaN);
|
---|
98 | }
|
---|
99 |
|
---|
100 | ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, double d)
|
---|
101 | {
|
---|
102 | JSValue* v = JSImmediate::from(d);
|
---|
103 | return v ? v : jsNumberCell(exec, d);
|
---|
104 | }
|
---|
105 |
|
---|
106 | ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, int i)
|
---|
107 | {
|
---|
108 | JSValue* v = JSImmediate::from(i);
|
---|
109 | return v ? v : jsNumberCell(exec, i);
|
---|
110 | }
|
---|
111 |
|
---|
112 | ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned i)
|
---|
113 | {
|
---|
114 | JSValue* v = JSImmediate::from(i);
|
---|
115 | return v ? v : jsNumberCell(exec, i);
|
---|
116 | }
|
---|
117 |
|
---|
118 | ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, long i)
|
---|
119 | {
|
---|
120 | JSValue* v = JSImmediate::from(i);
|
---|
121 | return v ? v : jsNumberCell(exec, i);
|
---|
122 | }
|
---|
123 |
|
---|
124 | ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned long i)
|
---|
125 | {
|
---|
126 | JSValue* v = JSImmediate::from(i);
|
---|
127 | return v ? v : jsNumberCell(exec, i);
|
---|
128 | }
|
---|
129 |
|
---|
130 | ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, long long i)
|
---|
131 | {
|
---|
132 | JSValue* v = JSImmediate::from(i);
|
---|
133 | return v ? v : jsNumberCell(exec, static_cast<double>(i));
|
---|
134 | }
|
---|
135 |
|
---|
136 | ALWAYS_INLINE JSValue* jsNumber(ExecState* exec, unsigned long long i)
|
---|
137 | {
|
---|
138 | JSValue* v = JSImmediate::from(i);
|
---|
139 | return v ? v : jsNumberCell(exec, static_cast<double>(i));
|
---|
140 | }
|
---|
141 |
|
---|
142 | // --- JSValue inlines ----------------------------
|
---|
143 |
|
---|
144 | inline double JSValue::uncheckedGetNumber() const
|
---|
145 | {
|
---|
146 | ASSERT(JSImmediate::isImmediate(this) || asCell()->isNumber());
|
---|
147 | return JSImmediate::isImmediate(this) ? JSImmediate::toDouble(this) : static_cast<const JSNumberCell*>(this)->value();
|
---|
148 | }
|
---|
149 |
|
---|
150 | inline int32_t JSNumberCell::toInt32() const
|
---|
151 | {
|
---|
152 | if (m_value >= -2147483648.0 && m_value < 2147483648.0)
|
---|
153 | return static_cast<int32_t>(m_value);
|
---|
154 | bool scratch;
|
---|
155 | return JSValue::toInt32SlowCase(m_value, scratch);
|
---|
156 | }
|
---|
157 |
|
---|
158 | inline uint32_t JSNumberCell::toUInt32() const
|
---|
159 | {
|
---|
160 | if (m_value >= 0.0 && m_value < 4294967296.0)
|
---|
161 | return static_cast<uint32_t>(m_value);
|
---|
162 | bool scratch;
|
---|
163 | return JSValue::toUInt32SlowCase(m_value, scratch);
|
---|
164 | }
|
---|
165 |
|
---|
166 | ALWAYS_INLINE JSValue* JSValue::toJSNumber(ExecState* exec) const
|
---|
167 | {
|
---|
168 | return JSImmediate::isNumber(this) ? const_cast<JSValue*>(this) : jsNumber(exec, this->toNumber(exec));
|
---|
169 | }
|
---|
170 |
|
---|
171 | } // namespace KJS
|
---|
172 |
|
---|
173 | #endif // JSNumberCell_h
|
---|