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

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

Issue 460923002: Decouple arity errors creation from throwing. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 4 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 {% 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 {# Overloaded methods have length checked during overload resolution #} 10 {# Overloaded methods have length checked during overload resolution #}
11 {% if method.number_of_required_arguments and not method.overload_index %} 11 {% if method.number_of_required_arguments and not method.overload_index %}
12 if (UNLIKELY(info.Length() < {{method.number_of_required_arguments}})) { 12 if (UNLIKELY(info.Length() < {{method.number_of_required_arguments}})) {
13 {{throw_minimum_arity_type_error(method, method.number_of_required_argum ents)}}; 13 {{throw_minimum_arity_type_error(method, method.number_of_required_argum ents) | indent(8)}}
14 return; 14 return;
15 } 15 }
16 {% endif %} 16 {% endif %}
17 {% if not method.is_static %} 17 {% if not method.is_static %}
18 {{cpp_class}}* impl = {{v8_class}}::toNative(info.Holder()); 18 {{cpp_class}}* impl = {{v8_class}}::toNative(info.Holder());
19 {% endif %} 19 {% endif %}
20 {% if method.is_custom_element_callbacks %} 20 {% if method.is_custom_element_callbacks %}
21 CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope; 21 CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
22 {% endif %} 22 {% endif %}
23 {# Security checks #} 23 {# Security checks #}
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 {# Fall back to null if none of the union members results are returned #} 314 {# Fall back to null if none of the union members results are returned #}
315 v8SetReturnValueNull(info); 315 v8SetReturnValueNull(info);
316 {% endmacro %} 316 {% endmacro %}
317 317
318 318
319 {######################################} 319 {######################################}
320 {% macro throw_type_error(method, error_message) %} 320 {% macro throw_type_error(method, error_message) %}
321 {% if method.has_exception_state %} 321 {% if method.has_exception_state %}
322 exceptionState.throwTypeError({{error_message}}); 322 exceptionState.throwTypeError({{error_message}});
323 {{throw_from_exception_state(method)}}; 323 {{throw_from_exception_state(method)}};
324 {% elif method.is_constructor %} 324 {% elif method.idl_type == 'Promise' %}
325 {% if method.idl_type == 'Promise' %} 325 v8SetReturnValue(info, ScriptPromise::rejectRaw(info.GetIsolate(), V8ThrowExcept ion::createTypeError({{type_error_message(method, error_message)}}, info.GetIsol ate())));
326 {# FIXME: reduce code duplication between sync / async exception handling. #}
327 v8SetReturnValue(info, ScriptPromise::rejectWithTypeError(ScriptState::current(i nfo.GetIsolate()), ExceptionMessages::failedToConstruct("{{interface_name}}", {{ error_message}})).v8Value());
328 {% else %} 326 {% else %}
329 V8ThrowException::throwTypeError(ExceptionMessages::failedToConstruct("{{interfa ce_name}}", {{error_message}}), info.GetIsolate()); 327 V8ThrowException::throwTypeError({{type_error_message(method, error_message)}}, info.GetIsolate());
330 {% endif %}
331 {% else %}{# method.has_exception_state #}
332 {% if method.idl_type == 'Promise' %}
333 v8SetReturnValue(info, ScriptPromise::rejectWithTypeError(ScriptState::current(i nfo.GetIsolate()), ExceptionMessages::failedToExecute("{{method.name}}", "{{inte rface_name}}", {{error_message}})).v8Value());
334 {% else %}
335 V8ThrowException::throwTypeError(ExceptionMessages::failedToExecute("{{method.na me}}", "{{interface_name}}", {{error_message}}), info.GetIsolate());
336 {% endif %}
337 {% endif %}{# method.has_exception_state #} 328 {% endif %}{# method.has_exception_state #}
338 {% endmacro %} 329 {% endmacro %}
339 330
340 331
341 {######################################} 332 {######################################}
333 {% macro type_error_message(method, error_message) %}
334 {% if method.is_constructor %}
335 ExceptionMessages::failedToConstruct("{{interface_name}}", {{error_message}})
336 {%- else %}
337 ExceptionMessages::failedToExecute("{{method.name}}", "{{interface_name}}", {{er ror_message}})
338 {%- endif %}
339 {%- endmacro %}
340
341
342 {######################################}
342 {% macro throw_from_exception_state(method) %} 343 {% macro throw_from_exception_state(method) %}
343 {% if method.idl_type == 'Promise' %} 344 {% if method.idl_type == 'Promise' %}
344 v8SetReturnValue(info, exceptionState.reject(ScriptState::current(info.GetIsolat e())).v8Value()) 345 v8SetReturnValue(info, exceptionState.reject(ScriptState::current(info.GetIsolat e())).v8Value())
345 {%- else %} 346 {%- else %}
346 exceptionState.throwIfNeeded() 347 exceptionState.throwIfNeeded()
347 {%- endif %} 348 {%- endif %}
348 {%- endmacro %} 349 {%- endmacro %}
349 350
350 351
351 {######################################} 352 {######################################}
352 {% macro throw_arity_type_error(method, valid_arities) %} 353 {% macro throw_minimum_arity_type_error(method, number_of_required_arguments) %}
353 {% if method.idl_type == 'Promise' %}
354 {% if method.has_exception_state %} 354 {% if method.has_exception_state %}
355 v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeError(ScriptState::curr ent(info.GetIsolate()), exceptionState, {{valid_arities}}, info.Length()).v8Valu e()) 355 setMinimumArityTypeError(exceptionState, {{number_of_required_arguments}}, info. Length());
356 {%- elif method.is_constructor %} 356 {{throw_from_exception_state(method)}};
357 v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeErrorForConstructor(Scr iptState::current(info.GetIsolate()), "{{interface_name}}", {{valid_arities}}, i nfo.Length()).v8Value()) 357 {%- elif method.idl_type == 'Promise' %}
358 v8SetReturnValue(info, ScriptPromise::rejectRaw(info.GetIsolate(), {{create_mini mum_arity_type_error_without_exception_state(method, number_of_required_argument s)}}));
358 {%- else %} 359 {%- else %}
359 v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeErrorForMethod(ScriptSt ate::current(info.GetIsolate()), "{{method.name}}", "{{interface_name}}", {{vali d_arities}}, info.Length()).v8Value()) 360 V8ThrowException::throwException({{create_minimum_arity_type_error_without_excep tion_state(method, number_of_required_arguments)}}, info.GetIsolate());
360 {%- endif %} 361 {%- endif %}
361 {%- else %}{# methods.idl_type == 'Promise' #} 362 {%- endmacro %}
362 {% if method.has_exception_state %}
363 throwArityTypeError(exceptionState, {{valid_arities}}, info.Length())
364 {%- elif method.is_constructor %}
365 throwArityTypeErrorForConstructor("{{interface_name}}", {{valid_arities}}, info. Length(), info.GetIsolate())
366 {%- else %}
367 throwArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{valid_ar ities}}, info.Length(), info.GetIsolate())
368 {%- endif %}
369 {%- endif %}{# methods.idl_type == 'Promise' #}
370 {% endmacro %}
371 363
372 364
373 {######################################} 365 {######################################}
374 {% macro throw_minimum_arity_type_error(method, number_of_required_arguments) %} 366 {% macro create_minimum_arity_type_error_without_exception_state(method, number_ of_required_arguments) %}
375 {% if method.idl_type == 'Promise' %} 367 {% if method.is_constructor %}
376 {% if method.has_exception_state %} 368 createMinimumArityTypeErrorForConstructor("{{interface_name}}", {{number_of_requ ired_arguments}}, info.Length(), info.GetIsolate())
377 v8SetReturnValue(info, ScriptPromise::rejectWithMinimumArityTypeError(ScriptStat e::current(info.GetIsolate()), exceptionState, {{number_of_required_arguments}}, info.Length()).v8Value())
378 {%- elif method.is_constructor %}
379 v8SetReturnValue(info, ScriptPromise::rejectWithMinimumArityTypeErrorForConstruc tor(ScriptState::current(info.GetIsolate()), "{{interface_name}}", {{number_of_r equired_arguments}}, info.Length()).v8Value())
380 {%- else %} 369 {%- else %}
381 v8SetReturnValue(info, ScriptPromise::rejectWithMinimumArityTypeErrorForMethod(S criptState::current(info.GetIsolate()), "{{method.name}}", "{{interface_name}}", {{number_of_required_arguments}}, info.Length()).v8Value()) 370 createMinimumArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{ number_of_required_arguments}}, info.Length(), info.GetIsolate())
382 {%- endif %} 371 {%- endif %}
383 {%- else %}{# methods.idl_type == 'Promise' #} 372 {%- endmacro %}
384 {% if method.has_exception_state %}
385 throwMinimumArityTypeError(exceptionState, {{number_of_required_arguments}}, inf o.Length())
386 {%- elif method.is_constructor %}
387 throwMinimumArityTypeErrorForConstructor("{{interface_name}}", {{number_of_requi red_arguments}}, info.Length(), info.GetIsolate())
388 {%- else %}
389 throwMinimumArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{n umber_of_required_arguments}}, info.Length(), info.GetIsolate())
390 {%- endif %}
391 {%- endif %}{# methods.idl_type == 'Promise' #}
392 {% endmacro %}
393 373
394 374
395 {##############################################################################} 375 {##############################################################################}
376 {# FIXME: We should return a rejected Promise if an error occurs in this
377 function when ALL methods in this overload return Promise. In order to do so,
378 we must ensure either ALL or NO methods in this overload return Promise #}
396 {% macro overload_resolution_method(overloads, world_suffix) %} 379 {% macro overload_resolution_method(overloads, world_suffix) %}
397 static void {{overloads.name}}Method{{world_suffix}}(const v8::FunctionCallbackI nfo<v8::Value>& info) 380 static void {{overloads.name}}Method{{world_suffix}}(const v8::FunctionCallbackI nfo<v8::Value>& info)
398 { 381 {
399 ExceptionState exceptionState(ExceptionState::ExecutionContext, "{{overloads .name}}", "{{interface_name}}", info.Holder(), info.GetIsolate()); 382 ExceptionState exceptionState(ExceptionState::ExecutionContext, "{{overloads .name}}", "{{interface_name}}", info.Holder(), info.GetIsolate());
400 {% if overloads.measure_all_as %} 383 {% if overloads.measure_all_as %}
401 UseCounter::count(callingExecutionContext(info.GetIsolate()), UseCounter::{{ overloads.measure_all_as}}); 384 UseCounter::count(callingExecutionContext(info.GetIsolate()), UseCounter::{{ overloads.measure_all_as}});
402 {% endif %} 385 {% endif %}
403 {% if overloads.deprecate_all_as %} 386 {% if overloads.deprecate_all_as %}
404 UseCounter::countDeprecation(callingExecutionContext(info.GetIsolate()), Use Counter::{{overloads.deprecate_all_as}}); 387 UseCounter::countDeprecation(callingExecutionContext(info.GetIsolate()), Use Counter::{{overloads.deprecate_all_as}});
405 {% endif %} 388 {% endif %}
(...skipping 20 matching lines...) Expand all
426 } 409 }
427 {% endfilter %} 410 {% endfilter %}
428 {% endfor %} 411 {% endfor %}
429 break; 412 break;
430 {% endfor %} 413 {% endfor %}
431 default: 414 default:
432 {# Invalid arity, throw error #} 415 {# Invalid arity, throw error #}
433 {# Report full list of valid arities if gaps and above minimum #} 416 {# Report full list of valid arities if gaps and above minimum #}
434 {% if overloads.valid_arities %} 417 {% if overloads.valid_arities %}
435 if (info.Length() >= {{overloads.minarg}}) { 418 if (info.Length() >= {{overloads.minarg}}) {
436 throwArityTypeError(exceptionState, "{{overloads.valid_arities}}", i nfo.Length()); 419 setArityTypeError(exceptionState, "{{overloads.valid_arities}}", inf o.Length());
420 exceptionState.throwIfNeeded();
437 return; 421 return;
438 } 422 }
439 {% endif %} 423 {% endif %}
440 {# Otherwise just report "not enough arguments" #} 424 {# Otherwise just report "not enough arguments" #}
441 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments({{ov erloads.minarg}}, info.Length())); 425 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments({{ov erloads.minarg}}, info.Length()));
442 exceptionState.throwIfNeeded(); 426 exceptionState.throwIfNeeded();
443 return; 427 return;
444 } 428 }
445 {# No match, throw error #} 429 {# No match, throw error #}
446 exceptionState.throwTypeError("No function was found that matched the signat ure provided."); 430 exceptionState.throwTypeError("No function was found that matched the signat ure provided.");
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
607 v8SetReturnValue(info, info.Holder()); 591 v8SetReturnValue(info, info.Holder());
608 return; 592 return;
609 } 593 }
610 {% endif %} 594 {% endif %}
611 {% if constructor.has_exception_state %} 595 {% if constructor.has_exception_state %}
612 ExceptionState exceptionState(ExceptionState::ConstructionContext, "{{interf ace_name}}", info.Holder(), info.GetIsolate()); 596 ExceptionState exceptionState(ExceptionState::ConstructionContext, "{{interf ace_name}}", info.Holder(), info.GetIsolate());
613 {% endif %} 597 {% endif %}
614 {# Overloaded constructors have length checked during overload resolution #} 598 {# Overloaded constructors have length checked during overload resolution #}
615 {% if constructor.number_of_required_arguments and not constructor.overload_ index %} 599 {% if constructor.number_of_required_arguments and not constructor.overload_ index %}
616 if (UNLIKELY(info.Length() < {{constructor.number_of_required_arguments}})) { 600 if (UNLIKELY(info.Length() < {{constructor.number_of_required_arguments}})) {
617 {{throw_minimum_arity_type_error(constructor, constructor.number_of_requ ired_arguments)}}; 601 {{throw_minimum_arity_type_error(constructor, constructor.number_of_requ ired_arguments) | indent(8)}}
618 return; 602 return;
619 } 603 }
620 {% endif %} 604 {% endif %}
621 {% if constructor.arguments %} 605 {% if constructor.arguments %}
622 {{generate_arguments(constructor) | indent}} 606 {{generate_arguments(constructor) | indent}}
623 {% endif %} 607 {% endif %}
624 {{cpp_method_call(constructor, constructor.v8_set_return_value, constructor. cpp_value) | indent}} 608 {{cpp_method_call(constructor, constructor.v8_set_return_value, constructor. cpp_value) | indent}}
625 } 609 }
626 {% endmacro %} 610 {% endmacro %}
627 611
628 612
629 {##############################################################################} 613 {##############################################################################}
630 {% macro generate_constructor_wrapper(constructor) %} 614 {% macro generate_constructor_wrapper(constructor) %}
631 {% if has_custom_wrap %} 615 {% if has_custom_wrap %}
632 v8::Handle<v8::Object> wrapper = wrap(impl.get(), info.Holder(), info.GetIsolate ()); 616 v8::Handle<v8::Object> wrapper = wrap(impl.get(), info.Holder(), info.GetIsolate ());
633 {% else %} 617 {% else %}
634 {% set constructor_class = v8_class + ('Constructor' 618 {% set constructor_class = v8_class + ('Constructor'
635 if constructor.is_named_constructor else 619 if constructor.is_named_constructor else
636 '') %} 620 '') %}
637 v8::Handle<v8::Object> wrapper = info.Holder(); 621 v8::Handle<v8::Object> wrapper = info.Holder();
638 V8DOMWrapper::associateObjectWithWrapper<{{v8_class}}>(impl.release(), &{{constr uctor_class}}::wrapperTypeInfo, wrapper, info.GetIsolate(), {{wrapper_configurat ion}}); 622 V8DOMWrapper::associateObjectWithWrapper<{{v8_class}}>(impl.release(), &{{constr uctor_class}}::wrapperTypeInfo, wrapper, info.GetIsolate(), {{wrapper_configurat ion}});
639 {% endif %} 623 {% endif %}
640 v8SetReturnValue(info, wrapper); 624 v8SetReturnValue(info, wrapper);
641 {% endmacro %} 625 {% endmacro %}
OLDNEW
« no previous file with comments | « Source/bindings/modules/v8/custom/V8SubtleCryptoCustom.cpp ('k') | Source/bindings/tests/results/V8TestInterface.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698