| 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 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 180 |
| 181 V8StringResource(const String& string) | 181 V8StringResource(const String& string) |
| 182 : m_mode(Externalize), m_string(string) {} | 182 : m_mode(Externalize), m_string(string) {} |
| 183 | 183 |
| 184 void operator=(v8::Local<v8::Value> object) { m_v8Object = object; } | 184 void operator=(v8::Local<v8::Value> object) { m_v8Object = object; } |
| 185 | 185 |
| 186 void operator=(const String& string) { setString(string); } | 186 void operator=(const String& string) { setString(string); } |
| 187 | 187 |
| 188 void operator=(std::nullptr_t) { setString(String()); } | 188 void operator=(std::nullptr_t) { setString(String()); } |
| 189 | 189 |
| 190 bool prepare() { | 190 bool prepare() { // DEPRECATED |
| 191 if (prepareFast()) | 191 if (prepareFast()) |
| 192 return true; | 192 return true; |
| 193 | 193 |
| 194 // TODO(bashi): Pass an isolate to this function and remove | |
| 195 // v8::Isolate::GetCurrent(). | |
| 196 return m_v8Object->ToString(v8::Isolate::GetCurrent()->GetCurrentContext()) | 194 return m_v8Object->ToString(v8::Isolate::GetCurrent()->GetCurrentContext()) |
| 197 .ToLocal(&m_v8Object); | 195 .ToLocal(&m_v8Object); |
| 198 } | 196 } |
| 199 | 197 |
| 200 bool prepare(ExceptionState& exceptionState) { | 198 bool prepare(v8::Isolate* isolate, ExceptionState& exceptionState) { |
| 201 if (prepareFast()) | 199 return prepareFast() || prepareSlow(isolate, exceptionState); |
| 202 return true; | 200 } |
| 203 | 201 |
| 204 // TODO(bashi): Pass an isolate to this function and remove | 202 bool prepare(ExceptionState& exceptionState) { // DEPRECATED |
| 205 // v8::Isolate::GetCurrent(). | 203 return prepareFast() || |
| 206 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 204 prepareSlow(v8::Isolate::GetCurrent(), exceptionState); |
| 207 v8::TryCatch block(isolate); | |
| 208 // Handle the case where an exception is thrown as part of invoking toString | |
| 209 // on the object. | |
| 210 if (!m_v8Object->ToString(isolate->GetCurrentContext()) | |
| 211 .ToLocal(&m_v8Object)) { | |
| 212 exceptionState.rethrowV8Exception(block.Exception()); | |
| 213 return false; | |
| 214 } | |
| 215 return true; | |
| 216 } | 205 } |
| 217 | 206 |
| 218 operator String() const { return toString<String>(); } | 207 operator String() const { return toString<String>(); } |
| 219 operator AtomicString() const { return toString<AtomicString>(); } | 208 operator AtomicString() const { return toString<AtomicString>(); } |
| 220 | 209 |
| 221 private: | 210 private: |
| 222 bool prepareFast() { | 211 bool prepareFast() { |
| 223 if (m_v8Object.IsEmpty()) | 212 if (m_v8Object.IsEmpty()) |
| 224 return true; | 213 return true; |
| 225 | 214 |
| 226 if (!isValid()) { | 215 if (!isValid()) { |
| 227 setString(fallbackString()); | 216 setString(fallbackString()); |
| 228 return true; | 217 return true; |
| 229 } | 218 } |
| 230 | 219 |
| 231 if (LIKELY(m_v8Object->IsString())) | 220 if (LIKELY(m_v8Object->IsString())) |
| 232 return true; | 221 return true; |
| 233 | 222 |
| 234 if (LIKELY(m_v8Object->IsInt32())) { | 223 if (LIKELY(m_v8Object->IsInt32())) { |
| 235 setString(int32ToWebCoreString(m_v8Object.As<v8::Int32>()->Value())); | 224 setString(int32ToWebCoreString(m_v8Object.As<v8::Int32>()->Value())); |
| 236 return true; | 225 return true; |
| 237 } | 226 } |
| 238 | 227 |
| 239 m_mode = DoNotExternalize; | 228 m_mode = DoNotExternalize; |
| 240 return false; | 229 return false; |
| 241 } | 230 } |
| 242 | 231 |
| 232 bool prepareSlow(v8::Isolate* isolate, ExceptionState& exceptionState) { |
| 233 v8::TryCatch tryCatch(isolate); |
| 234 if (!m_v8Object->ToString(isolate->GetCurrentContext()) |
| 235 .ToLocal(&m_v8Object)) { |
| 236 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
| 237 return false; |
| 238 } |
| 239 return true; |
| 240 } |
| 241 |
| 243 bool isValid() const; | 242 bool isValid() const; |
| 244 String fallbackString() const; | 243 String fallbackString() const; |
| 245 | 244 |
| 246 void setString(const String& string) { | 245 void setString(const String& string) { |
| 247 m_string = string; | 246 m_string = string; |
| 248 m_v8Object.Clear(); // To signal that String is ready. | 247 m_v8Object.Clear(); // To signal that String is ready. |
| 249 } | 248 } |
| 250 | 249 |
| 251 template <class StringType> | 250 template <class StringType> |
| 252 StringType toString() const { | 251 StringType toString() const { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 | 301 |
| 303 template <> | 302 template <> |
| 304 inline String | 303 inline String |
| 305 V8StringResource<TreatNullAndUndefinedAsNullString>::fallbackString() const { | 304 V8StringResource<TreatNullAndUndefinedAsNullString>::fallbackString() const { |
| 306 return String(); | 305 return String(); |
| 307 } | 306 } |
| 308 | 307 |
| 309 } // namespace blink | 308 } // namespace blink |
| 310 | 309 |
| 311 #endif // V8StringResource_h | 310 #endif // V8StringResource_h |
| OLD | NEW |