]>
Commit | Line | Data |
---|---|---|
9dae56ea A |
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 | ||
4e4e5a6f | 25 | #include "ExceptionHelpers.h" |
ba379fdc | 26 | #include "Interpreter.h" |
9dae56ea | 27 | #include "JSString.h" |
14957cd0 | 28 | #include "JSValueInlineMethods.h" |
9dae56ea A |
29 | |
30 | namespace JSC { | |
31 | ||
ba379fdc A |
32 | NEVER_INLINE JSValue jsAddSlowCase(CallFrame*, JSValue, JSValue); |
33 | JSValue jsTypeStringForValue(CallFrame*, JSValue); | |
34 | bool jsIsObjectType(JSValue); | |
35 | bool jsIsFunctionType(JSValue); | |
36 | ||
f9bf01c6 A |
37 | ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, JSString* s2) |
38 | { | |
6fe7ccc8 A |
39 | JSGlobalData& globalData = exec->globalData(); |
40 | ||
4e4e5a6f A |
41 | unsigned length1 = s1->length(); |
42 | if (!length1) | |
f9bf01c6 | 43 | return s2; |
4e4e5a6f A |
44 | unsigned length2 = s2->length(); |
45 | if (!length2) | |
f9bf01c6 | 46 | return s1; |
4e4e5a6f A |
47 | if ((length1 + length2) < length1) |
48 | return throwOutOfMemoryError(exec); | |
f9bf01c6 | 49 | |
6fe7ccc8 | 50 | return JSRopeString::create(globalData, s1, s2); |
f9bf01c6 A |
51 | } |
52 | ||
6fe7ccc8 | 53 | ALWAYS_INLINE JSValue jsString(ExecState* exec, const UString& u1, const UString& u2, const UString& u3) |
4e4e5a6f | 54 | { |
f9bf01c6 A |
55 | JSGlobalData* globalData = &exec->globalData(); |
56 | ||
14957cd0 A |
57 | unsigned length1 = u1.length(); |
58 | unsigned length2 = u2.length(); | |
59 | unsigned length3 = u3.length(); | |
4e4e5a6f | 60 | if (!length1) |
6fe7ccc8 | 61 | return jsString(exec, jsString(globalData, u2), jsString(globalData, u3)); |
4e4e5a6f | 62 | if (!length2) |
6fe7ccc8 | 63 | return jsString(exec, jsString(globalData, u1), jsString(globalData, u3)); |
4e4e5a6f | 64 | if (!length3) |
6fe7ccc8 | 65 | return jsString(exec, jsString(globalData, u1), jsString(globalData, u2)); |
f9bf01c6 | 66 | |
4e4e5a6f A |
67 | if ((length1 + length2) < length1) |
68 | return throwOutOfMemoryError(exec); | |
69 | if ((length1 + length2 + length3) < length3) | |
f9bf01c6 | 70 | return throwOutOfMemoryError(exec); |
4e4e5a6f | 71 | |
6fe7ccc8 | 72 | return JSRopeString::create(exec->globalData(), jsString(globalData, u1), jsString(globalData, u2), jsString(globalData, u3)); |
f9bf01c6 A |
73 | } |
74 | ||
75 | ALWAYS_INLINE JSValue jsString(ExecState* exec, Register* strings, unsigned count) | |
76 | { | |
f9bf01c6 | 77 | JSGlobalData* globalData = &exec->globalData(); |
6fe7ccc8 | 78 | JSRopeString::RopeBuilder ropeBuilder(*globalData); |
f9bf01c6 | 79 | |
6fe7ccc8 | 80 | unsigned oldLength = 0; |
4e4e5a6f | 81 | |
f9bf01c6 A |
82 | for (unsigned i = 0; i < count; ++i) { |
83 | JSValue v = strings[i].jsValue(); | |
6fe7ccc8 | 84 | ropeBuilder.append(v.toString(exec)); |
f9bf01c6 | 85 | |
6fe7ccc8 A |
86 | if (ropeBuilder.length() < oldLength) // True for overflow |
87 | return throwOutOfMemoryError(exec); | |
88 | } | |
4e4e5a6f | 89 | |
6fe7ccc8 | 90 | return ropeBuilder.release(); |
f9bf01c6 A |
91 | } |
92 | ||
6fe7ccc8 | 93 | ALWAYS_INLINE JSValue jsStringFromArguments(ExecState* exec, JSValue thisValue) |
f9bf01c6 | 94 | { |
6fe7ccc8 A |
95 | JSGlobalData* globalData = &exec->globalData(); |
96 | JSRopeString::RopeBuilder ropeBuilder(*globalData); | |
97 | ropeBuilder.append(thisValue.toString(exec)); | |
4e4e5a6f | 98 | |
6fe7ccc8 | 99 | unsigned oldLength = 0; |
4e4e5a6f | 100 | |
14957cd0 A |
101 | for (unsigned i = 0; i < exec->argumentCount(); ++i) { |
102 | JSValue v = exec->argument(i); | |
6fe7ccc8 | 103 | ropeBuilder.append(v.toString(exec)); |
4e4e5a6f | 104 | |
6fe7ccc8 A |
105 | if (ropeBuilder.length() < oldLength) // True for overflow |
106 | return throwOutOfMemoryError(exec); | |
107 | } | |
f9bf01c6 | 108 | |
6fe7ccc8 | 109 | return ropeBuilder.release(); |
f9bf01c6 A |
110 | } |
111 | ||
9dae56ea | 112 | // ECMA 11.9.3 |
ba379fdc | 113 | inline bool JSValue::equal(ExecState* exec, JSValue v1, JSValue v2) |
9dae56ea | 114 | { |
ba379fdc | 115 | if (v1.isInt32() && v2.isInt32()) |
9dae56ea A |
116 | return v1 == v2; |
117 | ||
118 | return equalSlowCase(exec, v1, v2); | |
119 | } | |
120 | ||
ba379fdc | 121 | ALWAYS_INLINE bool JSValue::equalSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2) |
9dae56ea | 122 | { |
9dae56ea A |
123 | do { |
124 | if (v1.isNumber() && v2.isNumber()) | |
6fe7ccc8 | 125 | return v1.asNumber() == v2.asNumber(); |
9dae56ea A |
126 | |
127 | bool s1 = v1.isString(); | |
128 | bool s2 = v2.isString(); | |
129 | if (s1 && s2) | |
f9bf01c6 | 130 | return asString(v1)->value(exec) == asString(v2)->value(exec); |
9dae56ea A |
131 | |
132 | if (v1.isUndefinedOrNull()) { | |
133 | if (v2.isUndefinedOrNull()) | |
134 | return true; | |
ba379fdc | 135 | if (!v2.isCell()) |
9dae56ea A |
136 | return false; |
137 | return v2.asCell()->structure()->typeInfo().masqueradesAsUndefined(); | |
138 | } | |
139 | ||
140 | if (v2.isUndefinedOrNull()) { | |
ba379fdc | 141 | if (!v1.isCell()) |
9dae56ea A |
142 | return false; |
143 | return v1.asCell()->structure()->typeInfo().masqueradesAsUndefined(); | |
144 | } | |
145 | ||
146 | if (v1.isObject()) { | |
147 | if (v2.isObject()) | |
148 | return v1 == v2; | |
ba379fdc | 149 | JSValue p1 = v1.toPrimitive(exec); |
9dae56ea A |
150 | if (exec->hadException()) |
151 | return false; | |
152 | v1 = p1; | |
ba379fdc | 153 | if (v1.isInt32() && v2.isInt32()) |
9dae56ea A |
154 | return v1 == v2; |
155 | continue; | |
156 | } | |
157 | ||
158 | if (v2.isObject()) { | |
ba379fdc | 159 | JSValue p2 = v2.toPrimitive(exec); |
9dae56ea A |
160 | if (exec->hadException()) |
161 | return false; | |
162 | v2 = p2; | |
ba379fdc | 163 | if (v1.isInt32() && v2.isInt32()) |
9dae56ea A |
164 | return v1 == v2; |
165 | continue; | |
166 | } | |
167 | ||
168 | if (s1 || s2) { | |
169 | double d1 = v1.toNumber(exec); | |
170 | double d2 = v2.toNumber(exec); | |
171 | return d1 == d2; | |
172 | } | |
173 | ||
174 | if (v1.isBoolean()) { | |
175 | if (v2.isNumber()) | |
6fe7ccc8 | 176 | return static_cast<double>(v1.asBoolean()) == v2.asNumber(); |
9dae56ea A |
177 | } else if (v2.isBoolean()) { |
178 | if (v1.isNumber()) | |
6fe7ccc8 | 179 | return v1.asNumber() == static_cast<double>(v2.asBoolean()); |
9dae56ea A |
180 | } |
181 | ||
182 | return v1 == v2; | |
183 | } while (true); | |
184 | } | |
185 | ||
186 | // ECMA 11.9.3 | |
f9bf01c6 | 187 | ALWAYS_INLINE bool JSValue::strictEqualSlowCaseInline(ExecState* exec, JSValue v1, JSValue v2) |
ba379fdc A |
188 | { |
189 | ASSERT(v1.isCell() && v2.isCell()); | |
190 | ||
191 | if (v1.asCell()->isString() && v2.asCell()->isString()) | |
f9bf01c6 | 192 | return asString(v1)->value(exec) == asString(v2)->value(exec); |
ba379fdc A |
193 | |
194 | return v1 == v2; | |
195 | } | |
196 | ||
f9bf01c6 | 197 | inline bool JSValue::strictEqual(ExecState* exec, JSValue v1, JSValue v2) |
9dae56ea | 198 | { |
ba379fdc | 199 | if (v1.isInt32() && v2.isInt32()) |
9dae56ea A |
200 | return v1 == v2; |
201 | ||
202 | if (v1.isNumber() && v2.isNumber()) | |
6fe7ccc8 | 203 | return v1.asNumber() == v2.asNumber(); |
9dae56ea | 204 | |
ba379fdc | 205 | if (!v1.isCell() || !v2.isCell()) |
9dae56ea A |
206 | return v1 == v2; |
207 | ||
f9bf01c6 | 208 | return strictEqualSlowCaseInline(exec, v1, v2); |
9dae56ea A |
209 | } |
210 | ||
6fe7ccc8 A |
211 | // See ES5 11.8.1/11.8.2/11.8.5 for definition of leftFirst, this value ensures correct |
212 | // evaluation ordering for argument conversions for '<' and '>'. For '<' pass the value | |
213 | // true, for leftFirst, for '>' pass the value false (and reverse operand order). | |
214 | template<bool leftFirst> | |
f9bf01c6 | 215 | ALWAYS_INLINE bool jsLess(CallFrame* callFrame, JSValue v1, JSValue v2) |
9dae56ea | 216 | { |
ba379fdc A |
217 | if (v1.isInt32() && v2.isInt32()) |
218 | return v1.asInt32() < v2.asInt32(); | |
9dae56ea | 219 | |
6fe7ccc8 A |
220 | if (v1.isNumber() && v2.isNumber()) |
221 | return v1.asNumber() < v2.asNumber(); | |
9dae56ea | 222 | |
6fe7ccc8 | 223 | if (isJSString(v1) && isJSString(v2)) |
f9bf01c6 | 224 | return asString(v1)->value(callFrame) < asString(v2)->value(callFrame); |
ba379fdc | 225 | |
6fe7ccc8 A |
226 | double n1; |
227 | double n2; | |
ba379fdc A |
228 | JSValue p1; |
229 | JSValue p2; | |
6fe7ccc8 A |
230 | bool wasNotString1; |
231 | bool wasNotString2; | |
232 | if (leftFirst) { | |
233 | wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1); | |
234 | wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2); | |
235 | } else { | |
236 | wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2); | |
237 | wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1); | |
238 | } | |
ba379fdc A |
239 | |
240 | if (wasNotString1 | wasNotString2) | |
241 | return n1 < n2; | |
f9bf01c6 | 242 | return asString(p1)->value(callFrame) < asString(p2)->value(callFrame); |
ba379fdc A |
243 | } |
244 | ||
6fe7ccc8 A |
245 | // See ES5 11.8.3/11.8.4/11.8.5 for definition of leftFirst, this value ensures correct |
246 | // evaluation ordering for argument conversions for '<=' and '=>'. For '<=' pass the | |
247 | // value true, for leftFirst, for '=>' pass the value false (and reverse operand order). | |
248 | template<bool leftFirst> | |
249 | ALWAYS_INLINE bool jsLessEq(CallFrame* callFrame, JSValue v1, JSValue v2) | |
ba379fdc A |
250 | { |
251 | if (v1.isInt32() && v2.isInt32()) | |
252 | return v1.asInt32() <= v2.asInt32(); | |
253 | ||
6fe7ccc8 A |
254 | if (v1.isNumber() && v2.isNumber()) |
255 | return v1.asNumber() <= v2.asNumber(); | |
ba379fdc | 256 | |
6fe7ccc8 | 257 | if (isJSString(v1) && isJSString(v2)) |
f9bf01c6 | 258 | return !(asString(v2)->value(callFrame) < asString(v1)->value(callFrame)); |
ba379fdc | 259 | |
6fe7ccc8 A |
260 | double n1; |
261 | double n2; | |
ba379fdc A |
262 | JSValue p1; |
263 | JSValue p2; | |
6fe7ccc8 A |
264 | bool wasNotString1; |
265 | bool wasNotString2; | |
266 | if (leftFirst) { | |
267 | wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1); | |
268 | wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2); | |
269 | } else { | |
270 | wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2); | |
271 | wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1); | |
272 | } | |
ba379fdc A |
273 | |
274 | if (wasNotString1 | wasNotString2) | |
275 | return n1 <= n2; | |
f9bf01c6 | 276 | return !(asString(p2)->value(callFrame) < asString(p1)->value(callFrame)); |
ba379fdc A |
277 | } |
278 | ||
279 | // Fast-path choices here are based on frequency data from SunSpider: | |
280 | // <times> Add case: <t1> <t2> | |
281 | // --------------------------- | |
282 | // 5626160 Add case: 3 3 (of these, 3637690 are for immediate values) | |
283 | // 247412 Add case: 5 5 | |
284 | // 20900 Add case: 5 6 | |
285 | // 13962 Add case: 5 3 | |
286 | // 4000 Add case: 3 5 | |
287 | ||
288 | ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2) | |
289 | { | |
6fe7ccc8 A |
290 | if (v1.isNumber() && v2.isNumber()) |
291 | return jsNumber(v1.asNumber() + v2.asNumber()); | |
ba379fdc | 292 | |
6fe7ccc8 A |
293 | if (v1.isString() && !v2.isObject()) |
294 | return jsString(callFrame, asString(v1), v2.toString(callFrame)); | |
ba379fdc A |
295 | |
296 | // All other cases are pretty uncommon | |
297 | return jsAddSlowCase(callFrame, v1, v2); | |
298 | } | |
299 | ||
300 | inline size_t normalizePrototypeChain(CallFrame* callFrame, JSValue base, JSValue slotBase, const Identifier& propertyName, size_t& slotOffset) | |
301 | { | |
14957cd0 | 302 | JSCell* cell = base.asCell(); |
ba379fdc A |
303 | size_t count = 0; |
304 | ||
305 | while (slotBase != cell) { | |
306 | JSValue v = cell->structure()->prototypeForLookup(callFrame); | |
307 | ||
308 | // If we didn't find slotBase in base's prototype chain, then base | |
309 | // must be a proxy for another object. | |
310 | ||
311 | if (v.isNull()) | |
312 | return 0; | |
313 | ||
14957cd0 | 314 | cell = v.asCell(); |
ba379fdc A |
315 | |
316 | // Since we're accessing a prototype in a loop, it's a good bet that it | |
317 | // should not be treated as a dictionary. | |
318 | if (cell->structure()->isDictionary()) { | |
14957cd0 | 319 | asObject(cell)->flattenDictionaryObject(callFrame->globalData()); |
ba379fdc | 320 | if (slotBase == cell) |
14957cd0 | 321 | slotOffset = cell->structure()->get(callFrame->globalData(), propertyName); |
ba379fdc A |
322 | } |
323 | ||
324 | ++count; | |
325 | } | |
326 | ||
327 | ASSERT(count); | |
328 | return count; | |
329 | } | |
330 | ||
f9bf01c6 A |
331 | inline size_t normalizePrototypeChain(CallFrame* callFrame, JSCell* base) |
332 | { | |
333 | size_t count = 0; | |
334 | while (1) { | |
335 | JSValue v = base->structure()->prototypeForLookup(callFrame); | |
336 | if (v.isNull()) | |
337 | return count; | |
338 | ||
14957cd0 | 339 | base = v.asCell(); |
f9bf01c6 A |
340 | |
341 | // Since we're accessing a prototype in a loop, it's a good bet that it | |
342 | // should not be treated as a dictionary. | |
343 | if (base->structure()->isDictionary()) | |
14957cd0 | 344 | asObject(base)->flattenDictionaryObject(callFrame->globalData()); |
f9bf01c6 A |
345 | |
346 | ++count; | |
347 | } | |
348 | } | |
349 | ||
14957cd0 | 350 | ALWAYS_INLINE JSValue resolveBase(CallFrame* callFrame, Identifier& property, ScopeChainNode* scopeChain, bool isStrictPut) |
ba379fdc A |
351 | { |
352 | ScopeChainIterator iter = scopeChain->begin(); | |
353 | ScopeChainIterator next = iter; | |
354 | ++next; | |
355 | ScopeChainIterator end = scopeChain->end(); | |
356 | ASSERT(iter != end); | |
357 | ||
358 | PropertySlot slot; | |
359 | JSObject* base; | |
360 | while (true) { | |
14957cd0 A |
361 | base = iter->get(); |
362 | if (next == end) { | |
363 | if (isStrictPut && !base->getPropertySlot(callFrame, property, slot)) | |
364 | return JSValue(); | |
365 | return base; | |
366 | } | |
367 | if (base->getPropertySlot(callFrame, property, slot)) | |
ba379fdc A |
368 | return base; |
369 | ||
370 | iter = next; | |
371 | ++next; | |
372 | } | |
373 | ||
374 | ASSERT_NOT_REACHED(); | |
375 | return JSValue(); | |
376 | } | |
ba379fdc | 377 | } // namespace JSC |
9dae56ea | 378 | |
ba379fdc | 379 | #endif // Operations_h |