1 | /*
|
---|
2 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
4 | * Copyright (C) 2003, 2004, 2005, 2006, 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 | #ifndef JSString_h
|
---|
24 | #define JSString_h
|
---|
25 |
|
---|
26 | #include "CallFrame.h"
|
---|
27 | #include "CommonIdentifiers.h"
|
---|
28 | #include "Identifier.h"
|
---|
29 | #include "JSNumberCell.h"
|
---|
30 | #include "PropertyDescriptor.h"
|
---|
31 | #include "PropertySlot.h"
|
---|
32 | #include "RopeImpl.h"
|
---|
33 |
|
---|
34 | namespace JSC {
|
---|
35 |
|
---|
36 | class JSString;
|
---|
37 |
|
---|
38 | JSString* jsEmptyString(JSGlobalData*);
|
---|
39 | JSString* jsEmptyString(ExecState*);
|
---|
40 | JSString* jsString(JSGlobalData*, const UString&); // returns empty string if passed null string
|
---|
41 | JSString* jsString(ExecState*, const UString&); // returns empty string if passed null string
|
---|
42 |
|
---|
43 | JSString* jsSingleCharacterString(JSGlobalData*, UChar);
|
---|
44 | JSString* jsSingleCharacterString(ExecState*, UChar);
|
---|
45 | JSString* jsSingleCharacterSubstring(ExecState*, const UString&, unsigned offset);
|
---|
46 | JSString* jsSubstring(JSGlobalData*, const UString&, unsigned offset, unsigned length);
|
---|
47 | JSString* jsSubstring(ExecState*, const UString&, unsigned offset, unsigned length);
|
---|
48 |
|
---|
49 | // Non-trivial strings are two or more characters long.
|
---|
50 | // These functions are faster than just calling jsString.
|
---|
51 | JSString* jsNontrivialString(JSGlobalData*, const UString&);
|
---|
52 | JSString* jsNontrivialString(ExecState*, const UString&);
|
---|
53 | JSString* jsNontrivialString(JSGlobalData*, const char*);
|
---|
54 | JSString* jsNontrivialString(ExecState*, const char*);
|
---|
55 |
|
---|
56 | // Should be used for strings that are owned by an object that will
|
---|
57 | // likely outlive the JSValue this makes, such as the parse tree or a
|
---|
58 | // DOM object that contains a UString
|
---|
59 | JSString* jsOwnedString(JSGlobalData*, const UString&);
|
---|
60 | JSString* jsOwnedString(ExecState*, const UString&);
|
---|
61 |
|
---|
62 | typedef void (*JSStringFinalizerCallback)(JSString*, void* context);
|
---|
63 | JSString* jsStringWithFinalizer(ExecState*, const UString&, JSStringFinalizerCallback callback, void* context);
|
---|
64 |
|
---|
65 | class JS_EXPORTCLASS JSString : public JSCell {
|
---|
66 | public:
|
---|
67 | friend class JIT;
|
---|
68 | friend class JSGlobalData;
|
---|
69 | friend class SpecializedThunkJIT;
|
---|
70 | friend struct ThunkHelpers;
|
---|
71 |
|
---|
72 | class RopeBuilder {
|
---|
73 | public:
|
---|
74 | RopeBuilder(unsigned fiberCount)
|
---|
75 | : m_index(0)
|
---|
76 | , m_rope(RopeImpl::tryCreateUninitialized(fiberCount))
|
---|
77 | {
|
---|
78 | }
|
---|
79 |
|
---|
80 | bool isOutOfMemory() { return !m_rope; }
|
---|
81 |
|
---|
82 | void append(RopeImpl::Fiber& fiber)
|
---|
83 | {
|
---|
84 | ASSERT(m_rope);
|
---|
85 | m_rope->initializeFiber(m_index, fiber);
|
---|
86 | }
|
---|
87 | void append(const UString& string)
|
---|
88 | {
|
---|
89 | ASSERT(m_rope);
|
---|
90 | m_rope->initializeFiber(m_index, string.rep());
|
---|
91 | }
|
---|
92 | void append(JSString* jsString)
|
---|
93 | {
|
---|
94 | if (jsString->isRope()) {
|
---|
95 | for (unsigned i = 0; i < jsString->m_fiberCount; ++i)
|
---|
96 | append(jsString->m_other.m_fibers[i]);
|
---|
97 | } else
|
---|
98 | append(jsString->string());
|
---|
99 | }
|
---|
100 |
|
---|
101 | PassRefPtr<RopeImpl> release()
|
---|
102 | {
|
---|
103 | ASSERT(m_index == m_rope->fiberCount());
|
---|
104 | return m_rope.release();
|
---|
105 | }
|
---|
106 |
|
---|
107 | unsigned length() { return m_rope->length(); }
|
---|
108 |
|
---|
109 | private:
|
---|
110 | unsigned m_index;
|
---|
111 | RefPtr<RopeImpl> m_rope;
|
---|
112 | };
|
---|
113 |
|
---|
114 | class RopeIterator {
|
---|
115 | public:
|
---|
116 | RopeIterator() { }
|
---|
117 |
|
---|
118 | RopeIterator(RopeImpl::Fiber* fibers, size_t fiberCount)
|
---|
119 | {
|
---|
120 | ASSERT(fiberCount);
|
---|
121 | m_workQueue.append(WorkItem(fibers, fiberCount));
|
---|
122 | skipRopes();
|
---|
123 | }
|
---|
124 |
|
---|
125 | RopeIterator& operator++()
|
---|
126 | {
|
---|
127 | WorkItem& item = m_workQueue.last();
|
---|
128 | ASSERT(!RopeImpl::isRope(item.fibers[item.i]));
|
---|
129 | if (++item.i == item.fiberCount)
|
---|
130 | m_workQueue.removeLast();
|
---|
131 | skipRopes();
|
---|
132 | return *this;
|
---|
133 | }
|
---|
134 |
|
---|
135 | UStringImpl* operator*()
|
---|
136 | {
|
---|
137 | WorkItem& item = m_workQueue.last();
|
---|
138 | RopeImpl::Fiber fiber = item.fibers[item.i];
|
---|
139 | ASSERT(!RopeImpl::isRope(fiber));
|
---|
140 | return static_cast<UStringImpl*>(fiber);
|
---|
141 | }
|
---|
142 |
|
---|
143 | bool operator!=(const RopeIterator& other) const
|
---|
144 | {
|
---|
145 | return m_workQueue != other.m_workQueue;
|
---|
146 | }
|
---|
147 |
|
---|
148 | private:
|
---|
149 | struct WorkItem {
|
---|
150 | WorkItem(RopeImpl::Fiber* fibers, size_t fiberCount)
|
---|
151 | : fibers(fibers)
|
---|
152 | , fiberCount(fiberCount)
|
---|
153 | , i(0)
|
---|
154 | {
|
---|
155 | }
|
---|
156 |
|
---|
157 | bool operator!=(const WorkItem& other) const
|
---|
158 | {
|
---|
159 | return fibers != other.fibers || fiberCount != other.fiberCount || i != other.i;
|
---|
160 | }
|
---|
161 |
|
---|
162 | RopeImpl::Fiber* fibers;
|
---|
163 | size_t fiberCount;
|
---|
164 | size_t i;
|
---|
165 | };
|
---|
166 |
|
---|
167 | void skipRopes()
|
---|
168 | {
|
---|
169 | if (m_workQueue.isEmpty())
|
---|
170 | return;
|
---|
171 |
|
---|
172 | while (1) {
|
---|
173 | WorkItem& item = m_workQueue.last();
|
---|
174 | RopeImpl::Fiber fiber = item.fibers[item.i];
|
---|
175 | if (!RopeImpl::isRope(fiber))
|
---|
176 | break;
|
---|
177 | RopeImpl* rope = static_cast<RopeImpl*>(fiber);
|
---|
178 | if (++item.i == item.fiberCount)
|
---|
179 | m_workQueue.removeLast();
|
---|
180 | m_workQueue.append(WorkItem(rope->fibers(), rope->fiberCount()));
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | Vector<WorkItem, 16> m_workQueue;
|
---|
185 | };
|
---|
186 |
|
---|
187 | ALWAYS_INLINE JSString(JSGlobalData* globalData, const UString& value)
|
---|
188 | : JSCell(globalData->stringStructure.get())
|
---|
189 | , m_length(value.size())
|
---|
190 | , m_value(value)
|
---|
191 | , m_fiberCount(0)
|
---|
192 | {
|
---|
193 | ASSERT(!m_value.isNull());
|
---|
194 | Heap::heap(this)->reportExtraMemoryCost(value.cost());
|
---|
195 | }
|
---|
196 |
|
---|
197 | enum HasOtherOwnerType { HasOtherOwner };
|
---|
198 | JSString(JSGlobalData* globalData, const UString& value, HasOtherOwnerType)
|
---|
199 | : JSCell(globalData->stringStructure.get())
|
---|
200 | , m_length(value.size())
|
---|
201 | , m_value(value)
|
---|
202 | , m_fiberCount(0)
|
---|
203 | {
|
---|
204 | ASSERT(!m_value.isNull());
|
---|
205 | }
|
---|
206 | JSString(JSGlobalData* globalData, PassRefPtr<UStringImpl> value, HasOtherOwnerType)
|
---|
207 | : JSCell(globalData->stringStructure.get())
|
---|
208 | , m_length(value->length())
|
---|
209 | , m_value(value)
|
---|
210 | , m_fiberCount(0)
|
---|
211 | {
|
---|
212 | ASSERT(!m_value.isNull());
|
---|
213 | }
|
---|
214 | JSString(JSGlobalData* globalData, PassRefPtr<RopeImpl> rope)
|
---|
215 | : JSCell(globalData->stringStructure.get())
|
---|
216 | , m_length(rope->length())
|
---|
217 | , m_fiberCount(1)
|
---|
218 | {
|
---|
219 | m_other.m_fibers[0] = rope.releaseRef();
|
---|
220 | }
|
---|
221 | // This constructor constructs a new string by concatenating s1 & s2.
|
---|
222 | // This should only be called with fiberCount <= 3.
|
---|
223 | JSString(JSGlobalData* globalData, unsigned fiberCount, JSString* s1, JSString* s2)
|
---|
224 | : JSCell(globalData->stringStructure.get())
|
---|
225 | , m_length(s1->length() + s2->length())
|
---|
226 | , m_fiberCount(fiberCount)
|
---|
227 | {
|
---|
228 | ASSERT(fiberCount <= s_maxInternalRopeLength);
|
---|
229 | unsigned index = 0;
|
---|
230 | appendStringInConstruct(index, s1);
|
---|
231 | appendStringInConstruct(index, s2);
|
---|
232 | ASSERT(fiberCount == index);
|
---|
233 | }
|
---|
234 | // This constructor constructs a new string by concatenating s1 & s2.
|
---|
235 | // This should only be called with fiberCount <= 3.
|
---|
236 | JSString(JSGlobalData* globalData, unsigned fiberCount, JSString* s1, const UString& u2)
|
---|
237 | : JSCell(globalData->stringStructure.get())
|
---|
238 | , m_length(s1->length() + u2.size())
|
---|
239 | , m_fiberCount(fiberCount)
|
---|
240 | {
|
---|
241 | ASSERT(fiberCount <= s_maxInternalRopeLength);
|
---|
242 | unsigned index = 0;
|
---|
243 | appendStringInConstruct(index, s1);
|
---|
244 | appendStringInConstruct(index, u2);
|
---|
245 | ASSERT(fiberCount == index);
|
---|
246 | }
|
---|
247 | // This constructor constructs a new string by concatenating s1 & s2.
|
---|
248 | // This should only be called with fiberCount <= 3.
|
---|
249 | JSString(JSGlobalData* globalData, unsigned fiberCount, const UString& u1, JSString* s2)
|
---|
250 | : JSCell(globalData->stringStructure.get())
|
---|
251 | , m_length(u1.size() + s2->length())
|
---|
252 | , m_fiberCount(fiberCount)
|
---|
253 | {
|
---|
254 | ASSERT(fiberCount <= s_maxInternalRopeLength);
|
---|
255 | unsigned index = 0;
|
---|
256 | appendStringInConstruct(index, u1);
|
---|
257 | appendStringInConstruct(index, s2);
|
---|
258 | ASSERT(fiberCount == index);
|
---|
259 | }
|
---|
260 | // This constructor constructs a new string by concatenating v1, v2 & v3.
|
---|
261 | // This should only be called with fiberCount <= 3 ... which since every
|
---|
262 | // value must require a fiberCount of at least one implies that the length
|
---|
263 | // for each value must be exactly 1!
|
---|
264 | JSString(ExecState* exec, JSValue v1, JSValue v2, JSValue v3)
|
---|
265 | : JSCell(exec->globalData().stringStructure.get())
|
---|
266 | , m_length(0)
|
---|
267 | , m_fiberCount(s_maxInternalRopeLength)
|
---|
268 | {
|
---|
269 | unsigned index = 0;
|
---|
270 | appendValueInConstructAndIncrementLength(exec, index, v1);
|
---|
271 | appendValueInConstructAndIncrementLength(exec, index, v2);
|
---|
272 | appendValueInConstructAndIncrementLength(exec, index, v3);
|
---|
273 | ASSERT(index == s_maxInternalRopeLength);
|
---|
274 | }
|
---|
275 |
|
---|
276 | // This constructor constructs a new string by concatenating u1 & u2.
|
---|
277 | JSString(JSGlobalData* globalData, const UString& u1, const UString& u2)
|
---|
278 | : JSCell(globalData->stringStructure.get())
|
---|
279 | , m_length(u1.size() + u2.size())
|
---|
280 | , m_fiberCount(2)
|
---|
281 | {
|
---|
282 | unsigned index = 0;
|
---|
283 | appendStringInConstruct(index, u1);
|
---|
284 | appendStringInConstruct(index, u2);
|
---|
285 | ASSERT(index <= s_maxInternalRopeLength);
|
---|
286 | }
|
---|
287 |
|
---|
288 | // This constructor constructs a new string by concatenating u1, u2 & u3.
|
---|
289 | JSString(JSGlobalData* globalData, const UString& u1, const UString& u2, const UString& u3)
|
---|
290 | : JSCell(globalData->stringStructure.get())
|
---|
291 | , m_length(u1.size() + u2.size() + u3.size())
|
---|
292 | , m_fiberCount(s_maxInternalRopeLength)
|
---|
293 | {
|
---|
294 | unsigned index = 0;
|
---|
295 | appendStringInConstruct(index, u1);
|
---|
296 | appendStringInConstruct(index, u2);
|
---|
297 | appendStringInConstruct(index, u3);
|
---|
298 | ASSERT(index <= s_maxInternalRopeLength);
|
---|
299 | }
|
---|
300 |
|
---|
301 | JSString(JSGlobalData* globalData, const UString& value, JSStringFinalizerCallback finalizer, void* context)
|
---|
302 | : JSCell(globalData->stringStructure.get())
|
---|
303 | , m_length(value.size())
|
---|
304 | , m_value(value)
|
---|
305 | , m_fiberCount(0)
|
---|
306 | {
|
---|
307 | ASSERT(!m_value.isNull());
|
---|
308 | // nasty hack because we can't union non-POD types
|
---|
309 | m_other.m_finalizerCallback = finalizer;
|
---|
310 | m_other.m_finalizerContext = context;
|
---|
311 | Heap::heap(this)->reportExtraMemoryCost(value.cost());
|
---|
312 | }
|
---|
313 |
|
---|
314 | ~JSString()
|
---|
315 | {
|
---|
316 | ASSERT(vptr() == JSGlobalData::jsStringVPtr);
|
---|
317 | for (unsigned i = 0; i < m_fiberCount; ++i)
|
---|
318 | RopeImpl::deref(m_other.m_fibers[i]);
|
---|
319 |
|
---|
320 | if (!m_fiberCount && m_other.m_finalizerCallback)
|
---|
321 | m_other.m_finalizerCallback(this, m_other.m_finalizerContext);
|
---|
322 | }
|
---|
323 |
|
---|
324 | const UString& value(ExecState* exec) const
|
---|
325 | {
|
---|
326 | if (isRope())
|
---|
327 | resolveRope(exec);
|
---|
328 | return m_value;
|
---|
329 | }
|
---|
330 | const UString tryGetValue() const
|
---|
331 | {
|
---|
332 | // If this is a rope, m_value should be null -
|
---|
333 | // if this is not a rope, m_value should be non-null.
|
---|
334 | ASSERT(isRope() == m_value.isNull());
|
---|
335 | return m_value;
|
---|
336 | }
|
---|
337 | unsigned length() { return m_length; }
|
---|
338 |
|
---|
339 | bool getStringPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
340 | bool getStringPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
341 | bool getStringPropertyDescriptor(ExecState*, const Identifier& propertyName, PropertyDescriptor&);
|
---|
342 |
|
---|
343 | bool canGetIndex(unsigned i) { return i < m_length; }
|
---|
344 | JSString* getIndex(ExecState*, unsigned);
|
---|
345 | JSString* getIndexSlowCase(ExecState*, unsigned);
|
---|
346 |
|
---|
347 | JSValue replaceCharacter(ExecState*, UChar, const UString& replacement);
|
---|
348 |
|
---|
349 | static PassRefPtr<Structure> createStructure(JSValue proto) { return Structure::create(proto, TypeInfo(StringType, OverridesGetOwnPropertySlot | NeedsThisConversion), AnonymousSlotCount); }
|
---|
350 |
|
---|
351 | private:
|
---|
352 | enum VPtrStealingHackType { VPtrStealingHack };
|
---|
353 | JSString(VPtrStealingHackType)
|
---|
354 | : JSCell(0)
|
---|
355 | , m_fiberCount(0)
|
---|
356 | {
|
---|
357 | }
|
---|
358 |
|
---|
359 | void resolveRope(ExecState*) const;
|
---|
360 |
|
---|
361 | void appendStringInConstruct(unsigned& index, const UString& string)
|
---|
362 | {
|
---|
363 | UStringImpl* impl = string.rep();
|
---|
364 | impl->ref();
|
---|
365 | m_other.m_fibers[index++] = impl;
|
---|
366 | }
|
---|
367 |
|
---|
368 | void appendStringInConstruct(unsigned& index, JSString* jsString)
|
---|
369 | {
|
---|
370 | if (jsString->isRope()) {
|
---|
371 | for (unsigned i = 0; i < jsString->m_fiberCount; ++i) {
|
---|
372 | RopeImpl::Fiber fiber = jsString->m_other.m_fibers[i];
|
---|
373 | fiber->ref();
|
---|
374 | m_other.m_fibers[index++] = fiber;
|
---|
375 | }
|
---|
376 | } else
|
---|
377 | appendStringInConstruct(index, jsString->string());
|
---|
378 | }
|
---|
379 |
|
---|
380 | void appendValueInConstructAndIncrementLength(ExecState* exec, unsigned& index, JSValue v)
|
---|
381 | {
|
---|
382 | if (v.isString()) {
|
---|
383 | ASSERT(asCell(v)->isString());
|
---|
384 | JSString* s = static_cast<JSString*>(asCell(v));
|
---|
385 | ASSERT(s->size() == 1);
|
---|
386 | appendStringInConstruct(index, s);
|
---|
387 | m_length += s->length();
|
---|
388 | } else {
|
---|
389 | UString u(v.toString(exec));
|
---|
390 | UStringImpl* impl = u.rep();
|
---|
391 | impl->ref();
|
---|
392 | m_other.m_fibers[index++] = impl;
|
---|
393 | m_length += u.size();
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | virtual JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const;
|
---|
398 | virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue& value);
|
---|
399 | virtual bool toBoolean(ExecState*) const;
|
---|
400 | virtual double toNumber(ExecState*) const;
|
---|
401 | virtual JSObject* toObject(ExecState*) const;
|
---|
402 | virtual UString toString(ExecState*) const;
|
---|
403 |
|
---|
404 | virtual JSObject* toThisObject(ExecState*) const;
|
---|
405 |
|
---|
406 | // Actually getPropertySlot, not getOwnPropertySlot (see JSCell).
|
---|
407 | virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
408 | virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
409 | virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
|
---|
410 |
|
---|
411 | static const unsigned s_maxInternalRopeLength = 3;
|
---|
412 |
|
---|
413 | // A string is represented either by a UString or a RopeImpl.
|
---|
414 | unsigned m_length;
|
---|
415 | mutable UString m_value;
|
---|
416 | mutable unsigned m_fiberCount;
|
---|
417 | // This structure exists to support a temporary workaround for a GC issue.
|
---|
418 | struct JSStringFinalizerStruct {
|
---|
419 | JSStringFinalizerStruct() : m_finalizerCallback(0) {}
|
---|
420 | union {
|
---|
421 | mutable RopeImpl::Fiber m_fibers[s_maxInternalRopeLength];
|
---|
422 | struct {
|
---|
423 | JSStringFinalizerCallback m_finalizerCallback;
|
---|
424 | void* m_finalizerContext;
|
---|
425 | };
|
---|
426 | };
|
---|
427 | } m_other;
|
---|
428 |
|
---|
429 | bool isRope() const { return m_fiberCount; }
|
---|
430 | UString& string() { ASSERT(!isRope()); return m_value; }
|
---|
431 | unsigned size() { return m_fiberCount ? m_fiberCount : 1; }
|
---|
432 |
|
---|
433 | friend JSValue jsString(ExecState* exec, JSString* s1, JSString* s2);
|
---|
434 | friend JSValue jsString(ExecState* exec, const UString& u1, JSString* s2);
|
---|
435 | friend JSValue jsString(ExecState* exec, JSString* s1, const UString& u2);
|
---|
436 | friend JSValue jsString(ExecState* exec, Register* strings, unsigned count);
|
---|
437 | friend JSValue jsString(ExecState* exec, JSValue thisValue, const ArgList& args);
|
---|
438 | friend JSString* jsStringWithFinalizer(ExecState*, const UString&, JSStringFinalizerCallback callback, void* context);
|
---|
439 | };
|
---|
440 |
|
---|
441 | JSString* asString(JSValue);
|
---|
442 |
|
---|
443 | // When an object is created from a different DLL, MSVC changes vptr to a "local" one right after invoking a constructor,
|
---|
444 | // see <http://groups.google.com/group/microsoft.public.vc.language/msg/55cdcefeaf770212>.
|
---|
445 | // This breaks isJSString(), and we don't need that hack anyway, so we change vptr back to primary one.
|
---|
446 | // The below function must be called by any inline function that invokes a JSString constructor.
|
---|
447 | #if COMPILER(MSVC) && !defined(BUILDING_JavaScriptCore)
|
---|
448 | inline JSString* fixupVPtr(JSGlobalData* globalData, JSString* string) { string->setVPtr(globalData->jsStringVPtr); return string; }
|
---|
449 | #else
|
---|
450 | inline JSString* fixupVPtr(JSGlobalData*, JSString* string) { return string; }
|
---|
451 | #endif
|
---|
452 |
|
---|
453 | inline JSString* asString(JSValue value)
|
---|
454 | {
|
---|
455 | ASSERT(asCell(value)->isString());
|
---|
456 | return static_cast<JSString*>(asCell(value));
|
---|
457 | }
|
---|
458 |
|
---|
459 | inline JSString* jsEmptyString(JSGlobalData* globalData)
|
---|
460 | {
|
---|
461 | return globalData->smallStrings.emptyString(globalData);
|
---|
462 | }
|
---|
463 |
|
---|
464 | inline JSString* jsSingleCharacterString(JSGlobalData* globalData, UChar c)
|
---|
465 | {
|
---|
466 | if (c <= 0xFF)
|
---|
467 | return globalData->smallStrings.singleCharacterString(globalData, c);
|
---|
468 | return fixupVPtr(globalData, new (globalData) JSString(globalData, UString(&c, 1)));
|
---|
469 | }
|
---|
470 |
|
---|
471 | inline JSString* jsSingleCharacterSubstring(ExecState* exec, const UString& s, unsigned offset)
|
---|
472 | {
|
---|
473 | JSGlobalData* globalData = &exec->globalData();
|
---|
474 | ASSERT(offset < static_cast<unsigned>(s.size()));
|
---|
475 | UChar c = s.data()[offset];
|
---|
476 | if (c <= 0xFF)
|
---|
477 | return globalData->smallStrings.singleCharacterString(globalData, c);
|
---|
478 | return fixupVPtr(globalData, new (globalData) JSString(globalData, UString(UStringImpl::create(s.rep(), offset, 1))));
|
---|
479 | }
|
---|
480 |
|
---|
481 | inline JSString* jsNontrivialString(JSGlobalData* globalData, const char* s)
|
---|
482 | {
|
---|
483 | ASSERT(s);
|
---|
484 | ASSERT(s[0]);
|
---|
485 | ASSERT(s[1]);
|
---|
486 | return fixupVPtr(globalData, new (globalData) JSString(globalData, s));
|
---|
487 | }
|
---|
488 |
|
---|
489 | inline JSString* jsNontrivialString(JSGlobalData* globalData, const UString& s)
|
---|
490 | {
|
---|
491 | ASSERT(s.size() > 1);
|
---|
492 | return fixupVPtr(globalData, new (globalData) JSString(globalData, s));
|
---|
493 | }
|
---|
494 |
|
---|
495 | inline JSString* JSString::getIndex(ExecState* exec, unsigned i)
|
---|
496 | {
|
---|
497 | ASSERT(canGetIndex(i));
|
---|
498 | if (isRope())
|
---|
499 | return getIndexSlowCase(exec, i);
|
---|
500 | ASSERT(i < m_value.size());
|
---|
501 | return jsSingleCharacterSubstring(exec, value(exec), i);
|
---|
502 | }
|
---|
503 |
|
---|
504 | inline JSString* jsString(JSGlobalData* globalData, const UString& s)
|
---|
505 | {
|
---|
506 | int size = s.size();
|
---|
507 | if (!size)
|
---|
508 | return globalData->smallStrings.emptyString(globalData);
|
---|
509 | if (size == 1) {
|
---|
510 | UChar c = s.data()[0];
|
---|
511 | if (c <= 0xFF)
|
---|
512 | return globalData->smallStrings.singleCharacterString(globalData, c);
|
---|
513 | }
|
---|
514 | return fixupVPtr(globalData, new (globalData) JSString(globalData, s));
|
---|
515 | }
|
---|
516 |
|
---|
517 | inline JSString* jsStringWithFinalizer(ExecState* exec, const UString& s, JSStringFinalizerCallback callback, void* context)
|
---|
518 | {
|
---|
519 | ASSERT(s.size() && (s.size() > 1 || s.data()[0] > 0xFF));
|
---|
520 | JSGlobalData* globalData = &exec->globalData();
|
---|
521 | return fixupVPtr(globalData, new (globalData) JSString(globalData, s, callback, context));
|
---|
522 | }
|
---|
523 |
|
---|
524 | inline JSString* jsSubstring(JSGlobalData* globalData, const UString& s, unsigned offset, unsigned length)
|
---|
525 | {
|
---|
526 | ASSERT(offset <= static_cast<unsigned>(s.size()));
|
---|
527 | ASSERT(length <= static_cast<unsigned>(s.size()));
|
---|
528 | ASSERT(offset + length <= static_cast<unsigned>(s.size()));
|
---|
529 | if (!length)
|
---|
530 | return globalData->smallStrings.emptyString(globalData);
|
---|
531 | if (length == 1) {
|
---|
532 | UChar c = s.data()[offset];
|
---|
533 | if (c <= 0xFF)
|
---|
534 | return globalData->smallStrings.singleCharacterString(globalData, c);
|
---|
535 | }
|
---|
536 | return fixupVPtr(globalData, new (globalData) JSString(globalData, UString(UStringImpl::create(s.rep(), offset, length)), JSString::HasOtherOwner));
|
---|
537 | }
|
---|
538 |
|
---|
539 | inline JSString* jsOwnedString(JSGlobalData* globalData, const UString& s)
|
---|
540 | {
|
---|
541 | int size = s.size();
|
---|
542 | if (!size)
|
---|
543 | return globalData->smallStrings.emptyString(globalData);
|
---|
544 | if (size == 1) {
|
---|
545 | UChar c = s.data()[0];
|
---|
546 | if (c <= 0xFF)
|
---|
547 | return globalData->smallStrings.singleCharacterString(globalData, c);
|
---|
548 | }
|
---|
549 | return fixupVPtr(globalData, new (globalData) JSString(globalData, s, JSString::HasOtherOwner));
|
---|
550 | }
|
---|
551 |
|
---|
552 | inline JSString* jsEmptyString(ExecState* exec) { return jsEmptyString(&exec->globalData()); }
|
---|
553 | inline JSString* jsString(ExecState* exec, const UString& s) { return jsString(&exec->globalData(), s); }
|
---|
554 | inline JSString* jsSingleCharacterString(ExecState* exec, UChar c) { return jsSingleCharacterString(&exec->globalData(), c); }
|
---|
555 | inline JSString* jsSubstring(ExecState* exec, const UString& s, unsigned offset, unsigned length) { return jsSubstring(&exec->globalData(), s, offset, length); }
|
---|
556 | inline JSString* jsNontrivialString(ExecState* exec, const UString& s) { return jsNontrivialString(&exec->globalData(), s); }
|
---|
557 | inline JSString* jsNontrivialString(ExecState* exec, const char* s) { return jsNontrivialString(&exec->globalData(), s); }
|
---|
558 | inline JSString* jsOwnedString(ExecState* exec, const UString& s) { return jsOwnedString(&exec->globalData(), s); }
|
---|
559 |
|
---|
560 | ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
561 | {
|
---|
562 | if (propertyName == exec->propertyNames().length) {
|
---|
563 | slot.setValue(jsNumber(exec, m_length));
|
---|
564 | return true;
|
---|
565 | }
|
---|
566 |
|
---|
567 | bool isStrictUInt32;
|
---|
568 | unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
|
---|
569 | if (isStrictUInt32 && i < m_length) {
|
---|
570 | slot.setValue(getIndex(exec, i));
|
---|
571 | return true;
|
---|
572 | }
|
---|
573 |
|
---|
574 | return false;
|
---|
575 | }
|
---|
576 |
|
---|
577 | ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
|
---|
578 | {
|
---|
579 | if (propertyName < m_length) {
|
---|
580 | slot.setValue(getIndex(exec, propertyName));
|
---|
581 | return true;
|
---|
582 | }
|
---|
583 |
|
---|
584 | return false;
|
---|
585 | }
|
---|
586 |
|
---|
587 | inline bool isJSString(JSGlobalData* globalData, JSValue v) { return v.isCell() && v.asCell()->vptr() == globalData->jsStringVPtr; }
|
---|
588 |
|
---|
589 | // --- JSValue inlines ----------------------------
|
---|
590 |
|
---|
591 | inline UString JSValue::toString(ExecState* exec) const
|
---|
592 | {
|
---|
593 | if (isString())
|
---|
594 | return static_cast<JSString*>(asCell())->value(exec);
|
---|
595 | if (isInt32())
|
---|
596 | return exec->globalData().numericStrings.add(asInt32());
|
---|
597 | if (isDouble())
|
---|
598 | return exec->globalData().numericStrings.add(asDouble());
|
---|
599 | if (isTrue())
|
---|
600 | return "true";
|
---|
601 | if (isFalse())
|
---|
602 | return "false";
|
---|
603 | if (isNull())
|
---|
604 | return "null";
|
---|
605 | if (isUndefined())
|
---|
606 | return "undefined";
|
---|
607 | ASSERT(isCell());
|
---|
608 | return asCell()->toString(exec);
|
---|
609 | }
|
---|
610 |
|
---|
611 | inline UString JSValue::toPrimitiveString(ExecState* exec) const
|
---|
612 | {
|
---|
613 | if (isString())
|
---|
614 | return static_cast<JSString*>(asCell())->value(exec);
|
---|
615 | if (isInt32())
|
---|
616 | return exec->globalData().numericStrings.add(asInt32());
|
---|
617 | if (isDouble())
|
---|
618 | return exec->globalData().numericStrings.add(asDouble());
|
---|
619 | if (isTrue())
|
---|
620 | return "true";
|
---|
621 | if (isFalse())
|
---|
622 | return "false";
|
---|
623 | if (isNull())
|
---|
624 | return "null";
|
---|
625 | if (isUndefined())
|
---|
626 | return "undefined";
|
---|
627 | ASSERT(isCell());
|
---|
628 | return asCell()->toPrimitive(exec, NoPreference).toString(exec);
|
---|
629 | }
|
---|
630 |
|
---|
631 | } // namespace JSC
|
---|
632 |
|
---|
633 | #endif // JSString_h
|
---|