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"
28 #include "CustomGetterSetter.h"
30 #include "ExceptionHelpers.h"
31 #include "GetterSetter.h"
32 #include "JSCJSValueInlines.h"
33 #include "JSFunction.h"
34 #include "JSGlobalObject.h"
35 #include "JSNotAnObject.h"
36 #include "NumberObject.h"
37 #include "StructureInlines.h"
38 #include <wtf/MathExtras.h>
39 #include <wtf/StringExtras.h>
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() ? PNaN
: 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());
80 vm
.throwException(exec
, createNotAnObjectError(exec
, *this));
81 return JSNotAnObject::create(vm
);
84 JSValue
JSValue::toThisSlowCase(ExecState
* exec
, ECMAMode ecmaMode
) const
88 if (ecmaMode
== StrictMode
)
91 if (isInt32() || isDouble())
92 return constructNumber(exec
, exec
->lexicalGlobalObject(), asValue());
93 if (isTrue() || isFalse())
94 return constructBooleanFromImmediateBoolean(exec
, exec
->lexicalGlobalObject(), asValue());
95 ASSERT(isUndefinedOrNull());
96 return exec
->globalThisValue();
99 JSObject
* JSValue::synthesizePrototype(ExecState
* exec
) const
103 return exec
->lexicalGlobalObject()->stringPrototype();
107 return exec
->lexicalGlobalObject()->numberPrototype();
109 return exec
->lexicalGlobalObject()->booleanPrototype();
111 ASSERT(isUndefinedOrNull());
113 vm
.throwException(exec
, createNotAnObjectError(exec
, *this));
114 return JSNotAnObject::create(vm
);
118 void JSValue::putToPrimitive(ExecState
* exec
, PropertyName propertyName
, JSValue value
, PutPropertySlot
& slot
)
122 unsigned index
= propertyName
.asIndex();
123 if (index
!= PropertyName::NotAnIndex
) {
124 putToPrimitiveByIndex(exec
, index
, value
, slot
.isStrictMode());
128 // Check if there are any setters or getters in the prototype chain
129 JSObject
* obj
= synthesizePrototype(exec
);
131 if (propertyName
!= exec
->propertyNames().underscoreProto
) {
132 for (; !obj
->structure()->hasReadOnlyOrGetterSetterPropertiesExcludingProto(); obj
= asObject(prototype
)) {
133 prototype
= obj
->prototype();
134 if (prototype
.isNull()) {
135 if (slot
.isStrictMode())
136 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
142 for (; ; obj
= asObject(prototype
)) {
144 JSCell
* specificValue
;
145 PropertyOffset offset
= obj
->structure()->get(vm
, propertyName
, attributes
, specificValue
);
146 if (offset
!= invalidOffset
) {
147 if (attributes
& ReadOnly
) {
148 if (slot
.isStrictMode())
149 exec
->vm().throwException(exec
, createTypeError(exec
, StrictModeReadonlyPropertyWriteError
));
153 JSValue gs
= obj
->getDirect(offset
);
154 if (gs
.isGetterSetter()) {
155 callSetter(exec
, *this, gs
, value
, slot
.isStrictMode() ? StrictMode
: NotStrictMode
);
159 if (gs
.isCustomGetterSetter()) {
160 callCustomSetter(exec
, gs
, obj
, slot
.thisValue(), value
);
164 // If there's an existing property on the object or one of its
165 // prototypes it should be replaced, so break here.
169 prototype
= obj
->prototype();
170 if (prototype
.isNull())
174 if (slot
.isStrictMode())
175 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
179 void JSValue::putToPrimitiveByIndex(ExecState
* exec
, unsigned propertyName
, JSValue value
, bool shouldThrow
)
181 if (propertyName
> MAX_ARRAY_INDEX
) {
182 PutPropertySlot
slot(*this, shouldThrow
);
183 putToPrimitive(exec
, Identifier::from(exec
, propertyName
), value
, slot
);
187 if (synthesizePrototype(exec
)->attemptToInterceptPutByIndexOnHoleForPrototype(exec
, *this, propertyName
, value
, shouldThrow
))
191 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
194 void JSValue::dump(PrintStream
& out
) const
196 dumpInContext(out
, 0);
199 void JSValue::dumpInContext(PrintStream
& out
, DumpContext
* context
) const
202 out
.print("<JSValue()>");
204 out
.printf("Int32: %d", asInt32());
205 else if (isDouble()) {
207 out
.printf("Double: %lld, %lf", (long long)reinterpretDoubleToInt64(asDouble()), asDouble());
211 uint32_t asTwoInt32s
[2];
213 u
.asDouble
= asDouble();
214 out
.printf("Double: %08x:%08x, %lf", u
.asTwoInt32s
[1], u
.asTwoInt32s
[0], asDouble());
216 } else if (isCell()) {
217 if (asCell()->inherits(JSString::info())) {
218 JSString
* string
= jsCast
<JSString
*>(asCell());
220 if (string
->isRope())
221 out
.print(" (rope)");
222 const StringImpl
* impl
= string
->tryGetValueImpl();
224 if (impl
->isAtomic())
225 out
.print(" (atomic)");
226 if (impl
->isAtomic())
227 out
.print(" (identifier)");
228 if (impl
->isEmptyUnique())
229 out
.print(" (unique)");
231 out
.print(" (unresolved)");
232 out
.print(": ", impl
);
233 } else if (asCell()->inherits(Structure::info()))
234 out
.print("Structure: ", inContext(*jsCast
<Structure
*>(asCell()), context
));
236 out
.print("Cell: ", RawPointer(asCell()));
237 out
.print(" (", inContext(*asCell()->structure(), context
), ")");
240 out
.print(", ID: ", asCell()->structureID());
248 else if (isUndefined())
249 out
.print("Undefined");
251 out
.print("INVALID");
254 void JSValue::dumpForBacktrace(PrintStream
& out
) const
257 out
.print("<JSValue()>");
259 out
.printf("%d", asInt32());
261 out
.printf("%lf", asDouble());
263 if (asCell()->inherits(JSString::info())) {
264 JSString
* string
= jsCast
<JSString
*>(asCell());
265 const StringImpl
* impl
= string
->tryGetValueImpl();
267 out
.print("\"", impl
, "\"");
269 out
.print("(unresolved string)");
270 } else if (asCell()->inherits(Structure::info())) {
271 out
.print("Structure[ ", asCell()->structure()->classInfo()->className
);
273 out
.print(" ID: ", asCell()->structureID());
275 out
.print("]: ", RawPointer(asCell()));
277 out
.print("Cell[", asCell()->structure()->classInfo()->className
);
279 out
.print(" ID: ", asCell()->structureID());
281 out
.print("]: ", RawPointer(asCell()));
289 else if (isUndefined())
290 out
.print("Undefined");
292 out
.print("INVALID");
295 // This in the ToInt32 operation is defined in section 9.5 of the ECMA-262 spec.
296 // Note that this operation is identical to ToUInt32 other than to interpretation
297 // of the resulting bit-pattern (as such this metod is also called to implement
300 // The operation can be descibed as round towards zero, then select the 32 least
301 // bits of the resulting value in 2s-complement representation.
302 int32_t toInt32(double number
)
304 int64_t bits
= WTF::bitwise_cast
<int64_t>(number
);
305 int32_t exp
= (static_cast<int32_t>(bits
>> 52) & 0x7ff) - 0x3ff;
307 // If exponent < 0 there will be no bits to the left of the decimal point
308 // after rounding; if the exponent is > 83 then no bits of precision can be
309 // left in the low 32-bit range of the result (IEEE-754 doubles have 52 bits
310 // of fractional precision).
311 // Note this case handles 0, -0, and all infinte, NaN, & denormal value.
312 if (exp
< 0 || exp
> 83)
315 // Select the appropriate 32-bits from the floating point mantissa. If the
316 // exponent is 52 then the bits we need to select are already aligned to the
317 // lowest bits of the 64-bit integer representation of tghe number, no need
318 // to shift. If the exponent is greater than 52 we need to shift the value
319 // left by (exp - 52), if the value is less than 52 we need to shift right
321 int32_t result
= (exp
> 52)
322 ? static_cast<int32_t>(bits
<< (exp
- 52))
323 : static_cast<int32_t>(bits
>> (52 - exp
));
325 // IEEE-754 double precision values are stored omitting an implicit 1 before
326 // the decimal point; we need to reinsert this now. We may also the shifted
327 // invalid bits into the result that are not a part of the mantissa (the sign
328 // and exponent bits from the floatingpoint representation); mask these out.
330 int32_t missingOne
= 1 << exp
;
331 result
&= missingOne
- 1;
332 result
+= missingOne
;
335 // If the input value was negative (we could test either 'number' or 'bits',
336 // but testing 'bits' is likely faster) invert the result appropriately.
337 return bits
< 0 ? -result
: result
;
340 bool JSValue::isValidCallee()
342 return asObject(asCell())->globalObject();
345 JSString
* JSValue::toStringSlowCase(ExecState
* exec
) const
350 return jsString(&vm
, vm
.numericStrings
.add(asInt32()));
352 return jsString(&vm
, vm
.numericStrings
.add(asDouble()));
354 return vm
.smallStrings
.trueString();
356 return vm
.smallStrings
.falseString();
358 return vm
.smallStrings
.nullString();
360 return vm
.smallStrings
.undefinedString();
363 JSValue value
= asCell()->toPrimitive(exec
, PreferString
);
364 if (exec
->hadException())
365 return jsEmptyString(exec
);
366 ASSERT(!value
.isObject());
367 return value
.toString(exec
);
370 String
JSValue::toWTFStringSlowCase(ExecState
* exec
) const
372 return inlineJSValueNotStringtoString(*this, exec
);