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