1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * Copyright (C) 1999-2001 Harri Porten ([email protected])
|
---|
4 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
5 | * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Library General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Library General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Library General Public License
|
---|
18 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
19 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
20 | * Boston, MA 02110-1301, USA.
|
---|
21 | *
|
---|
22 | */
|
---|
23 |
|
---|
24 | #ifndef JSString_h
|
---|
25 | #define JSString_h
|
---|
26 |
|
---|
27 | #include "CommonIdentifiers.h"
|
---|
28 | #include "JSCell.h"
|
---|
29 | #include "PropertySlot.h"
|
---|
30 | #include "identifier.h"
|
---|
31 | #include "ustring.h"
|
---|
32 |
|
---|
33 | namespace KJS {
|
---|
34 |
|
---|
35 | class JSString : public JSCell {
|
---|
36 | public:
|
---|
37 | JSString(const UString& value)
|
---|
38 | : m_value(value)
|
---|
39 | {
|
---|
40 | Heap::heap(this)->reportExtraMemoryCost(value.cost());
|
---|
41 | }
|
---|
42 |
|
---|
43 | enum HasOtherOwnerType { HasOtherOwner };
|
---|
44 | JSString(const UString& value, HasOtherOwnerType)
|
---|
45 | : m_value(value)
|
---|
46 | {
|
---|
47 | }
|
---|
48 |
|
---|
49 | const UString& value() const { return m_value; }
|
---|
50 |
|
---|
51 | bool getStringPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
52 | bool getStringPropertySlot(unsigned propertyName, PropertySlot&);
|
---|
53 |
|
---|
54 | bool canGetIndex(unsigned i) { return i < static_cast<unsigned>(m_value.size()); }
|
---|
55 | JSValue* getIndex(ExecState* exec, unsigned i)
|
---|
56 | {
|
---|
57 | ASSERT(canGetIndex(i));
|
---|
58 | return new (exec) JSString(m_value.substr(i, 1));
|
---|
59 | }
|
---|
60 |
|
---|
61 | private:
|
---|
62 | virtual JSType type() const { return StringType; }
|
---|
63 |
|
---|
64 | virtual JSValue* toPrimitive(ExecState*, JSType preferred = UnspecifiedType) const;
|
---|
65 | virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value);
|
---|
66 | virtual bool toBoolean(ExecState*) const;
|
---|
67 | virtual double toNumber(ExecState*) const;
|
---|
68 | virtual JSObject* toObject(ExecState*) const;
|
---|
69 | virtual UString toString(ExecState*) const;
|
---|
70 |
|
---|
71 | virtual JSObject* toThisObject(ExecState*) const;
|
---|
72 | virtual UString toThisString(ExecState*) const;
|
---|
73 | virtual JSString* toThisJSString(ExecState*);
|
---|
74 |
|
---|
75 | // Actually getPropertySlot, not getOwnPropertySlot (see JSCell).
|
---|
76 | virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&);
|
---|
77 | virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&);
|
---|
78 |
|
---|
79 | static JSValue* lengthGetter(ExecState*, const Identifier&, const PropertySlot&);
|
---|
80 | static JSValue* indexGetter(ExecState*, const Identifier&, const PropertySlot&);
|
---|
81 | static JSValue* indexNumericPropertyGetter(ExecState*, unsigned, const PropertySlot&);
|
---|
82 |
|
---|
83 | UString m_value;
|
---|
84 | };
|
---|
85 |
|
---|
86 | JSString* jsString(ExecState*, const UString&); // returns empty string if passed null string
|
---|
87 | JSString* jsString(ExecState*, const char* = ""); // returns empty string if passed 0
|
---|
88 |
|
---|
89 | // Should be used for strings that are owned by an object that will
|
---|
90 | // likely outlive the JSValue this makes, such as the parse tree or a
|
---|
91 | // DOM object that contains a UString
|
---|
92 | JSString* jsOwnedString(ExecState*, const UString&);
|
---|
93 |
|
---|
94 | ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
95 | {
|
---|
96 | if (propertyName == exec->propertyNames().length) {
|
---|
97 | slot.setCustom(this, lengthGetter);
|
---|
98 | return true;
|
---|
99 | }
|
---|
100 |
|
---|
101 | bool isStrictUInt32;
|
---|
102 | unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);
|
---|
103 | if (isStrictUInt32 && i < static_cast<unsigned>(m_value.size())) {
|
---|
104 | slot.setCustomIndex(this, i, indexGetter);
|
---|
105 | return true;
|
---|
106 | }
|
---|
107 |
|
---|
108 | return false;
|
---|
109 | }
|
---|
110 |
|
---|
111 | ALWAYS_INLINE bool JSString::getStringPropertySlot(unsigned propertyName, PropertySlot& slot)
|
---|
112 | {
|
---|
113 | if (propertyName < static_cast<unsigned>(m_value.size())) {
|
---|
114 | slot.setCustomNumeric(this, indexNumericPropertyGetter);
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return false;
|
---|
119 | }
|
---|
120 |
|
---|
121 | // --- JSValue inlines ----------------------------
|
---|
122 |
|
---|
123 | inline JSString* JSValue::toThisJSString(ExecState* exec)
|
---|
124 | {
|
---|
125 | return JSImmediate::isImmediate(this) ? jsString(exec, JSImmediate::toString(this)) : asCell()->toThisJSString(exec);
|
---|
126 | }
|
---|
127 |
|
---|
128 | } // namespace KJS
|
---|
129 |
|
---|
130 | #endif // JSString_h
|
---|