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

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: added comment 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
« no previous file with comments | « Source/bindings/scripts/v8_types.py ('k') | Source/bindings/tests/results/V8TestInterface.cpp » ('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 {# Overloaded methods have length checked during overload resolution #} 10 {# Overloaded methods have length checked during overload resolution #}
(...skipping 27 matching lines...) Expand all
38 } 38 }
39 {% endif %} 39 {% endif %}
40 {% if method.is_check_security_for_node %} 40 {% if method.is_check_security_for_node %}
41 if (!BindingSecurity::shouldAllowAccessToNode(info.GetIsolate(), impl->{{met hod.name}}(exceptionState), exceptionState)) { 41 if (!BindingSecurity::shouldAllowAccessToNode(info.GetIsolate(), impl->{{met hod.name}}(exceptionState), exceptionState)) {
42 v8SetReturnValueNull(info); 42 v8SetReturnValueNull(info);
43 exceptionState.throwIfNeeded(); 43 exceptionState.throwIfNeeded();
44 return; 44 return;
45 } 45 }
46 {% endif %} 46 {% endif %}
47 {# Call method #} 47 {# Call method #}
48 {% for argument in method.arguments %} 48 {% if method.arguments %}
49 {{generate_argument(method, argument, world_suffix) | indent}} 49 {{generate_arguments(method, world_suffix) | indent}}
50 {% endfor %} 50 {% endif %}
51 {% if world_suffix %} 51 {% if world_suffix %}
52 {{cpp_method_call(method, method.v8_set_return_value_for_main_world, method. cpp_value) | indent}} 52 {{cpp_method_call(method, method.v8_set_return_value_for_main_world, method. cpp_value) | indent}}
53 {% else %} 53 {% else %}
54 {{cpp_method_call(method, method.v8_set_return_value, method.cpp_value) | in dent}} 54 {{cpp_method_call(method, method.v8_set_return_value, method.cpp_value) | in dent}}
55 {% endif %} 55 {% endif %}
56 {# Post-call #} 56 {# Post-call #}
57 {% if method.has_event_listener_argument %} 57 {% if method.has_event_listener_argument %}
58 {{hidden_dependency_action(method.name) | indent}} 58 {{hidden_dependency_action(method.name) | indent}}
59 {% endif %} 59 {% endif %}
60 } 60 }
61 {% endfilter %} 61 {% endfilter %}
62 {% endmacro %} 62 {% endmacro %}
63 63
64 64
65 {######################################} 65 {######################################}
66 {% macro hidden_dependency_action(method_name) %} 66 {% macro hidden_dependency_action(method_name) %}
67 if (listener && !impl->toNode()) 67 if (listener && !impl->toNode())
68 {% if method_name == 'addEventListener' %} 68 {% if method_name == 'addEventListener' %}
69 addHiddenValueToArray(info.Holder(), info[1], {{v8_class}}::eventListenerCac heIndex, info.GetIsolate()); 69 addHiddenValueToArray(info.Holder(), info[1], {{v8_class}}::eventListenerCac heIndex, info.GetIsolate());
70 {% else %}{# method_name == 'removeEventListener' #} 70 {% else %}{# method_name == 'removeEventListener' #}
71 removeHiddenValueFromArray(info.Holder(), info[1], {{v8_class}}::eventListen erCacheIndex, info.GetIsolate()); 71 removeHiddenValueFromArray(info.Holder(), info[1], {{v8_class}}::eventListen erCacheIndex, info.GetIsolate());
72 {% endif %} 72 {% endif %}
73 {% endmacro %} 73 {% endmacro %}
74 74
75 75
76 {######################################} 76 {######################################}
77 {% macro generate_arguments(method, world_suffix) %}
78 {% for argument in method.arguments %}
79 {{generate_argument_var_declaration(argument)}};
80 {% endfor %}
81 {
82 {% if method.arguments_need_try_catch %}
83 v8::TryCatch block;
84 {% endif %}
85 {% for argument in method.arguments %}
86 {{generate_argument(method, argument, world_suffix) | indent}}
87 {% endfor %}
88 }
89 {% endmacro %}
90
91
92 {######################################}
93 {% macro generate_argument_var_declaration(argument) %}
94 {% if argument.is_callback_interface %}
95 {# FIXME: remove EventListener special case #}
96 {% if argument.idl_type == 'EventListener' %}
97 RefPtr<{{argument.idl_type}}> {{argument.name}}
98 {%- else %}
99 OwnPtr<{{argument.idl_type}}> {{argument.name}}
100 {%- endif %}{# argument.idl_type == 'EventListener' #}
101 {%- elif argument.is_clamp %}{# argument.is_callback_interface #}
102 {# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #}
103 {{argument.cpp_type}} {{argument.name}} = 0
104 {%- else %}
105 {{argument.cpp_type}} {{argument.name}}
106 {%- endif %}
107 {% endmacro %}
108
109
110 {######################################}
77 {% macro generate_argument(method, argument, world_suffix) %} 111 {% macro generate_argument(method, argument, world_suffix) %}
78 {% if argument.is_optional and not argument.has_default and 112 {% if argument.is_optional and not argument.has_default and
79 argument.idl_type != 'Dictionary' and 113 argument.idl_type != 'Dictionary' and
80 not argument.is_callback_interface %} 114 not argument.is_callback_interface %}
81 {# Optional arguments without a default value generate an early call with 115 {# Optional arguments without a default value generate an early call with
82 fewer arguments if they are omitted. 116 fewer arguments if they are omitted.
83 Optional Dictionary arguments default to empty dictionary. #} 117 Optional Dictionary arguments default to empty dictionary. #}
84 if (UNLIKELY(info.Length() <= {{argument.index}})) { 118 if (UNLIKELY(info.Length() <= {{argument.index}})) {
85 {% if world_suffix %} 119 {% if world_suffix %}
86 {{cpp_method_call(method, argument.v8_set_return_value_for_main_world, argum ent.cpp_value) | indent}} 120 {{cpp_method_call(method, argument.v8_set_return_value_for_main_world, argum ent.cpp_value) | indent}}
87 {% else %} 121 {% else %}
88 {{cpp_method_call(method, argument.v8_set_return_value, argument.cpp_value) | indent}} 122 {{cpp_method_call(method, argument.v8_set_return_value, argument.cpp_value) | indent}}
89 {% endif %} 123 {% endif %}
90 {% if argument.has_event_listener_argument %} 124 {% if argument.has_event_listener_argument %}
91 {{hidden_dependency_action(method.name) | indent}} 125 {{hidden_dependency_action(method.name) | indent}}
92 {% endif %} 126 {% endif %}
93 return; 127 return;
94 } 128 }
95 {% endif %} 129 {% endif %}
96 {% if argument.has_type_checking_interface %} 130 {% if argument.has_type_checking_interface %}
97 {# Type checking for wrapper interface types (if interface not implemented, 131 {# Type checking for wrapper interface types (if interface not implemented,
98 throw a TypeError), per http://www.w3.org/TR/WebIDL/#es-interface #} 132 throw a TypeError), per http://www.w3.org/TR/WebIDL/#es-interface #}
99 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())) { 133 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())) {
100 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' % 134 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' %
101 (argument.index + 1, argument.idl_type)) | indent }} 135 (argument.index + 1, argument.idl_type)) | indent }}
102 return; 136 return;
103 } 137 }
104 {% endif %} 138 {% endif %}{# argument.has_type_checking_interface #}
105 {% if argument.is_callback_interface %} 139 {% if argument.is_callback_interface %}
106 {# FIXME: remove EventListener special case #} 140 {# FIXME: remove EventListener special case #}
107 {% if argument.idl_type == 'EventListener' %} 141 {% if argument.idl_type == 'EventListener' %}
108 {% if method.name == 'removeEventListener' %} 142 {% if method.name == 'removeEventListener' %}
109 RefPtr<{{argument.idl_type}}> {{argument.name}} = V8EventListenerList::getEventL istener(ScriptState::current(info.GetIsolate()), info[1], false, ListenerFindOnl y); 143 {{argument.name}} = V8EventListenerList::getEventListener(ScriptState::current(i nfo.GetIsolate()), info[1], false, ListenerFindOnly);
110 {% else %}{# method.name == 'addEventListener' #} 144 {% else %}{# method.name == 'addEventListener' #}
111 RefPtr<{{argument.idl_type}}> {{argument.name}} = V8EventListenerList::getEventL istener(ScriptState::current(info.GetIsolate()), info[1], false, ListenerFindOrC reate); 145 {{argument.name}} = V8EventListenerList::getEventListener(ScriptState::current(i nfo.GetIsolate()), info[1], false, ListenerFindOrCreate);
112 {% endif %}{# method.name #} 146 {% endif %}{# method.name #}
113 {% else %} 147 {% else %}{# argument.idl_type == 'EventListener' #}
114 {# Callback functions must be functions: 148 {# Callback functions must be functions:
115 http://www.w3.org/TR/WebIDL/#es-callback-function #} 149 http://www.w3.org/TR/WebIDL/#es-callback-function #}
116 {% if argument.is_optional %} 150 {% if argument.is_optional %}
117 OwnPtr<{{argument.idl_type}}> {{argument.name}};
118 if (info.Length() > {{argument.index}} && !isUndefinedOrNull(info[{{argument.ind ex}}])) { 151 if (info.Length() > {{argument.index}} && !isUndefinedOrNull(info[{{argument.ind ex}}])) {
119 if (!info[{{argument.index}}]->IsFunction()) { 152 if (!info[{{argument.index}}]->IsFunction()) {
120 {{throw_type_error(method, 153 {{throw_type_error(method,
121 '"The callback provided as parameter %s is not a function."' % 154 '"The callback provided as parameter %s is not a function."' %
122 (argument.index + 1)) | indent(8)}} 155 (argument.index + 1)) | indent(8)}}
123 return; 156 return;
124 } 157 }
125 {{argument.name}} = V8{{argument.idl_type}}::create(v8::Handle<v8::Function> ::Cast(info[{{argument.index}}]), ScriptState::current(info.GetIsolate())); 158 {{argument.name}} = V8{{argument.idl_type}}::create(v8::Handle<v8::Function> ::Cast(info[{{argument.index}}]), ScriptState::current(info.GetIsolate()));
126 } 159 }
127 {% else %} 160 {% else %}{# argument.is_optional #}
128 if (info.Length() <= {{argument.index}} || !{% if argument.is_nullable %}(info[{ {argument.index}}]->IsFunction() || info[{{argument.index}}]->IsNull()){% else % }info[{{argument.index}}]->IsFunction(){% endif %}) { 161 if (info.Length() <= {{argument.index}} || !{% if argument.is_nullable %}(info[{ {argument.index}}]->IsFunction() || info[{{argument.index}}]->IsNull()){% else % }info[{{argument.index}}]->IsFunction(){% endif %}) {
129 {{throw_type_error(method, 162 {{throw_type_error(method,
130 '"The callback provided as parameter %s is not a function."' % 163 '"The callback provided as parameter %s is not a function."' %
131 (argument.index + 1)) | indent }} 164 (argument.index + 1)) | indent }}
132 return; 165 return;
133 } 166 }
134 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}}]), ScriptState:: current(info.GetIsolate())); 167 {{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()));
135 {% endif %}{# argument.is_optional #} 168 {% endif %}{# argument.is_optional #}
136 {% endif %}{# argument.idl_type == 'EventListener' #} 169 {% endif %}{# argument.idl_type == 'EventListener' #}
137 {% elif argument.is_clamp %}{# argument.is_callback_interface #} 170 {% elif argument.is_clamp %}{# argument.is_callback_interface #}
138 {# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #} 171 {# NaN is treated as 0: http://www.w3.org/TR/WebIDL/#es-type-mapping #}
139 {{argument.cpp_type}} {{argument.name}} = 0; 172 double {{argument.name}}NativeValue;
140 TONATIVE_VOID(double, {{argument.name}}NativeValue, info[{{argument.index}}]->Nu mberValue()); 173 TONATIVE_VOID_INTERNAL({{argument.name}}NativeValue, info[{{argument.index}}]->N umberValue());
141 if (!std::isnan({{argument.name}}NativeValue)) 174 if (!std::isnan({{argument.name}}NativeValue))
142 {# 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
143 IDL integer types have same internal C++ type (int or unsigned) #} 176 IDL integer types have same internal C++ type (int or unsigned) #}
144 {{argument.name}} = clampTo<{{argument.idl_type}}>({{argument.name}}NativeVa lue); 177 {{argument.name}} = clampTo<{{argument.idl_type}}>({{argument.name}}NativeVa lue);
145 {% elif argument.idl_type == 'SerializedScriptValue' %} 178 {% elif argument.idl_type == 'SerializedScriptValue' %}
146 {{argument.cpp_type}} {{argument.name}} = SerializedScriptValue::create(info[{{a rgument.index}}], 0, 0, exceptionState, info.GetIsolate()); 179 {{argument.name}} = SerializedScriptValue::create(info[{{argument.index}}], 0, 0 , exceptionState, info.GetIsolate());
147 if (exceptionState.throwIfNeeded()) 180 if (exceptionState.throwIfNeeded())
148 return; 181 return;
149 {% elif argument.is_variadic_wrapper_type %} 182 {% elif argument.is_variadic_wrapper_type %}
150 {{argument.vector_type}}<{{argument.cpp_type}} > {{argument.name}};
151 for (int i = {{argument.index}}; i < info.Length(); ++i) { 183 for (int i = {{argument.index}}; i < info.Length(); ++i) {
152 if (!V8{{argument.idl_type}}::hasInstance(info[i], info.GetIsolate())) { 184 if (!V8{{argument.idl_type}}::hasInstance(info[i], info.GetIsolate())) {
153 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' % 185 {{throw_type_error(method, '"parameter %s is not of type \'%s\'."' %
154 (argument.index + 1, argument.idl_type)) | in dent(8)}} 186 (argument.index + 1, argument.idl_type)) | in dent(8)}}
155 return; 187 return;
156 } 188 }
157 {{argument.name}}.append(V8{{argument.idl_type}}::toNative(v8::Handle<v8::Ob ject>::Cast(info[i]))); 189 {{argument.name}}.append(V8{{argument.idl_type}}::toNative(v8::Handle<v8::Ob ject>::Cast(info[i])));
158 } 190 }
159 {% else %} 191 {% else %}
160 {{argument.v8_value_to_local_cpp_value}}; 192 {{argument.v8_value_to_local_cpp_value}};
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 {# Fall back to null if none of the union members results are returned #} 279 {# Fall back to null if none of the union members results are returned #}
248 v8SetReturnValueNull(info); 280 v8SetReturnValueNull(info);
249 {%- endmacro %} 281 {%- endmacro %}
250 282
251 283
252 {######################################} 284 {######################################}
253 {% macro throw_type_error(method, error_message) %} 285 {% macro throw_type_error(method, error_message) %}
254 {% if method.has_exception_state %} 286 {% if method.has_exception_state %}
255 exceptionState.throwTypeError({{error_message}}); 287 exceptionState.throwTypeError({{error_message}});
256 exceptionState.throwIfNeeded(); 288 exceptionState.throwIfNeeded();
257 {%- elif method.is_constructor %} 289 {% elif method.is_constructor %}
258 throwTypeError(ExceptionMessages::failedToConstruct("{{interface_name}}", {{erro r_message}}), info.GetIsolate()); 290 throwTypeError(ExceptionMessages::failedToConstruct("{{interface_name}}", {{erro r_message}}), info.GetIsolate());
259 {%- else %} 291 {% else %}
260 throwTypeError(ExceptionMessages::failedToExecute("{{method.name}}", "{{interfac e_name}}", {{error_message}}), info.GetIsolate()); 292 throwTypeError(ExceptionMessages::failedToExecute("{{method.name}}", "{{interfac e_name}}", {{error_message}}), info.GetIsolate());
293 {% endif %}
294 {% if method.arguments_need_try_catch %}
295 block.ReThrow();
261 {%- endif %} 296 {%- endif %}
262 {% endmacro %} 297 {% endmacro %}
263 298
264 299
265 {######################################} 300 {######################################}
266 {% macro throw_arity_type_error(method, valid_arities) %} 301 {% macro throw_arity_type_error(method, valid_arities) %}
267 {% if method.has_exception_state %} 302 {% if method.has_exception_state %}
268 throwArityTypeError(exceptionState, {{valid_arities}}, info.Length()) 303 throwArityTypeError(exceptionState, {{valid_arities}}, info.Length())
269 {%- elif method.is_constructor %} 304 {%- elif method.is_constructor %}
270 throwArityTypeErrorForConstructor("{{interface_name}}", {{valid_arities}}, info. Length(), info.GetIsolate()) 305 throwArityTypeErrorForConstructor("{{interface_name}}", {{valid_arities}}, info. Length(), info.GetIsolate())
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 ExceptionState exceptionState(ExceptionState::ConstructionContext, "{{interf ace_name}}", info.Holder(), isolate); 466 ExceptionState exceptionState(ExceptionState::ConstructionContext, "{{interf ace_name}}", info.Holder(), isolate);
432 {% endif %} 467 {% endif %}
433 {# Overloaded constructors have length checked during overload resolution #} 468 {# Overloaded constructors have length checked during overload resolution #}
434 {% if interface_length and not constructor.overload_index %} 469 {% if interface_length and not constructor.overload_index %}
435 {# FIXME: remove UNLIKELY: constructors are expensive, so no difference. #} 470 {# FIXME: remove UNLIKELY: constructors are expensive, so no difference. #}
436 if (UNLIKELY(info.Length() < {{interface_length}})) { 471 if (UNLIKELY(info.Length() < {{interface_length}})) {
437 {{throw_minimum_arity_type_error(constructor, interface_length)}}; 472 {{throw_minimum_arity_type_error(constructor, interface_length)}};
438 return; 473 return;
439 } 474 }
440 {% endif %} 475 {% endif %}
441 {% for argument in constructor.arguments %} 476 {% if constructor.arguments %}
442 {{generate_argument(constructor, argument) | indent}} 477 {{generate_arguments(constructor) | indent}}
443 {% endfor %} 478 {% endif %}
444 {% if is_constructor_call_with_execution_context %} 479 {% if is_constructor_call_with_execution_context %}
445 ExecutionContext* context = currentExecutionContext(isolate); 480 ExecutionContext* context = currentExecutionContext(isolate);
446 {% endif %} 481 {% endif %}
447 {% if is_constructor_call_with_document %} 482 {% if is_constructor_call_with_document %}
448 Document& document = *toDocument(currentExecutionContext(isolate)); 483 Document& document = *toDocument(currentExecutionContext(isolate));
449 {% endif %} 484 {% endif %}
450 {{constructor.cpp_type}} impl = {{cpp_class}}::create({{constructor.argument _list | join(', ')}}); 485 {{constructor.cpp_type}} impl = {{cpp_class}}::create({{constructor.argument _list | join(', ')}});
451 {% if is_constructor_raises_exception %} 486 {% if is_constructor_raises_exception %}
452 if (exceptionState.throwIfNeeded()) 487 if (exceptionState.throwIfNeeded())
453 return; 488 return;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 532
498 {% if constructor.has_exception_state %} 533 {% if constructor.has_exception_state %}
499 ExceptionState exceptionState(ExceptionState::ConstructionContext, "{{interf ace_name}}", info.Holder(), isolate); 534 ExceptionState exceptionState(ExceptionState::ConstructionContext, "{{interf ace_name}}", info.Holder(), isolate);
500 {% endif %} 535 {% endif %}
501 {% if constructor.number_of_required_arguments %} 536 {% if constructor.number_of_required_arguments %}
502 if (UNLIKELY(info.Length() < {{constructor.number_of_required_arguments}})) { 537 if (UNLIKELY(info.Length() < {{constructor.number_of_required_arguments}})) {
503 {{throw_minimum_arity_type_error(constructor, constructor.number_of_requ ired_arguments)}}; 538 {{throw_minimum_arity_type_error(constructor, constructor.number_of_requ ired_arguments)}};
504 return; 539 return;
505 } 540 }
506 {% endif %} 541 {% endif %}
507 {% for argument in constructor.arguments %} 542 {% if constructor.arguments %}
508 {{generate_argument(constructor, argument) | indent}} 543 {{generate_arguments(constructor) | indent}}
509 {% endfor %} 544 {% endif %}
510 {{constructor.cpp_type}} impl = {{cpp_class}}::createForJSConstructor({{cons tructor.argument_list | join(', ')}}); 545 {{constructor.cpp_type}} impl = {{cpp_class}}::createForJSConstructor({{cons tructor.argument_list | join(', ')}});
511 {% if is_constructor_raises_exception %} 546 {% if is_constructor_raises_exception %}
512 if (exceptionState.throwIfNeeded()) 547 if (exceptionState.throwIfNeeded())
513 return; 548 return;
514 {% endif %} 549 {% endif %}
515 550
516 {{generate_constructor_wrapper(constructor) | indent}} 551 {{generate_constructor_wrapper(constructor) | indent}}
517 } 552 }
518 {% endmacro %} 553 {% endmacro %}
OLDNEW
« no previous file with comments | « Source/bindings/scripts/v8_types.py ('k') | Source/bindings/tests/results/V8TestInterface.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698