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"
27 #include "JSCJSValueInlines.h"
33 NEVER_INLINE JSValue jsAddSlowCase(CallFrame*, JSValue, JSValue);
34 JSValue jsTypeStringForValue(CallFrame*, JSValue);
35 JSValue jsTypeStringForValue(JSGlobalData&, JSGlobalObject*, JSValue);
36 bool jsIsObjectType(CallFrame*, JSValue);
37 bool jsIsFunctionType(JSValue);
39 ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, JSString* s2)
41 JSGlobalData& globalData = exec->globalData();
43 unsigned length1 = s1->length();
46 unsigned length2 = s2->length();
49 if ((length1 + length2) < length1)
50 return throwOutOfMemoryError(exec);
52 return JSRopeString::create(globalData, s1, s2);
55 ALWAYS_INLINE JSValue jsString(ExecState* exec, const String& u1, const String& u2, const String& u3)
57 JSGlobalData* globalData = &exec->globalData();
59 unsigned length1 = u1.length();
60 unsigned length2 = u2.length();
61 unsigned length3 = u3.length();
63 return jsString(exec, jsString(globalData, u2), jsString(globalData, u3));
65 return jsString(exec, jsString(globalData, u1), jsString(globalData, u3));
67 return jsString(exec, jsString(globalData, u1), jsString(globalData, u2));
69 if ((length1 + length2) < length1)
70 return throwOutOfMemoryError(exec);
71 if ((length1 + length2 + length3) < length3)
72 return throwOutOfMemoryError(exec);
74 return JSRopeString::create(exec->globalData(), jsString(globalData, u1), jsString(globalData, u2), jsString(globalData, u3));
77 ALWAYS_INLINE JSValue jsString(ExecState* exec, Register* strings, unsigned count)
79 JSGlobalData* globalData = &exec->globalData();
80 JSRopeString::RopeBuilder ropeBuilder(*globalData);
82 unsigned oldLength = 0;
84 for (unsigned i = 0; i < count; ++i) {
85 JSValue v = strings[i].jsValue();
86 ropeBuilder.append(v.toString(exec));
88 if (ropeBuilder.length() < oldLength) // True for overflow
89 return throwOutOfMemoryError(exec);
90 oldLength = ropeBuilder.length();
93 return ropeBuilder.release();
96 ALWAYS_INLINE JSValue jsStringFromArguments(ExecState* exec, JSValue thisValue)
98 JSGlobalData* globalData = &exec->globalData();
99 JSRopeString::RopeBuilder ropeBuilder(*globalData);
100 ropeBuilder.append(thisValue.toString(exec));
102 unsigned oldLength = 0;
104 for (unsigned i = 0; i < exec->argumentCount(); ++i) {
105 JSValue v = exec->argument(i);
106 ropeBuilder.append(v.toString(exec));
108 if (ropeBuilder.length() < oldLength) // True for overflow
109 return throwOutOfMemoryError(exec);
110 oldLength = ropeBuilder.length();
113 return ropeBuilder.release();
116 // See ES5 11.8.1/11.8.2/11.8.5 for definition of leftFirst, this value ensures correct
117 // evaluation ordering for argument conversions for '<' and '>'. For '<' pass the value
118 // true, for leftFirst, for '>' pass the value false (and reverse operand order).
119 template<bool leftFirst>
120 ALWAYS_INLINE bool jsLess(CallFrame* callFrame, JSValue v1, JSValue v2)
122 if (v1.isInt32() && v2.isInt32())
123 return v1.asInt32() < v2.asInt32();
125 if (v1.isNumber() && v2.isNumber())
126 return v1.asNumber() < v2.asNumber();
128 if (isJSString(v1) && isJSString(v2))
129 return codePointCompareLessThan(asString(v1)->value(callFrame), asString(v2)->value(callFrame));
138 wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
139 wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
141 wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
142 wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
145 if (wasNotString1 | wasNotString2)
147 return codePointCompareLessThan(asString(p1)->value(callFrame), asString(p2)->value(callFrame));
150 // See ES5 11.8.3/11.8.4/11.8.5 for definition of leftFirst, this value ensures correct
151 // evaluation ordering for argument conversions for '<=' and '=>'. For '<=' pass the
152 // value true, for leftFirst, for '=>' pass the value false (and reverse operand order).
153 template<bool leftFirst>
154 ALWAYS_INLINE bool jsLessEq(CallFrame* callFrame, JSValue v1, JSValue v2)
156 if (v1.isInt32() && v2.isInt32())
157 return v1.asInt32() <= v2.asInt32();
159 if (v1.isNumber() && v2.isNumber())
160 return v1.asNumber() <= v2.asNumber();
162 if (isJSString(v1) && isJSString(v2))
163 return !codePointCompareLessThan(asString(v2)->value(callFrame), asString(v1)->value(callFrame));
172 wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
173 wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
175 wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
176 wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
179 if (wasNotString1 | wasNotString2)
181 return !codePointCompareLessThan(asString(p2)->value(callFrame), asString(p1)->value(callFrame));
184 // Fast-path choices here are based on frequency data from SunSpider:
185 // <times> Add case: <t1> <t2>
186 // ---------------------------
187 // 5626160 Add case: 3 3 (of these, 3637690 are for immediate values)
188 // 247412 Add case: 5 5
189 // 20900 Add case: 5 6
190 // 13962 Add case: 5 3
191 // 4000 Add case: 3 5
193 ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2)
195 if (v1.isNumber() && v2.isNumber())
196 return jsNumber(v1.asNumber() + v2.asNumber());
198 if (v1.isString() && !v2.isObject())
199 return jsString(callFrame, asString(v1), v2.toString(callFrame));
201 // All other cases are pretty uncommon
202 return jsAddSlowCase(callFrame, v1, v2);
205 #define InvalidPrototypeChain (std::numeric_limits<size_t>::max())
207 inline size_t normalizePrototypeChainForChainAccess(CallFrame* callFrame, JSValue base, JSValue slotBase, const Identifier& propertyName, PropertyOffset& slotOffset)
209 JSCell* cell = base.asCell();
212 while (slotBase != cell) {
214 return InvalidPrototypeChain;
216 if (cell->structure()->typeInfo().hasImpureGetOwnPropertySlot())
217 return InvalidPrototypeChain;
219 JSValue v = cell->structure()->prototypeForLookup(callFrame);
221 // If we didn't find slotBase in base's prototype chain, then base
222 // must be a proxy for another object.
225 return InvalidPrototypeChain;
229 // Since we're accessing a prototype in a loop, it's a good bet that it
230 // should not be treated as a dictionary.
231 if (cell->structure()->isDictionary()) {
232 asObject(cell)->flattenDictionaryObject(callFrame->globalData());
233 if (slotBase == cell)
234 slotOffset = cell->structure()->get(callFrame->globalData(), propertyName);
244 inline size_t normalizePrototypeChain(CallFrame* callFrame, JSCell* base)
249 return InvalidPrototypeChain;
251 JSValue v = base->structure()->prototypeForLookup(callFrame);
257 // Since we're accessing a prototype in a loop, it's a good bet that it
258 // should not be treated as a dictionary.
259 if (base->structure()->isDictionary())
260 asObject(base)->flattenDictionaryObject(callFrame->globalData());
266 inline bool isPrototypeChainNormalized(JSGlobalObject* globalObject, Structure* structure)
269 if (structure->typeInfo().type() == ProxyType)
272 JSValue v = structure->prototypeForLookup(globalObject);
276 structure = v.asCell()->structure();
278 if (structure->isDictionary())
285 #endif // Operations_h