Chromium Code Reviews| 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 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 template <V8StringResourceMode Mode = DefaultMode> | 169 template <V8StringResourceMode Mode = DefaultMode> |
| 170 class V8StringResource { | 170 class V8StringResource { |
| 171 public: | 171 public: |
| 172 V8StringResource(v8::Handle<v8::Value> object) | 172 V8StringResource(v8::Handle<v8::Value> object) |
| 173 : m_v8Object(object) | 173 : m_v8Object(object) |
| 174 , m_mode(Externalize) | 174 , m_mode(Externalize) |
| 175 , m_string() | 175 , m_string() |
| 176 { | 176 { |
| 177 } | 177 } |
| 178 | 178 |
| 179 bool prepare(); | 179 // Returns false if calling ToString is needed. |
| 180 bool setStringAsPrimitive(); | |
| 181 // This function assumes that setStringAsPrimitive is already called. | |
| 182 // If ToString fails returns false and throws and JS exception. | |
| 183 bool setStringWithToString() | |
| 184 { | |
| 185 ASSERT(!m_v8Object.IsEmpty()); | |
| 186 ASSERT(!m_v8Object->IsString()); | |
| 187 ASSERT(!m_v8Object->IsInt32()); | |
| 188 ASSERT(m_mode == Externalize); | |
| 189 m_mode = DoNotExternalize; | |
| 190 m_v8Object = m_v8Object->ToString(); | |
| 191 return !m_v8Object.IsEmpty(); | |
| 192 } | |
| 193 | |
| 180 operator String() const { return toString<String>(); } | 194 operator String() const { return toString<String>(); } |
| 181 operator AtomicString() const { return toString<AtomicString>(); } | 195 operator AtomicString() const { return toString<AtomicString>(); } |
| 182 | 196 |
| 197 bool setStringUsingToString(); | |
|
haraken
2014/04/21 03:57:02
What's this?
yhirano
2014/04/21 04:08:06
Thanks, I deleted it.
| |
| 198 | |
| 183 private: | 199 private: |
| 184 bool prepareBase() | 200 bool setStringAsPrimitiveBase() |
| 185 { | 201 { |
| 186 if (m_v8Object.IsEmpty()) | 202 if (m_v8Object.IsEmpty()) |
| 187 return true; | 203 return true; |
| 188 | 204 |
| 189 if (LIKELY(m_v8Object->IsString())) | 205 if (LIKELY(m_v8Object->IsString())) |
| 190 return true; | 206 return true; |
| 191 | 207 |
| 192 if (LIKELY(m_v8Object->IsInt32())) { | 208 if (LIKELY(m_v8Object->IsInt32())) { |
| 193 setString(int32ToWebCoreString(m_v8Object->Int32Value())); | 209 setString(int32ToWebCoreString(m_v8Object->Int32Value())); |
| 194 return true; | 210 return true; |
| 195 } | 211 } |
| 196 | 212 return false; |
| 197 m_mode = DoNotExternalize; | |
| 198 v8::TryCatch block; | |
| 199 m_v8Object = m_v8Object->ToString(); | |
| 200 // Handle the case where an exception is thrown as part of invoking toSt ring on the object. | |
| 201 if (block.HasCaught()) { | |
| 202 block.ReThrow(); | |
| 203 return false; | |
| 204 } | |
| 205 return true; | |
| 206 } | 213 } |
| 207 | 214 |
| 208 void setString(const String& string) | 215 void setString(const String& string) |
| 209 { | 216 { |
| 210 m_string = string; | 217 m_string = string; |
| 211 m_v8Object.Clear(); // To signal that String is ready. | 218 m_v8Object.Clear(); // To signal that String is ready. |
| 212 } | 219 } |
| 213 | 220 |
| 214 template <class StringType> | 221 template <class StringType> |
| 215 StringType toString() const | 222 StringType toString() const |
| 216 { | 223 { |
| 217 if (LIKELY(!m_v8Object.IsEmpty())) | 224 if (LIKELY(!m_v8Object.IsEmpty())) |
| 218 return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8: :Value>*>(&m_v8Object)->As<v8::String>(), m_mode); | 225 return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8: :Value>*>(&m_v8Object)->As<v8::String>(), m_mode); |
| 219 | 226 |
| 220 return StringType(m_string); | 227 return StringType(m_string); |
| 221 } | 228 } |
| 222 | 229 |
| 223 v8::Handle<v8::Value> m_v8Object; | 230 v8::Handle<v8::Value> m_v8Object; |
| 224 ExternalMode m_mode; | 231 ExternalMode m_mode; |
| 225 String m_string; | 232 String m_string; |
| 226 }; | 233 }; |
| 227 | 234 |
| 228 template<> inline bool V8StringResource<DefaultMode>::prepare() | 235 template<> inline bool V8StringResource<DefaultMode>::setStringAsPrimitive() |
| 229 { | 236 { |
| 230 return prepareBase(); | 237 return setStringAsPrimitiveBase(); |
| 231 } | 238 } |
| 232 | 239 |
| 233 template<> inline bool V8StringResource<WithNullCheck>::prepare() | 240 template<> inline bool V8StringResource<WithNullCheck>::setStringAsPrimitive() |
| 234 { | 241 { |
| 235 if (m_v8Object.IsEmpty() || m_v8Object->IsNull()) { | 242 if (m_v8Object.IsEmpty() || m_v8Object->IsNull()) { |
| 236 setString(String()); | 243 setString(String()); |
| 237 return true; | 244 return true; |
| 238 } | 245 } |
| 239 return prepareBase(); | 246 return setStringAsPrimitiveBase(); |
| 240 } | 247 } |
| 241 | 248 |
| 242 template<> inline bool V8StringResource<WithUndefinedOrNullCheck>::prepare() | 249 template<> inline bool V8StringResource<WithUndefinedOrNullCheck>::setStringAsPr imitive() |
| 243 { | 250 { |
| 244 if (m_v8Object.IsEmpty() || m_v8Object->IsNull() || m_v8Object->IsUndefined( )) { | 251 if (m_v8Object.IsEmpty() || m_v8Object->IsNull() || m_v8Object->IsUndefined( )) { |
| 245 setString(String()); | 252 setString(String()); |
| 246 return true; | 253 return true; |
| 247 } | 254 } |
| 248 return prepareBase(); | 255 return setStringAsPrimitiveBase(); |
| 249 } | 256 } |
| 250 | 257 |
| 251 } // namespace WebCore | 258 } // namespace WebCore |
| 252 | 259 |
| 253 #endif // V8StringResource_h | 260 #endif // V8StringResource_h |
| OLD | NEW |