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 "JSCell.h"
|
---|
29 | #include "JSNumberCell.h"
|
---|
30 | #include "PropertySlot.h"
|
---|
31 | #include "identifier.h"
|
---|
32 | #include "ustring.h"
|
---|
33 |
|
---|
34 | namespace KJS {
|
---|
35 |
|
---|
36 | class JSString : public JSCell {
|
---|
37 | public:
|
---|
38 | JSString(const UString& value)
|
---|
39 | : m_value(value)
|
---|
40 | {
|
---|
41 | Heap::heap(this)->reportExtraMemoryCost(value.cost());
|
---|
42 | }
|
---|
43 |
|
---|
44 | enum HasOtherOwnerType { HasOtherOwner };
|
---|
45 | JSString(const UString& value, HasOtherOwnerType)
|
---|
46 | : m_value(value)
|
---|
47 | {
|
---|
48 | }
|
---|
49 |
|
---|
50 | const UString& value() const { return m_value; }
|
---|
51 |
|
---|
52 | bool getStringPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
53 | bool getStringPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
54 |
|
---|
55 | bool canGetIndex(unsigned i) { return i < static_cast<unsigned>(m_value.size()); }
|
---|
56 | JSValue* getIndex(ExecState* exec, unsigned i)
|
---|
57 | {
|
---|
58 | ASSERT(canGetIndex(i));
|
---|
59 | return new (exec) JSString(m_value.substr(i, 1));
|
---|
60 | }
|
---|
61 |
|
---|
62 | private:
|
---|
63 | virtual bool isString() const;
|
---|
64 |
|
---|
65 | virtual JSValue* toPrimitive(ExecState*, PreferredPrimitiveType) const;
|
---|
66 | virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
|
---|
67 | virtual bool toBoolean(ExecState*) const;
|
---|
68 | virtual double toNumber(ExecState*) const;
|
---|
69 | virtual JSObject* toObject(ExecState*) const;
|
---|
70 | virtual UString toString(ExecState*) const;
|
---|
71 |
|
---|
72 | virtual JSObject* toThisObject(ExecState*) const;
|
---|
73 | virtual UString toThisString(ExecState*) const;
|
---|
74 | virtual JSString* toThisJSString(ExecState*);
|
---|
75 |
|
---|
76 | // Actually getPropertySlot, not getOwnPropertySlot (see JSCell).
|
---|
77 | virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
78 | virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
79 |
|
---|
80 | UString m_value;
|
---|
81 | };
|
---|
82 |
|
---|
83 | JSString* jsString(ExecState*, const UString&); // returns empty string if passed null string
|
---|
84 | JSString* jsString(ExecState*, const char* = ""); // returns empty string if passed 0
|
---|
85 |
|
---|
86 | // Should be used for strings that are owned by an object that will
|
---|
87 | // likely outlive the JSValue this makes, such as the parse tree or a
|
---|
88 | // DOM object that contains a UString
|
---|
89 | JSString* jsOwnedString(ExecState*, const UString&);
|
---|
90 |
|
---|
91 | ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
92 | {
|
---|
93 | if (propertyName == exec->propertyNames().length) {
|
---|
94 | slot.setValue(jsNumber(exec, value().size()));
|
---|
95 | return true;
|
---|
96 | }
|
---|
97 |
|
---|
98 | bool isStrictUInt32;
|
---|
99 | unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
|
---|
100 | if (isStrictUInt32 && i < static_cast<unsigned>(m_value.size())) {
|
---|
101 | slot.setValue(jsString(exec, value().substr(i, 1)));
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 |
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 |
|
---|
108 | ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, unsigned propertyName, PropertySlot& slot)
|
---|
109 | {
|
---|
110 | if (propertyName < static_cast<unsigned>(m_value.size())) {
|
---|
111 | slot.setValue(jsString(exec, value().substr(propertyName, 1)));
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 |
|
---|
115 | return false;
|
---|
116 | }
|
---|
117 |
|
---|
118 | // --- JSValue inlines ----------------------------
|
---|
119 |
|
---|
120 | inline JSString* JSValue::toThisJSString(ExecState* exec)
|
---|
121 | {
|
---|
122 | return JSImmediate::isImmediate(this) ? jsString(exec, JSImmediate::toString(this)) : asCell()->toThisJSString(exec);
|
---|
123 | }
|
---|
124 |
|
---|
125 | } // namespace KJS
|
---|
126 |
|
---|
127 | #endif // JSString_h
|
---|