source: webkit/trunk/JavaScriptCore/runtime/JSString.cpp@ 53323

Last change on this file since 53323 was 53323, checked in by [email protected], 15 years ago

https://bugs.webkit.org/show_bug.cgi?id=33705
UStringImpl::create() should use internal storage

Reviewed by Oliver Hunt.

When creating a UStringImpl copying of a UChar*, we can use an internal buffer,
by calling UStringImpl::tryCreateUninitialized().

Also, remove duplicate of copyChars from JSString, call UStringImpl's version.

Small (max 0.5%) progression on Sunspidey.

  • runtime/JSString.cpp:

(JSC::JSString::resolveRope):

  • runtime/UStringImpl.h:

(JSC::UStringImpl::create):

  • Property svn:eol-style set to native
File size: 8.1 KB
Line 
1/*
2 * Copyright (C) 1999-2002 Harri Porten ([email protected])
3 * Copyright (C) 2001 Peter Kelly ([email protected])
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 "Operations.h"
29#include "StringObject.h"
30#include "StringPrototype.h"
31
32namespace JSC {
33
34void JSString::Rope::destructNonRecursive()
35{
36 Vector<Rope*, 32> workQueue;
37 Rope* rope = this;
38
39 while (true) {
40 unsigned length = rope->ropeLength();
41 for (unsigned i = 0; i < length; ++i) {
42 Fiber& fiber = rope->fibers(i);
43 if (fiber.isString())
44 fiber.string()->deref();
45 else {
46 Rope* nextRope = fiber.rope();
47 if (nextRope->hasOneRef())
48 workQueue.append(nextRope);
49 else
50 nextRope->deref();
51 }
52 }
53 if (rope != this)
54 fastFree(rope);
55
56 if (workQueue.isEmpty())
57 return;
58
59 rope = workQueue.last();
60 workQueue.removeLast();
61 }
62}
63
64JSString::Rope::~Rope()
65{
66 destructNonRecursive();
67}
68
69// Overview: this methods converts a JSString from holding a string in rope form
70// down to a simple UString representation. It does so by building up the string
71// backwards, since we want to avoid recursion, we expect that the tree structure
72// representing the rope is likely imbalanced with more nodes down the left side
73// (since appending to the string is likely more common) - and as such resolving
74// in this fashion should minimize work queue size. (If we built the queue forwards
75// we would likely have to place all of the constituent UString::Reps into the
76// Vector before performing any concatenation, but by working backwards we likely
77// only fill the queue with the number of substrings at any given level in a
78// rope-of-ropes.)
79void JSString::resolveRope(ExecState* exec) const
80{
81 ASSERT(isRope());
82
83 // Allocate the buffer to hold the final string, position initially points to the end.
84 UChar* buffer;
85 if (PassRefPtr<UStringImpl> newImpl = UStringImpl::tryCreateUninitialized(m_stringLength, buffer))
86 m_value = newImpl;
87 else {
88 for (unsigned i = 0; i < m_ropeLength; ++i)
89 m_fibers[i].deref();
90 m_ropeLength = 0;
91 ASSERT(!isRope());
92 ASSERT(m_value == UString());
93 throwOutOfMemoryError(exec);
94 return;
95 }
96 UChar* position = buffer + m_stringLength;
97
98 // Start with the current Rope.
99 Vector<Rope::Fiber, 32> workQueue;
100 Rope::Fiber currentFiber;
101 for (unsigned i = 0; i < (m_ropeLength - 1); ++i)
102 workQueue.append(m_fibers[i]);
103 currentFiber = m_fibers[m_ropeLength - 1];
104 while (true) {
105 if (currentFiber.isRope()) {
106 Rope* rope = currentFiber.rope();
107 // Copy the contents of the current rope into the workQueue, with the last item in 'currentFiber'
108 // (we will be working backwards over the rope).
109 unsigned ropeLengthMinusOne = rope->ropeLength() - 1;
110 for (unsigned i = 0; i < ropeLengthMinusOne; ++i)
111 workQueue.append(rope->fibers(i));
112 currentFiber = rope->fibers(ropeLengthMinusOne);
113 } else {
114 UString::Rep* string = currentFiber.string();
115 unsigned length = string->size();
116 position -= length;
117 UStringImpl::copyChars(position, string->data(), length);
118
119 // Was this the last item in the work queue?
120 if (workQueue.isEmpty()) {
121 // Create a string from the UChar buffer, clear the rope RefPtr.
122 ASSERT(buffer == position);
123 for (unsigned i = 0; i < m_ropeLength; ++i)
124 m_fibers[i].deref();
125 m_ropeLength = 0;
126
127 ASSERT(!isRope());
128 return;
129 }
130
131 // No! - set the next item up to process.
132 currentFiber = workQueue.last();
133 workQueue.removeLast();
134 }
135 }
136}
137
138JSValue JSString::toPrimitive(ExecState*, PreferredPrimitiveType) const
139{
140 return const_cast<JSString*>(this);
141}
142
143bool JSString::getPrimitiveNumber(ExecState* exec, double& number, JSValue& result)
144{
145 result = this;
146 number = value(exec).toDouble();
147 return false;
148}
149
150bool JSString::toBoolean(ExecState*) const
151{
152 return m_stringLength;
153}
154
155double JSString::toNumber(ExecState* exec) const
156{
157 return value(exec).toDouble();
158}
159
160UString JSString::toString(ExecState* exec) const
161{
162 return value(exec);
163}
164
165UString JSString::toThisString(ExecState* exec) const
166{
167 return value(exec);
168}
169
170JSString* JSString::toThisJSString(ExecState*)
171{
172 return this;
173}
174
175inline StringObject* StringObject::create(ExecState* exec, JSString* string)
176{
177 return new (exec) StringObject(exec->lexicalGlobalObject()->stringObjectStructure(), string);
178}
179
180JSObject* JSString::toObject(ExecState* exec) const
181{
182 return StringObject::create(exec, const_cast<JSString*>(this));
183}
184
185JSObject* JSString::toThisObject(ExecState* exec) const
186{
187 return StringObject::create(exec, const_cast<JSString*>(this));
188}
189
190bool JSString::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
191{
192 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
193 // This function should only be called by JSValue::get.
194 if (getStringPropertySlot(exec, propertyName, slot))
195 return true;
196 if (propertyName == exec->propertyNames().underscoreProto) {
197 slot.setValue(exec->lexicalGlobalObject()->stringPrototype());
198 return true;
199 }
200 slot.setBase(this);
201 JSObject* object;
202 for (JSValue prototype = exec->lexicalGlobalObject()->stringPrototype(); !prototype.isNull(); prototype = object->prototype()) {
203 object = asObject(prototype);
204 if (object->getOwnPropertySlot(exec, propertyName, slot))
205 return true;
206 }
207 slot.setUndefined();
208 return true;
209}
210
211bool JSString::getStringPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
212{
213 if (propertyName == exec->propertyNames().length) {
214 descriptor.setDescriptor(jsNumber(exec, m_stringLength), DontEnum | DontDelete | ReadOnly);
215 return true;
216 }
217
218 bool isStrictUInt32;
219 unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
220 if (isStrictUInt32 && i < m_stringLength) {
221 descriptor.setDescriptor(jsSingleCharacterSubstring(exec, value(exec), i), DontDelete | ReadOnly);
222 return true;
223 }
224
225 return false;
226}
227
228bool JSString::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor)
229{
230 if (getStringPropertyDescriptor(exec, propertyName, descriptor))
231 return true;
232 if (propertyName != exec->propertyNames().underscoreProto)
233 return false;
234 descriptor.setDescriptor(exec->lexicalGlobalObject()->stringPrototype(), DontEnum);
235 return true;
236}
237
238bool JSString::getOwnPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
239{
240 // The semantics here are really getPropertySlot, not getOwnPropertySlot.
241 // This function should only be called by JSValue::get.
242 if (getStringPropertySlot(exec, propertyName, slot))
243 return true;
244 return JSString::getOwnPropertySlot(exec, Identifier::from(exec, propertyName), slot);
245}
246
247} // namespace JSC
Note: See TracBrowser for help on using the repository browser.