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

Side by Side Diff: Source/bindings/templates/methods.cpp

Issue 232563003: API functions returning Promises should not throw exceptions. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 8 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
« no previous file with comments | « Source/bindings/scripts/v8_types.py ('k') | Source/bindings/tests/idls/TestObject.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 {##############################################################################} 1 {##############################################################################}
2 {% macro generate_method(method, world_suffix) %} 2 {% macro generate_method(method, world_suffix) %}
3 {% filter conditional(method.conditional_string) %} 3 {% filter conditional(method.conditional_string) %}
4 static void {{method.name}}{{method.overload_index}}Method{{world_suffix}}(const v8::FunctionCallbackInfo<v8::Value>& info) 4 static void {{method.name}}{{method.overload_index}}Method{{world_suffix}}(const v8::FunctionCallbackInfo<v8::Value>& info)
5 { 5 {
6 {# Local variables #} 6 {# Local variables #}
7 {% if method.has_exception_state %} 7 {% if method.has_exception_state %}
8 ExceptionState exceptionState(ExceptionState::ExecutionContext, "{{method.na me}}", "{{interface_name}}", info.Holder(), info.GetIsolate()); 8 ExceptionState exceptionState(ExceptionState::ExecutionContext, "{{method.na me}}", "{{interface_name}}", info.Holder(), info.GetIsolate());
9 {% endif %} 9 {% endif %}
10 {% if method.number_of_required_arguments %} 10 {% if method.number_of_required_arguments %}
11 if (UNLIKELY(info.Length() < {{method.number_of_required_arguments}})) { 11 if (UNLIKELY(info.Length() < {{method.number_of_required_arguments}})) {
12 {{throw_arity_type_error(method, method.number_of_required_arguments)}}; 12 {{throw_arity_type_error(method, method.number_of_required_arguments)}};
13 return; 13 return;
14 } 14 }
15 {% endif %} 15 {% endif %}
16 {% if not method.is_static %} 16 {% if not method.is_static %}
17 {{cpp_class}}* impl = {{v8_class}}::toNative(info.Holder()); 17 {{cpp_class}}* impl = {{v8_class}}::toNative(info.Holder());
18 {% endif %} 18 {% endif %}
19 {% if method.is_custom_element_callbacks %} 19 {% if method.is_custom_element_callbacks %}
20 CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope; 20 CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
21 {% endif %} 21 {% endif %}
22 {# Security checks #} 22 {# Security checks #}
23 {# FIXME: change to method.is_check_security_for_window #} 23 {# FIXME: change to method.is_check_security_for_window #}
24 {% if interface_name == 'EventTarget' %} 24 {% if interface_name == 'EventTarget' %}
25 if (DOMWindow* window = impl->toDOMWindow()) { 25 if (DOMWindow* window = impl->toDOMWindow()) {
26 if (!BindingSecurity::shouldAllowAccessToFrame(info.GetIsolate(), window ->frame(), exceptionState)) { 26 if (!BindingSecurity::shouldAllowAccessToFrame(info.GetIsolate(), window ->frame(), exceptionState)) {
27 exceptionState.throwIfNeeded(); 27 {{throw_from_exception_state(method)}};
28 return; 28 return;
29 } 29 }
30 if (!window->document()) 30 if (!window->document())
31 return; 31 return;
32 } 32 }
33 {% elif method.is_check_security_for_frame %} 33 {% elif method.is_check_security_for_frame %}
34 if (!BindingSecurity::shouldAllowAccessToFrame(info.GetIsolate(), impl->fram e(), exceptionState)) { 34 if (!BindingSecurity::shouldAllowAccessToFrame(info.GetIsolate(), impl->fram e(), exceptionState)) {
35 exceptionState.throwIfNeeded(); 35 {{throw_from_exception_state(method)}};
36 return; 36 return;
37 } 37 }
38 {% endif %} 38 {% endif %}
39 {% if method.is_check_security_for_node %} 39 {% if method.is_check_security_for_node %}
40 if (!BindingSecurity::shouldAllowAccessToNode(info.GetIsolate(), impl->{{met hod.name}}(exceptionState), exceptionState)) { 40 if (!BindingSecurity::shouldAllowAccessToNode(info.GetIsolate(), impl->{{met hod.name}}(exceptionState), exceptionState)) {
41 v8SetReturnValueNull(info); 41 v8SetReturnValueNull(info);
42 exceptionState.throwIfNeeded(); 42 {{throw_from_exception_state(method)}};
43 return; 43 return;
44 } 44 }
45 {% endif %} 45 {% endif %}
46 {# Call method #} 46 {# Call method #}
47 {% for argument in method.arguments %} 47 {% for argument in method.arguments %}
48 {{generate_argument(method, argument, world_suffix) | indent}} 48 {{generate_argument(method, argument, world_suffix) | indent}}
49 {% endfor %} 49 {% endfor %}
50 {% if world_suffix %} 50 {% if world_suffix %}
51 {{cpp_method_call(method, method.v8_set_return_value_for_main_world, method. cpp_value) | indent}} 51 {{cpp_method_call(method, method.v8_set_return_value_for_main_world, method. cpp_value) | indent}}
52 {% else %} 52 {% else %}
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 '"The callback provided as parameter %s is not a function."' % 127 '"The callback provided as parameter %s is not a function."' %
128 (argument.index + 1)) | indent }} 128 (argument.index + 1)) | indent }}
129 return; 129 return;
130 } 130 }
131 OwnPtr<{{argument.idl_type}}> {{argument.name}} = {% if argument.is_nullable %}i nfo[{{argument.index}}]->IsNull() ? nullptr : {% endif %}V8{{argument.idl_type}} ::create(v8::Handle<v8::Function>::Cast(info[{{argument.index}}]), currentExecut ionContext(info.GetIsolate())); 131 OwnPtr<{{argument.idl_type}}> {{argument.name}} = {% if argument.is_nullable %}i nfo[{{argument.index}}]->IsNull() ? nullptr : {% endif %}V8{{argument.idl_type}} ::create(v8::Handle<v8::Function>::Cast(info[{{argument.index}}]), currentExecut ionContext(info.GetIsolate()));
132 {% endif %}{# argument.is_optional #} 132 {% endif %}{# argument.is_optional #}
133 {% endif %}{# argument.idl_type == 'EventListener' #} 133 {% endif %}{# argument.idl_type == 'EventListener' #}
134 {% elif argument.is_clamp %}{# argument.is_callback_interface #} 134 {% elif argument.is_clamp %}{# argument.is_callback_interface #}
135 {# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #} 135 {# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #}
136 {{argument.cpp_type}} {{argument.name}} = 0; 136 {{argument.cpp_type}} {{argument.name}} = 0;
137 {% if method.idl_type == 'Promise' %}
138 TONATIVE_VOID_ASYNC(double, {{argument.name}}NativeValue, info[{{argument.index} }]->NumberValue(), info);
139 {% else %}
137 TONATIVE_VOID(double, {{argument.name}}NativeValue, info[{{argument.index}}]->Nu mberValue()); 140 TONATIVE_VOID(double, {{argument.name}}NativeValue, info[{{argument.index}}]->Nu mberValue());
141 {% endif %}
138 if (!std::isnan({{argument.name}}NativeValue)) 142 if (!std::isnan({{argument.name}}NativeValue))
139 {# IDL type is used for clamping, for the right bounds, since different 143 {# IDL type is used for clamping, for the right bounds, since different
140 IDL integer types have same internal C++ type (int or unsigned) #} 144 IDL integer types have same internal C++ type (int or unsigned) #}
141 {{argument.name}} = clampTo<{{argument.idl_type}}>({{argument.name}}NativeVa lue); 145 {{argument.name}} = clampTo<{{argument.idl_type}}>({{argument.name}}NativeVa lue);
142 {% elif argument.idl_type == 'SerializedScriptValue' %} 146 {% elif argument.idl_type == 'SerializedScriptValue' %}
143 {{argument.cpp_type}} {{argument.name}} = SerializedScriptValue::create(info[{{a rgument.index}}], 0, 0, exceptionState, info.GetIsolate()); 147 {{argument.cpp_type}} {{argument.name}} = SerializedScriptValue::create(info[{{a rgument.index}}], 0, 0, exceptionState, info.GetIsolate());
144 if (exceptionState.throwIfNeeded()) 148 if (exceptionState.hadException()) {
149 {{throw_from_exception_state(method)}};
145 return; 150 return;
151 }
146 {% elif argument.is_variadic_wrapper_type %} 152 {% elif argument.is_variadic_wrapper_type %}
147 {{argument.vector_type}}<{{argument.cpp_type}} > {{argument.name}}; 153 {{argument.vector_type}}<{{argument.cpp_type}} > {{argument.name}};
148 for (int i = {{argument.index}}; i < info.Length(); ++i) { 154 for (int i = {{argument.index}}; i < info.Length(); ++i) {
149 if (!V8{{argument.idl_type}}::hasInstance(info[i], info.GetIsolate())) { 155 if (!V8{{argument.idl_type}}::hasInstance(info[i], info.GetIsolate())) {
150 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' % 156 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' %
151 (argument.index + 1, argument.idl_type)) | in dent(8)}} 157 (argument.index + 1, argument.idl_type)) | in dent(8)}}
152 return; 158 return;
153 } 159 }
154 {{argument.name}}.append(V8{{argument.idl_type}}::toNative(v8::Handle<v8::Ob ject>::Cast(info[i]))); 160 {{argument.name}}.append(V8{{argument.idl_type}}::toNative(v8::Handle<v8::Ob ject>::Cast(info[i])));
155 } 161 }
162 {% else %}{# argument.is_nullable #}
163 {% if method.idl_type == 'Promise' %}
164 {{argument.v8_value_to_local_cpp_value_async}};
156 {% else %} 165 {% else %}
157 {{argument.v8_value_to_local_cpp_value}}; 166 {{argument.v8_value_to_local_cpp_value}};
158 {% endif %} 167 {% endif %}
168 {% endif %}{# argument.is_nullable #}
159 {% if argument.enum_validation_expression %} 169 {% if argument.enum_validation_expression %}
160 {# Methods throw on invalid enum values: http://www.w3.org/TR/WebIDL/#idl-enums #} 170 {# Methods throw on invalid enum values: http://www.w3.org/TR/WebIDL/#idl-enums #}
161 String string = {{argument.name}}; 171 String string = {{argument.name}};
162 if (!({{argument.enum_validation_expression}})) { 172 if (!({{argument.enum_validation_expression}})) {
163 {{throw_type_error(method, 173 {{throw_type_error(method,
164 '"parameter %s (\'" + string + "\') is not a valid enum value."' % 174 '"parameter %s (\'" + string + "\') is not a valid enum value."' %
165 (argument.index + 1)) | indent}} 175 (argument.index + 1)) | indent}}
166 return; 176 return;
167 } 177 }
168 {% endif %} 178 {% endif %}
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 {% if method.idl_type == 'void' %} 211 {% if method.idl_type == 'void' %}
202 {{cpp_value}}; 212 {{cpp_value}};
203 {% elif method.is_constructor %} 213 {% elif method.is_constructor %}
204 {{method.cpp_type}} impl = {{cpp_value}}; 214 {{method.cpp_type}} impl = {{cpp_value}};
205 {% elif method.is_call_with_script_state or method.is_call_with_new_script_state or method.is_raises_exception %} 215 {% elif method.is_call_with_script_state or method.is_call_with_new_script_state or method.is_raises_exception %}
206 {# FIXME: consider always using a local variable #} 216 {# FIXME: consider always using a local variable #}
207 {{method.cpp_type}} result = {{cpp_value}}; 217 {{method.cpp_type}} result = {{cpp_value}};
208 {% endif %} 218 {% endif %}
209 {# Post-call #} 219 {# Post-call #}
210 {% if method.is_raises_exception %} 220 {% if method.is_raises_exception %}
211 if (exceptionState.throwIfNeeded()) 221 if (exceptionState.hadException()) {
222 {{throw_from_exception_state(method)}};
212 return; 223 return;
224 }
213 {% endif %} 225 {% endif %}
214 {# Set return value #} 226 {# Set return value #}
215 {% if method.is_constructor %} 227 {% if method.is_constructor %}
216 {{generate_constructor_wrapper(method)}}{% elif method.union_arguments %} 228 {{generate_constructor_wrapper(method)}}{% elif method.union_arguments %}
217 {{union_type_method_call_and_set_return_value(method)}} 229 {{union_type_method_call_and_set_return_value(method)}}
218 {% elif v8_set_return_value %}{{v8_set_return_value}};{% endif %}{# None for voi d #} 230 {% elif v8_set_return_value %}{{v8_set_return_value}};{% endif %}{# None for voi d #}
219 {% endmacro %} 231 {% endmacro %}
220 232
221 233
222 {######################################} 234 {######################################}
(...skipping 15 matching lines...) Expand all
238 {% endfor %} 250 {% endfor %}
239 {# Fall back to null if none of the union members results are returned #} 251 {# Fall back to null if none of the union members results are returned #}
240 v8SetReturnValueNull(info); 252 v8SetReturnValueNull(info);
241 {%- endmacro %} 253 {%- endmacro %}
242 254
243 255
244 {######################################} 256 {######################################}
245 {% macro throw_type_error(method, error_message) %} 257 {% macro throw_type_error(method, error_message) %}
246 {% if method.has_exception_state %} 258 {% if method.has_exception_state %}
247 exceptionState.throwTypeError({{error_message}}); 259 exceptionState.throwTypeError({{error_message}});
248 exceptionState.throwIfNeeded(); 260 {{throw_from_exception_state(method)}};
249 {%- elif method.is_constructor %} 261 {%- elif method.is_constructor %}
262 {% if method.idl_type == 'Promise' %}
263 v8SetReturnValue(info, ScriptPromise::rejectWithTypeError(ExceptionMessages::fai ledToConstruct("{{interface_name}}", {{error_message}}), info.GetIsolate()).v8Va lue());
264 {%- else %}
250 throwTypeError(ExceptionMessages::failedToConstruct("{{interface_name}}", {{erro r_message}}), info.GetIsolate()); 265 throwTypeError(ExceptionMessages::failedToConstruct("{{interface_name}}", {{erro r_message}}), info.GetIsolate());
266 {%- endif %}
267 {%- else %}{# method.has_exception_state #}
268 {% if method.idl_type == 'Promise' %}
269 v8SetReturnValue(info, ScriptPromise::rejectWithTypeError(ExceptionMessages::fai ledToExecute("{{method.name}}", "{{interface_name}}", {{error_message}}), info.G etIsolate()).v8Value());
251 {%- else %} 270 {%- else %}
252 throwTypeError(ExceptionMessages::failedToExecute("{{method.name}}", "{{interfac e_name}}", {{error_message}}), info.GetIsolate()); 271 throwTypeError(ExceptionMessages::failedToExecute("{{method.name}}", "{{interfac e_name}}", {{error_message}}), info.GetIsolate());
253 {%- endif %} 272 {%- endif %}
273 {%- endif %}{# method.has_exception_state #}
274 {% endmacro %}
275
276
277 {######################################}
278 {% macro throw_from_exception_state(method) %}
279 {% if method.idl_type == 'Promise' %}
280 v8SetReturnValue(info, exceptionState.reject().v8Value())
281 {%- else %}
282 exceptionState.throwIfNeeded()
283 {%- endif %}
254 {% endmacro %} 284 {% endmacro %}
255 285
256 286
257 {######################################} 287 {######################################}
258 {% macro throw_arity_type_error(method, number_of_required_arguments) %} 288 {% macro throw_arity_type_error(method, number_of_required_arguments) %}
289 {% if method.idl_type == 'Promise' %}
290 {% if method.has_exception_state %}
291 v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeError(exceptionState, { {number_of_required_arguments}}, info.Length()).v8Value())
292 {%- elif method.is_constructor %}
293 v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeErrorForConstructor("{{ interface_name}}", {{number_of_required_arguments}}, info.Length(), info.GetIsol ate().v8Value())
294 {%- else %}
295 v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeErrorForMethod("{{metho d.name}}", "{{interface_name}}", {{number_of_required_arguments}}, info.Length() , info.GetIsolate()).v8Value())
296 {%- endif %}
297 {%- else %}{# methods.idl_type == 'Promise #}
Nils Barth (inactive) 2014/04/14 06:17:43 Missing ' here.
yhirano 2014/04/14 06:20:17 Thanks, done.
259 {% if method.has_exception_state %} 298 {% if method.has_exception_state %}
260 throwArityTypeError(exceptionState, {{number_of_required_arguments}}, info.Lengt h()) 299 throwArityTypeError(exceptionState, {{number_of_required_arguments}}, info.Lengt h())
261 {%- elif method.is_constructor %} 300 {%- elif method.is_constructor %}
262 throwArityTypeErrorForConstructor("{{interface_name}}", {{number_of_required_arg uments}}, info.Length(), info.GetIsolate()) 301 throwArityTypeErrorForConstructor("{{interface_name}}", {{number_of_required_arg uments}}, info.Length(), info.GetIsolate())
263 {%- else %} 302 {%- else %}
264 throwArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{number_o f_required_arguments}}, info.Length(), info.GetIsolate()) 303 throwArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{number_o f_required_arguments}}, info.Length(), info.GetIsolate())
265 {%- endif %} 304 {%- endif %}
305 {%- endif %}{# methods.idl_type == 'Promise' #}
266 {% endmacro %} 306 {% endmacro %}
267 307
268 308
269 {##############################################################################} 309 {##############################################################################}
270 {% macro overload_resolution_method(overloads, world_suffix) %} 310 {% macro overload_resolution_method(overloads, world_suffix) %}
271 static void {{overloads.name}}Method{{world_suffix}}(const v8::FunctionCallbackI nfo<v8::Value>& info) 311 static void {{overloads.name}}Method{{world_suffix}}(const v8::FunctionCallbackI nfo<v8::Value>& info)
272 { 312 {
273 {% for method in overloads.methods %} 313 {% for method in overloads.methods %}
274 if ({{method.overload_resolution_expression}}) { 314 if ({{method.overload_resolution_expression}}) {
275 {{method.name}}{{method.overload_index}}Method{{world_suffix}}(info); 315 {{method.name}}{{method.overload_index}}Method{{world_suffix}}(info);
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 {% endfor %} 492 {% endfor %}
453 {{constructor.cpp_type}} impl = {{cpp_class}}::createForJSConstructor({{cons tructor.argument_list | join(', ')}}); 493 {{constructor.cpp_type}} impl = {{cpp_class}}::createForJSConstructor({{cons tructor.argument_list | join(', ')}});
454 {% if is_constructor_raises_exception %} 494 {% if is_constructor_raises_exception %}
455 if (exceptionState.throwIfNeeded()) 495 if (exceptionState.throwIfNeeded())
456 return; 496 return;
457 {% endif %} 497 {% endif %}
458 498
459 {{generate_constructor_wrapper(constructor) | indent}} 499 {{generate_constructor_wrapper(constructor) | indent}}
460 } 500 }
461 {% endmacro %} 501 {% endmacro %}
OLDNEW
« no previous file with comments | « Source/bindings/scripts/v8_types.py ('k') | Source/bindings/tests/idls/TestObject.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698