Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(568)

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/ExceptionState.h

Issue 2715073002: Devirtualize ExceptionState's helper throwing methods.
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 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 are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 #endif // DCHECK_IS_ON() 97 #endif // DCHECK_IS_ON()
98 } 98 }
99 99
100 ~ExceptionState() { 100 ~ExceptionState() {
101 if (!m_exception.isEmpty()) { 101 if (!m_exception.isEmpty()) {
102 V8ThrowException::throwException(m_isolate, 102 V8ThrowException::throwException(m_isolate,
103 m_exception.newLocal(m_isolate)); 103 m_exception.newLocal(m_isolate));
104 } 104 }
105 } 105 }
106 106
107 // const char* versions to avoid code bloat from inlined temporary Strings.
107 void throwDOMException(ExceptionCode, const char* message); 108 void throwDOMException(ExceptionCode, const char* message);
108 void throwRangeError(const char* message); 109 void throwRangeError(const char* message);
109 void throwSecurityError(const char* sanitizedMessage, 110 void throwSecurityError(const char* sanitizedMessage,
110 const char* unsanitizedMessage = nullptr); 111 const char* unsanitizedMessage = nullptr);
111 void throwTypeError(const char* message); 112 void throwTypeError(const char* message);
112 113
113 virtual void throwDOMException(ExceptionCode, const String& message); 114 void throwDOMException(ExceptionCode, const String& message);
114 virtual void throwRangeError(const String& message); 115 void throwRangeError(const String& message);
115 virtual void throwSecurityError(const String& sanitizedMessage, 116 void throwSecurityError(const String& sanitizedMessage,
116 const String& unsanitizedMessage = String()); 117 const String& unsanitizedMessage = String());
117 virtual void throwTypeError(const String& message); 118 void throwTypeError(const String& message);
118 virtual void rethrowV8Exception(v8::Local<v8::Value>); 119
120 void rethrowV8Exception(v8::Local<v8::Value>);
119 121
120 bool hadException() const { return m_code; } 122 bool hadException() const { return m_code; }
121 void clearException(); 123 void clearException();
122 124
123 ExceptionCode code() const { return m_code; } 125 ExceptionCode code() const { return m_code; }
124 const String& message() const { return m_message; } 126 const String& message() const { return m_message; }
125 v8::Local<v8::Value> getException() { 127 v8::Local<v8::Value> getException() {
126 DCHECK(!m_exception.isEmpty()); 128 DCHECK(!m_exception.isEmpty());
127 return m_exception.newLocal(m_isolate); 129 return m_exception.newLocal(m_isolate);
128 } 130 }
129 131
130 // This method clears out the exception which |this| has. 132 // This method clears out the exception which |this| has.
131 ScriptPromise reject(ScriptState*); 133 ScriptPromise reject(ScriptState*);
132 134
133 // This method clears out the exception which |this| has. 135 // This method clears out the exception which |this| has.
134 void reject(ScriptPromiseResolver*); 136 void reject(ScriptPromiseResolver*);
135 137
136 ContextType context() const { return m_context; } 138 ContextType context() const { return m_context; }
137 const char* propertyName() const { return m_propertyName; } 139 const char* propertyName() const { return m_propertyName; }
138 const char* interfaceName() const { return m_interfaceName; } 140 const char* interfaceName() const { return m_interfaceName; }
139 141
140 String addExceptionContext(const String&) const; 142 String addExceptionContext(const String&) const;
141 143
142 protected: 144 protected:
143 // An ExceptionCode for the case that an exception is rethrown. In that 145 // An ExceptionCode for the case that an exception is rethrown. In that
144 // case, we cannot determine an exception code. 146 // case, we cannot determine an exception code.
145 static const int kRethrownException = UnknownError; 147 static const int kRethrownException = UnknownError;
146 148
147 void setException(ExceptionCode, const String&, v8::Local<v8::Value>); 149 virtual void setException(ExceptionCode, const String&, v8::Local<v8::Value>);
148 150
149 private: 151 private:
150 ExceptionCode m_code; 152 ExceptionCode m_code;
151 ContextType m_context; 153 ContextType m_context;
152 String m_message; 154 String m_message;
153 const char* m_propertyName; 155 const char* m_propertyName;
154 const char* m_interfaceName; 156 const char* m_interfaceName;
155 // The exception is empty when it was thrown through 157 // The exception is empty when it was thrown through
156 // DummyExceptionStateForTesting. 158 // DummyExceptionStateForTesting.
157 ScopedPersistent<v8::Value> m_exception; 159 ScopedPersistent<v8::Value> m_exception;
158 v8::Isolate* m_isolate; 160 v8::Isolate* m_isolate;
159 }; 161 };
160 162
161 // NonThrowableExceptionState never allow call sites to throw an exception. 163 // NonThrowableExceptionState never allow call sites to throw an exception.
162 // Should be used if an exception must not be thrown. 164 // Should be used if an exception must not be thrown.
163 class CORE_EXPORT NonThrowableExceptionState final : public ExceptionState { 165 class CORE_EXPORT NonThrowableExceptionState final : public ExceptionState {
164 public: 166 public:
165 NonThrowableExceptionState(); 167 NonThrowableExceptionState();
166 NonThrowableExceptionState(const char*, int); 168 NonThrowableExceptionState(const char*, int);
167 169
168 void throwDOMException(ExceptionCode, const String& message) override;
169 void throwTypeError(const String& message) override;
170 void throwSecurityError(const String& sanitizedMessage,
171 const String& unsanitizedMessage) override;
172 void throwRangeError(const String& message) override;
173 void rethrowV8Exception(v8::Local<v8::Value>) override;
174 ExceptionState& returnThis() { return *this; } 170 ExceptionState& returnThis() { return *this; }
175 171
172 protected:
173 void setException(ExceptionCode,
174 const String&,
175 v8::Local<v8::Value>) override;
176
176 private: 177 private:
177 const char* m_file; 178 const char* m_file;
178 const int m_line; 179 const int m_line;
179 }; 180 };
180 181
181 // Syntax sugar for NonThrowableExceptionState. 182 // Syntax sugar for NonThrowableExceptionState.
182 // This can be used as a default value of an ExceptionState parameter like this: 183 // This can be used as a default value of an ExceptionState parameter like this:
183 // 184 //
184 // Node* removeChild(Node*, ExceptionState& = ASSERT_NO_EXCEPTION); 185 // Node* removeChild(Node*, ExceptionState& = ASSERT_NO_EXCEPTION);
185 #if DCHECK_IS_ON() 186 #if DCHECK_IS_ON()
186 #define ASSERT_NO_EXCEPTION \ 187 #define ASSERT_NO_EXCEPTION \
187 (::blink::NonThrowableExceptionState(__FILE__, __LINE__).returnThis()) 188 (::blink::NonThrowableExceptionState(__FILE__, __LINE__).returnThis())
188 #else 189 #else
189 #define ASSERT_NO_EXCEPTION \ 190 #define ASSERT_NO_EXCEPTION \
190 (::blink::DummyExceptionStateForTesting().returnThis()) 191 (::blink::DummyExceptionStateForTesting().returnThis())
191 #endif 192 #endif
192 193
193 // DummyExceptionStateForTesting ignores all thrown exceptions. You should not 194 // DummyExceptionStateForTesting ignores all thrown exceptions. You should not
194 // use DummyExceptionStateForTesting in production code, where you need to 195 // use DummyExceptionStateForTesting in production code, where you need to
195 // handle all exceptions properly. If you really need to ignore exceptions in 196 // handle all exceptions properly. If you really need to ignore exceptions in
196 // production code for some special reason, explicitly call clearException(). 197 // production code for some special reason, explicitly call clearException().
197 class CORE_EXPORT DummyExceptionStateForTesting final : public ExceptionState { 198 class CORE_EXPORT DummyExceptionStateForTesting final : public ExceptionState {
198 public: 199 public:
199 DummyExceptionStateForTesting() 200 DummyExceptionStateForTesting()
200 : ExceptionState(nullptr, 201 : ExceptionState(nullptr,
201 ExceptionState::UnknownContext, 202 ExceptionState::UnknownContext,
202 nullptr, 203 nullptr,
203 nullptr) {} 204 nullptr) {}
204 ~DummyExceptionStateForTesting() { 205 ~DummyExceptionStateForTesting() {}
205 // Prevent the base class throw an exception. 206
206 if (hadException()) {
207 clearException();
208 }
209 }
210 void throwDOMException(ExceptionCode, const String& message) override;
211 void throwTypeError(const String& message) override;
212 void throwSecurityError(const String& sanitizedMessage,
213 const String& unsanitizedMessage) override;
214 void throwRangeError(const String& message) override;
215 void rethrowV8Exception(v8::Local<v8::Value>) override;
216 ExceptionState& returnThis() { return *this; } 207 ExceptionState& returnThis() { return *this; }
208
209 protected:
210 void setException(ExceptionCode,
211 const String&,
212 v8::Local<v8::Value>) override;
217 }; 213 };
218 214
219 // Syntax sugar for DummyExceptionStateForTesting. 215 // Syntax sugar for DummyExceptionStateForTesting.
220 // This can be used as a default value of an ExceptionState parameter like this: 216 // This can be used as a default value of an ExceptionState parameter like this:
221 // 217 //
222 // Node* removeChild(Node*, ExceptionState& = IGNORE_EXCEPTION_FOR_TESTING); 218 // Node* removeChild(Node*, ExceptionState& = IGNORE_EXCEPTION_FOR_TESTING);
223 #define IGNORE_EXCEPTION_FOR_TESTING \ 219 #define IGNORE_EXCEPTION_FOR_TESTING \
224 (::blink::DummyExceptionStateForTesting().returnThis()) 220 (::blink::DummyExceptionStateForTesting().returnThis())
225 221
226 } // namespace blink 222 } // namespace blink
227 223
228 #endif // ExceptionState_h 224 #endif // ExceptionState_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698