|
1 /* |
|
2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org) |
|
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com) |
|
4 * Copyright (C) 2003, 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 #include "config.h" |
|
24 #include "JSValue.h" |
|
25 |
|
26 #include "BooleanConstructor.h" |
|
27 #include "BooleanPrototype.h" |
|
28 #include "Error.h" |
|
29 #include "ExceptionHelpers.h" |
|
30 #include "JSGlobalObject.h" |
|
31 #include "JSFunction.h" |
|
32 #include "JSNotAnObject.h" |
|
33 #include "NumberObject.h" |
|
34 #include <wtf/MathExtras.h> |
|
35 #include <wtf/StringExtras.h> |
|
36 |
|
37 namespace JSC { |
|
38 |
|
39 static const double D32 = 4294967296.0; |
|
40 |
|
41 // ECMA 9.4 |
|
42 double JSValue::toInteger(ExecState* exec) const |
|
43 { |
|
44 if (isInt32()) |
|
45 return asInt32(); |
|
46 double d = toNumber(exec); |
|
47 return isnan(d) ? 0.0 : trunc(d); |
|
48 } |
|
49 |
|
50 double JSValue::toIntegerPreserveNaN(ExecState* exec) const |
|
51 { |
|
52 if (isInt32()) |
|
53 return asInt32(); |
|
54 return trunc(toNumber(exec)); |
|
55 } |
|
56 |
|
57 JSObject* JSValue::toObjectSlowCase(ExecState* exec) const |
|
58 { |
|
59 ASSERT(!isCell()); |
|
60 |
|
61 if (isInt32() || isDouble()) |
|
62 return constructNumber(exec, asValue()); |
|
63 if (isTrue() || isFalse()) |
|
64 return constructBooleanFromImmediateBoolean(exec, asValue()); |
|
65 ASSERT(isUndefinedOrNull()); |
|
66 JSNotAnObjectErrorStub* exception = createNotAnObjectErrorStub(exec, isNull()); |
|
67 throwError(exec, exception); |
|
68 return new (exec) JSNotAnObject(exec, exception); |
|
69 } |
|
70 |
|
71 JSObject* JSValue::toThisObjectSlowCase(ExecState* exec) const |
|
72 { |
|
73 ASSERT(!isCell()); |
|
74 |
|
75 if (isInt32() || isDouble()) |
|
76 return constructNumber(exec, asValue()); |
|
77 if (isTrue() || isFalse()) |
|
78 return constructBooleanFromImmediateBoolean(exec, asValue()); |
|
79 ASSERT(isUndefinedOrNull()); |
|
80 return exec->globalThisValue(); |
|
81 } |
|
82 |
|
83 JSObject* JSValue::synthesizeObject(ExecState* exec) const |
|
84 { |
|
85 ASSERT(!isCell()); |
|
86 if (isNumber()) |
|
87 return constructNumber(exec, asValue()); |
|
88 if (isBoolean()) |
|
89 return constructBooleanFromImmediateBoolean(exec, asValue()); |
|
90 |
|
91 JSNotAnObjectErrorStub* exception = createNotAnObjectErrorStub(exec, isNull()); |
|
92 throwError(exec, exception); |
|
93 return new (exec) JSNotAnObject(exec, exception); |
|
94 } |
|
95 |
|
96 JSObject* JSValue::synthesizePrototype(ExecState* exec) const |
|
97 { |
|
98 ASSERT(!isCell()); |
|
99 if (isNumber()) |
|
100 return exec->lexicalGlobalObject()->numberPrototype(); |
|
101 if (isBoolean()) |
|
102 return exec->lexicalGlobalObject()->booleanPrototype(); |
|
103 |
|
104 JSNotAnObjectErrorStub* exception = createNotAnObjectErrorStub(exec, isNull()); |
|
105 throwError(exec, exception); |
|
106 return new (exec) JSNotAnObject(exec, exception); |
|
107 } |
|
108 |
|
109 #ifndef NDEBUG |
|
110 char* JSValue::description() |
|
111 { |
|
112 static const size_t size = 32; |
|
113 static char description[size]; |
|
114 |
|
115 if (!*this) |
|
116 snprintf(description, size, "<JSValue()>"); |
|
117 else if (isInt32()) |
|
118 snprintf(description, size, "Int32: %d", asInt32()); |
|
119 else if (isDouble()) |
|
120 snprintf(description, size, "Double: %lf", asDouble()); |
|
121 else if (isCell()) |
|
122 snprintf(description, size, "Cell: %p", asCell()); |
|
123 else if (isTrue()) |
|
124 snprintf(description, size, "True"); |
|
125 else if (isFalse()) |
|
126 snprintf(description, size, "False"); |
|
127 else if (isNull()) |
|
128 snprintf(description, size, "Null"); |
|
129 else if (isUndefined()) |
|
130 snprintf(description, size, "Undefined"); |
|
131 else |
|
132 snprintf(description, size, "INVALID"); |
|
133 |
|
134 return description; |
|
135 } |
|
136 #endif |
|
137 |
|
138 int32_t toInt32SlowCase(double d, bool& ok) |
|
139 { |
|
140 ok = true; |
|
141 |
|
142 if (d >= -D32 / 2 && d < D32 / 2) |
|
143 return static_cast<int32_t>(d); |
|
144 |
|
145 if (isnan(d) || isinf(d)) { |
|
146 ok = false; |
|
147 return 0; |
|
148 } |
|
149 |
|
150 double d32 = fmod(trunc(d), D32); |
|
151 if (d32 >= D32 / 2) |
|
152 d32 -= D32; |
|
153 else if (d32 < -D32 / 2) |
|
154 d32 += D32; |
|
155 return static_cast<int32_t>(d32); |
|
156 } |
|
157 |
|
158 uint32_t toUInt32SlowCase(double d, bool& ok) |
|
159 { |
|
160 ok = true; |
|
161 |
|
162 if (d >= 0.0 && d < D32) |
|
163 return static_cast<uint32_t>(d); |
|
164 |
|
165 if (isnan(d) || isinf(d)) { |
|
166 ok = false; |
|
167 return 0; |
|
168 } |
|
169 |
|
170 double d32 = fmod(trunc(d), D32); |
|
171 if (d32 < 0) |
|
172 d32 += D32; |
|
173 return static_cast<uint32_t>(d32); |
|
174 } |
|
175 |
|
176 NEVER_INLINE double nonInlineNaN() |
|
177 { |
|
178 #if OS(SYMBIAN) |
|
179 return nanval(); |
|
180 #else |
|
181 return std::numeric_limits<double>::quiet_NaN(); |
|
182 #endif |
|
183 } |
|
184 |
|
185 bool JSValue::isValidCallee() |
|
186 { |
|
187 return asObject(asObject(asCell())->getAnonymousValue(0))->isGlobalObject(); |
|
188 } |
|
189 |
|
190 } // namespace JSC |