JSString.cpp 4.7 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 27 28 29 30
/*
 *  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"
#include "JSObject.h"
#include "StringObject.h"
#include "StringPrototype.h"

31
namespace JSC {
32

darin@apple.com's avatar
darin@apple.com committed
33
JSValue* JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
34
{
weinig@apple.com's avatar
weinig@apple.com committed
35
    return const_cast<JSString*>(this);
36 37 38 39 40 41 42 43 44
}

bool JSString::getPrimitiveNumber(ExecState*, double& number, JSValue*& value)
{
    value = this;
    number = m_value.toDouble();
    return false;
}

mjs@apple.com's avatar
mjs@apple.com committed
45 46 47 48 49
bool JSString::toBoolean(ExecState*) const
{
    return !m_value.isEmpty();
}

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
double JSString::toNumber(ExecState*) const
{
    return m_value.toDouble();
}

UString JSString::toString(ExecState*) const
{
    return m_value;
}

UString JSString::toThisString(ExecState*) const
{
    return m_value;
}

JSString* JSString::toThisJSString(ExecState*)
{
    return this;
}

inline StringObject* StringObject::create(ExecState* exec, JSString* string)
{
darin@apple.com's avatar
darin@apple.com committed
72
    return new (exec) StringObject(exec->lexicalGlobalObject()->stringObjectStructure(), string);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

JSObject* JSString::toObject(ExecState* exec) const
{
    return StringObject::create(exec, const_cast<JSString*>(this));
}

JSObject* JSString::toThisObject(ExecState* exec) const
{
    return StringObject::create(exec, const_cast<JSString*>(this));
}

bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
{
    // The semantics here are really getPropertySlot, not getOwnPropertySlot.
    // This function should only be called by JSValue::get.
    if (getStringPropertySlot(exec, propertyName, slot))
        return true;
    slot.setBase(this);
    JSObject* object;
ggaren@apple.com's avatar
ggaren@apple.com committed
93
    for (JSValue* prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype->isNull(); prototype = object->prototype()) {
94 95 96 97 98 99 100 101 102 103 104 105 106
        ASSERT(prototype->isObject());
        object = static_cast<JSObject*>(prototype);
        if (object->getOwnPropertySlot(exec, propertyName, slot))
            return true;
    }
    slot.setUndefined();
    return true;
}

bool JSString::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
{
    // The semantics here are really getPropertySlot, not getOwnPropertySlot.
    // This function should only be called by JSValue::get.
107
    if (getStringPropertySlot(exec, propertyName, slot))
108 109 110 111
        return true;
    return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
}

darin@apple.com's avatar
darin@apple.com committed
112
JSString* jsString(JSGlobalData* globalData, const UString& s)
weinig@apple.com's avatar
weinig@apple.com committed
113
{
darin@apple.com's avatar
darin@apple.com committed
114 115
    int size = s.size();
    if (!size)
darin@apple.com's avatar
darin@apple.com committed
116
        return globalData->smallStrings.emptyString(globalData);
darin@apple.com's avatar
darin@apple.com committed
117 118 119
    if (size == 1) {
        UChar c = s.data()[0];
        if (c <= 0xFF)
darin@apple.com's avatar
darin@apple.com committed
120
            return globalData->smallStrings.singleCharacterString(globalData, c);
darin@apple.com's avatar
darin@apple.com committed
121
    }
darin@apple.com's avatar
darin@apple.com committed
122
    return new (globalData) JSString(globalData, s);
darin@apple.com's avatar
darin@apple.com committed
123 124
}
    
darin@apple.com's avatar
darin@apple.com committed
125
JSString* jsSubstring(JSGlobalData* globalData, const UString& s, unsigned offset, unsigned length)
darin@apple.com's avatar
darin@apple.com committed
126 127 128 129 130
{
    ASSERT(offset <= static_cast<unsigned>(s.size()));
    ASSERT(length <= static_cast<unsigned>(s.size()));
    ASSERT(offset + length <= static_cast<unsigned>(s.size()));
    if (!length)
darin@apple.com's avatar
darin@apple.com committed
131
        return globalData->smallStrings.emptyString(globalData);
darin@apple.com's avatar
darin@apple.com committed
132 133 134
    if (length == 1) {
        UChar c = s.data()[offset];
        if (c <= 0xFF)
darin@apple.com's avatar
darin@apple.com committed
135
            return globalData->smallStrings.singleCharacterString(globalData, c);
darin@apple.com's avatar
darin@apple.com committed
136
    }
darin@apple.com's avatar
darin@apple.com committed
137
    return new (globalData) JSString(globalData, UString::Rep::create(s.rep(), offset, length));
weinig@apple.com's avatar
weinig@apple.com committed
138 139
}

darin@apple.com's avatar
darin@apple.com committed
140
JSString* jsOwnedString(JSGlobalData* globalData, const UString& s)
weinig@apple.com's avatar
weinig@apple.com committed
141
{
darin@apple.com's avatar
darin@apple.com committed
142 143
    int size = s.size();
    if (!size)
darin@apple.com's avatar
darin@apple.com committed
144
        return globalData->smallStrings.emptyString(globalData);
darin@apple.com's avatar
darin@apple.com committed
145 146 147
    if (size == 1) {
        UChar c = s.data()[0];
        if (c <= 0xFF)
darin@apple.com's avatar
darin@apple.com committed
148
            return globalData->smallStrings.singleCharacterString(globalData, c);
darin@apple.com's avatar
darin@apple.com committed
149
    }
darin@apple.com's avatar
darin@apple.com committed
150
    return new (globalData) JSString(globalData, s, JSString::HasOtherOwner);
weinig@apple.com's avatar
weinig@apple.com committed
151 152
}

153
} // namespace JSC