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

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, 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 #}
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
159 '"The callback provided as parameter %s is not a function."' % 159 '"The callback provided as parameter %s is not a function."' %
160 (argument.index + 1)) | indent }} 160 (argument.index + 1)) | indent }}
161 return; 161 return;
162 } 162 }
163 {{argument.name}} = {% if argument.is_nullable %}info[{{argument.index}}]->IsNul l() ? nullptr : {% endif %}V8{{argument.idl_type}}::create(v8::Handle<v8::Functi on>::Cast(info[{{argument.index}}]), ScriptState::current(info.GetIsolate())); 163 {{argument.name}} = {% if argument.is_nullable %}info[{{argument.index}}]->IsNul l() ? nullptr : {% endif %}V8{{argument.idl_type}}::create(v8::Handle<v8::Functi on>::Cast(info[{{argument.index}}]), ScriptState::current(info.GetIsolate()));
164 {% endif %}{# argument.is_optional #} 164 {% endif %}{# argument.is_optional #}
165 {% endif %}{# argument.idl_type == 'EventListener' #} 165 {% endif %}{# argument.idl_type == 'EventListener' #}
166 {% elif argument.is_clamp %}{# argument.is_callback_interface #} 166 {% elif argument.is_clamp %}{# argument.is_callback_interface #}
167 {# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #} 167 {# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #}
168 double {{argument.name}}NativeValue; 168 double {{argument.name}}NativeValue;
169 {% if method.idl_type == 'Promise' %}
170 TONATIVE_VOID_ASYNC_INTERNAL({{argument.name}}NativeValue, info[{{argument.index }}]->NumberValue(), info);
171 {% else %}
169 TONATIVE_VOID_INTERNAL({{argument.name}}NativeValue, info[{{argument.index}}]->N umberValue()); 172 TONATIVE_VOID_INTERNAL({{argument.name}}NativeValue, info[{{argument.index}}]->N umberValue());
173 {% endif %}
170 if (!std::isnan({{argument.name}}NativeValue)) 174 if (!std::isnan({{argument.name}}NativeValue))
171 {# IDL type is used for clamping, for the right bounds, since different 175 {# IDL type is used for clamping, for the right bounds, since different
172 IDL integer types have same internal C++ type (int or unsigned) #} 176 IDL integer types have same internal C++ type (int or unsigned) #}
173 {{argument.name}} = clampTo<{{argument.idl_type}}>({{argument.name}}NativeVa lue); 177 {{argument.name}} = clampTo<{{argument.idl_type}}>({{argument.name}}NativeVa lue);
174 {% elif argument.idl_type == 'SerializedScriptValue' %} 178 {% elif argument.idl_type == 'SerializedScriptValue' %}
175 {{argument.name}} = SerializedScriptValue::create(info[{{argument.index}}], 0, 0 , exceptionState, info.GetIsolate()); 179 {{argument.name}} = SerializedScriptValue::create(info[{{argument.index}}], 0, 0 , exceptionState, info.GetIsolate());
176 if (exceptionState.hadException()) { 180 if (exceptionState.hadException()) {
177 {{throw_from_exception_state(method)}}; 181 {{throw_from_exception_state(method)}};
178 return; 182 return;
179 } 183 }
180 {% elif argument.is_variadic_wrapper_type %} 184 {% elif argument.is_variadic_wrapper_type %}
181 for (int i = {{argument.index}}; i < info.Length(); ++i) { 185 for (int i = {{argument.index}}; i < info.Length(); ++i) {
182 if (!V8{{argument.idl_type}}::hasInstance(info[i], info.GetIsolate())) { 186 if (!V8{{argument.idl_type}}::hasInstance(info[i], info.GetIsolate())) {
183 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' % 187 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' %
184 (argument.index + 1, argument.idl_type)) | in dent(8)}} 188 (argument.index + 1, argument.idl_type)) | in dent(8)}}
185 return; 189 return;
186 } 190 }
187 {{argument.name}}.append(V8{{argument.idl_type}}::toNative(v8::Handle<v8::Ob ject>::Cast(info[i]))); 191 {{argument.name}}.append(V8{{argument.idl_type}}::toNative(v8::Handle<v8::Ob ject>::Cast(info[i])));
188 } 192 }
189 {% else %}{# argument.is_nullable #} 193 {% else %}{# argument.is_nullable #}
194 {% if method.idl_type == 'Promise' %}
195 {{argument.v8_value_to_local_cpp_value_async}};
bashi 2014/07/31 02:02:24 Could you explain why we need v8_value_to_local_cp
yhirano 2014/07/31 03:38:03 Done.
196 {% else %}
190 {{argument.v8_value_to_local_cpp_value}}; 197 {{argument.v8_value_to_local_cpp_value}};
198 {% endif %}
191 {% endif %}{# argument.is_nullable #} 199 {% endif %}{# argument.is_nullable #}
192 {# Type checking, possibly throw a TypeError, per: 200 {# Type checking, possibly throw a TypeError, per:
193 http://www.w3.org/TR/WebIDL/#es-type-mapping #} 201 http://www.w3.org/TR/WebIDL/#es-type-mapping #}
194 {% if argument.has_type_checking_unrestricted %} 202 {% if argument.has_type_checking_unrestricted %}
195 {# Non-finite floating point values (NaN, +Infinity or −Infinity), per: 203 {# Non-finite floating point values (NaN, +Infinity or −Infinity), per:
196 http://heycam.github.io/webidl/#es-float 204 http://heycam.github.io/webidl/#es-float
197 http://heycam.github.io/webidl/#es-double #} 205 http://heycam.github.io/webidl/#es-double #}
198 if (!std::isfinite({{argument.name}})) { 206 if (!std::isfinite({{argument.name}})) {
199 {{throw_type_error(method, '"%s parameter %s is non-finite."' % 207 {{throw_type_error(method, '"%s parameter %s is non-finite."' %
200 (argument.idl_type, argument.index + 1)) | indent }} 208 (argument.idl_type, argument.index + 1)) | indent }}
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 v8SetReturnValueNull(info); 319 v8SetReturnValueNull(info);
312 {% endmacro %} 320 {% endmacro %}
313 321
314 322
315 {######################################} 323 {######################################}
316 {% macro throw_type_error(method, error_message) %} 324 {% macro throw_type_error(method, error_message) %}
317 {% if method.has_exception_state %} 325 {% if method.has_exception_state %}
318 exceptionState.throwTypeError({{error_message}}); 326 exceptionState.throwTypeError({{error_message}});
319 {{throw_from_exception_state(method)}}; 327 {{throw_from_exception_state(method)}};
320 {% elif method.is_constructor %} 328 {% elif method.is_constructor %}
329 {% if method.idl_type == 'Promise' %}
Jens Widell 2014/07/30 10:29:11 It would be really nice if we could come up with s
yhirano 2014/07/31 03:03:25 I would do that in a future CL. Added a FIXME.
330 v8SetReturnValue(info, ScriptPromise::rejectWithTypeError(ScriptState::current(i nfo.GetIsolate()), ExceptionMessages::failedToConstruct("{{interface_name}}", {{ error_message}})).v8Value());
331 {% else %}
321 throwTypeError(ExceptionMessages::failedToConstruct("{{interface_name}}", {{erro r_message}}), info.GetIsolate()); 332 throwTypeError(ExceptionMessages::failedToConstruct("{{interface_name}}", {{erro r_message}}), info.GetIsolate());
333 {% endif %}
322 {% else %}{# method.has_exception_state #} 334 {% else %}{# method.has_exception_state #}
335 {% if method.idl_type == 'Promise' %}
336 v8SetReturnValue(info, ScriptPromise::rejectWithTypeError(ScriptState::current(i nfo.GetIsolate()), ExceptionMessages::failedToExecute("{{method.name}}", "{{inte rface_name}}", {{error_message}})).v8Value());
337 {% else %}
323 throwTypeError(ExceptionMessages::failedToExecute("{{method.name}}", "{{interfac e_name}}", {{error_message}}), info.GetIsolate()); 338 throwTypeError(ExceptionMessages::failedToExecute("{{method.name}}", "{{interfac e_name}}", {{error_message}}), info.GetIsolate());
339 {% endif %}
324 {% endif %}{# method.has_exception_state #} 340 {% endif %}{# method.has_exception_state #}
325 {% endmacro %} 341 {% endmacro %}
326 342
327 343
328 {######################################} 344 {######################################}
329 {# FIXME: return a rejected Promise if method.idl_type == 'Promise' #}
330 {% macro throw_from_exception_state(method) %} 345 {% macro throw_from_exception_state(method) %}
346 {% if method.idl_type == 'Promise' %}
347 v8SetReturnValue(info, exceptionState.reject(ScriptState::current(info.GetIsolat e())).v8Value())
348 {%- else %}
331 exceptionState.throwIfNeeded() 349 exceptionState.throwIfNeeded()
350 {%- endif %}
332 {%- endmacro %} 351 {%- endmacro %}
333 352
334 353
335 {######################################} 354 {######################################}
336 {% macro throw_arity_type_error(method, valid_arities) %} 355 {% macro throw_arity_type_error(method, valid_arities) %}
356 {% if method.idl_type == 'Promise' %}
357 {% if method.has_exception_state %}
358 v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeError(ScriptState::curr ent(info.GetIsolate()), exceptionState, {{valid_arities}}, info.Length()).v8Valu e())
359 {%- elif method.is_constructor %}
360 v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeErrorForConstructor(Scr iptState::current(info.GetIsolate()), "{{interface_name}}", {{valid_arities}}, i nfo.Length()).v8Value())
361 {%- else %}
362 v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeErrorForMethod(ScriptSt ate::current(info.GetIsolate()), "{{method.name}}", "{{interface_name}}", {{vali d_arities}}, info.Length()).v8Value())
363 {%- endif %}
364 {%- else %}{# methods.idl_type == 'Promise' #}
337 {% if method.has_exception_state %} 365 {% if method.has_exception_state %}
338 throwArityTypeError(exceptionState, {{valid_arities}}, info.Length()) 366 throwArityTypeError(exceptionState, {{valid_arities}}, info.Length())
339 {%- elif method.is_constructor %} 367 {%- elif method.is_constructor %}
340 throwArityTypeErrorForConstructor("{{interface_name}}", {{valid_arities}}, info. Length(), info.GetIsolate()) 368 throwArityTypeErrorForConstructor("{{interface_name}}", {{valid_arities}}, info. Length(), info.GetIsolate())
341 {%- else %} 369 {%- else %}
342 throwArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{valid_ar ities}}, info.Length(), info.GetIsolate()) 370 throwArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{valid_ar ities}}, info.Length(), info.GetIsolate())
343 {%- endif %} 371 {%- endif %}
372 {%- endif %}{# methods.idl_type == 'Promise' #}
344 {% endmacro %} 373 {% endmacro %}
345 374
346 375
347 {######################################} 376 {######################################}
348 {% macro throw_minimum_arity_type_error(method, number_of_required_arguments) %} 377 {% macro throw_minimum_arity_type_error(method, number_of_required_arguments) %}
378 {% if method.idl_type == 'Promise' %}
379 {% if method.has_exception_state %}
380 v8SetReturnValue(info, ScriptPromise::rejectWithMinimumArityTypeError(ScriptStat e::current(info.GetIsolate()), exceptionState, {{number_of_required_arguments}}, info.Length()).v8Value())
381 {%- elif method.is_constructor %}
382 v8SetReturnValue(info, ScriptPromise::rejectWithMinimumArityTypeErrorForConstruc tor(ScriptState::current(info.GetIsolate()), "{{interface_name}}", {{number_of_r equired_arguments}}, info.Length()).v8Value())
383 {%- else %}
384 v8SetReturnValue(info, ScriptPromise::rejectWithMinimumArityTypeErrorForMethod(S criptState::current(info.GetIsolate()), "{{method.name}}", "{{interface_name}}", {{number_of_required_arguments}}, info.Length()).v8Value())
385 {%- endif %}
386 {%- else %}{# methods.idl_type == 'Promise' #}
349 {% if method.has_exception_state %} 387 {% if method.has_exception_state %}
350 throwMinimumArityTypeError(exceptionState, {{number_of_required_arguments}}, inf o.Length()) 388 throwMinimumArityTypeError(exceptionState, {{number_of_required_arguments}}, inf o.Length())
351 {%- elif method.is_constructor %} 389 {%- elif method.is_constructor %}
352 throwMinimumArityTypeErrorForConstructor("{{interface_name}}", {{number_of_requi red_arguments}}, info.Length(), info.GetIsolate()) 390 throwMinimumArityTypeErrorForConstructor("{{interface_name}}", {{number_of_requi red_arguments}}, info.Length(), info.GetIsolate())
353 {%- else %} 391 {%- else %}
354 throwMinimumArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{n umber_of_required_arguments}}, info.Length(), info.GetIsolate()) 392 throwMinimumArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{n umber_of_required_arguments}}, info.Length(), info.GetIsolate())
355 {%- endif %} 393 {%- endif %}
394 {%- endif %}{# methods.idl_type == 'Promise' #}
356 {% endmacro %} 395 {% endmacro %}
357 396
358 397
359 {##############################################################################} 398 {##############################################################################}
360 {% macro overload_resolution_method(overloads, world_suffix) %} 399 {% macro overload_resolution_method(overloads, world_suffix) %}
361 static void {{overloads.name}}Method{{world_suffix}}(const v8::FunctionCallbackI nfo<v8::Value>& info) 400 static void {{overloads.name}}Method{{world_suffix}}(const v8::FunctionCallbackI nfo<v8::Value>& info)
362 { 401 {
363 ExceptionState exceptionState(ExceptionState::ExecutionContext, "{{overloads .name}}", "{{interface_name}}", info.Holder(), info.GetIsolate()); 402 ExceptionState exceptionState(ExceptionState::ExecutionContext, "{{overloads .name}}", "{{interface_name}}", info.Holder(), info.GetIsolate());
364 {% if overloads.measure_all_as %} 403 {% if overloads.measure_all_as %}
365 UseCounter::count(callingExecutionContext(info.GetIsolate()), UseCounter::{{ overloads.measure_all_as}}); 404 UseCounter::count(callingExecutionContext(info.GetIsolate()), UseCounter::{{ overloads.measure_all_as}});
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 v8::Handle<v8::Object> wrapper = wrap(impl.get(), info.Holder(), info.GetIsolate ()); 631 v8::Handle<v8::Object> wrapper = wrap(impl.get(), info.Holder(), info.GetIsolate ());
593 {% else %} 632 {% else %}
594 {% set constructor_class = v8_class + ('Constructor' 633 {% set constructor_class = v8_class + ('Constructor'
595 if constructor.is_named_constructor else 634 if constructor.is_named_constructor else
596 '') %} 635 '') %}
597 v8::Handle<v8::Object> wrapper = info.Holder(); 636 v8::Handle<v8::Object> wrapper = info.Holder();
598 V8DOMWrapper::associateObjectWithWrapper<{{v8_class}}>(impl.release(), &{{constr uctor_class}}::wrapperTypeInfo, wrapper, info.GetIsolate(), {{wrapper_configurat ion}}); 637 V8DOMWrapper::associateObjectWithWrapper<{{v8_class}}>(impl.release(), &{{constr uctor_class}}::wrapperTypeInfo, wrapper, info.GetIsolate(), {{wrapper_configurat ion}});
599 {% endif %} 638 {% endif %}
600 v8SetReturnValue(info, wrapper); 639 v8SetReturnValue(info, wrapper);
601 {% endmacro %} 640 {% endmacro %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698