Changeset 35027 in webkit for trunk/JavaScriptCore/kjs/JSString.h
- Timestamp:
- Jul 6, 2008, 7:49:29 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/JSString.h
r34964 r35027 33 33 namespace KJS { 34 34 35 class JSString : public JSCell { 36 public: 37 JSString(const UString& value) : m_value(value) { Heap::heap(this)->reportExtraMemoryCost(value.cost()); } 38 enum HasOtherOwnerType { HasOtherOwner }; 39 JSString(const UString& value, HasOtherOwnerType) : m_value(value) { } 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 } 40 42 41 const UString& value() const { return m_value; } 43 enum HasOtherOwnerType { HasOtherOwner }; 44 JSString(const UString& value, HasOtherOwnerType) 45 : m_value(value) 46 { 47 } 42 48 43 bool getStringPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&); 44 bool getStringPropertySlot(unsigned propertyName, PropertySlot&); 49 const UString& value() const { return m_value; } 45 50 46 bool canGetIndex(unsigned i) { return i < static_cast<unsigned>(m_value.size()); } 47 JSValue* getIndex(ExecState* exec, unsigned i) 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) 48 95 { 49 ASSERT(canGetIndex(i)); 50 return new (exec) JSString(m_value.substr(i, 1)); 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; 51 119 } 52 120 53 private: 54 virtual JSType type() const { return StringType; } 121 // --- JSValue inlines ---------------------------- 55 122 56 virtual JSValue* toPrimitive(ExecState*, JSType preferred = UnspecifiedType) const; 57 virtual bool getPrimitiveNumber(ExecState*, double& number, JSValue*& value); 58 virtual bool toBoolean(ExecState*) const; 59 virtual double toNumber(ExecState*) const; 60 virtual JSObject* toObject(ExecState*) const; 61 virtual UString toString(ExecState*) const; 62 63 virtual JSObject* toThisObject(ExecState*) const; 64 virtual UString toThisString(ExecState*) const; 65 virtual JSString* toThisJSString(ExecState*); 66 67 // Actually getPropertySlot, not getOwnPropertySlot (see JSCell). 68 virtual bool getOwnPropertySlot(ExecState*, const Identifier& propertyName, PropertySlot&); 69 virtual bool getOwnPropertySlot(ExecState*, unsigned propertyName, PropertySlot&); 70 71 static JSValue* lengthGetter(ExecState*, const Identifier&, const PropertySlot&); 72 static JSValue* indexGetter(ExecState*, const Identifier&, const PropertySlot&); 73 static JSValue* indexNumericPropertyGetter(ExecState*, unsigned, const PropertySlot&); 74 75 UString m_value; 76 }; 77 78 JSString* jsString(ExecState*, const UString&); // returns empty string if passed null string 79 JSString* jsString(ExecState*, const char* = ""); // returns empty string if passed 0 80 81 // Should be used for strings that are owned by an object that will 82 // likely outlive the JSValue this makes, such as the parse tree or a 83 // DOM object that contains a UString 84 JSString* jsOwnedString(ExecState*, const UString&); 85 86 ALWAYS_INLINE bool JSString::getStringPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) 87 { 88 if (propertyName == exec->propertyNames().length) { 89 slot.setCustom(this, lengthGetter); 90 return true; 123 inline JSString* JSValue::toThisJSString(ExecState* exec) 124 { 125 return JSImmediate::isImmediate(this) ? jsString(exec, JSImmediate::toString(this)) : asCell()->toThisJSString(exec); 91 126 } 92 93 bool isStrictUInt32;94 unsigned i = propertyName.toStrictUInt32(&isStrictUInt32);95 if (isStrictUInt32 && i < static_cast<unsigned>(m_value.size())) {96 slot.setCustomIndex(this, i, indexGetter);97 return true;98 }99 100 return false;101 }102 103 ALWAYS_INLINE bool JSString::getStringPropertySlot(unsigned propertyName, PropertySlot& slot)104 {105 if (propertyName < static_cast<unsigned>(m_value.size())) {106 slot.setCustomNumeric(this, indexNumericPropertyGetter);107 return true;108 }109 110 return false;111 }112 113 // --- JSValue inlines ----------------------------114 115 inline JSString* JSValue::toThisJSString(ExecState* exec)116 {117 return JSImmediate::isImmediate(this) ? jsString(exec, JSImmediate::toString(this)) : asCell()->toThisJSString(exec);118 }119 127 120 128 } // namespace KJS
Note:
See TracChangeset
for help on using the changeset viewer.