| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 }; | 155 }; |
| 156 | 156 |
| 157 template <typename StringType> | 157 template <typename StringType> |
| 158 StringType v8StringToWebCoreString(v8::Handle<v8::String>, ExternalMode); | 158 StringType v8StringToWebCoreString(v8::Handle<v8::String>, ExternalMode); |
| 159 String int32ToWebCoreString(int value); | 159 String int32ToWebCoreString(int value); |
| 160 | 160 |
| 161 // V8StringResource is an adapter class that converts V8 values to Strings | 161 // V8StringResource is an adapter class that converts V8 values to Strings |
| 162 // or AtomicStrings as appropriate, using multiple typecast operators. | 162 // or AtomicStrings as appropriate, using multiple typecast operators. |
| 163 enum V8StringResourceMode { | 163 enum V8StringResourceMode { |
| 164 DefaultMode, | 164 DefaultMode, |
| 165 WithNullCheck, | 165 TreatNullAsEmptyString, |
| 166 WithUndefinedOrNullCheck | 166 TreatNullAsNullString, |
| 167 TreatNullAndUndefinedAsNullString |
| 167 }; | 168 }; |
| 168 | 169 |
| 169 template <V8StringResourceMode Mode = DefaultMode> | 170 template <V8StringResourceMode Mode = DefaultMode> |
| 170 class V8StringResource { | 171 class V8StringResource { |
| 171 public: | 172 public: |
| 172 V8StringResource() | 173 V8StringResource() |
| 173 : m_mode(Externalize) | 174 : m_mode(Externalize) |
| 174 { | 175 { |
| 175 } | 176 } |
| 176 | 177 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 194 { | 195 { |
| 195 setString(String()); | 196 setString(String()); |
| 196 } | 197 } |
| 197 | 198 |
| 198 bool prepare() | 199 bool prepare() |
| 199 { | 200 { |
| 200 if (m_v8Object.IsEmpty()) | 201 if (m_v8Object.IsEmpty()) |
| 201 return true; | 202 return true; |
| 202 | 203 |
| 203 if (!isValid()) { | 204 if (!isValid()) { |
| 204 setString(String()); | 205 setString(fallbackString()); |
| 205 return true; | 206 return true; |
| 206 } | 207 } |
| 207 | 208 |
| 208 if (LIKELY(m_v8Object->IsString())) | 209 if (LIKELY(m_v8Object->IsString())) |
| 209 return true; | 210 return true; |
| 210 | 211 |
| 211 if (LIKELY(m_v8Object->IsInt32())) { | 212 if (LIKELY(m_v8Object->IsInt32())) { |
| 212 setString(int32ToWebCoreString(m_v8Object->Int32Value())); | 213 setString(int32ToWebCoreString(m_v8Object->Int32Value())); |
| 213 return true; | 214 return true; |
| 214 } | 215 } |
| 215 | 216 |
| 216 m_mode = DoNotExternalize; | 217 m_mode = DoNotExternalize; |
| 217 v8::TryCatch block; | 218 v8::TryCatch block; |
| 218 m_v8Object = m_v8Object->ToString(); | 219 m_v8Object = m_v8Object->ToString(); |
| 219 // Handle the case where an exception is thrown as part of invoking toSt
ring on the object. | 220 // Handle the case where an exception is thrown as part of invoking toSt
ring on the object. |
| 220 if (block.HasCaught()) { | 221 if (block.HasCaught()) { |
| 221 block.ReThrow(); | 222 block.ReThrow(); |
| 222 return false; | 223 return false; |
| 223 } | 224 } |
| 224 return true; | 225 return true; |
| 225 } | 226 } |
| 226 operator String() const { return toString<String>(); } | 227 operator String() const { return toString<String>(); } |
| 227 operator AtomicString() const { return toString<AtomicString>(); } | 228 operator AtomicString() const { return toString<AtomicString>(); } |
| 228 | 229 |
| 229 private: | 230 private: |
| 230 bool isValid() const; | 231 bool isValid() const; |
| 232 String fallbackString() const; |
| 231 | 233 |
| 232 void setString(const String& string) | 234 void setString(const String& string) |
| 233 { | 235 { |
| 234 m_string = string; | 236 m_string = string; |
| 235 m_v8Object.Clear(); // To signal that String is ready. | 237 m_v8Object.Clear(); // To signal that String is ready. |
| 236 } | 238 } |
| 237 | 239 |
| 238 template <class StringType> | 240 template <class StringType> |
| 239 StringType toString() const | 241 StringType toString() const |
| 240 { | 242 { |
| 241 if (LIKELY(!m_v8Object.IsEmpty())) | 243 if (LIKELY(!m_v8Object.IsEmpty())) |
| 242 return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8:
:Value>*>(&m_v8Object)->As<v8::String>(), m_mode); | 244 return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8:
:Value>*>(&m_v8Object)->As<v8::String>(), m_mode); |
| 243 | 245 |
| 244 return StringType(m_string); | 246 return StringType(m_string); |
| 245 } | 247 } |
| 246 | 248 |
| 247 v8::Handle<v8::Value> m_v8Object; | 249 v8::Handle<v8::Value> m_v8Object; |
| 248 ExternalMode m_mode; | 250 ExternalMode m_mode; |
| 249 String m_string; | 251 String m_string; |
| 250 }; | 252 }; |
| 251 | 253 |
| 252 template<> inline bool V8StringResource<DefaultMode>::isValid() const | 254 template<> inline bool V8StringResource<DefaultMode>::isValid() const |
| 253 { | 255 { |
| 254 return true; | 256 return true; |
| 255 } | 257 } |
| 256 | 258 |
| 257 template<> inline bool V8StringResource<WithNullCheck>::isValid() const | 259 template<> inline String V8StringResource<DefaultMode>::fallbackString() const |
| 260 { |
| 261 ASSERT_NOT_REACHED(); |
| 262 return String(); |
| 263 } |
| 264 |
| 265 template<> inline bool V8StringResource<TreatNullAsEmptyString>::isValid() const |
| 258 { | 266 { |
| 259 return !m_v8Object->IsNull(); | 267 return !m_v8Object->IsNull(); |
| 260 } | 268 } |
| 261 | 269 |
| 262 template<> inline bool V8StringResource<WithUndefinedOrNullCheck>::isValid() con
st | 270 template<> inline String V8StringResource<TreatNullAsEmptyString>::fallbackStrin
g() const |
| 271 { |
| 272 return emptyString(); |
| 273 } |
| 274 |
| 275 template<> inline bool V8StringResource<TreatNullAsNullString>::isValid() const |
| 276 { |
| 277 return !m_v8Object->IsNull(); |
| 278 } |
| 279 |
| 280 template<> inline String V8StringResource<TreatNullAsNullString>::fallbackString
() const |
| 281 { |
| 282 return String(); |
| 283 } |
| 284 |
| 285 template<> inline bool V8StringResource<TreatNullAndUndefinedAsNullString>::isVa
lid() const |
| 263 { | 286 { |
| 264 return !m_v8Object->IsNull() && !m_v8Object->IsUndefined(); | 287 return !m_v8Object->IsNull() && !m_v8Object->IsUndefined(); |
| 265 } | 288 } |
| 266 | 289 |
| 290 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa
llbackString() const |
| 291 { |
| 292 return String(); |
| 293 } |
| 294 |
| 267 } // namespace WebCore | 295 } // namespace WebCore |
| 268 | 296 |
| 269 #endif // V8StringResource_h | 297 #endif // V8StringResource_h |
| OLD | NEW |