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

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

Issue 265293004: Create fewer local v8::TryCatch objects in generated bindings code (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 7 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 {% if method.number_of_required_arguments %} 10 {% if method.number_of_required_arguments %}
(...skipping 26 matching lines...) Expand all
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 exceptionState.throwIfNeeded();
43 return; 43 return;
44 } 44 }
45 {% endif %} 45 {% endif %}
46 {# Call method #} 46 {# Call method #}
47 {% for argument in method.arguments %} 47 {% if method.arguments %}
48 {{generate_argument(method, argument, world_suffix) | indent}} 48 {{generate_arguments(method, world_suffix) | indent}}
49 {% endfor %} 49 {% endif %}
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 %}
53 {{cpp_method_call(method, method.v8_set_return_value, method.cpp_value) | in dent}} 53 {{cpp_method_call(method, method.v8_set_return_value, method.cpp_value) | in dent}}
54 {% endif %} 54 {% endif %}
55 {# Post-call #} 55 {# Post-call #}
56 {% if method.has_event_listener_argument %} 56 {% if method.has_event_listener_argument %}
57 {{hidden_dependency_action(method.name) | indent}} 57 {{hidden_dependency_action(method.name) | indent}}
58 {% endif %} 58 {% endif %}
59 } 59 }
60 {% endfilter %} 60 {% endfilter %}
61 {% endmacro %} 61 {% endmacro %}
62 62
63 63
64 {######################################} 64 {######################################}
65 {% macro hidden_dependency_action(method_name) %} 65 {% macro hidden_dependency_action(method_name) %}
66 if (listener && !impl->toNode()) 66 if (listener && !impl->toNode())
67 {% if method_name == 'addEventListener' %} 67 {% if method_name == 'addEventListener' %}
68 addHiddenValueToArray(info.Holder(), info[1], {{v8_class}}::eventListenerCac heIndex, info.GetIsolate()); 68 addHiddenValueToArray(info.Holder(), info[1], {{v8_class}}::eventListenerCac heIndex, info.GetIsolate());
69 {% else %}{# method_name == 'removeEventListener' #} 69 {% else %}{# method_name == 'removeEventListener' #}
70 removeHiddenValueFromArray(info.Holder(), info[1], {{v8_class}}::eventListen erCacheIndex, info.GetIsolate()); 70 removeHiddenValueFromArray(info.Holder(), info[1], {{v8_class}}::eventListen erCacheIndex, info.GetIsolate());
71 {% endif %} 71 {% endif %}
72 {% endmacro %} 72 {% endmacro %}
73 73
74 74
75 {######################################} 75 {######################################}
Nils Barth (inactive) 2014/05/07 03:02:19 Could you rename this as generate_argument_var_dec
76 {% macro generate_argument_var(argument) %}
Nils Barth (inactive) 2014/05/07 03:02:19 This is a one-line macro, so please put the ; at t
Jens Widell 2014/05/07 10:34:46 Thanks for the pointers! The white-space handling
Nils Barth (inactive) 2014/05/08 00:55:13 Jinja whitespace handling was the bane of my life
77 {% if argument.is_callback_interface %}
78 {# FIXME: remove EventListener special case #}
79 {% if argument.idl_type == 'EventListener' %}
80 RefPtr<{{argument.idl_type}}> {{argument.name}};
81 {% else %}
82 OwnPtr<{{argument.idl_type}}> {{argument.name}};
83 {% endif %}{# argument.idl_type == 'EventListener' #}
84 {% elif argument.is_clamp %}{# argument.is_callback_interface #}
85 {# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #}
86 {{argument.cpp_type}} {{argument.name}} = 0;
87 {% elif argument.is_variadic_wrapper_type %}
88 {{argument.vector_type}}<{{argument.cpp_type}} > {{argument.name}};
89 {% else %}
90 {{argument.cpp_type}} {{argument.name}};
91 {% endif %}
92 {% endmacro %}
93
94
95 {######################################}
76 {% macro generate_argument(method, argument, world_suffix) %} 96 {% macro generate_argument(method, argument, world_suffix) %}
77 {% if argument.is_optional and not argument.has_default and 97 {% if argument.is_optional and not argument.has_default and
78 argument.idl_type != 'Dictionary' and 98 argument.idl_type != 'Dictionary' and
79 not argument.is_callback_interface %} 99 not argument.is_callback_interface %}
80 {# Optional arguments without a default value generate an early call with 100 {# Optional arguments without a default value generate an early call with
81 fewer arguments if they are omitted. 101 fewer arguments if they are omitted.
82 Optional Dictionary arguments default to empty dictionary. #} 102 Optional Dictionary arguments default to empty dictionary. #}
83 if (UNLIKELY(info.Length() <= {{argument.index}})) { 103 if (UNLIKELY(info.Length() <= {{argument.index}})) {
84 {% if world_suffix %} 104 {% if world_suffix %}
85 {{cpp_method_call(method, argument.v8_set_return_value_for_main_world, argum ent.cpp_value) | indent}} 105 {{cpp_method_call(method, argument.v8_set_return_value_for_main_world, argum ent.cpp_value) | indent}}
86 {% else %} 106 {% else %}
87 {{cpp_method_call(method, argument.v8_set_return_value, argument.cpp_value) | indent}} 107 {{cpp_method_call(method, argument.v8_set_return_value, argument.cpp_value) | indent}}
88 {% endif %} 108 {% endif %}
89 {% if argument.has_event_listener_argument %} 109 {% if argument.has_event_listener_argument %}
90 {{hidden_dependency_action(method.name) | indent}} 110 {{hidden_dependency_action(method.name) | indent}}
91 {% endif %} 111 {% endif %}
92 return; 112 return;
93 } 113 }
94 {% endif %} 114 {% endif %}
95 {% if argument.has_type_checking_interface %} 115 {% if argument.has_type_checking_interface %}
96 {# Type checking for wrapper interface types (if interface not implemented, 116 {# Type checking for wrapper interface types (if interface not implemented,
97 throw a TypeError), per http://www.w3.org/TR/WebIDL/#es-interface #} 117 throw a TypeError), per http://www.w3.org/TR/WebIDL/#es-interface #}
98 if (info.Length() > {{argument.index}} && {% if argument.is_nullable %}!isUndefi nedOrNull(info[{{argument.index}}]) && {% endif %}!V8{{argument.idl_type}}::hasI nstance(info[{{argument.index}}], info.GetIsolate())) { 118 if (info.Length() > {{argument.index}} && {% if argument.is_nullable %}!isUndefi nedOrNull(info[{{argument.index}}]) && {% endif %}!V8{{argument.idl_type}}::hasI nstance(info[{{argument.index}}], info.GetIsolate())) {
99 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' % 119 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' %
100 (argument.index + 1, argument.idl_type)) | indent }} 120 (argument.index + 1, argument.idl_type)) | indent }}
101 return; 121 return;
102 } 122 }
103 {% endif %} 123 {% endif %}{# argument.has_type_checking_interface #}
104 {% if argument.is_callback_interface %} 124 {% if argument.is_callback_interface %}
105 {# FIXME: remove EventListener special case #} 125 {# FIXME: remove EventListener special case #}
106 {% if argument.idl_type == 'EventListener' %} 126 {% if argument.idl_type == 'EventListener' %}
107 {% if method.name == 'removeEventListener' %} 127 {% if method.name == 'removeEventListener' %}
108 RefPtr<{{argument.idl_type}}> {{argument.name}} = V8EventListenerList::getEventL istener(info[1], false, ListenerFindOnly); 128 {{argument.name}} = V8EventListenerList::getEventListener(info[1], false, Listen erFindOnly);
109 {% else %}{# method.name == 'addEventListener' #} 129 {% else %}{# method.name == 'addEventListener' #}
110 RefPtr<{{argument.idl_type}}> {{argument.name}} = V8EventListenerList::getEventL istener(info[1], false, ListenerFindOrCreate); 130 {{argument.name}} = V8EventListenerList::getEventListener(info[1], false, Listen erFindOrCreate);
111 {% endif %}{# method.name #} 131 {% endif %}{# method.name #}
112 {% else %} 132 {% else %}{# argument.idl_type == 'EventListener' #}
113 {# Callback functions must be functions: 133 {# Callback functions must be functions:
114 http://www.w3.org/TR/WebIDL/#es-callback-function #} 134 http://www.w3.org/TR/WebIDL/#es-callback-function #}
115 {% if argument.is_optional %} 135 {% if argument.is_optional %}
116 OwnPtr<{{argument.idl_type}}> {{argument.name}};
117 if (info.Length() > {{argument.index}} && !isUndefinedOrNull(info[{{argument.ind ex}}])) { 136 if (info.Length() > {{argument.index}} && !isUndefinedOrNull(info[{{argument.ind ex}}])) {
118 if (!info[{{argument.index}}]->IsFunction()) { 137 if (!info[{{argument.index}}]->IsFunction()) {
119 {{throw_type_error(method, 138 {{throw_type_error(method,
120 '"The callback provided as parameter %s is not a function."' % 139 '"The callback provided as parameter %s is not a function."' %
121 (argument.index + 1)) | indent(8)}} 140 (argument.index + 1)) | indent(8)}}
122 return; 141 return;
123 } 142 }
124 {{argument.name}} = V8{{argument.idl_type}}::create(v8::Handle<v8::Function> ::Cast(info[{{argument.index}}]), currentExecutionContext(info.GetIsolate())); 143 {{argument.name}} = V8{{argument.idl_type}}::create(v8::Handle<v8::Function> ::Cast(info[{{argument.index}}]), currentExecutionContext(info.GetIsolate()));
125 } 144 }
126 {% else %} 145 {% else %}{# argument.is_optional #}
127 if (info.Length() <= {{argument.index}} || !{% if argument.is_nullable %}(info[{ {argument.index}}]->IsFunction() || info[{{argument.index}}]->IsNull()){% else % }info[{{argument.index}}]->IsFunction(){% endif %}) { 146 if (info.Length() <= {{argument.index}} || !{% if argument.is_nullable %}(info[{ {argument.index}}]->IsFunction() || info[{{argument.index}}]->IsNull()){% else % }info[{{argument.index}}]->IsFunction(){% endif %}) {
128 {{throw_type_error(method, 147 {{throw_type_error(method,
129 '"The callback provided as parameter %s is not a function."' % 148 '"The callback provided as parameter %s is not a function."' %
130 (argument.index + 1)) | indent }} 149 (argument.index + 1)) | indent }}
131 return; 150 return;
132 } 151 }
133 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())); 152 {{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}}]), currentExecutionContext(info.GetIsolate())) ;
134 {% endif %}{# argument.is_optional #} 153 {% endif %}{# argument.is_optional #}
135 {% endif %}{# argument.idl_type == 'EventListener' #} 154 {% endif %}{# argument.idl_type == 'EventListener' #}
136 {% elif argument.is_clamp %}{# argument.is_callback_interface #} 155 {% elif argument.is_clamp %}{# argument.is_callback_interface #}
137 {# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #} 156 {# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #}
138 {{argument.cpp_type}} {{argument.name}} = 0; 157 double {{argument.name}}NativeValue;
139 TONATIVE_VOID(double, {{argument.name}}NativeValue, info[{{argument.index}}]->Nu mberValue()); 158 TONATIVE_VOID_NO_DECL({{argument.name}}NativeValue, info[{{argument.index}}]->Nu mberValue());
140 if (!std::isnan({{argument.name}}NativeValue)) 159 if (!std::isnan({{argument.name}}NativeValue))
141 {# IDL type is used for clamping, for the right bounds, since different 160 {# IDL type is used for clamping, for the right bounds, since different
142 IDL integer types have same internal C++ type (int or unsigned) #} 161 IDL integer types have same internal C++ type (int or unsigned) #}
143 {{argument.name}} = clampTo<{{argument.idl_type}}>({{argument.name}}NativeVa lue); 162 {{argument.name}} = clampTo<{{argument.idl_type}}>({{argument.name}}NativeVa lue);
144 {% elif argument.idl_type == 'SerializedScriptValue' %} 163 {% elif argument.idl_type == 'SerializedScriptValue' %}
145 {{argument.cpp_type}} {{argument.name}} = SerializedScriptValue::create(info[{{a rgument.index}}], 0, 0, exceptionState, info.GetIsolate()); 164 {{argument.name}} = SerializedScriptValue::create(info[{{argument.index}}], 0, 0 , exceptionState, info.GetIsolate());
146 if (exceptionState.throwIfNeeded()) 165 if (exceptionState.throwIfNeeded())
147 return; 166 return;
148 {% elif argument.is_variadic_wrapper_type %} 167 {% elif argument.is_variadic_wrapper_type %}
149 {{argument.vector_type}}<{{argument.cpp_type}} > {{argument.name}};
150 for (int i = {{argument.index}}; i < info.Length(); ++i) { 168 for (int i = {{argument.index}}; i < info.Length(); ++i) {
151 if (!V8{{argument.idl_type}}::hasInstance(info[i], info.GetIsolate())) { 169 if (!V8{{argument.idl_type}}::hasInstance(info[i], info.GetIsolate())) {
152 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' % 170 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' %
153 (argument.index + 1, argument.idl_type)) | in dent(8)}} 171 (argument.index + 1, argument.idl_type)) | in dent(8)}}
154 return; 172 return;
155 } 173 }
156 {{argument.name}}.append(V8{{argument.idl_type}}::toNative(v8::Handle<v8::Ob ject>::Cast(info[i]))); 174 {{argument.name}}.append(V8{{argument.idl_type}}::toNative(v8::Handle<v8::Ob ject>::Cast(info[i])));
157 } 175 }
158 {% else %} 176 {% else %}
159 {{argument.v8_value_to_local_cpp_value}}; 177 {{argument.v8_value_to_local_cpp_value}};
(...skipping 26 matching lines...) Expand all
186 if (!{{argument.name}}.isUndefinedOrNull() && !{{argument.name}}.isObject()) { 204 if (!{{argument.name}}.isUndefinedOrNull() && !{{argument.name}}.isObject()) {
187 {{throw_type_error(method, '"parameter %s (\'%s\') is not an object."' % 205 {{throw_type_error(method, '"parameter %s (\'%s\') is not an object."' %
188 (argument.index + 1, argument.name)) | indent}} 206 (argument.index + 1, argument.name)) | indent}}
189 return; 207 return;
190 } 208 }
191 {% endif %} 209 {% endif %}
192 {% endmacro %} 210 {% endmacro %}
193 211
194 212
195 {######################################} 213 {######################################}
214 {% macro generate_arguments(method, world_suffix) %}
Nils Barth (inactive) 2014/05/07 03:02:19 Could you move this *up* above the generate_argume
215 {% for argument in method.arguments %}
216 {{generate_argument_var(argument) | indent}}
Nils Barth (inactive) 2014/05/07 03:02:19 No |indent|, trailing ; here.
217 {% endfor %}
218 {
219 {% if method.arguments_need_try_catch %}
220 v8::TryCatch block;
221 {% endif %}
222 {% for argument in method.arguments %}
223 {{generate_argument(method, argument, world_suffix) | indent}}
224 {% endfor %}
225 }
226 {% endmacro %}
227
228
229 {######################################}
196 {% macro cpp_method_call(method, v8_set_return_value, cpp_value) %} 230 {% macro cpp_method_call(method, v8_set_return_value, cpp_value) %}
197 {# Local variables #} 231 {# Local variables #}
198 {% if method.is_partial_interface_member and not method.is_static %} 232 {% if method.is_partial_interface_member and not method.is_static %}
199 {# instance members (non-static members) in partial interface take |impl| #} 233 {# instance members (non-static members) in partial interface take |impl| #}
200 ASSERT(impl); 234 ASSERT(impl);
201 {% endif %} 235 {% endif %}
202 {% if method.is_call_with_script_state %} 236 {% if method.is_call_with_script_state %}
203 ScriptState* state = ScriptState::current(info.GetIsolate()); 237 ScriptState* state = ScriptState::current(info.GetIsolate());
204 {% endif %} 238 {% endif %}
205 {% if method.is_call_with_execution_context %} 239 {% if method.is_call_with_execution_context %}
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 {% if constructor.has_exception_state %} 434 {% if constructor.has_exception_state %}
401 ExceptionState exceptionState(ExceptionState::ConstructionContext, "{{interf ace_name}}", info.Holder(), isolate); 435 ExceptionState exceptionState(ExceptionState::ConstructionContext, "{{interf ace_name}}", info.Holder(), isolate);
402 {% endif %} 436 {% endif %}
403 {% if interface_length and not constructor.overload_index %} 437 {% if interface_length and not constructor.overload_index %}
404 {# FIXME: remove UNLIKELY: constructors are expensive, so no difference. #} 438 {# FIXME: remove UNLIKELY: constructors are expensive, so no difference. #}
405 if (UNLIKELY(info.Length() < {{interface_length}})) { 439 if (UNLIKELY(info.Length() < {{interface_length}})) {
406 {{throw_arity_type_error(constructor, interface_length)}}; 440 {{throw_arity_type_error(constructor, interface_length)}};
407 return; 441 return;
408 } 442 }
409 {% endif %} 443 {% endif %}
410 {% for argument in constructor.arguments %} 444 {% if constructor.arguments %}
Nils Barth (inactive) 2014/05/07 03:02:19 Could you move this {% if %} test into the generat
Jens Widell 2014/05/07 10:34:46 Would love to. One problem: I can't seem to figure
Nils Barth (inactive) 2014/05/08 00:55:13 Oh, good point (>.<) That's exactly the trickiest
411 {{generate_argument(constructor, argument) | indent}} 445 {{generate_arguments(constructor) | indent}}
412 {% endfor %} 446 {% endif %}
413 {% if is_constructor_call_with_execution_context %} 447 {% if is_constructor_call_with_execution_context %}
414 ExecutionContext* context = currentExecutionContext(isolate); 448 ExecutionContext* context = currentExecutionContext(isolate);
415 {% endif %} 449 {% endif %}
416 {% if is_constructor_call_with_document %} 450 {% if is_constructor_call_with_document %}
417 Document& document = *toDocument(currentExecutionContext(isolate)); 451 Document& document = *toDocument(currentExecutionContext(isolate));
418 {% endif %} 452 {% endif %}
419 {{constructor.cpp_type}} impl = {{cpp_class}}::create({{constructor.argument _list | join(', ')}}); 453 {{constructor.cpp_type}} impl = {{cpp_class}}::create({{constructor.argument _list | join(', ')}});
420 {% if is_constructor_raises_exception %} 454 {% if is_constructor_raises_exception %}
421 if (exceptionState.throwIfNeeded()) 455 if (exceptionState.throwIfNeeded())
422 return; 456 return;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 500
467 {% if constructor.has_exception_state %} 501 {% if constructor.has_exception_state %}
468 ExceptionState exceptionState(ExceptionState::ConstructionContext, "{{interf ace_name}}", info.Holder(), isolate); 502 ExceptionState exceptionState(ExceptionState::ConstructionContext, "{{interf ace_name}}", info.Holder(), isolate);
469 {% endif %} 503 {% endif %}
470 {% if constructor.number_of_required_arguments %} 504 {% if constructor.number_of_required_arguments %}
471 if (UNLIKELY(info.Length() < {{constructor.number_of_required_arguments}})) { 505 if (UNLIKELY(info.Length() < {{constructor.number_of_required_arguments}})) {
472 {{throw_arity_type_error(constructor, constructor.number_of_required_arg uments)}}; 506 {{throw_arity_type_error(constructor, constructor.number_of_required_arg uments)}};
473 return; 507 return;
474 } 508 }
475 {% endif %} 509 {% endif %}
476 {% for argument in constructor.arguments %} 510 {% if constructor.arguments %}
477 {{generate_argument(constructor, argument) | indent}} 511 {{generate_arguments(constructor) | indent}}
478 {% endfor %} 512 {% endif %}
479 {{constructor.cpp_type}} impl = {{cpp_class}}::createForJSConstructor({{cons tructor.argument_list | join(', ')}}); 513 {{constructor.cpp_type}} impl = {{cpp_class}}::createForJSConstructor({{cons tructor.argument_list | join(', ')}});
480 {% if is_constructor_raises_exception %} 514 {% if is_constructor_raises_exception %}
481 if (exceptionState.throwIfNeeded()) 515 if (exceptionState.throwIfNeeded())
482 return; 516 return;
483 {% endif %} 517 {% endif %}
484 518
485 {{generate_constructor_wrapper(constructor) | indent}} 519 {{generate_constructor_wrapper(constructor) | indent}}
486 } 520 }
487 {% endmacro %} 521 {% endmacro %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698