2 * Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "StringObject.h"
25 #include "PropertyNameArray.h"
29 ASSERT_CLASS_FITS_IN_CELL(StringObject
);
30 ASSERT_HAS_TRIVIAL_DESTRUCTOR(StringObject
);
32 const ClassInfo
StringObject::s_info
= { "String", &JSWrapperObject::s_info
, 0, 0, CREATE_METHOD_TABLE(StringObject
) };
34 StringObject::StringObject(JSGlobalData
& globalData
, Structure
* structure
)
35 : JSWrapperObject(globalData
, structure
)
39 void StringObject::finishCreation(JSGlobalData
& globalData
, JSString
* string
)
41 Base::finishCreation(globalData
);
42 ASSERT(inherits(&s_info
));
43 setInternalValue(globalData
, string
);
46 bool StringObject::getOwnPropertySlot(JSCell
* cell
, ExecState
* exec
, const Identifier
& propertyName
, PropertySlot
& slot
)
48 StringObject
* thisObject
= jsCast
<StringObject
*>(cell
);
49 if (thisObject
->internalValue()->getStringPropertySlot(exec
, propertyName
, slot
))
51 return JSObject::getOwnPropertySlot(thisObject
, exec
, propertyName
, slot
);
54 bool StringObject::getOwnPropertySlotByIndex(JSCell
* cell
, ExecState
* exec
, unsigned propertyName
, PropertySlot
& slot
)
56 StringObject
* thisObject
= jsCast
<StringObject
*>(cell
);
57 if (thisObject
->internalValue()->getStringPropertySlot(exec
, propertyName
, slot
))
59 return JSObject::getOwnPropertySlot(thisObject
, exec
, Identifier::from(exec
, propertyName
), slot
);
62 bool StringObject::getOwnPropertyDescriptor(JSObject
* object
, ExecState
* exec
, const Identifier
& propertyName
, PropertyDescriptor
& descriptor
)
64 StringObject
* thisObject
= jsCast
<StringObject
*>(object
);
65 if (thisObject
->internalValue()->getStringPropertyDescriptor(exec
, propertyName
, descriptor
))
67 return JSObject::getOwnPropertyDescriptor(thisObject
, exec
, propertyName
, descriptor
);
70 void StringObject::put(JSCell
* cell
, ExecState
* exec
, const Identifier
& propertyName
, JSValue value
, PutPropertySlot
& slot
)
72 if (propertyName
== exec
->propertyNames().length
) {
73 if (slot
.isStrictMode())
74 throwTypeError(exec
, StrictModeReadonlyPropertyWriteError
);
77 JSObject::put(cell
, exec
, propertyName
, value
, slot
);
80 bool StringObject::defineOwnProperty(JSObject
* object
, ExecState
* exec
, const Identifier
& propertyName
, PropertyDescriptor
& descriptor
, bool throwException
)
82 StringObject
* thisObject
= jsCast
<StringObject
*>(object
);
84 if (propertyName
== exec
->propertyNames().length
) {
85 if (!object
->isExtensible()) {
87 throwError(exec
, createTypeError(exec
, "Attempting to define property on object that is not extensible."));
90 if (descriptor
.configurablePresent() && descriptor
.configurable()) {
92 throwError(exec
, createTypeError(exec
, "Attempting to configurable attribute of unconfigurable property."));
95 if (descriptor
.enumerablePresent() && descriptor
.enumerable()) {
97 throwError(exec
, createTypeError(exec
, "Attempting to change enumerable attribute of unconfigurable property."));
100 if (descriptor
.isAccessorDescriptor()) {
102 throwError(exec
, createTypeError(exec
, "Attempting to change access mechanism for an unconfigurable property."));
105 if (descriptor
.writablePresent() && descriptor
.writable()) {
107 throwError(exec
, createTypeError(exec
, "Attempting to change writable attribute of unconfigurable property."));
110 if (!descriptor
.value())
112 if (propertyName
== exec
->propertyNames().length
&& sameValue(exec
, descriptor
.value(), jsNumber(thisObject
->internalValue()->length())))
115 throwError(exec
, createTypeError(exec
, "Attempting to change value of a readonly property."));
119 return Base::defineOwnProperty(object
, exec
, propertyName
, descriptor
, throwException
);
122 bool StringObject::deleteProperty(JSCell
* cell
, ExecState
* exec
, const Identifier
& propertyName
)
124 StringObject
* thisObject
= jsCast
<StringObject
*>(cell
);
125 if (propertyName
== exec
->propertyNames().length
)
128 unsigned i
= propertyName
.toUInt32(isStrictUInt32
);
129 if (isStrictUInt32
&& thisObject
->internalValue()->canGetIndex(i
))
131 return JSObject::deleteProperty(thisObject
, exec
, propertyName
);
134 void StringObject::getOwnPropertyNames(JSObject
* object
, ExecState
* exec
, PropertyNameArray
& propertyNames
, EnumerationMode mode
)
136 StringObject
* thisObject
= jsCast
<StringObject
*>(object
);
137 int size
= thisObject
->internalValue()->length();
138 for (int i
= 0; i
< size
; ++i
)
139 propertyNames
.add(Identifier(exec
, UString::number(i
)));
140 if (mode
== IncludeDontEnumProperties
)
141 propertyNames
.add(exec
->propertyNames().length
);
142 return JSObject::getOwnPropertyNames(thisObject
, exec
, propertyNames
, mode
);