1 /* 2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org) 3 * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 4 * 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. 9 * 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. 14 * 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. 19 * 20 */ 21 22 #ifndef Operations_h 23 #define Operations_h 24 25 #include "Interpreter.h" 26 #include "JSImmediate.h" 27 #include "JSNumberCell.h" 28 #include "JSString.h" 29 30 namespace JSC { 31 32 NEVER_INLINE JSValue throwOutOfMemoryError(ExecState*); 33 NEVER_INLINE JSValue jsAddSlowCase(CallFrame*, JSValue, JSValue); 34 JSValue jsTypeStringForValue(CallFrame*, JSValue); 35 bool jsIsObjectType(JSValue); 36 bool jsIsFunctionType(JSValue); 37 38 // ECMA 11.9.3 equal(ExecState * exec,JSValue v1,JSValue v2)39 inline bool JSValue::equal(ExecState* exec, JSValue v1, JSValue v2) 40 { 41 if (v1.isInt32() && v2.isInt32()) 42 return v1 == v2; 43 44 return equalSlowCase(exec, v1, v2); 45 } 46 equalSlowCaseInline(ExecState * exec,JSValue v1,JSValue v2)47 ALWAYS_INLINE bool JSValue::equalSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2) 48 { 49 do { 50 if (v1.isNumber() && v2.isNumber()) 51 return v1.uncheckedGetNumber() == v2.uncheckedGetNumber(); 52 53 bool s1 = v1.isString(); 54 bool s2 = v2.isString(); 55 if (s1 && s2) 56 return asString(v1)->value() == asString(v2)->value(); 57 58 if (v1.isUndefinedOrNull()) { 59 if (v2.isUndefinedOrNull()) 60 return true; 61 if (!v2.isCell()) 62 return false; 63 return v2.asCell()->structure()->typeInfo().masqueradesAsUndefined(); 64 } 65 66 if (v2.isUndefinedOrNull()) { 67 if (!v1.isCell()) 68 return false; 69 return v1.asCell()->structure()->typeInfo().masqueradesAsUndefined(); 70 } 71 72 if (v1.isObject()) { 73 if (v2.isObject()) 74 return v1 == v2; 75 JSValue p1 = v1.toPrimitive(exec); 76 if (exec->hadException()) 77 return false; 78 v1 = p1; 79 if (v1.isInt32() && v2.isInt32()) 80 return v1 == v2; 81 continue; 82 } 83 84 if (v2.isObject()) { 85 JSValue p2 = v2.toPrimitive(exec); 86 if (exec->hadException()) 87 return false; 88 v2 = p2; 89 if (v1.isInt32() && v2.isInt32()) 90 return v1 == v2; 91 continue; 92 } 93 94 if (s1 || s2) { 95 double d1 = v1.toNumber(exec); 96 double d2 = v2.toNumber(exec); 97 return d1 == d2; 98 } 99 100 if (v1.isBoolean()) { 101 if (v2.isNumber()) 102 return static_cast<double>(v1.getBoolean()) == v2.uncheckedGetNumber(); 103 } else if (v2.isBoolean()) { 104 if (v1.isNumber()) 105 return v1.uncheckedGetNumber() == static_cast<double>(v2.getBoolean()); 106 } 107 108 return v1 == v2; 109 } while (true); 110 } 111 112 // ECMA 11.9.3 strictEqualSlowCaseInline(JSValue v1,JSValue v2)113 ALWAYS_INLINE bool JSValue::strictEqualSlowCaseInline(JSValue v1, JSValue v2) 114 { 115 ASSERT(v1.isCell() && v2.isCell()); 116 117 if (v1.asCell()->isString() && v2.asCell()->isString()) 118 return asString(v1)->value() == asString(v2)->value(); 119 120 return v1 == v2; 121 } 122 strictEqual(JSValue v1,JSValue v2)123 inline bool JSValue::strictEqual(JSValue v1, JSValue v2) 124 { 125 if (v1.isInt32() && v2.isInt32()) 126 return v1 == v2; 127 128 if (v1.isNumber() && v2.isNumber()) 129 return v1.uncheckedGetNumber() == v2.uncheckedGetNumber(); 130 131 if (!v1.isCell() || !v2.isCell()) 132 return v1 == v2; 133 134 return strictEqualSlowCaseInline(v1, v2); 135 } 136 jsLess(CallFrame * callFrame,JSValue v1,JSValue v2)137 inline bool jsLess(CallFrame* callFrame, JSValue v1, JSValue v2) 138 { 139 if (v1.isInt32() && v2.isInt32()) 140 return v1.asInt32() < v2.asInt32(); 141 142 double n1; 143 double n2; 144 if (v1.getNumber(n1) && v2.getNumber(n2)) 145 return n1 < n2; 146 147 JSGlobalData* globalData = &callFrame->globalData(); 148 if (isJSString(globalData, v1) && isJSString(globalData, v2)) 149 return asString(v1)->value() < asString(v2)->value(); 150 151 JSValue p1; 152 JSValue p2; 153 bool wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1); 154 bool wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2); 155 156 if (wasNotString1 | wasNotString2) 157 return n1 < n2; 158 159 return asString(p1)->value() < asString(p2)->value(); 160 } 161 jsLessEq(CallFrame * callFrame,JSValue v1,JSValue v2)162 inline bool jsLessEq(CallFrame* callFrame, JSValue v1, JSValue v2) 163 { 164 if (v1.isInt32() && v2.isInt32()) 165 return v1.asInt32() <= v2.asInt32(); 166 167 double n1; 168 double n2; 169 if (v1.getNumber(n1) && v2.getNumber(n2)) 170 return n1 <= n2; 171 172 JSGlobalData* globalData = &callFrame->globalData(); 173 if (isJSString(globalData, v1) && isJSString(globalData, v2)) 174 return !(asString(v2)->value() < asString(v1)->value()); 175 176 JSValue p1; 177 JSValue p2; 178 bool wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1); 179 bool wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2); 180 181 if (wasNotString1 | wasNotString2) 182 return n1 <= n2; 183 184 return !(asString(p2)->value() < asString(p1)->value()); 185 } 186 187 // Fast-path choices here are based on frequency data from SunSpider: 188 // <times> Add case: <t1> <t2> 189 // --------------------------- 190 // 5626160 Add case: 3 3 (of these, 3637690 are for immediate values) 191 // 247412 Add case: 5 5 192 // 20900 Add case: 5 6 193 // 13962 Add case: 5 3 194 // 4000 Add case: 3 5 195 jsAdd(CallFrame * callFrame,JSValue v1,JSValue v2)196 ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2) 197 { 198 double left; 199 double right = 0.0; 200 201 bool rightIsNumber = v2.getNumber(right); 202 if (rightIsNumber && v1.getNumber(left)) 203 return jsNumber(callFrame, left + right); 204 205 bool leftIsString = v1.isString(); 206 if (leftIsString && v2.isString()) { 207 RefPtr<UString::Rep> value = concatenate(asString(v1)->value().rep(), asString(v2)->value().rep()); 208 if (!value) 209 return throwOutOfMemoryError(callFrame); 210 return jsString(callFrame, value.release()); 211 } 212 213 if (rightIsNumber & leftIsString) { 214 RefPtr<UString::Rep> value = v2.isInt32() ? 215 concatenate(asString(v1)->value().rep(), v2.asInt32()) : 216 concatenate(asString(v1)->value().rep(), right); 217 218 if (!value) 219 return throwOutOfMemoryError(callFrame); 220 return jsString(callFrame, value.release()); 221 } 222 223 // All other cases are pretty uncommon 224 return jsAddSlowCase(callFrame, v1, v2); 225 } 226 countPrototypeChainEntriesAndCheckForProxies(CallFrame * callFrame,JSValue baseValue,const PropertySlot & slot)227 inline size_t countPrototypeChainEntriesAndCheckForProxies(CallFrame* callFrame, JSValue baseValue, const PropertySlot& slot) 228 { 229 JSCell* cell = asCell(baseValue); 230 size_t count = 0; 231 232 while (slot.slotBase() != cell) { 233 JSValue v = cell->structure()->prototypeForLookup(callFrame); 234 235 // If we didn't find slotBase in baseValue's prototype chain, then baseValue 236 // must be a proxy for another object. 237 238 if (v.isNull()) 239 return 0; 240 241 cell = asCell(v); 242 243 // Since we're accessing a prototype in a loop, it's a good bet that it 244 // should not be treated as a dictionary. 245 if (cell->structure()->isDictionary()) 246 asObject(cell)->setStructure(Structure::fromDictionaryTransition(cell->structure())); 247 248 ++count; 249 } 250 251 ASSERT(count); 252 return count; 253 } 254 resolveBase(CallFrame * callFrame,Identifier & property,ScopeChainNode * scopeChain)255 ALWAYS_INLINE JSValue resolveBase(CallFrame* callFrame, Identifier& property, ScopeChainNode* scopeChain) 256 { 257 ScopeChainIterator iter = scopeChain->begin(); 258 ScopeChainIterator next = iter; 259 ++next; 260 ScopeChainIterator end = scopeChain->end(); 261 ASSERT(iter != end); 262 263 PropertySlot slot; 264 JSObject* base; 265 while (true) { 266 base = *iter; 267 if (next == end || base->getPropertySlot(callFrame, property, slot)) 268 return base; 269 270 iter = next; 271 ++next; 272 } 273 274 ASSERT_NOT_REACHED(); 275 return JSValue(); 276 } 277 concatenateStrings(CallFrame * callFrame,Register * strings,unsigned count)278 ALWAYS_INLINE JSValue concatenateStrings(CallFrame* callFrame, Register* strings, unsigned count) 279 { 280 ASSERT(count >= 3); 281 282 // Estimate the amount of space required to hold the entire string. If all 283 // arguments are strings, we can easily calculate the exact amount of space 284 // required. For any other arguments, for now let's assume they may require 285 // 11 UChars of storage. This is enouch to hold any int, and likely is also 286 // reasonable for the other immediates. We may want to come back and tune 287 // this value at some point. 288 unsigned bufferSize = 0; 289 for (unsigned i = 0; i < count; ++i) { 290 JSValue v = strings[i].jsValue(); 291 if (LIKELY(v.isString())) 292 bufferSize += asString(v)->value().size(); 293 else 294 bufferSize += 11; 295 } 296 297 // Allocate an output string to store the result. 298 // If the first argument is a String, and if it has the capacity (or can grow 299 // its capacity) to hold the entire result then use this as a base to concatenate 300 // onto. Otherwise, allocate a new empty output buffer. 301 JSValue firstValue = strings[0].jsValue(); 302 RefPtr<UString::Rep> resultRep; 303 if (firstValue.isString() && (resultRep = asString(firstValue)->value().rep())->reserveCapacity(bufferSize)) { 304 // We're going to concatenate onto the first string - remove it from the list of items to be appended. 305 ++strings; 306 --count; 307 } else 308 resultRep = UString::Rep::createEmptyBuffer(bufferSize); 309 UString result(resultRep); 310 311 // Loop over the openards, writing them into the output buffer. 312 for (unsigned i = 0; i < count; ++i) { 313 JSValue v = strings[i].jsValue(); 314 if (LIKELY(v.isString())) 315 result.append(asString(v)->value()); 316 else if (v.isInt32()) 317 result.appendNumeric(v.asInt32()); 318 else { 319 double d; 320 if (v.getNumber(d)) 321 result.appendNumeric(d); 322 else 323 result.append(v.toString(callFrame)); 324 } 325 } 326 327 return jsString(callFrame, result); 328 } 329 330 } // namespace JSC 331 332 #endif // Operations_h 333