0
|
1 |
/*
|
|
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.
|
|
5 |
*
|
|
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.
|
|
10 |
*
|
|
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.
|
|
15 |
*
|
|
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.
|
|
20 |
*
|
|
21 |
*/
|
|
22 |
|
|
23 |
#include "config.h"
|
|
24 |
#include "JSString.h"
|
|
25 |
|
|
26 |
#include "JSGlobalObject.h"
|
|
27 |
#include "JSObject.h"
|
|
28 |
#include "StringObject.h"
|
|
29 |
#include "StringPrototype.h"
|
|
30 |
|
|
31 |
namespace JSC {
|
|
32 |
|
|
33 |
JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
|
|
34 |
{
|
|
35 |
return const_cast<JSString*>(this);
|
|
36 |
}
|
|
37 |
|
|
38 |
bool JSString::getPrimitiveNumber(ExecState*, double& number, JSValue& value)
|
|
39 |
{
|
|
40 |
value = this;
|
|
41 |
number = m_value.toDouble();
|
|
42 |
return false;
|
|
43 |
}
|
|
44 |
|
|
45 |
bool JSString::toBoolean(ExecState*) const
|
|
46 |
{
|
|
47 |
return !m_value.isEmpty();
|
|
48 |
}
|
|
49 |
|
|
50 |
double JSString::toNumber(ExecState*) const
|
|
51 |
{
|
|
52 |
return m_value.toDouble();
|
|
53 |
}
|
|
54 |
|
|
55 |
UString JSString::toString(ExecState*) const
|
|
56 |
{
|
|
57 |
return m_value;
|
|
58 |
}
|
|
59 |
|
|
60 |
UString JSString::toThisString(ExecState*) const
|
|
61 |
{
|
|
62 |
return m_value;
|
|
63 |
}
|
|
64 |
|
|
65 |
JSString* JSString::toThisJSString(ExecState*)
|
|
66 |
{
|
|
67 |
return this;
|
|
68 |
}
|
|
69 |
|
|
70 |
inline StringObject* StringObject::create(ExecState* exec, JSString* string)
|
|
71 |
{
|
|
72 |
return new (exec) StringObject(exec->lexicalGlobalObject()->stringObjectStructure(), string);
|
|
73 |
}
|
|
74 |
|
|
75 |
JSObject* JSString::toObject(ExecState* exec) const
|
|
76 |
{
|
|
77 |
return StringObject::create(exec, const_cast<JSString*>(this));
|
|
78 |
}
|
|
79 |
|
|
80 |
JSObject* JSString::toThisObject(ExecState* exec) const
|
|
81 |
{
|
|
82 |
return StringObject::create(exec, const_cast<JSString*>(this));
|
|
83 |
}
|
|
84 |
|
|
85 |
bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
|
86 |
{
|
|
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))
|
|
90 |
return true;
|
|
91 |
if (propertyName == exec->propertyNames().underscoreProto) {
|
|
92 |
slot.setValue(exec->lexicalGlobalObject()->stringPrototype());
|
|
93 |
return true;
|
|
94 |
}
|
|
95 |
slot.setBase(this);
|
|
96 |
JSObject* object;
|
|
97 |
for (JSValue prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype.isNull(); prototype = object->prototype()) {
|
|
98 |
object = asObject(prototype);
|
|
99 |
if (object->getOwnPropertySlot(exec, propertyName, slot))
|
|
100 |
return true;
|
|
101 |
}
|
|
102 |
slot.setUndefined();
|
|
103 |
return true;
|
|
104 |
}
|
|
105 |
|
|
106 |
bool JSString::getStringPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
|
|
107 |
{
|
|
108 |
if (propertyName == exec->propertyNames().length) {
|
|
109 |
descriptor.setDescriptor(jsNumber(exec, m_value.size()), DontEnum | DontDelete | ReadOnly);
|
|
110 |
return true;
|
|
111 |
}
|
|
112 |
|
|
113 |
bool isStrictUInt32;
|
|
114 |
unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
|
|
115 |
if (isStrictUInt32 && i < static_cast<unsigned>(m_value.size())) {
|
|
116 |
descriptor.setDescriptor(jsSingleCharacterSubstring(exec, m_value, i), DontDelete | ReadOnly);
|
|
117 |
return true;
|
|
118 |
}
|
|
119 |
|
|
120 |
return false;
|
|
121 |
}
|
|
122 |
|
|
123 |
bool JSString::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
|
|
124 |
{
|
|
125 |
if (getStringPropertyDescriptor(exec, propertyName, descriptor))
|
|
126 |
return true;
|
|
127 |
if (propertyName != exec->propertyNames().underscoreProto)
|
|
128 |
return false;
|
|
129 |
descriptor.setDescriptor(exec->lexicalGlobalObject()->stringPrototype(), DontEnum);
|
|
130 |
return true;
|
|
131 |
}
|
|
132 |
|
|
133 |
bool JSString::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
|
|
134 |
{
|
|
135 |
// The semantics here are really getPropertySlot, not getOwnPropertySlot.
|
|
136 |
// This function should only be called by JSValue::get.
|
|
137 |
if (getStringPropertySlot(exec, propertyName, slot))
|
|
138 |
return true;
|
|
139 |
return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
|
|
140 |
}
|
|
141 |
|
|
142 |
} // namespace JSC
|