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