Operations.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #ifndef Operations_h
  22. #define Operations_h
  23. #include "ExceptionHelpers.h"
  24. #include "Interpreter.h"
  25. #include "JSCJSValueInlines.h"
  26. #include "JSProxy.h"
  27. #include "JSString.h"
  28. #include "StructureInlines.h"
  29. namespace JSC {
  30. NEVER_INLINE JSValue jsAddSlowCase(CallFrame*, JSValue, JSValue);
  31. JSValue jsTypeStringForValue(CallFrame*, JSValue);
  32. JSValue jsTypeStringForValue(VM&, JSGlobalObject*, JSValue);
  33. bool jsIsObjectType(CallFrame*, JSValue);
  34. bool jsIsFunctionType(JSValue);
  35. ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, JSString* s2)
  36. {
  37. VM& vm = exec->vm();
  38. int32_t length1 = s1->length();
  39. if (!length1)
  40. return s2;
  41. int32_t length2 = s2->length();
  42. if (!length2)
  43. return s1;
  44. if ((length1 + length2) < 0)
  45. return throwOutOfMemoryError(exec);
  46. return JSRopeString::create(vm, s1, s2);
  47. }
  48. ALWAYS_INLINE JSValue jsString(ExecState* exec, const String& u1, const String& u2, const String& u3)
  49. {
  50. VM* vm = &exec->vm();
  51. int32_t length1 = u1.length();
  52. int32_t length2 = u2.length();
  53. int32_t length3 = u3.length();
  54. if (length1 < 0 || length2 < 0 || length3 < 0)
  55. return throwOutOfMemoryError(exec);
  56. if (!length1)
  57. return jsString(exec, jsString(vm, u2), jsString(vm, u3));
  58. if (!length2)
  59. return jsString(exec, jsString(vm, u1), jsString(vm, u3));
  60. if (!length3)
  61. return jsString(exec, jsString(vm, u1), jsString(vm, u2));
  62. if ((length1 + length2) < 0)
  63. return throwOutOfMemoryError(exec);
  64. if ((length1 + length2 + length3) < 0)
  65. return throwOutOfMemoryError(exec);
  66. return JSRopeString::create(exec->vm(), jsString(vm, u1), jsString(vm, u2), jsString(vm, u3));
  67. }
  68. ALWAYS_INLINE JSValue jsString(ExecState* exec, Register* strings, unsigned count)
  69. {
  70. VM* vm = &exec->vm();
  71. JSRopeString::RopeBuilder ropeBuilder(*vm);
  72. for (unsigned i = 0; i < count; ++i) {
  73. JSValue v = strings[i].jsValue();
  74. if (!ropeBuilder.append(v.toString(exec)))
  75. return throwOutOfMemoryError(exec);
  76. }
  77. return ropeBuilder.release();
  78. }
  79. ALWAYS_INLINE JSValue jsStringFromArguments(ExecState* exec, JSValue thisValue)
  80. {
  81. VM* vm = &exec->vm();
  82. JSRopeString::RopeBuilder ropeBuilder(*vm);
  83. ropeBuilder.append(thisValue.toString(exec));
  84. for (unsigned i = 0; i < exec->argumentCount(); ++i) {
  85. JSValue v = exec->argument(i);
  86. if (!ropeBuilder.append(v.toString(exec)))
  87. return throwOutOfMemoryError(exec);
  88. }
  89. return ropeBuilder.release();
  90. }
  91. // See ES5 11.8.1/11.8.2/11.8.5 for definition of leftFirst, this value ensures correct
  92. // evaluation ordering for argument conversions for '<' and '>'. For '<' pass the value
  93. // true, for leftFirst, for '>' pass the value false (and reverse operand order).
  94. template<bool leftFirst>
  95. ALWAYS_INLINE bool jsLess(CallFrame* callFrame, JSValue v1, JSValue v2)
  96. {
  97. if (v1.isInt32() && v2.isInt32())
  98. return v1.asInt32() < v2.asInt32();
  99. if (v1.isNumber() && v2.isNumber())
  100. return v1.asNumber() < v2.asNumber();
  101. if (isJSString(v1) && isJSString(v2))
  102. return codePointCompareLessThan(asString(v1)->value(callFrame), asString(v2)->value(callFrame));
  103. double n1;
  104. double n2;
  105. JSValue p1;
  106. JSValue p2;
  107. bool wasNotString1;
  108. bool wasNotString2;
  109. if (leftFirst) {
  110. wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
  111. wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
  112. } else {
  113. wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
  114. wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
  115. }
  116. if (wasNotString1 | wasNotString2)
  117. return n1 < n2;
  118. return codePointCompareLessThan(asString(p1)->value(callFrame), asString(p2)->value(callFrame));
  119. }
  120. // See ES5 11.8.3/11.8.4/11.8.5 for definition of leftFirst, this value ensures correct
  121. // evaluation ordering for argument conversions for '<=' and '=>'. For '<=' pass the
  122. // value true, for leftFirst, for '=>' pass the value false (and reverse operand order).
  123. template<bool leftFirst>
  124. ALWAYS_INLINE bool jsLessEq(CallFrame* callFrame, JSValue v1, JSValue v2)
  125. {
  126. if (v1.isInt32() && v2.isInt32())
  127. return v1.asInt32() <= v2.asInt32();
  128. if (v1.isNumber() && v2.isNumber())
  129. return v1.asNumber() <= v2.asNumber();
  130. if (isJSString(v1) && isJSString(v2))
  131. return !codePointCompareLessThan(asString(v2)->value(callFrame), asString(v1)->value(callFrame));
  132. double n1;
  133. double n2;
  134. JSValue p1;
  135. JSValue p2;
  136. bool wasNotString1;
  137. bool wasNotString2;
  138. if (leftFirst) {
  139. wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
  140. wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
  141. } else {
  142. wasNotString2 = v2.getPrimitiveNumber(callFrame, n2, p2);
  143. wasNotString1 = v1.getPrimitiveNumber(callFrame, n1, p1);
  144. }
  145. if (wasNotString1 | wasNotString2)
  146. return n1 <= n2;
  147. return !codePointCompareLessThan(asString(p2)->value(callFrame), asString(p1)->value(callFrame));
  148. }
  149. // Fast-path choices here are based on frequency data from SunSpider:
  150. // <times> Add case: <t1> <t2>
  151. // ---------------------------
  152. // 5626160 Add case: 3 3 (of these, 3637690 are for immediate values)
  153. // 247412 Add case: 5 5
  154. // 20900 Add case: 5 6
  155. // 13962 Add case: 5 3
  156. // 4000 Add case: 3 5
  157. ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2)
  158. {
  159. if (v1.isNumber() && v2.isNumber())
  160. return jsNumber(v1.asNumber() + v2.asNumber());
  161. if (v1.isString() && !v2.isObject())
  162. return jsString(callFrame, asString(v1), v2.toString(callFrame));
  163. // All other cases are pretty uncommon
  164. return jsAddSlowCase(callFrame, v1, v2);
  165. }
  166. #define InvalidPrototypeChain (std::numeric_limits<size_t>::max())
  167. inline size_t normalizePrototypeChainForChainAccess(CallFrame* callFrame, JSValue base, JSValue slotBase, const Identifier& propertyName, PropertyOffset& slotOffset)
  168. {
  169. JSCell* cell = base.asCell();
  170. size_t count = 0;
  171. while (slotBase != cell) {
  172. if (cell->isProxy())
  173. return InvalidPrototypeChain;
  174. if (cell->structure()->typeInfo().hasImpureGetOwnPropertySlot())
  175. return InvalidPrototypeChain;
  176. JSValue v = cell->structure()->prototypeForLookup(callFrame);
  177. // If we didn't find slotBase in base's prototype chain, then base
  178. // must be a proxy for another object.
  179. if (v.isNull())
  180. return InvalidPrototypeChain;
  181. cell = v.asCell();
  182. // Since we're accessing a prototype in a loop, it's a good bet that it
  183. // should not be treated as a dictionary.
  184. if (cell->structure()->isDictionary()) {
  185. asObject(cell)->flattenDictionaryObject(callFrame->vm());
  186. if (slotBase == cell)
  187. slotOffset = cell->structure()->get(callFrame->vm(), propertyName);
  188. }
  189. ++count;
  190. }
  191. ASSERT(count);
  192. return count;
  193. }
  194. inline size_t normalizePrototypeChain(CallFrame* callFrame, JSCell* base)
  195. {
  196. size_t count = 0;
  197. while (1) {
  198. if (base->isProxy())
  199. return InvalidPrototypeChain;
  200. JSValue v = base->structure()->prototypeForLookup(callFrame);
  201. if (v.isNull())
  202. return count;
  203. base = v.asCell();
  204. // Since we're accessing a prototype in a loop, it's a good bet that it
  205. // should not be treated as a dictionary.
  206. if (base->structure()->isDictionary())
  207. asObject(base)->flattenDictionaryObject(callFrame->vm());
  208. ++count;
  209. }
  210. }
  211. inline bool isPrototypeChainNormalized(JSGlobalObject* globalObject, Structure* structure)
  212. {
  213. for (;;) {
  214. if (structure->typeInfo().type() == ProxyType)
  215. return false;
  216. JSValue v = structure->prototypeForLookup(globalObject);
  217. if (v.isNull())
  218. return true;
  219. structure = v.asCell()->structure();
  220. if (structure->isDictionary())
  221. return false;
  222. }
  223. }
  224. } // namespace JSC
  225. #endif // Operations_h