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