source: webkit/trunk/JavaScriptCore/runtime/JSString.h@ 37981

Last change on this file since 37981 was 37938, checked in by [email protected], 17 years ago

2008-10-28 Cameron Zwarich <[email protected]>

Rubber-stamped by Sam Weinig.

Create a runtime directory in JavaScriptCore and begin moving files to
it. This is the first step towards removing the kjs directory and
placing files in more meaningful subdirectories of JavaScriptCore.

  • API/JSBase.cpp:
  • API/JSCallbackConstructor.cpp:
  • API/JSCallbackConstructor.h:
  • API/JSCallbackFunction.cpp:
  • API/JSClassRef.cpp:
  • API/JSClassRef.h:
  • API/JSStringRefCF.cpp:
  • API/JSValueRef.cpp:
  • API/OpaqueJSString.cpp:
  • DerivedSources.make:
  • GNUmakefile.am:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.vcproj:
  • JavaScriptCore.xcodeproj/project.pbxproj:
  • kjs/AllInOneFile.cpp:
  • kjs/ArrayConstructor.cpp: Removed.
  • kjs/ArrayConstructor.h: Removed.
  • kjs/ArrayPrototype.cpp: Removed.
  • kjs/ArrayPrototype.h: Removed.
  • kjs/BooleanConstructor.cpp: Removed.
  • kjs/BooleanConstructor.h: Removed.
  • kjs/BooleanObject.cpp: Removed.
  • kjs/BooleanObject.h: Removed.
  • kjs/BooleanPrototype.cpp: Removed.
  • kjs/BooleanPrototype.h: Removed.
  • kjs/CallData.cpp: Removed.
  • kjs/CallData.h: Removed.
  • kjs/ClassInfo.h: Removed.
  • kjs/ConstructData.cpp: Removed.
  • kjs/ConstructData.h: Removed.
  • kjs/DateConstructor.cpp: Removed.
  • kjs/DateConstructor.h: Removed.
  • kjs/DateInstance.cpp: Removed.
  • kjs/DateInstance.h: Removed.
  • kjs/DateMath.cpp: Removed.
  • kjs/DateMath.h: Removed.
  • kjs/DatePrototype.cpp: Removed.
  • kjs/DatePrototype.h: Removed.
  • kjs/Error.cpp: Removed.
  • kjs/Error.h: Removed.
  • kjs/ErrorConstructor.cpp: Removed.
  • kjs/ErrorConstructor.h: Removed.
  • kjs/ErrorInstance.cpp: Removed.
  • kjs/ErrorInstance.h: Removed.
  • kjs/ErrorPrototype.cpp: Removed.
  • kjs/ErrorPrototype.h: Removed.
  • kjs/FunctionConstructor.cpp: Removed.
  • kjs/FunctionConstructor.h: Removed.
  • kjs/FunctionPrototype.cpp: Removed.
  • kjs/FunctionPrototype.h: Removed.
  • kjs/GlobalEvalFunction.cpp: Removed.
  • kjs/GlobalEvalFunction.h: Removed.
  • kjs/InternalFunction.cpp: Removed.
  • kjs/InternalFunction.h: Removed.
  • kjs/JSArray.cpp: Removed.
  • kjs/JSArray.h: Removed.
  • kjs/JSCell.cpp: Removed.
  • kjs/JSCell.h: Removed.
  • kjs/JSFunction.cpp: Removed.
  • kjs/JSFunction.h: Removed.
  • kjs/JSGlobalObject.cpp: Removed.
  • kjs/JSGlobalObject.h: Removed.
  • kjs/JSGlobalObjectFunctions.cpp: Removed.
  • kjs/JSGlobalObjectFunctions.h: Removed.
  • kjs/JSImmediate.cpp: Removed.
  • kjs/JSImmediate.h: Removed.
  • kjs/JSNotAnObject.cpp: Removed.
  • kjs/JSNotAnObject.h: Removed.
  • kjs/JSNumberCell.cpp: Removed.
  • kjs/JSNumberCell.h: Removed.
  • kjs/JSObject.cpp: Removed.
  • kjs/JSObject.h: Removed.
  • kjs/JSString.cpp: Removed.
  • kjs/JSString.h: Removed.
  • kjs/JSValue.cpp: Removed.
  • kjs/JSValue.h: Removed.
  • kjs/JSVariableObject.cpp: Removed.
  • kjs/JSVariableObject.h: Removed.
  • kjs/JSWrapperObject.cpp: Removed.
  • kjs/JSWrapperObject.h: Removed.
  • kjs/MathObject.cpp: Removed.
  • kjs/MathObject.h: Removed.
  • kjs/NativeErrorConstructor.cpp: Removed.
  • kjs/NativeErrorConstructor.h: Removed.
  • kjs/NativeErrorPrototype.cpp: Removed.
  • kjs/NativeErrorPrototype.h: Removed.
  • kjs/NumberConstructor.cpp: Removed.
  • kjs/NumberConstructor.h: Removed.
  • kjs/NumberObject.cpp: Removed.
  • kjs/NumberObject.h: Removed.
  • kjs/NumberPrototype.cpp: Removed.
  • kjs/NumberPrototype.h: Removed.
  • kjs/ObjectConstructor.cpp: Removed.
  • kjs/ObjectConstructor.h: Removed.
  • kjs/ObjectPrototype.cpp: Removed.
  • kjs/ObjectPrototype.h: Removed.
  • kjs/PropertyMap.cpp: Removed.
  • kjs/PropertyMap.h: Removed.
  • kjs/PropertySlot.cpp: Removed.
  • kjs/PropertySlot.h: Removed.
  • kjs/PrototypeFunction.cpp: Removed.
  • kjs/PrototypeFunction.h: Removed.
  • kjs/PutPropertySlot.h: Removed.
  • kjs/SmallStrings.cpp: Removed.
  • kjs/SmallStrings.h: Removed.
  • kjs/StringConstructor.cpp: Removed.
  • kjs/StringConstructor.h: Removed.
  • kjs/StringObject.cpp: Removed.
  • kjs/StringObject.h: Removed.
  • kjs/StringObjectThatMasqueradesAsUndefined.h: Removed.
  • kjs/StringPrototype.cpp: Removed.
  • kjs/StringPrototype.h: Removed.
  • kjs/StructureID.cpp: Removed.
  • kjs/StructureID.h: Removed.
  • kjs/completion.h:
  • kjs/interpreter.h:
  • runtime: Added.
  • runtime/ArrayConstructor.cpp: Copied from kjs/ArrayConstructor.cpp.
  • runtime/ArrayConstructor.h: Copied from kjs/ArrayConstructor.h.
  • runtime/ArrayPrototype.cpp: Copied from kjs/ArrayPrototype.cpp.
  • runtime/ArrayPrototype.h: Copied from kjs/ArrayPrototype.h.
  • runtime/BooleanConstructor.cpp: Copied from kjs/BooleanConstructor.cpp.
  • runtime/BooleanConstructor.h: Copied from kjs/BooleanConstructor.h.
  • runtime/BooleanObject.cpp: Copied from kjs/BooleanObject.cpp.
  • runtime/BooleanObject.h: Copied from kjs/BooleanObject.h.
  • runtime/BooleanPrototype.cpp: Copied from kjs/BooleanPrototype.cpp.
  • runtime/BooleanPrototype.h: Copied from kjs/BooleanPrototype.h.
  • runtime/CallData.cpp: Copied from kjs/CallData.cpp.
  • runtime/CallData.h: Copied from kjs/CallData.h.
  • runtime/ClassInfo.h: Copied from kjs/ClassInfo.h.
  • runtime/ConstructData.cpp: Copied from kjs/ConstructData.cpp.
  • runtime/ConstructData.h: Copied from kjs/ConstructData.h.
  • runtime/DateConstructor.cpp: Copied from kjs/DateConstructor.cpp.
  • runtime/DateConstructor.h: Copied from kjs/DateConstructor.h.
  • runtime/DateInstance.cpp: Copied from kjs/DateInstance.cpp.
  • runtime/DateInstance.h: Copied from kjs/DateInstance.h.
  • runtime/DateMath.cpp: Copied from kjs/DateMath.cpp.
  • runtime/DateMath.h: Copied from kjs/DateMath.h.
  • runtime/DatePrototype.cpp: Copied from kjs/DatePrototype.cpp.
  • runtime/DatePrototype.h: Copied from kjs/DatePrototype.h.
  • runtime/Error.cpp: Copied from kjs/Error.cpp.
  • runtime/Error.h: Copied from kjs/Error.h.
  • runtime/ErrorConstructor.cpp: Copied from kjs/ErrorConstructor.cpp.
  • runtime/ErrorConstructor.h: Copied from kjs/ErrorConstructor.h.
  • runtime/ErrorInstance.cpp: Copied from kjs/ErrorInstance.cpp.
  • runtime/ErrorInstance.h: Copied from kjs/ErrorInstance.h.
  • runtime/ErrorPrototype.cpp: Copied from kjs/ErrorPrototype.cpp.
  • runtime/ErrorPrototype.h: Copied from kjs/ErrorPrototype.h.
  • runtime/FunctionConstructor.cpp: Copied from kjs/FunctionConstructor.cpp.
  • runtime/FunctionConstructor.h: Copied from kjs/FunctionConstructor.h.
  • runtime/FunctionPrototype.cpp: Copied from kjs/FunctionPrototype.cpp.
  • runtime/FunctionPrototype.h: Copied from kjs/FunctionPrototype.h.
  • runtime/GlobalEvalFunction.cpp: Copied from kjs/GlobalEvalFunction.cpp.
  • runtime/GlobalEvalFunction.h: Copied from kjs/GlobalEvalFunction.h.
  • runtime/InternalFunction.cpp: Copied from kjs/InternalFunction.cpp.
  • runtime/InternalFunction.h: Copied from kjs/InternalFunction.h.
  • runtime/JSArray.cpp: Copied from kjs/JSArray.cpp.
  • runtime/JSArray.h: Copied from kjs/JSArray.h.
  • runtime/JSCell.cpp: Copied from kjs/JSCell.cpp.
  • runtime/JSCell.h: Copied from kjs/JSCell.h.
  • runtime/JSFunction.cpp: Copied from kjs/JSFunction.cpp.
  • runtime/JSFunction.h: Copied from kjs/JSFunction.h.
  • runtime/JSGlobalObject.cpp: Copied from kjs/JSGlobalObject.cpp.
  • runtime/JSGlobalObject.h: Copied from kjs/JSGlobalObject.h.
  • runtime/JSGlobalObjectFunctions.cpp: Copied from kjs/JSGlobalObjectFunctions.cpp.
  • runtime/JSGlobalObjectFunctions.h: Copied from kjs/JSGlobalObjectFunctions.h.
  • runtime/JSImmediate.cpp: Copied from kjs/JSImmediate.cpp.
  • runtime/JSImmediate.h: Copied from kjs/JSImmediate.h.
  • runtime/JSNotAnObject.cpp: Copied from kjs/JSNotAnObject.cpp.
  • runtime/JSNotAnObject.h: Copied from kjs/JSNotAnObject.h.
  • runtime/JSNumberCell.cpp: Copied from kjs/JSNumberCell.cpp.
  • runtime/JSNumberCell.h: Copied from kjs/JSNumberCell.h.
  • runtime/JSObject.cpp: Copied from kjs/JSObject.cpp.
  • runtime/JSObject.h: Copied from kjs/JSObject.h.
  • runtime/JSString.cpp: Copied from kjs/JSString.cpp.
  • runtime/JSString.h: Copied from kjs/JSString.h.
  • runtime/JSValue.cpp: Copied from kjs/JSValue.cpp.
  • runtime/JSValue.h: Copied from kjs/JSValue.h.
  • runtime/JSVariableObject.cpp: Copied from kjs/JSVariableObject.cpp.
  • runtime/JSVariableObject.h: Copied from kjs/JSVariableObject.h.
  • runtime/JSWrapperObject.cpp: Copied from kjs/JSWrapperObject.cpp.
  • runtime/JSWrapperObject.h: Copied from kjs/JSWrapperObject.h.
  • runtime/MathObject.cpp: Copied from kjs/MathObject.cpp.
  • runtime/MathObject.h: Copied from kjs/MathObject.h.
  • runtime/NativeErrorConstructor.cpp: Copied from kjs/NativeErrorConstructor.cpp.
  • runtime/NativeErrorConstructor.h: Copied from kjs/NativeErrorConstructor.h.
  • runtime/NativeErrorPrototype.cpp: Copied from kjs/NativeErrorPrototype.cpp.
  • runtime/NativeErrorPrototype.h: Copied from kjs/NativeErrorPrototype.h.
  • runtime/NumberConstructor.cpp: Copied from kjs/NumberConstructor.cpp.
  • runtime/NumberConstructor.h: Copied from kjs/NumberConstructor.h.
  • runtime/NumberObject.cpp: Copied from kjs/NumberObject.cpp.
  • runtime/NumberObject.h: Copied from kjs/NumberObject.h.
  • runtime/NumberPrototype.cpp: Copied from kjs/NumberPrototype.cpp.
  • runtime/NumberPrototype.h: Copied from kjs/NumberPrototype.h.
  • runtime/ObjectConstructor.cpp: Copied from kjs/ObjectConstructor.cpp.
  • runtime/ObjectConstructor.h: Copied from kjs/ObjectConstructor.h.
  • runtime/ObjectPrototype.cpp: Copied from kjs/ObjectPrototype.cpp.
  • runtime/ObjectPrototype.h: Copied from kjs/ObjectPrototype.h.
  • runtime/PropertyMap.cpp: Copied from kjs/PropertyMap.cpp.
  • runtime/PropertyMap.h: Copied from kjs/PropertyMap.h.
  • runtime/PropertySlot.cpp: Copied from kjs/PropertySlot.cpp.
  • runtime/PropertySlot.h: Copied from kjs/PropertySlot.h.
  • runtime/PrototypeFunction.cpp: Copied from kjs/PrototypeFunction.cpp.
  • runtime/PrototypeFunction.h: Copied from kjs/PrototypeFunction.h.
  • runtime/PutPropertySlot.h: Copied from kjs/PutPropertySlot.h.
  • runtime/SmallStrings.cpp: Copied from kjs/SmallStrings.cpp.
  • runtime/SmallStrings.h: Copied from kjs/SmallStrings.h.
  • runtime/StringConstructor.cpp: Copied from kjs/StringConstructor.cpp.
  • runtime/StringConstructor.h: Copied from kjs/StringConstructor.h.
  • runtime/StringObject.cpp: Copied from kjs/StringObject.cpp.
  • runtime/StringObject.h: Copied from kjs/StringObject.h.
  • runtime/StringObjectThatMasqueradesAsUndefined.h: Copied from kjs/StringObjectThatMasqueradesAsUndefined.h.
  • runtime/StringPrototype.cpp: Copied from kjs/StringPrototype.cpp.
  • runtime/StringPrototype.h: Copied from kjs/StringPrototype.h.
  • runtime/StructureID.cpp: Copied from kjs/StructureID.cpp.
  • runtime/StructureID.h: Copied from kjs/StructureID.h.
  • Property svn:eol-style set to native
File size: 8.7 KB
Line 
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 "CommonIdentifiers.h"
27#include "ExecState.h"
28#include "JSNumberCell.h"
29#include "PropertySlot.h"
30#include "identifier.h"
31
32namespace JSC {
33
34 class JSString;
35
36 JSString* jsEmptyString(JSGlobalData*);
37 JSString* jsEmptyString(ExecState*);
38 JSString* jsString(JSGlobalData*, const UString&); // returns empty string if passed null string
39 JSString* jsString(ExecState*, const UString&); // returns empty string if passed null string
40
41 JSString* jsSingleCharacterString(JSGlobalData*, UChar);
42 JSString* jsSingleCharacterString(ExecState*, UChar);
43 JSString* jsSingleCharacterSubstring(JSGlobalData*, const UString&, unsigned offset);
44 JSString* jsSingleCharacterSubstring(ExecState*, const UString&, unsigned offset);
45 JSString* jsSubstring(JSGlobalData*, const UString&, unsigned offset, unsigned length);
46 JSString* jsSubstring(ExecState*, const UString&, unsigned offset, unsigned length);
47
48 // Non-trivial strings are two or more characters long.
49 // These functions are faster than just calling jsString.
50 JSString* jsNontrivialString(JSGlobalData*, const UString&);
51 JSString* jsNontrivialString(ExecState*, const UString&);
52 JSString* jsNontrivialString(JSGlobalData*, const char*);
53 JSString* jsNontrivialString(ExecState*, const char*);
54
55 // Should be used for strings that are owned by an object that will
56 // likely outlive the JSValue this makes, such as the parse tree or a
57 // DOM object that contains a UString
58 JSString* jsOwnedString(JSGlobalData*, const UString&);
59 JSString* jsOwnedString(ExecState*, const UString&);
60
61 class JSString : public JSCell {
62 friend class CTI;
63 friend class Machine;
64
65 public:
66 JSString(JSGlobalData* globalData, const UString& value)
67 : JSCell(globalData->stringStructureID.get())
68 , m_value(value)
69 {
70 Heap::heap(this)->reportExtraMemoryCost(value.cost());
71 }
72
73 enum HasOtherOwnerType { HasOtherOwner };
74 JSString(JSGlobalData* globalData, const UString& value, HasOtherOwnerType)
75 : JSCell(globalData->stringStructureID.get())
76 , m_value(value)
77 {
78 }
79 JSString(JSGlobalData* globalData, PassRefPtr<UString::Rep> value, HasOtherOwnerType)
80 : JSCell(globalData->stringStructureID.get())
81 , m_value(value)
82 {
83 }
84
85 const UString& value() const { return m_value; }
86
87 bool getStringPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
88 bool getStringPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
89
90 bool canGetIndex(unsigned i) { return i < static_cast<unsigned>(m_value.size()); }
91 JSString* getIndex(JSGlobalData*, unsigned);
92
93 static PassRefPtr<StructureID> createStructureID(JSValue* proto) { return StructureID::create(proto, TypeInfo(StringType, NeedsThisConversion)); }
94
95 private:
96 enum VPtrStealingHackType { VPtrStealingHack };
97 JSString(VPtrStealingHackType)
98 : JSCell(0)
99 {
100 }
101
102 virtual JSValue* toPrimitive(ExecState*, PreferredPrimitiveType) const;
103 virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
104 virtual bool toBoolean(ExecState*) const;
105 virtual double toNumber(ExecState*) const;
106 virtual JSObject* toObject(ExecState*) const;
107 virtual UString toString(ExecState*) const;
108
109 virtual JSObject* toThisObject(ExecState*) const;
110 virtual UString toThisString(ExecState*) const;
111 virtual JSString* toThisJSString(ExecState*);
112
113 // Actually getPropertySlot, not getOwnPropertySlot (see JSCell).
114 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
115 virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
116
117 UString m_value;
118 };
119
120 JSString* asString(JSValue*);
121
122 inline JSString* asString(JSValue* value)
123 {
124 ASSERT(asCell(value)->isString());
125 return static_cast<JSString*>(asCell(value));
126 }
127
128 inline JSString* jsEmptyString(JSGlobalData* globalData)
129 {
130 return globalData->smallStrings.emptyString(globalData);
131 }
132
133 inline JSString* jsSingleCharacterString(JSGlobalData* globalData, UChar c)
134 {
135 if (c <= 0xFF)
136 return globalData->smallStrings.singleCharacterString(globalData, c);
137 return new (globalData) JSString(globalData, UString(&c, 1));
138 }
139
140 inline JSString* jsSingleCharacterSubstring(JSGlobalData* globalData, const UString& s, unsigned offset)
141 {
142 ASSERT(offset < static_cast<unsigned>(s.size()));
143 UChar c = s.data()[offset];
144 if (c <= 0xFF)
145 return globalData->smallStrings.singleCharacterString(globalData, c);
146 return new (globalData) JSString(globalData, UString::Rep::create(s.rep(), offset, 1));
147 }
148
149 inline JSString* jsNontrivialString(JSGlobalData* globalData, const char* s)
150 {
151 ASSERT(s);
152 ASSERT(s[0]);
153 ASSERT(s[1]);
154 return new (globalData) JSString(globalData, s);
155 }
156
157 inline JSString* jsNontrivialString(JSGlobalData* globalData, const UString& s)
158 {
159 ASSERT(s.size() > 1);
160 return new (globalData) JSString(globalData, s);
161 }
162
163 inline JSString* JSString::getIndex(JSGlobalData* globalData, unsigned i)
164 {
165 ASSERT(canGetIndex(i));
166 return jsSingleCharacterSubstring(globalData, m_value, i);
167 }
168
169 inline JSString* jsEmptyString(ExecState* exec) { return jsEmptyString(&exec->globalData()); }
170 inline JSString* jsString(ExecState* exec, const UString& s) { return jsString(&exec->globalData(), s); }
171 inline JSString* jsSingleCharacterString(ExecState* exec, UChar c) { return jsSingleCharacterString(&exec->globalData(), c); }
172 inline JSString* jsSingleCharacterSubstring(ExecState* exec, const UString& s, unsigned offset) { return jsSingleCharacterSubstring(&exec->globalData(), s, offset); }
173 inline JSString* jsSubstring(ExecState* exec, const UString& s, unsigned offset, unsigned length) { return jsSubstring(&exec->globalData(), s, offset, length); }
174 inline JSString* jsNontrivialString(ExecState* exec, const UString& s) { return jsNontrivialString(&exec->globalData(), s); }
175 inline JSString* jsNontrivialString(ExecState* exec, const char* s) { return jsNontrivialString(&exec->globalData(), s); }
176 inline JSString* jsOwnedString(ExecState* exec, const UString& s) { return jsOwnedString(&exec->globalData(), s); }
177
178 ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
179 {
180 if (propertyName == exec->propertyNames().length) {
181 slot.setValue(jsNumber(exec, m_value.size()));
182 return true;
183 }
184
185 bool isStrictUInt32;
186 unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
187 if (isStrictUInt32 && i < static_cast<unsigned>(m_value.size())) {
188 slot.setValue(jsSingleCharacterSubstring(exec, m_value, i));
189 return true;
190 }
191
192 return false;
193 }
194
195 ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
196 {
197 if (propertyName < static_cast<unsigned>(m_value.size())) {
198 slot.setValue(jsSingleCharacterSubstring(exec, m_value, propertyName));
199 return true;
200 }
201
202 return false;
203 }
204
205 // --- JSValue inlines ----------------------------
206
207 inline JSString* JSValue::toThisJSString(ExecState* exec)
208 {
209 return JSImmediate::isImmediate(asValue()) ? jsString(exec, JSImmediate::toString(asValue())) : asCell()->toThisJSString(exec);
210 }
211
212} // namespace JSC
213
214#endif // JSString_h
Note: See TracBrowser for help on using the repository browser.