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 bool prepare() |
180 operator String() const { return toString<String>(); } | |
181 operator AtomicString() const { return toString<AtomicString>(); } | |
182 | |
183 private: | |
184 bool prepareBase() | |
185 { | 180 { |
186 if (m_v8Object.IsEmpty()) | 181 if (m_v8Object.IsEmpty()) |
187 return true; | 182 return true; |
188 | 183 |
| 184 if (!isValid()) { |
| 185 setString(String()); |
| 186 return true; |
| 187 } |
| 188 |
189 if (LIKELY(m_v8Object->IsString())) | 189 if (LIKELY(m_v8Object->IsString())) |
190 return true; | 190 return true; |
191 | 191 |
192 if (LIKELY(m_v8Object->IsInt32())) { | 192 if (LIKELY(m_v8Object->IsInt32())) { |
193 setString(int32ToWebCoreString(m_v8Object->Int32Value())); | 193 setString(int32ToWebCoreString(m_v8Object->Int32Value())); |
194 return true; | 194 return true; |
195 } | 195 } |
196 | 196 |
197 m_mode = DoNotExternalize; | 197 m_mode = DoNotExternalize; |
198 v8::TryCatch block; | 198 v8::TryCatch block; |
199 m_v8Object = m_v8Object->ToString(); | 199 m_v8Object = m_v8Object->ToString(); |
200 // Handle the case where an exception is thrown as part of invoking toSt
ring on the object. | 200 // Handle the case where an exception is thrown as part of invoking toSt
ring on the object. |
201 if (block.HasCaught()) { | 201 if (block.HasCaught()) { |
202 block.ReThrow(); | 202 block.ReThrow(); |
203 return false; | 203 return false; |
204 } | 204 } |
205 return true; | 205 return true; |
206 } | 206 } |
| 207 operator String() const { return toString<String>(); } |
| 208 operator AtomicString() const { return toString<AtomicString>(); } |
| 209 |
| 210 private: |
| 211 bool isValid() const; |
207 | 212 |
208 void setString(const String& string) | 213 void setString(const String& string) |
209 { | 214 { |
210 m_string = string; | 215 m_string = string; |
211 m_v8Object.Clear(); // To signal that String is ready. | 216 m_v8Object.Clear(); // To signal that String is ready. |
212 } | 217 } |
213 | 218 |
214 template <class StringType> | 219 template <class StringType> |
215 StringType toString() const | 220 StringType toString() const |
216 { | 221 { |
217 if (LIKELY(!m_v8Object.IsEmpty())) | 222 if (LIKELY(!m_v8Object.IsEmpty())) |
218 return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8:
:Value>*>(&m_v8Object)->As<v8::String>(), m_mode); | 223 return v8StringToWebCoreString<StringType>(const_cast<v8::Handle<v8:
:Value>*>(&m_v8Object)->As<v8::String>(), m_mode); |
219 | 224 |
220 return StringType(m_string); | 225 return StringType(m_string); |
221 } | 226 } |
222 | 227 |
223 v8::Handle<v8::Value> m_v8Object; | 228 v8::Handle<v8::Value> m_v8Object; |
224 ExternalMode m_mode; | 229 ExternalMode m_mode; |
225 String m_string; | 230 String m_string; |
226 }; | 231 }; |
227 | 232 |
228 template<> inline bool V8StringResource<DefaultMode>::prepare() | 233 template<> inline bool V8StringResource<DefaultMode>::isValid() const |
229 { | 234 { |
230 return prepareBase(); | 235 return true; |
231 } | 236 } |
232 | 237 |
233 template<> inline bool V8StringResource<WithNullCheck>::prepare() | 238 template<> inline bool V8StringResource<WithNullCheck>::isValid() const |
234 { | 239 { |
235 if (m_v8Object.IsEmpty() || m_v8Object->IsNull()) { | 240 return !m_v8Object->IsNull(); |
236 setString(String()); | |
237 return true; | |
238 } | |
239 return prepareBase(); | |
240 } | 241 } |
241 | 242 |
242 template<> inline bool V8StringResource<WithUndefinedOrNullCheck>::prepare() | 243 template<> inline bool V8StringResource<WithUndefinedOrNullCheck>::isValid() con
st |
243 { | 244 { |
244 if (m_v8Object.IsEmpty() || m_v8Object->IsNull() || m_v8Object->IsUndefined(
)) { | 245 return !m_v8Object->IsNull() && !m_v8Object->IsUndefined(); |
245 setString(String()); | |
246 return true; | |
247 } | |
248 return prepareBase(); | |
249 } | 246 } |
250 | 247 |
251 } // namespace WebCore | 248 } // namespace WebCore |
252 | 249 |
253 #endif // V8StringResource_h | 250 #endif // V8StringResource_h |
OLD | NEW |