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, 2012 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.
24 #include "JSCJSValue.h"
26 #include "BooleanConstructor.h"
27 #include "BooleanPrototype.h"
29 #include "ExceptionHelpers.h"
30 #include "GetterSetter.h"
31 #include "JSCJSValueInlines.h"
32 #include "JSFunction.h"
33 #include "JSGlobalObject.h"
34 #include "JSNotAnObject.h"
35 #include "NumberObject.h"
36 #include <wtf/MathExtras.h>
37 #include <wtf/StringExtras.h>
41 static const double D32
= 4294967296.0;
44 double JSValue::toInteger(ExecState
* exec
) const
48 double d
= toNumber(exec
);
49 return std::isnan(d
) ? 0.0 : trunc(d
);
52 double JSValue::toIntegerPreserveNaN(ExecState
* exec
) const
56 return trunc(toNumber(exec
));
59 double JSValue::toNumberSlowCase(ExecState
* exec
) const
61 ASSERT(!isInt32() && !isDouble());
63 return asCell()->toNumber(exec
);
66 return isUndefined() ? QNaN
: 0; // null and false both convert to 0.
69 JSObject
* JSValue::toObjectSlowCase(ExecState
* exec
, JSGlobalObject
* globalObject
) const
73 if (isInt32() || isDouble())
74 return constructNumber(exec
, globalObject
, asValue());
75 if (isTrue() || isFalse())
76 return constructBooleanFromImmediateBoolean(exec
, globalObject
, asValue());
78 ASSERT(isUndefinedOrNull());
79 throwError(exec
, createNotAnObjectError(exec
, *this));
80 return JSNotAnObject::create(exec
);
83 JSObject
* JSValue::toThisObjectSlowCase(ExecState
* exec
) const
87 if (isInt32() || isDouble())
88 return constructNumber(exec
, exec
->lexicalGlobalObject(), asValue());
89 if (isTrue() || isFalse())
90 return constructBooleanFromImmediateBoolean(exec
, exec
->lexicalGlobalObject(), asValue());
91 ASSERT(isUndefinedOrNull());
92 return exec
->globalThisValue();
95 JSObject
* JSValue::synthesizePrototype(ExecState
* exec
) const
99 return exec
->lexicalGlobalObject()->stringPrototype();
103 return exec
->lexicalGlobalObject()->numberPrototype();
105 return exec
->lexicalGlobalObject()->booleanPrototype();
107 ASSERT(isUndefinedOrNull());
108 throwError(exec
, createNotAnObjectError(exec
, *this));
109 return JSNotAnObject::create(exec
);
113 void JSValue::putToPrimitive(ExecState
* exec
, PropertyName propertyName
, JSValue value
, PutPropertySlot
& slot
)
117 unsigned index
= propertyName
.asIndex();
118 if (index
!= PropertyName::NotAnIndex
) {
119 putToPrimitiveByIndex(exec
, index
, value
, slot
.isStrictMode());
123 // Check if there are any setters or getters in the prototype chain
124 JSObject
* obj
= synthesizePrototype(exec
);
126 if (propertyName
!= exec
->propertyNames().underscoreProto
) {
127 for (; !obj
->structure()->hasReadOnlyOrGetterSetterPropertiesExcludingProto(); obj
= asObject(prototype
)) {
128 prototype
= obj
->prototype();
129 if (prototype
.isNull()) {
130 if (slot
.isStrictMode())
131 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
137 for (; ; obj
= asObject(prototype
)) {
139 JSCell
* specificValue
;
140 PropertyOffset offset
= obj
->structure()->get(vm
, propertyName
, attributes
, specificValue
);
141 if (offset
!= invalidOffset
) {
142 if (attributes
& ReadOnly
) {
143 if (slot
.isStrictMode())
144 throwError(exec
, createTypeError(exec
, StrictModeReadonlyPropertyWriteError
));
148 JSValue gs
= obj
->getDirect(offset
);
149 if (gs
.isGetterSetter()) {
150 JSObject
* setterFunc
= asGetterSetter(gs
)->setter();
152 if (slot
.isStrictMode())
153 throwError(exec
, createTypeError(exec
, ASCIILiteral("setting a property that has only a getter")));
158 CallType callType
= setterFunc
->methodTable()->getCallData(setterFunc
, callData
);
159 MarkedArgumentBuffer args
;
162 // If this is WebCore's global object then we need to substitute the shell.
163 call(exec
, setterFunc
, callType
, callData
, *this, args
);
167 // If there's an existing property on the object or one of its
168 // prototypes it should be replaced, so break here.
172 prototype
= obj
->prototype();
173 if (prototype
.isNull())
177 if (slot
.isStrictMode())
178 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
182 void JSValue::putToPrimitiveByIndex(ExecState
* exec
, unsigned propertyName
, JSValue value
, bool shouldThrow
)
184 if (propertyName
> MAX_ARRAY_INDEX
) {
185 PutPropertySlot
slot(shouldThrow
);
186 putToPrimitive(exec
, Identifier::from(exec
, propertyName
), value
, slot
);
190 if (synthesizePrototype(exec
)->attemptToInterceptPutByIndexOnHoleForPrototype(exec
, *this, propertyName
, value
, shouldThrow
))
194 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
197 void JSValue::dump(PrintStream
& out
) const
200 out
.print("<JSValue()>");
202 out
.printf("Int32: %d", asInt32());
203 else if (isDouble()) {
205 out
.printf("Double: %lld, %lf", (long long)reinterpretDoubleToInt64(asDouble()), asDouble());
209 uint32_t asTwoInt32s
[2];
211 u
.asDouble
= asDouble();
212 out
.printf("Double: %08x:%08x, %lf", u
.asTwoInt32s
[1], u
.asTwoInt32s
[0], asDouble());
214 } else if (isCell()) {
215 if (asCell()->inherits(&JSString::s_info
)) {
216 JSString
* string
= jsCast
<JSString
*>(asCell());
217 out
.print("String: ");
218 if (string
->isRope())
219 out
.print("(rope) ");
220 out
.print(string
->tryGetValue());
221 } else if (asCell()->inherits(&Structure::s_info
)) {
222 Structure
* structure
= jsCast
<Structure
*>(asCell());
224 "Structure: ", RawPointer(structure
), ": ", structure
->classInfo()->className
,
225 ", ", IndexingTypeDump(structure
->indexingTypeIncludingHistory()));
227 out
.print("Cell: ", RawPointer(asCell()));
228 if (isObject() && asObject(*this)->butterfly())
229 out
.print("->", RawPointer(asObject(*this)->butterfly()));
231 " (", RawPointer(asCell()->structure()), ": ", asCell()->structure()->classInfo()->className
,
232 ", ", IndexingTypeDump(asCell()->structure()->indexingTypeIncludingHistory()), ")");
240 else if (isUndefined())
241 out
.print("Undefined");
243 out
.print("INVALID");
246 // This in the ToInt32 operation is defined in section 9.5 of the ECMA-262 spec.
247 // Note that this operation is identical to ToUInt32 other than to interpretation
248 // of the resulting bit-pattern (as such this metod is also called to implement
251 // The operation can be descibed as round towards zero, then select the 32 least
252 // bits of the resulting value in 2s-complement representation.
253 int32_t toInt32(double number
)
255 int64_t bits
= WTF::bitwise_cast
<int64_t>(number
);
256 int32_t exp
= (static_cast<int32_t>(bits
>> 52) & 0x7ff) - 0x3ff;
258 // If exponent < 0 there will be no bits to the left of the decimal point
259 // after rounding; if the exponent is > 83 then no bits of precision can be
260 // left in the low 32-bit range of the result (IEEE-754 doubles have 52 bits
261 // of fractional precision).
262 // Note this case handles 0, -0, and all infinte, NaN, & denormal value.
263 if (exp
< 0 || exp
> 83)
266 // Select the appropriate 32-bits from the floating point mantissa. If the
267 // exponent is 52 then the bits we need to select are already aligned to the
268 // lowest bits of the 64-bit integer representation of tghe number, no need
269 // to shift. If the exponent is greater than 52 we need to shift the value
270 // left by (exp - 52), if the value is less than 52 we need to shift right
272 int32_t result
= (exp
> 52)
273 ? static_cast<int32_t>(bits
<< (exp
- 52))
274 : static_cast<int32_t>(bits
>> (52 - exp
));
276 // IEEE-754 double precision values are stored omitting an implicit 1 before
277 // the decimal point; we need to reinsert this now. We may also the shifted
278 // invalid bits into the result that are not a part of the mantissa (the sign
279 // and exponent bits from the floatingpoint representation); mask these out.
281 int32_t missingOne
= 1 << exp
;
282 result
&= missingOne
- 1;
283 result
+= missingOne
;
286 // If the input value was negative (we could test either 'number' or 'bits',
287 // but testing 'bits' is likely faster) invert the result appropriately.
288 return bits
< 0 ? -result
: result
;
291 bool JSValue::isValidCallee()
293 return asObject(asCell())->globalObject();
296 JSString
* JSValue::toStringSlowCase(ExecState
* exec
) const
301 return jsString(&vm
, vm
.numericStrings
.add(asInt32()));
303 return jsString(&vm
, vm
.numericStrings
.add(asDouble()));
305 return vm
.smallStrings
.trueString();
307 return vm
.smallStrings
.falseString();
309 return vm
.smallStrings
.nullString();
311 return vm
.smallStrings
.undefinedString();
314 JSValue value
= asCell()->toPrimitive(exec
, PreferString
);
315 if (exec
->hadException())
316 return jsEmptyString(exec
);
317 ASSERT(!value
.isObject());
318 return value
.toString(exec
);
321 String
JSValue::toWTFStringSlowCase(ExecState
* exec
) const
323 return inlineJSValueNotStringtoString(*this, exec
);