2
* Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3
* Copyright (C) 2002, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
25
#include "ExceptionHelpers.h"
26
#include "Interpreter.h"
29
#include "JSValueInlines.h"
33
NEVER_INLINE JSValue jsAddSlowCase(CallFrame*, JSValue, JSValue);
34
JSValue jsTypeStringForValue(CallFrame*, JSValue);
35
bool jsIsObjectType(CallFrame*, JSValue);
36
bool jsIsFunctionType(JSValue);
38
ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, JSString* s2)
40
JSGlobalData& globalData = exec->globalData();
42
unsigned length1 = s1->length();
45
unsigned length2 = s2->length();
48
if ((length1 + length2) < length1)
49
return throwOutOfMemoryError(exec);
51
return JSRopeString::create(globalData, s1, s2);
54
ALWAYS_INLINE JSValue jsString(ExecState* exec, const String& u1, const String& u2, const String& u3)
56
JSGlobalData* globalData = &exec->globalData();
58
unsigned length1 = u1.length();
59
unsigned length2 = u2.length();
60
unsigned length3 = u3.length();
62
return jsString(exec, jsString(globalData, u2), jsString(globalData, u3));
64
return jsString(exec, jsString(globalData, u1), jsString(globalData, u3));
66
return jsString(exec, jsString(globalData, u1), jsString(globalData, u2));
68
if ((length1 + length2) < length1)
69
return throwOutOfMemoryError(exec);
70
if ((length1 + length2 + length3) < length3)
71
return throwOutOfMemoryError(exec);
73
return JSRopeString::create(exec->globalData(), jsString(globalData, u1), jsString(globalData, u2), jsString(globalData, u3));
76
ALWAYS_INLINE JSValue jsString(ExecState* exec, Register* strings, unsigned count)
78
JSGlobalData* globalData = &exec->globalData();
79
JSRopeString::RopeBuilder ropeBuilder(*globalData);
81
unsigned oldLength = 0;
83
for (unsigned i = 0; i < count; ++i) {
84
JSValue v = strings[i].jsValue();
85
ropeBuilder.append(v.toString(exec));
87
if (ropeBuilder.length() < oldLength) // True for overflow
88
return throwOutOfMemoryError(exec);
89
oldLength = ropeBuilder.length();
92
return ropeBuilder.release();
95
ALWAYS_INLINE JSValue jsStringFromArguments(ExecState* exec, JSValue thisValue)
97
JSGlobalData* globalData = &exec->globalData();
98
JSRopeString::RopeBuilder ropeBuilder(*globalData);
99
ropeBuilder.append(thisValue.toString(exec));
101
unsigned oldLength = 0;
103
for (unsigned i = 0; i < exec->argumentCount(); ++i) {
104
JSValue v = exec->argument(i);
105
ropeBuilder.append(v.toString(exec));
107
if (ropeBuilder.length() < oldLength) // True for overflow
108
return throwOutOfMemoryError(exec);
109
oldLength = ropeBuilder.length();
112
return ropeBuilder.release();
116
inline bool JSValue::equal(ExecState* exec, JSValue v1, JSValue v2)
118
if (v1.isInt32() && v2.isInt32())
121
return equalSlowCase(exec, v1, v2);
124
ALWAYS_INLINE bool JSValue::equalSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2)
127
if (v1.isNumber() && v2.isNumber())
128
return v1.asNumber() == v2.asNumber();
130
bool s1 = v1.isString();
131
bool s2 = v2.isString();
133
return asString(v1)->value(exec) == asString(v2)->value(exec);
135
if (v1.isUndefinedOrNull()) {
136
if (v2.isUndefinedOrNull())
140
return v2.asCell()->structure()->masqueradesAsUndefined(exec->lexicalGlobalObject());
143
if (v2.isUndefinedOrNull()) {
146
return v1.asCell()->structure()->masqueradesAsUndefined(exec->lexicalGlobalObject());
152
JSValue p1 = v1.toPrimitive(exec);
153
if (exec->hadException())
156
if (v1.isInt32() && v2.isInt32())
162
JSValue p2 = v2.toPrimitive(exec);
163
if (exec->hadException())
166
if (v1.isInt32() && v2.isInt32())
172
double d1 = v1.toNumber(exec);
173
double d2 = v2.toNumber(exec);
177
if (v1.isBoolean()) {
179
return static_cast<double>(v1.asBoolean()) == v2.asNumber();
180
} else if (v2.isBoolean()) {
182
return v1.asNumber() == static_cast<double>(v2.asBoolean());
190
ALWAYS_INLINE bool JSValue::strictEqualSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2)
192
ASSERT(v1.isCell() && v2.isCell());
194
if (v1.asCell()->isString() && v2.asCell()->isString())
195
return asString(v1)->value(exec) == asString(v2)->value(exec);
200
inline bool JSValue::strictEqual(ExecState* exec, JSValue v1, JSValue v2)
202
if (v1.isInt32() && v2.isInt32())
205
if (v1.isNumber() && v2.isNumber())
206
return v1.asNumber() == v2.asNumber();
208
if (!v1.isCell() || !v2.isCell())
211
return strictEqualSlowCaseInline(exec, v1, v2);
214
// See ES5 11.8.1/11.8.2/11.8.5 for definition of leftFirst, this value ensures correct
215
// evaluation ordering for argument conversions for '<' and '>'. For '<' pass the value
216
// true, for leftFirst, for '>' pass the value false (and reverse operand order).
217
template<bool leftFirst>
218
ALWAYS_INLINE bool jsLess(CallFrame* callFrame, JSValue v1, JSValue v2)
220
if (v1.isInt32() && v2.isInt32())
221
return v1.asInt32() < v2.asInt32();
223
if (v1.isNumber() && v2.isNumber())
224
return v1.asNumber() < v2.asNumber();
226
if (isJSString(v1) && isJSString(v2))
227
return codePointCompareLessThan(asString(v1)->value(callFrame), asString(v2)->value(callFrame));
236
wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
237
wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
239
wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
240
wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
243
if (wasNotString1 | wasNotString2)
245
return codePointCompareLessThan(asString(p1)->value(callFrame), asString(p2)->value(callFrame));
248
// See ES5 11.8.3/11.8.4/11.8.5 for definition of leftFirst, this value ensures correct
249
// evaluation ordering for argument conversions for '<=' and '=>'. For '<=' pass the
250
// value true, for leftFirst, for '=>' pass the value false (and reverse operand order).
251
template<bool leftFirst>
252
ALWAYS_INLINE bool jsLessEq(CallFrame* callFrame, JSValue v1, JSValue v2)
254
if (v1.isInt32() && v2.isInt32())
255
return v1.asInt32() <= v2.asInt32();
257
if (v1.isNumber() && v2.isNumber())
258
return v1.asNumber() <= v2.asNumber();
260
if (isJSString(v1) && isJSString(v2))
261
return !codePointCompareLessThan(asString(v2)->value(callFrame), asString(v1)->value(callFrame));
270
wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
271
wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
273
wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
274
wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
277
if (wasNotString1 | wasNotString2)
279
return !codePointCompareLessThan(asString(p2)->value(callFrame), asString(p1)->value(callFrame));
282
// Fast-path choices here are based on frequency data from SunSpider:
283
// <times> Add case: <t1> <t2>
284
// ---------------------------
285
// 5626160 Add case: 3 3 (of these, 3637690 are for immediate values)
286
// 247412 Add case: 5 5
287
// 20900 Add case: 5 6
288
// 13962 Add case: 5 3
289
// 4000 Add case: 3 5
291
ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2)
293
if (v1.isNumber() && v2.isNumber())
294
return jsNumber(v1.asNumber() + v2.asNumber());
296
if (v1.isString() && !v2.isObject())
297
return jsString(callFrame, asString(v1), v2.toString(callFrame));
299
// All other cases are pretty uncommon
300
return jsAddSlowCase(callFrame, v1, v2);
303
#define InvalidPrototypeChain (std::numeric_limits<size_t>::max())
305
inline size_t normalizePrototypeChain(CallFrame* callFrame, JSValue base, JSValue slotBase, const Identifier& propertyName, PropertyOffset& slotOffset)
307
JSCell* cell = base.asCell();
310
while (slotBase != cell) {
312
return InvalidPrototypeChain;
314
JSValue v = cell->structure()->prototypeForLookup(callFrame);
316
// If we didn't find slotBase in base's prototype chain, then base
317
// must be a proxy for another object.
320
return InvalidPrototypeChain;
324
// Since we're accessing a prototype in a loop, it's a good bet that it
325
// should not be treated as a dictionary.
326
if (cell->structure()->isDictionary()) {
327
asObject(cell)->flattenDictionaryObject(callFrame->globalData());
328
if (slotBase == cell)
329
slotOffset = cell->structure()->get(callFrame->globalData(), propertyName);
339
inline size_t normalizePrototypeChain(CallFrame* callFrame, JSCell* base)
344
return InvalidPrototypeChain;
346
JSValue v = base->structure()->prototypeForLookup(callFrame);
352
// Since we're accessing a prototype in a loop, it's a good bet that it
353
// should not be treated as a dictionary.
354
if (base->structure()->isDictionary())
355
asObject(base)->flattenDictionaryObject(callFrame->globalData());
361
inline bool isPrototypeChainNormalized(JSGlobalObject* globalObject, Structure* structure)
364
if (structure->typeInfo().type() == ProxyType)
367
JSValue v = structure->prototypeForLookup(globalObject);
371
structure = v.asCell()->structure();
373
if (structure->isDictionary())
380
#endif // Operations_h