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