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 |
|
---|
33 | namespace JSC {
|
---|
34 |
|
---|
35 | class JSString;
|
---|
36 |
|
---|
37 | JSString* jsEmptyString(JSGlobalData*);
|
---|
38 | JSString* jsEmptyString(ExecState*);
|
---|
39 | JSString* jsString(JSGlobalData*, const UString&); // returns empty string if passed null string
|
---|
40 | JSString* jsString(ExecState*, const UString&); // returns empty string if passed null string
|
---|
41 |
|
---|
42 | JSString* jsSingleCharacterString(JSGlobalData*, UChar);
|
---|
43 | JSString* jsSingleCharacterString(ExecState*, UChar);
|
---|
44 | JSString* jsSingleCharacterSubstring(JSGlobalData*, const UString&, unsigned offset);
|
---|
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 | class JSString : public JSCell {
|
---|
63 | friend class JIT;
|
---|
64 | friend struct VPtrSet;
|
---|
65 |
|
---|
66 | public:
|
---|
67 | JSString(JSGlobalData* globalData, const UString& value)
|
---|
68 | : JSCell(globalData->stringStructure.get())
|
---|
69 | , m_value(value)
|
---|
70 | {
|
---|
71 | Heap::heap(this)->reportExtraMemoryCost(value.cost());
|
---|
72 | }
|
---|
73 |
|
---|
74 | enum HasOtherOwnerType { HasOtherOwner };
|
---|
75 | JSString(JSGlobalData* globalData, const UString& value, HasOtherOwnerType)
|
---|
76 | : JSCell(globalData->stringStructure.get())
|
---|
77 | , m_value(value)
|
---|
78 | {
|
---|
79 | }
|
---|
80 | JSString(JSGlobalData* globalData, PassRefPtr<UString::Rep> value, HasOtherOwnerType)
|
---|
81 | : JSCell(globalData->stringStructure.get())
|
---|
82 | , m_value(value)
|
---|
83 | {
|
---|
84 | }
|
---|
85 |
|
---|
86 | const UString& value() const { return m_value; }
|
---|
87 |
|
---|
88 | bool getStringPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
89 | bool getStringPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
90 | bool getStringPropertyDescriptor(ExecState*, const Identifier& propertyName, PropertyDescriptor&);
|
---|
91 |
|
---|
92 | bool canGetIndex(unsigned i) { return i < static_cast<unsigned>(m_value.size()); }
|
---|
93 | JSString* getIndex(JSGlobalData*, unsigned);
|
---|
94 |
|
---|
95 | static PassRefPtr<Structure> createStructure(JSValue proto) { return Structure::create(proto, TypeInfo(StringType, OverridesGetOwnPropertySlot | NeedsThisConversion)); }
|
---|
96 |
|
---|
97 | private:
|
---|
98 | enum VPtrStealingHackType { VPtrStealingHack };
|
---|
99 | JSString(VPtrStealingHackType)
|
---|
100 | : JSCell(0)
|
---|
101 | {
|
---|
102 | }
|
---|
103 |
|
---|
104 | virtual JSValue toPrimitive(ExecState*, PreferredPrimitiveType) const;
|
---|
105 | virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue& value);
|
---|
106 | virtual bool toBoolean(ExecState*) const;
|
---|
107 | virtual double toNumber(ExecState*) const;
|
---|
108 | virtual JSObject* toObject(ExecState*) const;
|
---|
109 | virtual UString toString(ExecState*) const;
|
---|
110 |
|
---|
111 | virtual JSObject* toThisObject(ExecState*) const;
|
---|
112 | virtual UString toThisString(ExecState*) const;
|
---|
113 | virtual JSString* toThisJSString(ExecState*);
|
---|
114 |
|
---|
115 | // Actually getPropertySlot, not getOwnPropertySlot (see JSCell).
|
---|
116 | virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
117 | virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
118 | virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
|
---|
119 |
|
---|
120 | UString m_value;
|
---|
121 | };
|
---|
122 |
|
---|
123 | JSString* asString(JSValue);
|
---|
124 |
|
---|
125 | inline JSString* asString(JSValue value)
|
---|
126 | {
|
---|
127 | ASSERT(asCell(value)->isString());
|
---|
128 | return static_cast<JSString*>(asCell(value));
|
---|
129 | }
|
---|
130 |
|
---|
131 | inline JSString* jsEmptyString(JSGlobalData* globalData)
|
---|
132 | {
|
---|
133 | return globalData->smallStrings.emptyString(globalData);
|
---|
134 | }
|
---|
135 |
|
---|
136 | inline JSString* jsSingleCharacterString(JSGlobalData* globalData, UChar c)
|
---|
137 | {
|
---|
138 | if (c <= 0xFF)
|
---|
139 | return globalData->smallStrings.singleCharacterString(globalData, c);
|
---|
140 | return new (globalData) JSString(globalData, UString(&c, 1));
|
---|
141 | }
|
---|
142 |
|
---|
143 | inline JSString* jsSingleCharacterSubstring(JSGlobalData* globalData, const UString& s, unsigned offset)
|
---|
144 | {
|
---|
145 | ASSERT(offset < static_cast<unsigned>(s.size()));
|
---|
146 | UChar c = s.data()[offset];
|
---|
147 | if (c <= 0xFF)
|
---|
148 | return globalData->smallStrings.singleCharacterString(globalData, c);
|
---|
149 | return new (globalData) JSString(globalData, UString::Rep::create(s.rep(), offset, 1));
|
---|
150 | }
|
---|
151 |
|
---|
152 | inline JSString* jsNontrivialString(JSGlobalData* globalData, const char* s)
|
---|
153 | {
|
---|
154 | ASSERT(s);
|
---|
155 | ASSERT(s[0]);
|
---|
156 | ASSERT(s[1]);
|
---|
157 | return new (globalData) JSString(globalData, s);
|
---|
158 | }
|
---|
159 |
|
---|
160 | inline JSString* jsNontrivialString(JSGlobalData* globalData, const UString& s)
|
---|
161 | {
|
---|
162 | ASSERT(s.size() > 1);
|
---|
163 | return new (globalData) JSString(globalData, s);
|
---|
164 | }
|
---|
165 |
|
---|
166 | inline JSString* JSString::getIndex(JSGlobalData* globalData, unsigned i)
|
---|
167 | {
|
---|
168 | ASSERT(canGetIndex(i));
|
---|
169 | return jsSingleCharacterSubstring(globalData, m_value, i);
|
---|
170 | }
|
---|
171 |
|
---|
172 | inline JSString* jsString(JSGlobalData* globalData, const UString& s)
|
---|
173 | {
|
---|
174 | int size = s.size();
|
---|
175 | if (!size)
|
---|
176 | return globalData->smallStrings.emptyString(globalData);
|
---|
177 | if (size == 1) {
|
---|
178 | UChar c = s.data()[0];
|
---|
179 | if (c <= 0xFF)
|
---|
180 | return globalData->smallStrings.singleCharacterString(globalData, c);
|
---|
181 | }
|
---|
182 | return new (globalData) JSString(globalData, s);
|
---|
183 | }
|
---|
184 |
|
---|
185 | inline JSString* jsSubstring(JSGlobalData* globalData, const UString& s, unsigned offset, unsigned length)
|
---|
186 | {
|
---|
187 | ASSERT(offset <= static_cast<unsigned>(s.size()));
|
---|
188 | ASSERT(length <= static_cast<unsigned>(s.size()));
|
---|
189 | ASSERT(offset + length <= static_cast<unsigned>(s.size()));
|
---|
190 | if (!length)
|
---|
191 | return globalData->smallStrings.emptyString(globalData);
|
---|
192 | if (length == 1) {
|
---|
193 | UChar c = s.data()[offset];
|
---|
194 | if (c <= 0xFF)
|
---|
195 | return globalData->smallStrings.singleCharacterString(globalData, c);
|
---|
196 | }
|
---|
197 | return new (globalData) JSString(globalData, UString::Rep::create(s.rep(), offset, length));
|
---|
198 | }
|
---|
199 |
|
---|
200 | inline JSString* jsOwnedString(JSGlobalData* globalData, const UString& s)
|
---|
201 | {
|
---|
202 | int size = s.size();
|
---|
203 | if (!size)
|
---|
204 | return globalData->smallStrings.emptyString(globalData);
|
---|
205 | if (size == 1) {
|
---|
206 | UChar c = s.data()[0];
|
---|
207 | if (c <= 0xFF)
|
---|
208 | return globalData->smallStrings.singleCharacterString(globalData, c);
|
---|
209 | }
|
---|
210 | return new (globalData) JSString(globalData, s, JSString::HasOtherOwner);
|
---|
211 | }
|
---|
212 |
|
---|
213 | inline JSString* jsEmptyString(ExecState* exec) { return jsEmptyString(&exec->globalData()); }
|
---|
214 | inline JSString* jsString(ExecState* exec, const UString& s) { return jsString(&exec->globalData(), s); }
|
---|
215 | inline JSString* jsSingleCharacterString(ExecState* exec, UChar c) { return jsSingleCharacterString(&exec->globalData(), c); }
|
---|
216 | inline JSString* jsSingleCharacterSubstring(ExecState* exec, const UString& s, unsigned offset) { return jsSingleCharacterSubstring(&exec->globalData(), s, offset); }
|
---|
217 | inline JSString* jsSubstring(ExecState* exec, const UString& s, unsigned offset, unsigned length) { return jsSubstring(&exec->globalData(), s, offset, length); }
|
---|
218 | inline JSString* jsNontrivialString(ExecState* exec, const UString& s) { return jsNontrivialString(&exec->globalData(), s); }
|
---|
219 | inline JSString* jsNontrivialString(ExecState* exec, const char* s) { return jsNontrivialString(&exec->globalData(), s); }
|
---|
220 | inline JSString* jsOwnedString(ExecState* exec, const UString& s) { return jsOwnedString(&exec->globalData(), s); }
|
---|
221 |
|
---|
222 | ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
223 | {
|
---|
224 | if (propertyName == exec->propertyNames().length) {
|
---|
225 | slot.setValue(jsNumber(exec, m_value.size()));
|
---|
226 | return true;
|
---|
227 | }
|
---|
228 |
|
---|
229 | bool isStrictUInt32;
|
---|
230 | unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
|
---|
231 | if (isStrictUInt32 && i < static_cast<unsigned>(m_value.size())) {
|
---|
232 | slot.setValue(jsSingleCharacterSubstring(exec, m_value, i));
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 |
|
---|
236 | return false;
|
---|
237 | }
|
---|
238 |
|
---|
239 | ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
|
---|
240 | {
|
---|
241 | if (propertyName < static_cast<unsigned>(m_value.size())) {
|
---|
242 | slot.setValue(jsSingleCharacterSubstring(exec, m_value, propertyName));
|
---|
243 | return true;
|
---|
244 | }
|
---|
245 |
|
---|
246 | return false;
|
---|
247 | }
|
---|
248 |
|
---|
249 | inline bool isJSString(JSGlobalData* globalData, JSValue v) { return v.isCell() && v.asCell()->vptr() == globalData->jsStringVPtr; }
|
---|
250 |
|
---|
251 | // --- JSValue inlines ----------------------------
|
---|
252 |
|
---|
253 | inline JSString* JSValue::toThisJSString(ExecState* exec)
|
---|
254 | {
|
---|
255 | return isCell() ? asCell()->toThisJSString(exec) : jsString(exec, toString(exec));
|
---|
256 | }
|
---|
257 |
|
---|
258 | inline UString JSValue::toString(ExecState* exec) const
|
---|
259 | {
|
---|
260 | if (isString())
|
---|
261 | return static_cast<JSString*>(asCell())->value();
|
---|
262 | if (isInt32())
|
---|
263 | return exec->globalData().numericStrings.add(asInt32());
|
---|
264 | if (isDouble())
|
---|
265 | return exec->globalData().numericStrings.add(asDouble());
|
---|
266 | if (isTrue())
|
---|
267 | return "true";
|
---|
268 | if (isFalse())
|
---|
269 | return "false";
|
---|
270 | if (isNull())
|
---|
271 | return "null";
|
---|
272 | if (isUndefined())
|
---|
273 | return "undefined";
|
---|
274 | ASSERT(isCell());
|
---|
275 | return asCell()->toString(exec);
|
---|
276 | }
|
---|
277 |
|
---|
278 | } // namespace JSC
|
---|
279 |
|
---|
280 | #endif // JSString_h
|
---|