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

Unified 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, 5 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 side-by-side diff with in-line comments
Download patch
Index: Source/bindings/templates/methods.cpp
diff --git a/Source/bindings/templates/methods.cpp b/Source/bindings/templates/methods.cpp
index 5033ad4fd1b99449b4b992572f92ba912b8ac755..90237085f487fca741f69846d7554ae666265446 100644
--- a/Source/bindings/templates/methods.cpp
+++ b/Source/bindings/templates/methods.cpp
@@ -166,7 +166,11 @@ if (info.Length() <= {{argument.index}} || !{% if argument.is_nullable %}(info[{
{% elif argument.is_clamp %}{# argument.is_callback_interface #}
{# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #}
double {{argument.name}}NativeValue;
+{% if method.idl_type == 'Promise' %}
+TONATIVE_VOID_ASYNC_INTERNAL({{argument.name}}NativeValue, info[{{argument.index}}]->NumberValue(), info);
+{% else %}
TONATIVE_VOID_INTERNAL({{argument.name}}NativeValue, info[{{argument.index}}]->NumberValue());
+{% endif %}
if (!std::isnan({{argument.name}}NativeValue))
{# IDL type is used for clamping, for the right bounds, since different
IDL integer types have same internal C++ type (int or unsigned) #}
@@ -187,7 +191,11 @@ for (int i = {{argument.index}}; i < info.Length(); ++i) {
{{argument.name}}.append(V8{{argument.idl_type}}::toNative(v8::Handle<v8::Object>::Cast(info[i])));
}
{% else %}{# argument.is_nullable #}
+{% if method.idl_type == 'Promise' %}
+{{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.
+{% else %}
{{argument.v8_value_to_local_cpp_value}};
+{% endif %}
{% endif %}{# argument.is_nullable #}
{# Type checking, possibly throw a TypeError, per:
http://www.w3.org/TR/WebIDL/#es-type-mapping #}
@@ -318,22 +326,42 @@ v8SetReturnValueNull(info);
exceptionState.throwTypeError({{error_message}});
{{throw_from_exception_state(method)}};
{% elif method.is_constructor %}
+{% 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.
+v8SetReturnValue(info, ScriptPromise::rejectWithTypeError(ScriptState::current(info.GetIsolate()), ExceptionMessages::failedToConstruct("{{interface_name}}", {{error_message}})).v8Value());
+{% else %}
throwTypeError(ExceptionMessages::failedToConstruct("{{interface_name}}", {{error_message}}), info.GetIsolate());
+{% endif %}
{% else %}{# method.has_exception_state #}
+{% if method.idl_type == 'Promise' %}
+v8SetReturnValue(info, ScriptPromise::rejectWithTypeError(ScriptState::current(info.GetIsolate()), ExceptionMessages::failedToExecute("{{method.name}}", "{{interface_name}}", {{error_message}})).v8Value());
+{% else %}
throwTypeError(ExceptionMessages::failedToExecute("{{method.name}}", "{{interface_name}}", {{error_message}}), info.GetIsolate());
+{% endif %}
{% endif %}{# method.has_exception_state #}
{% endmacro %}
{######################################}
-{# FIXME: return a rejected Promise if method.idl_type == 'Promise' #}
{% macro throw_from_exception_state(method) %}
+{% if method.idl_type == 'Promise' %}
+v8SetReturnValue(info, exceptionState.reject(ScriptState::current(info.GetIsolate())).v8Value())
+{%- else %}
exceptionState.throwIfNeeded()
+{%- endif %}
{%- endmacro %}
{######################################}
{% macro throw_arity_type_error(method, valid_arities) %}
+{% if method.idl_type == 'Promise' %}
+{% if method.has_exception_state %}
+v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeError(ScriptState::current(info.GetIsolate()), exceptionState, {{valid_arities}}, info.Length()).v8Value())
+{%- elif method.is_constructor %}
+v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeErrorForConstructor(ScriptState::current(info.GetIsolate()), "{{interface_name}}", {{valid_arities}}, info.Length()).v8Value())
+{%- else %}
+v8SetReturnValue(info, ScriptPromise::rejectWithArityTypeErrorForMethod(ScriptState::current(info.GetIsolate()), "{{method.name}}", "{{interface_name}}", {{valid_arities}}, info.Length()).v8Value())
+{%- endif %}
+{%- else %}{# methods.idl_type == 'Promise' #}
{% if method.has_exception_state %}
throwArityTypeError(exceptionState, {{valid_arities}}, info.Length())
{%- elif method.is_constructor %}
@@ -341,11 +369,21 @@ throwArityTypeErrorForConstructor("{{interface_name}}", {{valid_arities}}, info.
{%- else %}
throwArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{valid_arities}}, info.Length(), info.GetIsolate())
{%- endif %}
+{%- endif %}{# methods.idl_type == 'Promise' #}
{% endmacro %}
{######################################}
{% macro throw_minimum_arity_type_error(method, number_of_required_arguments) %}
+{% if method.idl_type == 'Promise' %}
+{% if method.has_exception_state %}
+v8SetReturnValue(info, ScriptPromise::rejectWithMinimumArityTypeError(ScriptState::current(info.GetIsolate()), exceptionState, {{number_of_required_arguments}}, info.Length()).v8Value())
+{%- elif method.is_constructor %}
+v8SetReturnValue(info, ScriptPromise::rejectWithMinimumArityTypeErrorForConstructor(ScriptState::current(info.GetIsolate()), "{{interface_name}}", {{number_of_required_arguments}}, info.Length()).v8Value())
+{%- else %}
+v8SetReturnValue(info, ScriptPromise::rejectWithMinimumArityTypeErrorForMethod(ScriptState::current(info.GetIsolate()), "{{method.name}}", "{{interface_name}}", {{number_of_required_arguments}}, info.Length()).v8Value())
+{%- endif %}
+{%- else %}{# methods.idl_type == 'Promise' #}
{% if method.has_exception_state %}
throwMinimumArityTypeError(exceptionState, {{number_of_required_arguments}}, info.Length())
{%- elif method.is_constructor %}
@@ -353,6 +391,7 @@ throwMinimumArityTypeErrorForConstructor("{{interface_name}}", {{number_of_requi
{%- else %}
throwMinimumArityTypeErrorForMethod("{{method.name}}", "{{interface_name}}", {{number_of_required_arguments}}, info.Length(), info.GetIsolate())
{%- endif %}
+{%- endif %}{# methods.idl_type == 'Promise' #}
{% endmacro %}

Powered by Google App Engine
This is Rietveld 408576698