JSString.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2001 Peter Kelly (pmk@post.com)
  4. * Copyright (C) 2004, 2007, 2008 Apple Inc. All rights reserved.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Library General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Library General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Library General Public License
  17. * along with this library; see the file COPYING.LIB. If not, write to
  18. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19. * Boston, MA 02110-1301, USA.
  20. *
  21. */
  22. #include "config.h"
  23. #include "JSString.h"
  24. #include "JSGlobalObject.h"
  25. #include "JSGlobalObjectFunctions.h"
  26. #include "JSObject.h"
  27. #include "Operations.h"
  28. #include "StringObject.h"
  29. #include "StringPrototype.h"
  30. namespace JSC {
  31. #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  32. const ClassInfo JSString::s_info = { "string", 0, 0, 0, CREATE_METHOD_TABLE(JSString) };
  33. void JSRopeString::RopeBuilder::expand()
  34. {
  35. ASSERT(m_index == JSRopeString::s_maxInternalRopeLength);
  36. JSString* jsString = m_jsString;
  37. RELEASE_ASSERT(jsString);
  38. m_jsString = jsStringBuilder(&m_vm);
  39. m_index = 0;
  40. append(jsString);
  41. }
  42. void JSString::destroy(JSCell* cell)
  43. {
  44. JSString* thisObject = static_cast<JSString*>(cell);
  45. thisObject->JSString::~JSString();
  46. }
  47. void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor)
  48. {
  49. JSString* thisObject = jsCast<JSString*>(cell);
  50. Base::visitChildren(thisObject, visitor);
  51. MARK_LOG_MESSAGE1("[%u]: ", thisObject->length());
  52. #if ENABLE(OBJECT_MARK_LOGGING)
  53. if (!thisObject->isRope()) {
  54. WTF::StringImpl* ourImpl = thisObject->m_value.impl();
  55. if (ourImpl->is8Bit())
  56. MARK_LOG_MESSAGE1("[8 %p]", ourImpl->characters8());
  57. else
  58. MARK_LOG_MESSAGE1("[16 %p]", ourImpl->characters16());
  59. } else
  60. MARK_LOG_MESSAGE0("[rope]: ");
  61. #endif
  62. if (thisObject->isRope())
  63. static_cast<JSRopeString*>(thisObject)->visitFibers(visitor);
  64. }
  65. void JSRopeString::visitFibers(SlotVisitor& visitor)
  66. {
  67. for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
  68. visitor.append(&m_fibers[i]);
  69. }
  70. void JSRopeString::resolveRope(ExecState* exec) const
  71. {
  72. ASSERT(isRope());
  73. if (is8Bit()) {
  74. LChar* buffer;
  75. if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) {
  76. Heap::heap(this)->reportExtraMemoryCost(newImpl->cost());
  77. m_value = newImpl.release();
  78. } else {
  79. outOfMemory(exec);
  80. return;
  81. }
  82. for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
  83. if (m_fibers[i]->isRope())
  84. return resolveRopeSlowCase8(buffer);
  85. }
  86. LChar* position = buffer;
  87. for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
  88. StringImpl* string = m_fibers[i]->m_value.impl();
  89. unsigned length = string->length();
  90. StringImpl::copyChars(position, string->characters8(), length);
  91. position += length;
  92. m_fibers[i].clear();
  93. }
  94. ASSERT((buffer + m_length) == position);
  95. ASSERT(!isRope());
  96. return;
  97. }
  98. UChar* buffer;
  99. if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) {
  100. Heap::heap(this)->reportExtraMemoryCost(newImpl->cost());
  101. m_value = newImpl.release();
  102. } else {
  103. outOfMemory(exec);
  104. return;
  105. }
  106. for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
  107. if (m_fibers[i]->isRope())
  108. return resolveRopeSlowCase(buffer);
  109. }
  110. UChar* position = buffer;
  111. for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
  112. StringImpl* string = m_fibers[i]->m_value.impl();
  113. unsigned length = string->length();
  114. if (string->is8Bit())
  115. StringImpl::copyChars(position, string->characters8(), length);
  116. else
  117. StringImpl::copyChars(position, string->characters16(), length);
  118. position += length;
  119. m_fibers[i].clear();
  120. }
  121. ASSERT((buffer + m_length) == position);
  122. ASSERT(!isRope());
  123. }
  124. // Overview: These functions convert a JSString from holding a string in rope form
  125. // down to a simple String representation. It does so by building up the string
  126. // backwards, since we want to avoid recursion, we expect that the tree structure
  127. // representing the rope is likely imbalanced with more nodes down the left side
  128. // (since appending to the string is likely more common) - and as such resolving
  129. // in this fashion should minimize work queue size. (If we built the queue forwards
  130. // we would likely have to place all of the constituent StringImpls into the
  131. // Vector before performing any concatenation, but by working backwards we likely
  132. // only fill the queue with the number of substrings at any given level in a
  133. // rope-of-ropes.)
  134. void JSRopeString::resolveRopeSlowCase8(LChar* buffer) const
  135. {
  136. LChar* position = buffer + m_length; // We will be working backwards over the rope.
  137. Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // Putting strings into a Vector is only OK because there are no GC points in this method.
  138. for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
  139. workQueue.append(m_fibers[i].get());
  140. // Clearing here works only because there are no GC points in this method.
  141. m_fibers[i].clear();
  142. }
  143. while (!workQueue.isEmpty()) {
  144. JSString* currentFiber = workQueue.last();
  145. workQueue.removeLast();
  146. if (currentFiber->isRope()) {
  147. JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
  148. for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->m_fibers[i]; ++i)
  149. workQueue.append(currentFiberAsRope->m_fibers[i].get());
  150. continue;
  151. }
  152. StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
  153. unsigned length = string->length();
  154. position -= length;
  155. StringImpl::copyChars(position, string->characters8(), length);
  156. }
  157. ASSERT(buffer == position);
  158. ASSERT(!isRope());
  159. }
  160. void JSRopeString::resolveRopeSlowCase(UChar* buffer) const
  161. {
  162. UChar* position = buffer + m_length; // We will be working backwards over the rope.
  163. Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // These strings are kept alive by the parent rope, so using a Vector is OK.
  164. for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
  165. workQueue.append(m_fibers[i].get());
  166. while (!workQueue.isEmpty()) {
  167. JSString* currentFiber = workQueue.last();
  168. workQueue.removeLast();
  169. if (currentFiber->isRope()) {
  170. JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
  171. for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->m_fibers[i]; ++i)
  172. workQueue.append(currentFiberAsRope->m_fibers[i].get());
  173. continue;
  174. }
  175. StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
  176. unsigned length = string->length();
  177. position -= length;
  178. if (string->is8Bit())
  179. StringImpl::copyChars(position, string->characters8(), length);
  180. else
  181. StringImpl::copyChars(position, string->characters16(), length);
  182. }
  183. ASSERT(buffer == position);
  184. ASSERT(!isRope());
  185. }
  186. void JSRopeString::outOfMemory(ExecState* exec) const
  187. {
  188. for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
  189. m_fibers[i].clear();
  190. ASSERT(isRope());
  191. ASSERT(m_value.isNull());
  192. if (exec)
  193. throwOutOfMemoryError(exec);
  194. }
  195. JSString* JSRopeString::getIndexSlowCase(ExecState* exec, unsigned i)
  196. {
  197. ASSERT(isRope());
  198. resolveRope(exec);
  199. // Return a safe no-value result, this should never be used, since the excetion will be thrown.
  200. if (exec->exception())
  201. return jsEmptyString(exec);
  202. ASSERT(!isRope());
  203. RELEASE_ASSERT(i < m_value.length());
  204. return jsSingleCharacterSubstring(exec, m_value, i);
  205. }
  206. JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
  207. {
  208. return const_cast<JSString*>(this);
  209. }
  210. bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const
  211. {
  212. result = this;
  213. number = jsToNumber(value(exec));
  214. return false;
  215. }
  216. #endif // #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  217. bool JSString::toBoolean() const
  218. {
  219. return m_length;
  220. }
  221. #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  222. double JSString::toNumber(ExecState* exec) const
  223. {
  224. return jsToNumber(value(exec));
  225. }
  226. inline StringObject* StringObject::create(ExecState* exec, JSGlobalObject* globalObject, JSString* string)
  227. {
  228. StringObject* object = new (NotNull, allocateCell<StringObject>(*exec->heap())) StringObject(exec->vm(), globalObject->stringObjectStructure());
  229. object->finishCreation(exec->vm(), string);
  230. return object;
  231. }
  232. JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const
  233. {
  234. return StringObject::create(exec, globalObject, const_cast<JSString*>(this));
  235. }
  236. JSObject* JSString::toThisObject(JSCell* cell, ExecState* exec)
  237. {
  238. return StringObject::create(exec, exec->lexicalGlobalObject(), jsCast<JSString*>(cell));
  239. }
  240. bool JSString::getOwnPropertySlot(JSCell* cell, ExecState* exec, PropertyName propertyName, PropertySlot& slot)
  241. {
  242. JSString* thisObject = jsCast<JSString*>(cell);
  243. // The semantics here are really getPropertySlot, not getOwnPropertySlot.
  244. // This function should only be called by JSValue::get.
  245. if (thisObject->getStringPropertySlot(exec, propertyName, slot))
  246. return true;
  247. slot.setBase(thisObject);
  248. JSObject* object;
  249. for (JSValue prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype.isNull(); prototype = object->prototype()) {
  250. object = asObject(prototype);
  251. if (object->methodTable()->getOwnPropertySlot(object, exec, propertyName, slot))
  252. return true;
  253. }
  254. slot.setUndefined();
  255. return true;
  256. }
  257. bool JSString::getStringPropertyDescriptor(ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
  258. {
  259. if (propertyName == exec->propertyNames().length) {
  260. descriptor.setDescriptor(jsNumber(m_length), DontEnum | DontDelete | ReadOnly);
  261. return true;
  262. }
  263. unsigned i = propertyName.asIndex();
  264. if (i < m_length) {
  265. ASSERT(i != PropertyName::NotAnIndex); // No need for an explicit check, the above test would always fail!
  266. descriptor.setDescriptor(getIndex(exec, i), DontDelete | ReadOnly);
  267. return true;
  268. }
  269. return false;
  270. }
  271. bool JSString::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)
  272. {
  273. JSString* thisObject = jsCast<JSString*>(cell);
  274. // The semantics here are really getPropertySlot, not getOwnPropertySlot.
  275. // This function should only be called by JSValue::get.
  276. if (thisObject->getStringPropertySlot(exec, propertyName, slot))
  277. return true;
  278. return JSString::getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot);
  279. }
  280. #endif // #if !(ENABLE(DETACHED_JIT) && BUILDING_DETACHED_JIT)
  281. } // namespace JSC