]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSString.cpp
86f95e0dbd62e1f2c3339adb627b313215d11da6
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"
28 #include "StringObject.h"
29 #include "StringPrototype.h"
33 JSValue
JSString::toPrimitive(ExecState
*, PreferredPrimitiveType
) const
35 return const_cast<JSString
*>(this);
38 bool JSString::getPrimitiveNumber(ExecState
*, double& number
, JSValue
& value
)
41 number
= m_value
.toDouble();
45 bool JSString::toBoolean(ExecState
*) const
47 return !m_value
.isEmpty();
50 double JSString::toNumber(ExecState
*) const
52 return m_value
.toDouble();
55 UString
JSString::toString(ExecState
*) const
60 UString
JSString::toThisString(ExecState
*) const
65 JSString
* JSString::toThisJSString(ExecState
*)
70 inline StringObject
* StringObject::create(ExecState
* exec
, JSString
* string
)
72 return new (exec
) StringObject(exec
->lexicalGlobalObject()->stringObjectStructure(), string
);
75 JSObject
* JSString::toObject(ExecState
* exec
) const
77 return StringObject::create(exec
, const_cast<JSString
*>(this));
80 JSObject
* JSString::toThisObject(ExecState
* exec
) const
82 return StringObject::create(exec
, const_cast<JSString
*>(this));
85 bool JSString::getOwnPropertySlot(ExecState
* exec
, const Identifier
& propertyName
, PropertySlot
& slot
)
87 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
88 // This function should only be called by JSValue::get.
89 if (getStringPropertySlot(exec
, propertyName
, slot
))
91 if (propertyName
== exec
->propertyNames().underscoreProto
) {
92 slot
.setValue(exec
->lexicalGlobalObject()->stringPrototype());
97 for (JSValue prototype
= exec
->lexicalGlobalObject()->stringPrototype(); !prototype
.isNull(); prototype
= object
->prototype()) {
98 object
= asObject(prototype
);
99 if (object
->getOwnPropertySlot(exec
, propertyName
, slot
))
106 bool JSString::getOwnPropertySlot(ExecState
* exec
, unsigned propertyName
, PropertySlot
& slot
)
108 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
109 // This function should only be called by JSValue::get.
110 if (getStringPropertySlot(exec
, propertyName
, slot
))
112 return JSString::getOwnPropertySlot(exec
, Identifier::from(exec
, propertyName
), slot
);
115 JSString
* jsString(JSGlobalData
* globalData
, const UString
& s
)
119 return globalData
->smallStrings
.emptyString(globalData
);
121 UChar c
= s
.data()[0];
123 return globalData
->smallStrings
.singleCharacterString(globalData
, c
);
125 return new (globalData
) JSString(globalData
, s
);
128 JSString
* jsSubstring(JSGlobalData
* globalData
, const UString
& s
, unsigned offset
, unsigned length
)
130 ASSERT(offset
<= static_cast<unsigned>(s
.size()));
131 ASSERT(length
<= static_cast<unsigned>(s
.size()));
132 ASSERT(offset
+ length
<= static_cast<unsigned>(s
.size()));
134 return globalData
->smallStrings
.emptyString(globalData
);
136 UChar c
= s
.data()[offset
];
138 return globalData
->smallStrings
.singleCharacterString(globalData
, c
);
140 return new (globalData
) JSString(globalData
, UString::Rep::create(s
.rep(), offset
, length
));
143 JSString
* jsOwnedString(JSGlobalData
* globalData
, const UString
& s
)
147 return globalData
->smallStrings
.emptyString(globalData
);
149 UChar c
= s
.data()[0];
151 return globalData
->smallStrings
.singleCharacterString(globalData
, c
);
153 return new (globalData
) JSString(globalData
, s
, JSString::HasOtherOwner
);