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 "JSCInlines.h"
30 #include "StringObject.h"
31 #include "StringPrototype.h"
35 const ClassInfo JSString::s_info = { "string", 0, 0, 0, CREATE_METHOD_TABLE(JSString) };
37 void JSRopeString::RopeBuilder::expand()
39 ASSERT(m_index == JSRopeString::s_maxInternalRopeLength);
40 JSString* jsString = m_jsString;
41 RELEASE_ASSERT(jsString);
42 m_jsString = jsStringBuilder(&m_vm);
47 void JSString::destroy(JSCell* cell)
49 JSString* thisObject = static_cast<JSString*>(cell);
50 thisObject->JSString::~JSString();
53 void JSString::dumpToStream(const JSCell* cell, PrintStream& out)
55 const JSString* thisObject = jsCast<const JSString*>(cell);
56 out.printf("<%p, %s, [%u], ", thisObject, thisObject->className(), thisObject->length());
57 if (thisObject->isRope())
60 WTF::StringImpl* ourImpl = thisObject->m_value.impl();
61 if (ourImpl->is8Bit())
62 out.printf("[8 %p]", ourImpl->characters8());
64 out.printf("[16 %p]", ourImpl->characters16());
69 void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor)
71 JSString* thisObject = jsCast<JSString*>(cell);
72 Base::visitChildren(thisObject, visitor);
74 if (thisObject->isRope())
75 static_cast<JSRopeString*>(thisObject)->visitFibers(visitor);
77 StringImpl* impl = thisObject->m_value.impl();
79 visitor.reportExtraMemoryUsage(thisObject, impl->costDuringGC());
83 void JSRopeString::visitFibers(SlotVisitor& visitor)
85 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
86 visitor.append(&m_fibers[i]);
89 static const unsigned maxLengthForOnStackResolve = 2048;
91 void JSRopeString::resolveRopeInternal8(LChar* buffer) const
93 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
94 if (m_fibers[i]->isRope()) {
95 resolveRopeSlowCase8(buffer);
100 LChar* position = buffer;
101 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
102 const StringImpl& fiberString = *m_fibers[i]->m_value.impl();
103 unsigned length = fiberString.length();
104 StringImpl::copyChars(position, fiberString.characters8(), length);
107 ASSERT((buffer + m_length) == position);
110 void JSRopeString::resolveRopeInternal16(UChar* buffer) const
112 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
113 if (m_fibers[i]->isRope()) {
114 resolveRopeSlowCase(buffer);
119 UChar* position = buffer;
120 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
121 const StringImpl& fiberString = *m_fibers[i]->m_value.impl();
122 unsigned length = fiberString.length();
123 if (fiberString.is8Bit())
124 StringImpl::copyChars(position, fiberString.characters8(), length);
126 StringImpl::copyChars(position, fiberString.characters16(), length);
129 ASSERT((buffer + m_length) == position);
132 void JSRopeString::resolveRopeToAtomicString(ExecState* exec) const
134 if (m_length > maxLengthForOnStackResolve) {
136 m_value = AtomicString(m_value);
141 LChar buffer[maxLengthForOnStackResolve];
142 resolveRopeInternal8(buffer);
143 m_value = AtomicString(buffer, m_length);
145 UChar buffer[maxLengthForOnStackResolve];
146 resolveRopeInternal16(buffer);
147 m_value = AtomicString(buffer, m_length);
152 // If we resolved a string that didn't previously exist, notify the heap that we've grown.
153 if (m_value.impl()->hasOneRef())
154 Heap::heap(this)->reportExtraMemoryCost(m_value.impl()->cost());
157 void JSRopeString::clearFibers() const
159 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
163 AtomicStringImpl* JSRopeString::resolveRopeToExistingAtomicString(ExecState* exec) const
165 if (m_length > maxLengthForOnStackResolve) {
167 if (AtomicStringImpl* existingAtomicString = AtomicString::find(m_value.impl())) {
168 m_value = *existingAtomicString;
170 return existingAtomicString;
176 LChar buffer[maxLengthForOnStackResolve];
177 resolveRopeInternal8(buffer);
178 if (AtomicStringImpl* existingAtomicString = AtomicString::find(buffer, m_length)) {
179 m_value = *existingAtomicString;
181 return existingAtomicString;
184 UChar buffer[maxLengthForOnStackResolve];
185 resolveRopeInternal16(buffer);
186 if (AtomicStringImpl* existingAtomicString = AtomicString::find(buffer, m_length)) {
187 m_value = *existingAtomicString;
189 return existingAtomicString;
196 void JSRopeString::resolveRope(ExecState* exec) const
202 if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) {
203 Heap::heap(this)->reportExtraMemoryCost(newImpl->cost());
204 m_value = newImpl.release();
209 resolveRopeInternal8(buffer);
216 if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) {
217 Heap::heap(this)->reportExtraMemoryCost(newImpl->cost());
218 m_value = newImpl.release();
224 resolveRopeInternal16(buffer);
229 // Overview: These functions convert a JSString from holding a string in rope form
230 // down to a simple String representation. It does so by building up the string
231 // backwards, since we want to avoid recursion, we expect that the tree structure
232 // representing the rope is likely imbalanced with more nodes down the left side
233 // (since appending to the string is likely more common) - and as such resolving
234 // in this fashion should minimize work queue size. (If we built the queue forwards
235 // we would likely have to place all of the constituent StringImpls into the
236 // Vector before performing any concatenation, but by working backwards we likely
237 // only fill the queue with the number of substrings at any given level in a
239 void JSRopeString::resolveRopeSlowCase8(LChar* buffer) const
241 LChar* position = buffer + m_length; // We will be working backwards over the rope.
242 Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // Putting strings into a Vector is only OK because there are no GC points in this method.
244 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
245 workQueue.append(m_fibers[i].get());
247 while (!workQueue.isEmpty()) {
248 JSString* currentFiber = workQueue.last();
249 workQueue.removeLast();
251 if (currentFiber->isRope()) {
252 JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
253 for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->m_fibers[i]; ++i)
254 workQueue.append(currentFiberAsRope->m_fibers[i].get());
258 StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
259 unsigned length = string->length();
261 StringImpl::copyChars(position, string->characters8(), length);
264 ASSERT(buffer == position);
267 void JSRopeString::resolveRopeSlowCase(UChar* buffer) const
269 UChar* position = buffer + m_length; // We will be working backwards over the rope.
270 Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // These strings are kept alive by the parent rope, so using a Vector is OK.
272 for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
273 workQueue.append(m_fibers[i].get());
275 while (!workQueue.isEmpty()) {
276 JSString* currentFiber = workQueue.last();
277 workQueue.removeLast();
279 if (currentFiber->isRope()) {
280 JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
281 for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->m_fibers[i]; ++i)
282 workQueue.append(currentFiberAsRope->m_fibers[i].get());
286 StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
287 unsigned length = string->length();
289 if (string->is8Bit())
290 StringImpl::copyChars(position, string->characters8(), length);
292 StringImpl::copyChars(position, string->characters16(), length);
295 ASSERT(buffer == position);
298 void JSRopeString::outOfMemory(ExecState* exec) const
302 ASSERT(m_value.isNull());
304 throwOutOfMemoryError(exec);
307 JSString* JSRopeString::getIndexSlowCase(ExecState* exec, unsigned i)
311 // Return a safe no-value result, this should never be used, since the excetion will be thrown.
312 if (exec->exception())
313 return jsEmptyString(exec);
315 RELEASE_ASSERT(i < m_value.length());
316 return jsSingleCharacterSubstring(exec, m_value, i);
319 JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
321 return const_cast<JSString*>(this);
324 bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const
327 number = jsToNumber(value(exec));
331 bool JSString::toBoolean() const
336 double JSString::toNumber(ExecState* exec) const
338 return jsToNumber(value(exec));
341 inline StringObject* StringObject::create(VM& vm, JSGlobalObject* globalObject, JSString* string)
343 StringObject* object = new (NotNull, allocateCell<StringObject>(vm.heap)) StringObject(vm, globalObject->stringObjectStructure());
344 object->finishCreation(vm, string);
348 JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const
350 return StringObject::create(exec->vm(), globalObject, const_cast<JSString*>(this));
353 JSValue JSString::toThis(JSCell* cell, ExecState* exec, ECMAMode ecmaMode)
355 if (ecmaMode == StrictMode)
357 return StringObject::create(exec->vm(), exec->lexicalGlobalObject(), jsCast<JSString*>(cell));
360 bool JSString::getStringPropertyDescriptor(ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
362 if (propertyName == exec->propertyNames().length) {
363 descriptor.setDescriptor(jsNumber(m_length), DontEnum | DontDelete | ReadOnly);
367 unsigned i = propertyName.asIndex();
369 ASSERT(i != PropertyName::NotAnIndex); // No need for an explicit check, the above test would always fail!
370 descriptor.setDescriptor(getIndex(exec, i), DontDelete | ReadOnly);
377 JSString* jsStringWithCacheSlowCase(VM& vm, StringImpl& stringImpl)
379 auto addResult = vm.stringCache.add(&stringImpl, nullptr);
380 if (addResult.isNewEntry)
381 addResult.iterator->value = jsString(&vm, String(stringImpl));
382 vm.lastCachedString = addResult.iterator->value.get();
383 return vm.lastCachedString.get();