JSString.cpp 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *  Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
 *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
 *  Copyright (C) 2004, 2007, 2008 Apple Inc. All rights reserved.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"
#include "JSString.h"

#include "JSGlobalObject.h"
27
#include "JSGlobalObjectFunctions.h"
28
#include "JSObject.h"
29
#include "Operations.h"
30 31 32
#include "StringObject.h"
#include "StringPrototype.h"

33
namespace JSC {
34
    
35
const ClassInfo JSString::s_info = { "string", 0, 0, 0, CREATE_METHOD_TABLE(JSString) };
36

37
void JSRopeString::RopeBuilder::expand()
38
{
39
    ASSERT(m_index == JSRopeString::s_maxInternalRopeLength);
40
    JSString* jsString = m_jsString;
41
    m_jsString = jsStringBuilder(&m_vm);
42
    m_index = 0;
43 44 45
    append(jsString);
}

46
void JSString::destroy(JSCell* cell)
47
{
48
    JSString* thisObject = static_cast<JSString*>(cell);
49
    thisObject->JSString::~JSString();
50 51 52 53
}

void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
54
    JSString* thisObject = jsCast<JSString*>(cell);
55
    Base::visitChildren(thisObject, visitor);
56
    
57 58 59 60 61 62 63 64 65 66 67 68 69
    MARK_LOG_MESSAGE1("[%u]: ", thisObject->length());

#if ENABLE(OBJECT_MARK_LOGGING)
    if (!thisObject->isRope()) {
        WTF::StringImpl* ourImpl = thisObject->m_value.impl();
        if (ourImpl->is8Bit())
            MARK_LOG_MESSAGE1("[8 %p]", ourImpl->characters8());
        else
            MARK_LOG_MESSAGE1("[16 %p]", ourImpl->characters16());
    } else
        MARK_LOG_MESSAGE0("[rope]: ");
#endif

70 71
    if (thisObject->isRope())
        static_cast<JSRopeString*>(thisObject)->visitFibers(visitor);
72 73 74
    else {
        StringImpl* impl = thisObject->m_value.impl();
        ASSERT(impl);
75
        visitor.reportExtraMemoryUsage(thisObject, impl->costDuringGC());
76
    }
77 78 79 80 81 82
}

void JSRopeString::visitFibers(SlotVisitor& visitor)
{
    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
        visitor.append(&m_fibers[i]);
83 84
}

85
void JSRopeString::resolveRope(ExecState* exec) const
86 87 88
{
    ASSERT(isRope());

89 90
    if (is8Bit()) {
        LChar* buffer;
91 92
        if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) {
            Heap::heap(this)->reportExtraMemoryCost(newImpl->cost());
93
            m_value = newImpl.release();
94
        } else {
95 96 97 98 99 100
            outOfMemory(exec);
            return;
        }

        for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
            if (m_fibers[i]->isRope())
101
                return resolveRopeSlowCase8(buffer);
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        }

        LChar* position = buffer;
        for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
            StringImpl* string = m_fibers[i]->m_value.impl();
            unsigned length = string->length();
            StringImpl::copyChars(position, string->characters8(), length);
            position += length;
            m_fibers[i].clear();
        }
        ASSERT((buffer + m_length) == position);
        ASSERT(!isRope());

        return;
    }

118
    UChar* buffer;
119 120
    if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer)) {
        Heap::heap(this)->reportExtraMemoryCost(newImpl->cost());
121
        m_value = newImpl.release();
122
    } else {
123 124 125 126
        outOfMemory(exec);
        return;
    }

127
    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
128
        if (m_fibers[i]->isRope())
129
            return resolveRopeSlowCase(buffer);
130 131 132
    }

    UChar* position = buffer;
133
    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
134 135
        StringImpl* string = m_fibers[i]->m_value.impl();
        unsigned length = string->length();
136 137 138 139
        if (string->is8Bit())
            StringImpl::copyChars(position, string->characters8(), length);
        else
            StringImpl::copyChars(position, string->characters16(), length);
140
        position += length;
141
        m_fibers[i].clear();
142 143 144 145 146
    }
    ASSERT((buffer + m_length) == position);
    ASSERT(!isRope());
}

147
// Overview: These functions convert a JSString from holding a string in rope form
148
// down to a simple String representation. It does so by building up the string
149 150 151 152
// backwards, since we want to avoid recursion, we expect that the tree structure
// representing the rope is likely imbalanced with more nodes down the left side
// (since appending to the string is likely more common) - and as such resolving
// in this fashion should minimize work queue size.  (If we built the queue forwards
153
// we would likely have to place all of the constituent StringImpls into the
154 155
// Vector before performing any concatenation, but by working backwards we likely
// only fill the queue with the number of substrings at any given level in a
156
// rope-of-ropes.)    
157
void JSRopeString::resolveRopeSlowCase8(LChar* buffer) const
158
{
159
    LChar* position = buffer + m_length; // We will be working backwards over the rope.
160
    Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // Putting strings into a Vector is only OK because there are no GC points in this method.
161
    
162
    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
163
        workQueue.append(m_fibers[i].get());
164 165 166
        // Clearing here works only because there are no GC points in this method.
        m_fibers[i].clear();
    }
167 168 169 170 171

    while (!workQueue.isEmpty()) {
        JSString* currentFiber = workQueue.last();
        workQueue.removeLast();

172
        if (currentFiber->isRope()) {
173 174 175
            JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
            for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->m_fibers[i]; ++i)
                workQueue.append(currentFiberAsRope->m_fibers[i].get());
176
            continue;
177
        }
178 179 180 181

        StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
        unsigned length = string->length();
        position -= length;
182 183 184 185 186 187 188
        StringImpl::copyChars(position, string->characters8(), length);
    }

    ASSERT(buffer == position);
    ASSERT(!isRope());
}

189
void JSRopeString::resolveRopeSlowCase(UChar* buffer) const
190 191
{
    UChar* position = buffer + m_length; // We will be working backwards over the rope.
192
    Vector<JSString*, 32, UnsafeVectorOverflow> workQueue; // These strings are kept alive by the parent rope, so using a Vector is OK.
193 194 195 196 197 198 199 200 201

    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
        workQueue.append(m_fibers[i].get());

    while (!workQueue.isEmpty()) {
        JSString* currentFiber = workQueue.last();
        workQueue.removeLast();

        if (currentFiber->isRope()) {
202 203 204
            JSRopeString* currentFiberAsRope = static_cast<JSRopeString*>(currentFiber);
            for (size_t i = 0; i < s_maxInternalRopeLength && currentFiberAsRope->m_fibers[i]; ++i)
                workQueue.append(currentFiberAsRope->m_fibers[i].get());
205 206 207 208 209 210
            continue;
        }

        StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
        unsigned length = string->length();
        position -= length;
211 212 213 214
        if (string->is8Bit())
            StringImpl::copyChars(position, string->characters8(), length);
        else
            StringImpl::copyChars(position, string->characters16(), length);
215
    }
216 217 218

    ASSERT(buffer == position);
    ASSERT(!isRope());
219
}
220

221
void JSRopeString::outOfMemory(ExecState* exec) const
222
{
223 224
    for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
        m_fibers[i].clear();
225
    ASSERT(isRope());
226
    ASSERT(m_value.isNull());
227 228 229 230
    if (exec)
        throwOutOfMemoryError(exec);
}

231
JSString* JSRopeString::getIndexSlowCase(ExecState* exec, unsigned i)
232 233 234 235 236
{
    ASSERT(isRope());
    resolveRope(exec);
    // Return a safe no-value result, this should never be used, since the excetion will be thrown.
    if (exec->exception())
237
        return jsEmptyString(exec);
238
    ASSERT(!isRope());
239
    RELEASE_ASSERT(i < m_value.length());
240 241 242
    return jsSingleCharacterSubstring(exec, m_value, i);
}

ggaren@apple.com's avatar
ggaren@apple.com committed
243
JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
244
{
weinig@apple.com's avatar
weinig@apple.com committed
245
    return const_cast<JSString*>(this);
246 247
}

248
bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const
249
{
250
    result = this;
251
    number = jsToNumber(value(exec));
252 253 254
    return false;
}

255
bool JSString::toBoolean() const
mjs@apple.com's avatar
mjs@apple.com committed
256
{
257
    return m_length;
mjs@apple.com's avatar
mjs@apple.com committed
258 259
}

260
double JSString::toNumber(ExecState* exec) const
261
{
262
    return jsToNumber(value(exec));
263 264
}

265
inline StringObject* StringObject::create(VM& vm, JSGlobalObject* globalObject, JSString* string)
266
{
267 268
    StringObject* object = new (NotNull, allocateCell<StringObject>(vm.heap)) StringObject(vm, globalObject->stringObjectStructure());
    object->finishCreation(vm, string);
269
    return object;
270 271
}

272
JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const
273
{
274
    return StringObject::create(exec->vm(), globalObject, const_cast<JSString*>(this));
275 276
}

277
JSValue JSString::toThis(JSCell* cell, ExecState* exec, ECMAMode ecmaMode)
278
{
279 280
    if (ecmaMode == StrictMode)
        return cell;
281
    return StringObject::create(exec->vm(), exec->lexicalGlobalObject(), jsCast<JSString*>(cell));
282 283
}

284
bool JSString::getStringPropertyDescriptor(ExecState* exec, PropertyName propertyName, PropertyDescriptor& descriptor)
285 286
{
    if (propertyName == exec->propertyNames().length) {
287
        descriptor.setDescriptor(jsNumber(m_length), DontEnum | DontDelete | ReadOnly);
288 289 290
        return true;
    }
    
291 292 293
    unsigned i = propertyName.asIndex();
    if (i < m_length) {
        ASSERT(i != PropertyName::NotAnIndex); // No need for an explicit check, the above test would always fail!
294
        descriptor.setDescriptor(getIndex(exec, i), DontDelete | ReadOnly);
295 296 297 298 299 300
        return true;
    }
    
    return false;
}

301
} // namespace JSC