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.
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.
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.
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.
26 #include "JSGlobalObject.h"
27 #include "JSGlobalObjectFunctions.h"
29 #include "Operations.h"
30 #include "StringObject.h"
31 #include "StringPrototype.h"
35 static const unsigned substringFromRopeCutoff = 4;
37 const ClassInfo JSString::s_info = { "string", 0, 0, 0, CREATE_METHOD_TABLE(JSString) };
39 void JSString::RopeBuilder::expand()
41 ASSERT(m_index == JSString::s_maxInternalRopeLength);
42 JSString* jsString = m_jsString;
43 m_jsString = jsStringBuilder(&m_globalData);
50 ASSERT(vptr() == JSGlobalData::jsStringVPtr);
53 void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor)
55 JSString* thisObject = jsCast<JSString*>(cell);
56 Base::visitChildren(thisObject, visitor);
57 for (size_t i = 0; i < s_maxInternalRopeLength && thisObject->m_fibers[i]; ++i)
58 visitor.append(&thisObject->m_fibers[i]);
61 void JSString::resolveRope(ExecState* exec) const
67 if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer))
68 m_value = newImpl.release();
74 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
75 if (m_fibers[i]->isRope())
76 return resolveRopeSlowCase8(buffer);
79 LChar* position = buffer;
80 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
81 StringImpl* string = m_fibers[i]->m_value.impl();
82 unsigned length = string->length();
83 StringImpl::copyChars(position, string->characters8(), length);
87 ASSERT((buffer + m_length) == position);
94 if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer))
95 m_value = newImpl.release();
101 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
102 if (m_fibers[i]->isRope())
103 return resolveRopeSlowCase(buffer);
106 UChar* position = buffer;
107 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
108 StringImpl* string = m_fibers[i]->m_value.impl();
109 unsigned length = string->length();
110 StringImpl::copyChars(position, string->characters(), length);
114 ASSERT((buffer + m_length) == position);
118 // Overview: These functions convert a JSString from holding a string in rope form
119 // down to a simple UString representation. It does so by building up the string
120 // backwards, since we want to avoid recursion, we expect that the tree structure
121 // representing the rope is likely imbalanced with more nodes down the left side
122 // (since appending to the string is likely more common) - and as such resolving
123 // in this fashion should minimize work queue size. (If we built the queue forwards
124 // we would likely have to place all of the constituent StringImpls into the
125 // Vector before performing any concatenation, but by working backwards we likely
126 // only fill the queue with the number of substrings at any given level in a
128 void JSString::resolveRopeSlowCase8(LChar* buffer) const
130 LChar* position = buffer + m_length; // We will be working backwards over the rope.
131 Vector<JSString*, 32> workQueue; // Putting strings into a Vector is only OK because there are no GC points in this method.
133 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
134 workQueue.append(m_fibers[i].get());
135 // Clearing here works only because there are no GC points in this method.
139 while (!workQueue.isEmpty()) {
140 JSString* currentFiber = workQueue.last();
141 workQueue.removeLast();
143 if (currentFiber->isRope()) {
144 for (size_t i = 0; i < s_maxInternalRopeLength && currentFiber->m_fibers[i]; ++i)
145 workQueue.append(currentFiber->m_fibers[i].get());
149 StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
150 unsigned length = string->length();
152 StringImpl::copyChars(position, string->characters8(), length);
155 ASSERT(buffer == position);
159 void JSString::resolveRopeSlowCase(UChar* buffer) const
161 UChar* position = buffer + m_length; // We will be working backwards over the rope.
162 Vector<JSString*, 32> 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());
167 while (!workQueue.isEmpty()) {
168 JSString* currentFiber = workQueue.last();
169 workQueue.removeLast();
171 if (currentFiber->isRope()) {
172 for (size_t i = 0; i < s_maxInternalRopeLength && currentFiber->m_fibers[i]; ++i)
173 workQueue.append(currentFiber->m_fibers[i].get());
177 StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
178 unsigned length = string->length();
180 StringImpl::copyChars(position, string->characters(), length);
183 ASSERT(buffer == position);
187 void JSString::outOfMemory(ExecState* exec) const
189 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
192 ASSERT(m_value == UString());
194 throwOutOfMemoryError(exec);
197 JSValue JSString::replaceCharacter(ExecState* exec, UChar character, const UString& replacement)
199 size_t matchPosition = value(exec).find(character);
200 if (matchPosition == notFound)
201 return JSValue(this);
202 return jsString(exec, m_value.substringSharingImpl(0, matchPosition), replacement, value(exec).substringSharingImpl(matchPosition + 1));
205 JSString* JSString::getIndexSlowCase(ExecState* exec, unsigned i)
209 // Return a safe no-value result, this should never be used, since the excetion will be thrown.
210 if (exec->exception())
211 return jsString(exec, "");
213 ASSERT(i < m_value.length());
214 return jsSingleCharacterSubstring(exec, m_value, i);
217 JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
219 return const_cast<JSString*>(this);
222 bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const
225 number = jsToNumber(value(exec));
229 bool JSString::toBoolean(ExecState*) const
234 double JSString::toNumber(ExecState* exec) const
236 return jsToNumber(value(exec));
239 UString JSString::toString(ExecState* exec) const
244 inline StringObject* StringObject::create(ExecState* exec, JSGlobalObject* globalObject, JSString* string)
246 StringObject* object = new (allocateCell<StringObject>(*exec->heap())) StringObject(exec->globalData(), globalObject->stringObjectStructure());
247 object->finishCreation(exec->globalData(), string);
251 JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const
253 return StringObject::create(exec, globalObject, const_cast<JSString*>(this));
256 JSObject* JSString::toThisObject(JSCell* cell, ExecState* exec)
258 return StringObject::create(exec, exec->lexicalGlobalObject(), jsCast<JSString*>(cell));
261 bool JSString::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
263 JSString* thisObject = jsCast<JSString*>(cell);
264 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
265 // This function should only be called by JSValue::get.
266 if (thisObject->getStringPropertySlot(exec, propertyName, slot))
268 if (propertyName == exec->propertyNames().underscoreProto) {
269 slot.setValue(exec->lexicalGlobalObject()->stringPrototype());
272 slot.setBase(thisObject);
274 for (JSValue prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype.isNull(); prototype = object->prototype()) {
275 object = asObject(prototype);
276 if (object->methodTable()->getOwnPropertySlot(object, exec, propertyName, slot))
283 bool JSString::getStringPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
285 if (propertyName == exec->propertyNames().length) {
286 descriptor.setDescriptor(jsNumber(m_length), DontEnum | DontDelete | ReadOnly);
291 unsigned i = propertyName.toUInt32(isStrictUInt32);
292 if (isStrictUInt32 && i < m_length) {
293 descriptor.setDescriptor(getIndex(exec, i), DontDelete | ReadOnly);
300 bool JSString::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)
302 JSString* thisObject = jsCast<JSString*>(cell);
303 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
304 // This function should only be called by JSValue::get.
305 if (thisObject->getStringPropertySlot(exec, propertyName, slot))
307 return JSString::getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot);