summaryrefslogtreecommitdiffstats
path: root/Objects/namespaceobject.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-05-18 15:33:31 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-05-18 15:33:31 (GMT)
commita2c145c2f3def66a39295422a3d99b0b357978d5 (patch)
tree253eb48a4404eff9c45dec6c9f2930f10d13b395 /Objects/namespaceobject.c
parentc0937f79ec12fb46938416004fd1fd002ae75a12 (diff)
parent5bf3120e244575189cb7090a0fb6026036f1f979 (diff)
downloadcpython-a2c145c2f3def66a39295422a3d99b0b357978d5.zip
cpython-a2c145c2f3def66a39295422a3d99b0b357978d5.tar.gz
cpython-a2c145c2f3def66a39295422a3d99b0b357978d5.tar.bz2
Issue #24091: Fixed various crashes in corner cases in C implementation of
ElementTree.
Diffstat (limited to 'Objects/namespaceobject.c')
0 files changed, 0 insertions, 0 deletions
id='n103' href='#n103'>103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/*
 *  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"

namespace JSC {

JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
{
    return const_cast<JSString*>(this);
}

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

bool JSString::toBoolean(ExecState*) const
{
    return !m_value.isEmpty();
}

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)
{
    return new (exec) StringObject(exec->lexicalGlobalObject()->stringObjectStructure(), string);
}

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;
    if (propertyName == exec->propertyNames().underscoreProto) {
        slot.setValue(exec->lexicalGlobalObject()->stringPrototype());
        return true;
    }
    slot.setBase(this);
    JSObject* object;
    for (JSValue prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype.isNull(); prototype = object->prototype()) {
        object = asObject(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.
    if (getStringPropertySlot(exec, propertyName, slot))
        return true;
    return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
}

JSString* jsString(JSGlobalData* globalData, const UString& s)
{
    int size = s.size();
    if (!size)
        return globalData->smallStrings.emptyString(globalData);
    if (size == 1) {
        UChar c = s.data()[0];
        if (c <= 0xFF)
            return globalData->smallStrings.singleCharacterString(globalData, c);
    }
    return new (globalData) JSString(globalData, s);
}
    
JSString* jsSubstring(JSGlobalData* globalData, const UString& s, unsigned offset, unsigned length)
{
    ASSERT(offset <= static_cast<unsigned>(s.size()));
    ASSERT(length <= static_cast<unsigned>(s.size()));
    ASSERT(offset + length <= static_cast<unsigned>(s.size()));
    if (!length)
        return globalData->smallStrings.emptyString(globalData);
    if (length == 1) {
        UChar c = s.data()[offset];
        if (c <= 0xFF)
            return globalData->smallStrings.singleCharacterString(globalData, c);
    }
    return new (globalData) JSString(globalData, UString::Rep::create(s.rep(), offset, length));
}

JSString* jsOwnedString(JSGlobalData* globalData, const UString& s)
{
    int size = s.size();
    if (!size)
        return globalData->smallStrings.emptyString(globalData);
    if (size == 1) {
        UChar c = s.data()[0];
        if (c <= 0xFF)
            return globalData->smallStrings.singleCharacterString(globalData, c);
    }
    return new (globalData) JSString(globalData, s, JSString::HasOtherOwner);
}

} // namespace JSC