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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 | 151 |
| 152 enum ExternalMode { | 152 enum ExternalMode { |
| 153 Externalize, | 153 Externalize, |
| 154 DoNotExternalize | 154 DoNotExternalize |
| 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 enum V8StringResourceInitMode { | |
| 162 V8StringResourceDoNotCatchException, | |
| 163 V8StringResourceRethrowException, | |
| 164 V8StringResourceStoreException, | |
| 165 }; | |
| 166 | |
| 167 template<V8StringResourceInitMode> | |
| 168 struct V8StringResourceToString; | |
|
yhirano
2014/04/24 06:52:51
I know this is a bit complex, but It is needed to
| |
| 169 | |
| 170 template<> | |
| 171 struct V8StringResourceToString<V8StringResourceDoNotCatchException> { | |
| 172 typedef V8StringResourceToString<V8StringResourceDoNotCatchException> ThisCl ass; | |
| 173 static bool call(ThisClass*, v8::Handle<v8::Value>* value) | |
| 174 { | |
| 175 *value = (*value)->ToString(); | |
| 176 return !value->IsEmpty(); | |
| 177 } | |
| 178 }; | |
| 179 | |
| 180 template<> | |
| 181 struct V8StringResourceToString<V8StringResourceRethrowException> { | |
| 182 typedef V8StringResourceToString<V8StringResourceRethrowException> ThisClass ; | |
| 183 static bool call(ThisClass*, v8::Handle<v8::Value>* value) | |
| 184 { | |
| 185 v8::TryCatch block; | |
| 186 *value = (*value)->ToString(); | |
| 187 if (block.HasCaught()) { | |
| 188 block.ReThrow(); | |
| 189 return false; | |
| 190 } | |
| 191 return true; | |
| 192 } | |
| 193 }; | |
| 194 | |
| 195 template<> | |
| 196 struct V8StringResourceToString<V8StringResourceStoreException> { | |
|
yhirano
2014/04/24 06:52:51
Nobody uses this class yet, but I wrote it as a pr
| |
| 197 typedef V8StringResourceToString<V8StringResourceStoreException> ThisClass; | |
| 198 bool call(ThisClass* context, v8::Handle<v8::Value>* value) | |
| 199 { | |
| 200 v8::TryCatch block; | |
| 201 *value = (*value)->ToString(); | |
| 202 if (block.HasCaught()) { | |
| 203 context->exception = block.Exception(); | |
| 204 return false; | |
| 205 } | |
| 206 return true; | |
| 207 } | |
| 208 v8::Handle<v8::Value> exception; | |
| 209 }; | |
| 210 | |
| 161 // V8StringResource is an adapter class that converts V8 values to Strings | 211 // V8StringResource is an adapter class that converts V8 values to Strings |
| 162 // or AtomicStrings as appropriate, using multiple typecast operators. | 212 // or AtomicStrings as appropriate, using multiple typecast operators. |
| 163 enum V8StringResourceMode { | 213 enum V8StringResourceMode { |
| 164 DefaultMode, | 214 DefaultMode, |
| 165 WithNullCheck, | 215 WithNullCheck, |
| 166 WithUndefinedOrNullCheck | 216 WithUndefinedOrNullCheck |
| 167 }; | 217 }; |
| 168 | 218 |
| 169 template <V8StringResourceMode Mode = DefaultMode> | 219 template <V8StringResourceMode Mode = DefaultMode> |
| 170 class V8StringResource { | 220 class V8StringResource { |
| 171 public: | 221 public: |
| 172 V8StringResource(v8::Handle<v8::Value> object) | 222 V8StringResource(v8::Handle<v8::Value> object) |
| 173 : m_v8Object(object) | 223 : m_v8Object(object) |
| 174 , m_mode(Externalize) | 224 , m_mode(Externalize) |
| 175 , m_string() | 225 , m_string() |
| 176 { | 226 { |
| 177 } | 227 } |
| 178 | 228 |
| 179 bool prepare(); | 229 // Initializes this object, which means sets m_v8Object, m_mode and |
| 180 operator String() const { return toString<String>(); } | 230 // m_string appropriately. |
| 181 operator AtomicString() const { return toString<AtomicString>(); } | 231 // This function returns true if intialization is done successfully. |
| 182 | 232 template<V8StringResourceInitMode initMode> |
| 183 private: | 233 bool init(V8StringResourceToString<initMode>* stringifier) |
| 184 bool prepareBase() | |
| 185 { | 234 { |
| 186 if (m_v8Object.IsEmpty()) | 235 if (m_v8Object.IsEmpty()) |
| 187 return true; | 236 return true; |
| 188 | 237 |
| 238 if (!isValid()) { | |
| 239 setString(String()); | |
| 240 return true; | |
| 241 } | |
| 242 | |
| 189 if (LIKELY(m_v8Object->IsString())) | 243 if (LIKELY(m_v8Object->IsString())) |
| 190 return true; | 244 return true; |
| 191 | 245 |
| 192 if (LIKELY(m_v8Object->IsInt32())) { | 246 if (LIKELY(m_v8Object->IsInt32())) { |
| 193 setString(int32ToWebCoreString(m_v8Object->Int32Value())); | 247 setString(int32ToWebCoreString(m_v8Object->Int32Value())); |
| 194 return true; | 248 return true; |
| 195 } | 249 } |
| 250 m_mode = DoNotExternalize; | |
| 251 return V8StringResourceToString<initMode>::call(stringifier, &m_v8Object ); | |
| 252 } | |
| 196 | 253 |
| 197 m_mode = DoNotExternalize; | 254 operator String() const { return toString<String>(); } |
| 198 v8::TryCatch block; | 255 operator AtomicString() const { return toString<AtomicString>(); } |
| 199 m_v8Object = m_v8Object->ToString(); | 256 |
| 200 // Handle the case where an exception is thrown as part of invoking toSt ring on the object. | 257 private: |
| 201 if (block.HasCaught()) { | 258 bool isValid(); |
| 202 block.ReThrow(); | |
| 203 return false; | |
| 204 } | |
| 205 return true; | |
| 206 } | |
| 207 | 259 |
| 208 void setString(const String& string) | 260 void setString(const String& string) |
| 209 { | 261 { |
| 210 m_string = string; | 262 m_string = string; |
| 211 m_v8Object.Clear(); // To signal that String is ready. | 263 m_v8Object.Clear(); // To signal that String is ready. |
| 212 } | 264 } |
| 213 | 265 |
| 214 template <class StringType> | 266 template <class StringType> |
| 215 StringType toString() const | 267 StringType toString() const |
| 216 { | 268 { |
| 217 if (LIKELY(!m_v8Object.IsEmpty())) | 269 if (LIKELY(!m_v8Object.IsEmpty())) |
| 218 return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8: :Value>*>(&m_v8Object)->As<v8::String>(), m_mode); | 270 return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8: :Value>*>(&m_v8Object)->As<v8::String>(), m_mode); |
| 219 | 271 |
| 220 return StringType(m_string); | 272 return StringType(m_string); |
| 221 } | 273 } |
| 222 | 274 |
| 223 v8::Handle<v8::Value> m_v8Object; | 275 v8::Handle<v8::Value> m_v8Object; |
| 224 ExternalMode m_mode; | 276 ExternalMode m_mode; |
| 225 String m_string; | 277 String m_string; |
| 226 }; | 278 }; |
| 227 | 279 |
| 228 template<> inline bool V8StringResource<DefaultMode>::prepare() | 280 template<> inline bool V8StringResource<DefaultMode>::isValid() |
| 229 { | 281 { |
| 230 return prepareBase(); | 282 return true; |
| 231 } | 283 } |
| 232 | 284 |
| 233 template<> inline bool V8StringResource<WithNullCheck>::prepare() | 285 template<> inline bool V8StringResource<WithNullCheck>::isValid() |
| 234 { | 286 { |
| 235 if (m_v8Object.IsEmpty() || m_v8Object->IsNull()) { | 287 return !m_v8Object->IsNull(); |
| 236 setString(String()); | |
| 237 return true; | |
| 238 } | |
| 239 return prepareBase(); | |
| 240 } | 288 } |
| 241 | 289 |
| 242 template<> inline bool V8StringResource<WithUndefinedOrNullCheck>::prepare() | 290 template<> inline bool V8StringResource<WithUndefinedOrNullCheck>::isValid() |
| 243 { | 291 { |
| 244 if (m_v8Object.IsEmpty() || m_v8Object->IsNull() || m_v8Object->IsUndefined( )) { | 292 return !m_v8Object->IsNull() && !m_v8Object->IsUndefined(); |
| 245 setString(String()); | |
| 246 return true; | |
| 247 } | |
| 248 return prepareBase(); | |
| 249 } | 293 } |
| 250 | 294 |
| 251 } // namespace WebCore | 295 } // namespace WebCore |
| 252 | 296 |
| 253 #endif // V8StringResource_h | 297 #endif // V8StringResource_h |
| OLD | NEW |