]>
Commit | Line | Data |
---|---|---|
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, 2012 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 "JSCJSValue.h" | |
25 | ||
26 | #include "BooleanConstructor.h" | |
27 | #include "BooleanPrototype.h" | |
28 | #include "CustomGetterSetter.h" | |
29 | #include "Error.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> | |
40 | ||
41 | namespace JSC { | |
42 | ||
43 | // ECMA 9.4 | |
44 | double JSValue::toInteger(ExecState* exec) const | |
45 | { | |
46 | if (isInt32()) | |
47 | return asInt32(); | |
48 | double d = toNumber(exec); | |
49 | return std::isnan(d) ? 0.0 : trunc(d); | |
50 | } | |
51 | ||
52 | double JSValue::toIntegerPreserveNaN(ExecState* exec) const | |
53 | { | |
54 | if (isInt32()) | |
55 | return asInt32(); | |
56 | return trunc(toNumber(exec)); | |
57 | } | |
58 | ||
59 | double JSValue::toNumberSlowCase(ExecState* exec) const | |
60 | { | |
61 | ASSERT(!isInt32() && !isDouble()); | |
62 | if (isCell()) | |
63 | return asCell()->toNumber(exec); | |
64 | if (isTrue()) | |
65 | return 1.0; | |
66 | return isUndefined() ? PNaN : 0; // null and false both convert to 0. | |
67 | } | |
68 | ||
69 | JSObject* JSValue::toObjectSlowCase(ExecState* exec, JSGlobalObject* globalObject) const | |
70 | { | |
71 | ASSERT(!isCell()); | |
72 | ||
73 | if (isInt32() || isDouble()) | |
74 | return constructNumber(exec, globalObject, asValue()); | |
75 | if (isTrue() || isFalse()) | |
76 | return constructBooleanFromImmediateBoolean(exec, globalObject, asValue()); | |
77 | ||
78 | ASSERT(isUndefinedOrNull()); | |
79 | VM& vm = exec->vm(); | |
80 | vm.throwException(exec, createNotAnObjectError(exec, *this)); | |
81 | return JSNotAnObject::create(vm); | |
82 | } | |
83 | ||
84 | JSValue JSValue::toThisSlowCase(ExecState* exec, ECMAMode ecmaMode) const | |
85 | { | |
86 | ASSERT(!isCell()); | |
87 | ||
88 | if (ecmaMode == StrictMode) | |
89 | return *this; | |
90 | ||
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(); | |
97 | } | |
98 | ||
99 | JSObject* JSValue::synthesizePrototype(ExecState* exec) const | |
100 | { | |
101 | if (isCell()) { | |
102 | if (isString()) | |
103 | return exec->lexicalGlobalObject()->stringPrototype(); | |
104 | ASSERT(isSymbol()); | |
105 | return exec->lexicalGlobalObject()->symbolPrototype(); | |
106 | } | |
107 | ||
108 | if (isNumber()) | |
109 | return exec->lexicalGlobalObject()->numberPrototype(); | |
110 | if (isBoolean()) | |
111 | return exec->lexicalGlobalObject()->booleanPrototype(); | |
112 | ||
113 | ASSERT(isUndefinedOrNull()); | |
114 | VM& vm = exec->vm(); | |
115 | vm.throwException(exec, createNotAnObjectError(exec, *this)); | |
116 | return JSNotAnObject::create(vm); | |
117 | } | |
118 | ||
119 | // ECMA 8.7.2 | |
120 | void JSValue::putToPrimitive(ExecState* exec, PropertyName propertyName, JSValue value, PutPropertySlot& slot) | |
121 | { | |
122 | VM& vm = exec->vm(); | |
123 | ||
124 | if (Optional<uint32_t> index = parseIndex(propertyName)) { | |
125 | putToPrimitiveByIndex(exec, index.value(), value, slot.isStrictMode()); | |
126 | return; | |
127 | } | |
128 | ||
129 | // Check if there are any setters or getters in the prototype chain | |
130 | JSObject* obj = synthesizePrototype(exec); | |
131 | JSValue prototype; | |
132 | if (propertyName != exec->propertyNames().underscoreProto) { | |
133 | for (; !obj->structure()->hasReadOnlyOrGetterSetterPropertiesExcludingProto(); obj = asObject(prototype)) { | |
134 | prototype = obj->prototype(); | |
135 | if (prototype.isNull()) { | |
136 | if (slot.isStrictMode()) | |
137 | throwTypeError(exec, StrictModeReadonlyPropertyWriteError); | |
138 | return; | |
139 | } | |
140 | } | |
141 | } | |
142 | ||
143 | for (; ; obj = asObject(prototype)) { | |
144 | unsigned attributes; | |
145 | PropertyOffset offset = obj->structure()->get(vm, propertyName, attributes); | |
146 | if (offset != invalidOffset) { | |
147 | if (attributes & ReadOnly) { | |
148 | if (slot.isStrictMode()) | |
149 | exec->vm().throwException(exec, createTypeError(exec, StrictModeReadonlyPropertyWriteError)); | |
150 | return; | |
151 | } | |
152 | ||
153 | JSValue gs = obj->getDirect(offset); | |
154 | if (gs.isGetterSetter()) { | |
155 | callSetter(exec, *this, gs, value, slot.isStrictMode() ? StrictMode : NotStrictMode); | |
156 | return; | |
157 | } | |
158 | ||
159 | if (gs.isCustomGetterSetter()) { | |
160 | callCustomSetter(exec, gs, obj, slot.thisValue(), value); | |
161 | return; | |
162 | } | |
163 | ||
164 | // If there's an existing property on the object or one of its | |
165 | // prototypes it should be replaced, so break here. | |
166 | break; | |
167 | } | |
168 | ||
169 | prototype = obj->prototype(); | |
170 | if (prototype.isNull()) | |
171 | break; | |
172 | } | |
173 | ||
174 | if (slot.isStrictMode()) | |
175 | throwTypeError(exec, StrictModeReadonlyPropertyWriteError); | |
176 | return; | |
177 | } | |
178 | ||
179 | void JSValue::putToPrimitiveByIndex(ExecState* exec, unsigned propertyName, JSValue value, bool shouldThrow) | |
180 | { | |
181 | if (propertyName > MAX_ARRAY_INDEX) { | |
182 | PutPropertySlot slot(*this, shouldThrow); | |
183 | putToPrimitive(exec, Identifier::from(exec, propertyName), value, slot); | |
184 | return; | |
185 | } | |
186 | ||
187 | if (synthesizePrototype(exec)->attemptToInterceptPutByIndexOnHoleForPrototype(exec, *this, propertyName, value, shouldThrow)) | |
188 | return; | |
189 | ||
190 | if (shouldThrow) | |
191 | throwTypeError(exec, StrictModeReadonlyPropertyWriteError); | |
192 | } | |
193 | ||
194 | void JSValue::dump(PrintStream& out) const | |
195 | { | |
196 | dumpInContext(out, 0); | |
197 | } | |
198 | ||
199 | void JSValue::dumpInContext(PrintStream& out, DumpContext* context) const | |
200 | { | |
201 | dumpInContextAssumingStructure( | |
202 | out, context, (!!*this && isCell()) ? asCell()->structure() : nullptr); | |
203 | } | |
204 | ||
205 | void JSValue::dumpInContextAssumingStructure( | |
206 | PrintStream& out, DumpContext* context, Structure* structure) const | |
207 | { | |
208 | if (!*this) | |
209 | out.print("<JSValue()>"); | |
210 | else if (isInt32()) | |
211 | out.printf("Int32: %d", asInt32()); | |
212 | else if (isDouble()) { | |
213 | #if USE(JSVALUE64) | |
214 | out.printf("Double: %lld, %lf", (long long)reinterpretDoubleToInt64(asDouble()), asDouble()); | |
215 | #else | |
216 | union { | |
217 | double asDouble; | |
218 | uint32_t asTwoInt32s[2]; | |
219 | } u; | |
220 | u.asDouble = asDouble(); | |
221 | out.printf("Double: %08x:%08x, %lf", u.asTwoInt32s[1], u.asTwoInt32s[0], asDouble()); | |
222 | #endif | |
223 | } else if (isCell()) { | |
224 | if (structure->classInfo()->isSubClassOf(JSString::info())) { | |
225 | JSString* string = jsCast<JSString*>(asCell()); | |
226 | out.print("String"); | |
227 | if (string->isRope()) | |
228 | out.print(" (rope)"); | |
229 | const StringImpl* impl = string->tryGetValueImpl(); | |
230 | if (impl) { | |
231 | if (impl->isAtomic()) | |
232 | out.print(" (atomic)"); | |
233 | if (impl->isAtomic()) | |
234 | out.print(" (identifier)"); | |
235 | if (impl->isSymbol()) | |
236 | out.print(" (symbol)"); | |
237 | } else | |
238 | out.print(" (unresolved)"); | |
239 | out.print(": ", impl); | |
240 | } else if (structure->classInfo()->isSubClassOf(Structure::info())) | |
241 | out.print("Structure: ", inContext(*jsCast<Structure*>(asCell()), context)); | |
242 | else { | |
243 | out.print("Cell: ", RawPointer(asCell())); | |
244 | out.print(" (", inContext(*structure, context), ")"); | |
245 | } | |
246 | #if USE(JSVALUE64) | |
247 | out.print(", ID: ", asCell()->structureID()); | |
248 | #endif | |
249 | } else if (isTrue()) | |
250 | out.print("True"); | |
251 | else if (isFalse()) | |
252 | out.print("False"); | |
253 | else if (isNull()) | |
254 | out.print("Null"); | |
255 | else if (isUndefined()) | |
256 | out.print("Undefined"); | |
257 | else | |
258 | out.print("INVALID"); | |
259 | } | |
260 | ||
261 | void JSValue::dumpForBacktrace(PrintStream& out) const | |
262 | { | |
263 | if (!*this) | |
264 | out.print("<JSValue()>"); | |
265 | else if (isInt32()) | |
266 | out.printf("%d", asInt32()); | |
267 | else if (isDouble()) | |
268 | out.printf("%lf", asDouble()); | |
269 | else if (isCell()) { | |
270 | if (asCell()->inherits(JSString::info())) { | |
271 | JSString* string = jsCast<JSString*>(asCell()); | |
272 | const StringImpl* impl = string->tryGetValueImpl(); | |
273 | if (impl) | |
274 | out.print("\"", impl, "\""); | |
275 | else | |
276 | out.print("(unresolved string)"); | |
277 | } else if (asCell()->inherits(Structure::info())) { | |
278 | out.print("Structure[ ", asCell()->structure()->classInfo()->className); | |
279 | #if USE(JSVALUE64) | |
280 | out.print(" ID: ", asCell()->structureID()); | |
281 | #endif | |
282 | out.print("]: ", RawPointer(asCell())); | |
283 | } else { | |
284 | out.print("Cell[", asCell()->structure()->classInfo()->className); | |
285 | #if USE(JSVALUE64) | |
286 | out.print(" ID: ", asCell()->structureID()); | |
287 | #endif | |
288 | out.print("]: ", RawPointer(asCell())); | |
289 | } | |
290 | } else if (isTrue()) | |
291 | out.print("True"); | |
292 | else if (isFalse()) | |
293 | out.print("False"); | |
294 | else if (isNull()) | |
295 | out.print("Null"); | |
296 | else if (isUndefined()) | |
297 | out.print("Undefined"); | |
298 | else | |
299 | out.print("INVALID"); | |
300 | } | |
301 | ||
302 | // This in the ToInt32 operation is defined in section 9.5 of the ECMA-262 spec. | |
303 | // Note that this operation is identical to ToUInt32 other than to interpretation | |
304 | // of the resulting bit-pattern (as such this metod is also called to implement | |
305 | // ToUInt32). | |
306 | // | |
307 | // The operation can be descibed as round towards zero, then select the 32 least | |
308 | // bits of the resulting value in 2s-complement representation. | |
309 | int32_t toInt32(double number) | |
310 | { | |
311 | int64_t bits = WTF::bitwise_cast<int64_t>(number); | |
312 | int32_t exp = (static_cast<int32_t>(bits >> 52) & 0x7ff) - 0x3ff; | |
313 | ||
314 | // If exponent < 0 there will be no bits to the left of the decimal point | |
315 | // after rounding; if the exponent is > 83 then no bits of precision can be | |
316 | // left in the low 32-bit range of the result (IEEE-754 doubles have 52 bits | |
317 | // of fractional precision). | |
318 | // Note this case handles 0, -0, and all infinte, NaN, & denormal value. | |
319 | if (exp < 0 || exp > 83) | |
320 | return 0; | |
321 | ||
322 | // Select the appropriate 32-bits from the floating point mantissa. If the | |
323 | // exponent is 52 then the bits we need to select are already aligned to the | |
324 | // lowest bits of the 64-bit integer representation of tghe number, no need | |
325 | // to shift. If the exponent is greater than 52 we need to shift the value | |
326 | // left by (exp - 52), if the value is less than 52 we need to shift right | |
327 | // accordingly. | |
328 | int32_t result = (exp > 52) | |
329 | ? static_cast<int32_t>(bits << (exp - 52)) | |
330 | : static_cast<int32_t>(bits >> (52 - exp)); | |
331 | ||
332 | // IEEE-754 double precision values are stored omitting an implicit 1 before | |
333 | // the decimal point; we need to reinsert this now. We may also the shifted | |
334 | // invalid bits into the result that are not a part of the mantissa (the sign | |
335 | // and exponent bits from the floatingpoint representation); mask these out. | |
336 | if (exp < 32) { | |
337 | int32_t missingOne = 1 << exp; | |
338 | result &= missingOne - 1; | |
339 | result += missingOne; | |
340 | } | |
341 | ||
342 | // If the input value was negative (we could test either 'number' or 'bits', | |
343 | // but testing 'bits' is likely faster) invert the result appropriately. | |
344 | return bits < 0 ? -result : result; | |
345 | } | |
346 | ||
347 | bool JSValue::isValidCallee() | |
348 | { | |
349 | return asObject(asCell())->globalObject(); | |
350 | } | |
351 | ||
352 | JSString* JSValue::toStringSlowCase(ExecState* exec) const | |
353 | { | |
354 | VM& vm = exec->vm(); | |
355 | ASSERT(!isString()); | |
356 | if (isInt32()) { | |
357 | auto integer = asInt32(); | |
358 | if (static_cast<unsigned>(integer) <= 9) | |
359 | return vm.smallStrings.singleCharacterString(integer + '0'); | |
360 | return jsNontrivialString(&vm, vm.numericStrings.add(integer)); | |
361 | } | |
362 | if (isDouble()) | |
363 | return jsString(&vm, vm.numericStrings.add(asDouble())); | |
364 | if (isTrue()) | |
365 | return vm.smallStrings.trueString(); | |
366 | if (isFalse()) | |
367 | return vm.smallStrings.falseString(); | |
368 | if (isNull()) | |
369 | return vm.smallStrings.nullString(); | |
370 | if (isUndefined()) | |
371 | return vm.smallStrings.undefinedString(); | |
372 | if (isSymbol()) { | |
373 | throwTypeError(exec); | |
374 | return jsEmptyString(exec); | |
375 | } | |
376 | ||
377 | ASSERT(isCell()); | |
378 | JSValue value = asCell()->toPrimitive(exec, PreferString); | |
379 | if (exec->hadException()) | |
380 | return jsEmptyString(exec); | |
381 | ASSERT(!value.isObject()); | |
382 | return value.toString(exec); | |
383 | } | |
384 | ||
385 | String JSValue::toWTFStringSlowCase(ExecState* exec) const | |
386 | { | |
387 | VM& vm = exec->vm(); | |
388 | if (isInt32()) | |
389 | return vm.numericStrings.add(asInt32()); | |
390 | if (isDouble()) | |
391 | return vm.numericStrings.add(asDouble()); | |
392 | if (isTrue()) | |
393 | return vm.propertyNames->trueKeyword.string(); | |
394 | if (isFalse()) | |
395 | return vm.propertyNames->falseKeyword.string(); | |
396 | if (isNull()) | |
397 | return vm.propertyNames->nullKeyword.string(); | |
398 | if (isUndefined()) | |
399 | return vm.propertyNames->undefinedKeyword.string(); | |
400 | return toString(exec)->value(exec); | |
401 | } | |
402 | ||
403 | } // namespace JSC |