tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / JSString.cpp
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 "JSGlobalObjectFunctions.h"
28 #include "JSObject.h"
29 #include "Operations.h"
30 #include "StringObject.h"
31 #include "StringPrototype.h"
32
33 namespace JSC {
34     
35 static const unsigned substringFromRopeCutoff = 4;
36
37 const ClassInfo JSString::s_info = { "string", 0, 0, 0, CREATE_METHOD_TABLE(JSString) };
38
39 void JSString::RopeBuilder::expand()
40 {
41     ASSERT(m_index == JSString::s_maxInternalRopeLength);
42     JSString* jsString = m_jsString;
43     m_jsString = jsStringBuilder(&m_globalData);
44     m_index = 0;
45     append(jsString);
46 }
47
48 JSString::~JSString()
49 {
50     ASSERT(vptr() == JSGlobalData::jsStringVPtr);
51 }
52
53 void JSString::visitChildren(JSCell* cell, SlotVisitor& visitor)
54 {
55     JSString* thisObject = jsCast<JSString*>(cell);
56     Base::visitChildren(thisObject, visitor);
57     for (size_t i = 0; i < s_maxInternalRopeLength && thisObject->m_fibers[i]; ++i)
58         visitor.append(&thisObject->m_fibers[i]);
59 }
60
61 void JSString::resolveRope(ExecState* exec) const
62 {
63     ASSERT(isRope());
64
65     if (is8Bit()) {
66         LChar* buffer;
67         if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer))
68             m_value = newImpl.release();
69         else {
70             outOfMemory(exec);
71             return;
72         }
73
74         for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
75             if (m_fibers[i]->isRope())
76                 return resolveRopeSlowCase8(buffer);
77         }
78
79         LChar* position = buffer;
80         for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
81             StringImpl* string = m_fibers[i]->m_value.impl();
82             unsigned length = string->length();
83             StringImpl::copyChars(position, string->characters8(), length);
84             position += length;
85             m_fibers[i].clear();
86         }
87         ASSERT((buffer + m_length) == position);
88         ASSERT(!isRope());
89
90         return;
91     }
92
93     UChar* buffer;
94     if (RefPtr<StringImpl> newImpl = StringImpl::tryCreateUninitialized(m_length, buffer))
95         m_value = newImpl.release();
96     else {
97         outOfMemory(exec);
98         return;
99     }
100
101     for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
102         if (m_fibers[i]->isRope())
103             return resolveRopeSlowCase(buffer);
104     }
105
106     UChar* position = buffer;
107     for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
108         StringImpl* string = m_fibers[i]->m_value.impl();
109         unsigned length = string->length();
110         StringImpl::copyChars(position, string->characters(), length);
111         position += length;
112         m_fibers[i].clear();
113     }
114     ASSERT((buffer + m_length) == position);
115     ASSERT(!isRope());
116 }
117
118 // Overview: These functions convert a JSString from holding a string in rope form
119 // down to a simple UString representation.  It does so by building up the string
120 // backwards, since we want to avoid recursion, we expect that the tree structure
121 // representing the rope is likely imbalanced with more nodes down the left side
122 // (since appending to the string is likely more common) - and as such resolving
123 // in this fashion should minimize work queue size.  (If we built the queue forwards
124 // we would likely have to place all of the constituent StringImpls into the
125 // Vector before performing any concatenation, but by working backwards we likely
126 // only fill the queue with the number of substrings at any given level in a
127 // rope-of-ropes.)    
128 void JSString::resolveRopeSlowCase8(LChar* buffer) const
129 {
130     LChar* position = buffer + m_length; // We will be working backwards over the rope.
131     Vector<JSString*, 32> workQueue; // Putting strings into a Vector is only OK because there are no GC points in this method.
132     
133     for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i) {
134         workQueue.append(m_fibers[i].get());
135         // Clearing here works only because there are no GC points in this method.
136         m_fibers[i].clear();
137     }
138
139     while (!workQueue.isEmpty()) {
140         JSString* currentFiber = workQueue.last();
141         workQueue.removeLast();
142
143         if (currentFiber->isRope()) {
144             for (size_t i = 0; i < s_maxInternalRopeLength && currentFiber->m_fibers[i]; ++i)
145                 workQueue.append(currentFiber->m_fibers[i].get());
146             continue;
147         }
148
149         StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
150         unsigned length = string->length();
151         position -= length;
152         StringImpl::copyChars(position, string->characters8(), length);
153     }
154
155     ASSERT(buffer == position);
156     ASSERT(!isRope());
157 }
158
159 void JSString::resolveRopeSlowCase(UChar* buffer) const
160 {
161     UChar* position = buffer + m_length; // We will be working backwards over the rope.
162     Vector<JSString*, 32> workQueue; // These strings are kept alive by the parent rope, so using a Vector is OK.
163
164     for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
165         workQueue.append(m_fibers[i].get());
166
167     while (!workQueue.isEmpty()) {
168         JSString* currentFiber = workQueue.last();
169         workQueue.removeLast();
170
171         if (currentFiber->isRope()) {
172             for (size_t i = 0; i < s_maxInternalRopeLength && currentFiber->m_fibers[i]; ++i)
173                 workQueue.append(currentFiber->m_fibers[i].get());
174             continue;
175         }
176
177         StringImpl* string = static_cast<StringImpl*>(currentFiber->m_value.impl());
178         unsigned length = string->length();
179         position -= length;
180         StringImpl::copyChars(position, string->characters(), length);
181     }
182
183     ASSERT(buffer == position);
184     ASSERT(!isRope());
185 }
186
187 void JSString::outOfMemory(ExecState* exec) const
188 {
189     for (size_t i = 0; i < s_maxInternalRopeLength && m_fibers[i]; ++i)
190         m_fibers[i].clear();
191     ASSERT(!isRope());
192     ASSERT(m_value == UString());
193     if (exec)
194         throwOutOfMemoryError(exec);
195 }
196
197 JSValue JSString::replaceCharacter(ExecState* exec, UChar character, const UString& replacement)
198 {
199     size_t matchPosition = value(exec).find(character);
200     if (matchPosition == notFound)
201         return JSValue(this);
202     return jsString(exec, m_value.substringSharingImpl(0, matchPosition), replacement, value(exec).substringSharingImpl(matchPosition + 1));
203 }
204
205 JSString* JSString::getIndexSlowCase(ExecState* exec, unsigned i)
206 {
207     ASSERT(isRope());
208     resolveRope(exec);
209     // Return a safe no-value result, this should never be used, since the excetion will be thrown.
210     if (exec->exception())
211         return jsString(exec, "");
212     ASSERT(!isRope());
213     ASSERT(i < m_value.length());
214     return jsSingleCharacterSubstring(exec, m_value, i);
215 }
216
217 JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
218 {
219     return const_cast<JSString*>(this);
220 }
221
222 bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result) const
223 {
224     result = this;
225     number = jsToNumber(value(exec));
226     return false;
227 }
228
229 bool JSString::toBoolean(ExecState*) const
230 {
231     return m_length;
232 }
233
234 double JSString::toNumber(ExecState* exec) const
235 {
236     return jsToNumber(value(exec));
237 }
238
239 UString JSString::toString(ExecState* exec) const
240 {
241     return value(exec);
242 }
243
244 inline StringObject* StringObject::create(ExecState* exec, JSGlobalObject* globalObject, JSString* string)
245 {
246     StringObject* object = new (allocateCell<StringObject>(*exec->heap())) StringObject(exec->globalData(), globalObject->stringObjectStructure());
247     object->finishCreation(exec->globalData(), string);
248     return object;
249 }
250
251 JSObject* JSString::toObject(ExecState* exec, JSGlobalObject* globalObject) const
252 {
253     return StringObject::create(exec, globalObject, const_cast<JSString*>(this));
254 }
255
256 JSObject* JSString::toThisObject(JSCell* cell, ExecState* exec)
257 {
258     return StringObject::create(exec, exec->lexicalGlobalObject(), jsCast<JSString*>(cell));
259 }
260
261 bool JSString::getOwnPropertySlot(JSCell* cell, ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
262 {
263     JSString* thisObject = jsCast<JSString*>(cell);
264     // The semantics here are really getPropertySlot, not getOwnPropertySlot.
265     // This function should only be called by JSValue::get.
266     if (thisObject->getStringPropertySlot(exec, propertyName, slot))
267         return true;
268     if (propertyName == exec->propertyNames().underscoreProto) {
269         slot.setValue(exec->lexicalGlobalObject()->stringPrototype());
270         return true;
271     }
272     slot.setBase(thisObject);
273     JSObject* object;
274     for (JSValue prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype.isNull(); prototype = object->prototype()) {
275         object = asObject(prototype);
276         if (object->methodTable()->getOwnPropertySlot(object, exec, propertyName, slot))
277             return true;
278     }
279     slot.setUndefined();
280     return true;
281 }
282
283 bool JSString::getStringPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
284 {
285     if (propertyName == exec->propertyNames().length) {
286         descriptor.setDescriptor(jsNumber(m_length), DontEnum | DontDelete | ReadOnly);
287         return true;
288     }
289     
290     bool isStrictUInt32;
291     unsigned i = propertyName.toUInt32(isStrictUInt32);
292     if (isStrictUInt32 && i < m_length) {
293         descriptor.setDescriptor(getIndex(exec, i), DontDelete | ReadOnly);
294         return true;
295     }
296     
297     return false;
298 }
299
300 bool JSString::getOwnPropertySlotByIndex(JSCell* cell, ExecState* exec, unsigned propertyName, PropertySlot& slot)
301 {
302     JSString* thisObject = jsCast<JSString*>(cell);
303     // The semantics here are really getPropertySlot, not getOwnPropertySlot.
304     // This function should only be called by JSValue::get.
305     if (thisObject->getStringPropertySlot(exec, propertyName, slot))
306         return true;
307     return JSString::getOwnPropertySlot(thisObject, exec, Identifier::from(exec, propertyName), slot);
308 }
309
310 } // namespace JSC