Chromium Code Reviews| Index: Source/bindings/v8/V8StringResource.h |
| diff --git a/Source/bindings/v8/V8StringResource.h b/Source/bindings/v8/V8StringResource.h |
| index e11002e5b0492b719f3bb4e560c5610ef63c0dce..1ad98b093f4c5644e86ddae8effb7b6eab8bb6b2 100644 |
| --- a/Source/bindings/v8/V8StringResource.h |
| +++ b/Source/bindings/v8/V8StringResource.h |
| @@ -158,6 +158,56 @@ template <typename StringType> |
| StringType v8StringToWebCoreString(v8::Handle<v8::String>, ExternalMode); |
| String int32ToWebCoreString(int value); |
| +enum V8StringResourceInitMode { |
| + V8StringResourceDoNotCatchException, |
| + V8StringResourceRethrowException, |
| + V8StringResourceStoreException, |
| +}; |
| + |
| +template<V8StringResourceInitMode> |
| +struct V8StringResourceToString; |
|
yhirano
2014/04/24 06:52:51
I know this is a bit complex, but It is needed to
|
| + |
| +template<> |
| +struct V8StringResourceToString<V8StringResourceDoNotCatchException> { |
| + typedef V8StringResourceToString<V8StringResourceDoNotCatchException> ThisClass; |
| + static bool call(ThisClass*, v8::Handle<v8::Value>* value) |
| + { |
| + *value = (*value)->ToString(); |
| + return !value->IsEmpty(); |
| + } |
| +}; |
| + |
| +template<> |
| +struct V8StringResourceToString<V8StringResourceRethrowException> { |
| + typedef V8StringResourceToString<V8StringResourceRethrowException> ThisClass; |
| + static bool call(ThisClass*, v8::Handle<v8::Value>* value) |
| + { |
| + v8::TryCatch block; |
| + *value = (*value)->ToString(); |
| + if (block.HasCaught()) { |
| + block.ReThrow(); |
| + return false; |
| + } |
| + return true; |
| + } |
| +}; |
| + |
| +template<> |
| +struct V8StringResourceToString<V8StringResourceStoreException> { |
|
yhirano
2014/04/24 06:52:51
Nobody uses this class yet, but I wrote it as a pr
|
| + typedef V8StringResourceToString<V8StringResourceStoreException> ThisClass; |
| + bool call(ThisClass* context, v8::Handle<v8::Value>* value) |
| + { |
| + v8::TryCatch block; |
| + *value = (*value)->ToString(); |
| + if (block.HasCaught()) { |
| + context->exception = block.Exception(); |
| + return false; |
| + } |
| + return true; |
| + } |
| + v8::Handle<v8::Value> exception; |
| +}; |
| + |
| // V8StringResource is an adapter class that converts V8 values to Strings |
| // or AtomicStrings as appropriate, using multiple typecast operators. |
| enum V8StringResourceMode { |
| @@ -176,16 +226,20 @@ public: |
| { |
| } |
| - bool prepare(); |
| - operator String() const { return toString<String>(); } |
| - operator AtomicString() const { return toString<AtomicString>(); } |
| - |
| -private: |
| - bool prepareBase() |
| + // Initializes this object, which means sets m_v8Object, m_mode and |
| + // m_string appropriately. |
| + // This function returns true if intialization is done successfully. |
| + template<V8StringResourceInitMode initMode> |
| + bool init(V8StringResourceToString<initMode>* stringifier) |
| { |
| if (m_v8Object.IsEmpty()) |
| return true; |
| + if (!isValid()) { |
| + setString(String()); |
| + return true; |
| + } |
| + |
| if (LIKELY(m_v8Object->IsString())) |
| return true; |
| @@ -193,18 +247,16 @@ private: |
| setString(int32ToWebCoreString(m_v8Object->Int32Value())); |
| return true; |
| } |
| - |
| m_mode = DoNotExternalize; |
| - v8::TryCatch block; |
| - m_v8Object = m_v8Object->ToString(); |
| - // Handle the case where an exception is thrown as part of invoking toString on the object. |
| - if (block.HasCaught()) { |
| - block.ReThrow(); |
| - return false; |
| - } |
| - return true; |
| + return V8StringResourceToString<initMode>::call(stringifier, &m_v8Object); |
| } |
| + operator String() const { return toString<String>(); } |
| + operator AtomicString() const { return toString<AtomicString>(); } |
| + |
| +private: |
| + bool isValid(); |
| + |
| void setString(const String& string) |
| { |
| m_string = string; |
| @@ -225,27 +277,19 @@ private: |
| String m_string; |
| }; |
| -template<> inline bool V8StringResource<DefaultMode>::prepare() |
| +template<> inline bool V8StringResource<DefaultMode>::isValid() |
| { |
| - return prepareBase(); |
| + return true; |
| } |
| -template<> inline bool V8StringResource<WithNullCheck>::prepare() |
| +template<> inline bool V8StringResource<WithNullCheck>::isValid() |
| { |
| - if (m_v8Object.IsEmpty() || m_v8Object->IsNull()) { |
| - setString(String()); |
| - return true; |
| - } |
| - return prepareBase(); |
| + return !m_v8Object->IsNull(); |
| } |
| -template<> inline bool V8StringResource<WithUndefinedOrNullCheck>::prepare() |
| +template<> inline bool V8StringResource<WithUndefinedOrNullCheck>::isValid() |
| { |
| - if (m_v8Object.IsEmpty() || m_v8Object->IsNull() || m_v8Object->IsUndefined()) { |
| - setString(String()); |
| - return true; |
| - } |
| - return prepareBase(); |
| + return !m_v8Object->IsNull() && !m_v8Object->IsUndefined(); |
| } |
| } // namespace WebCore |