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

Side by Side Diff: sky/engine/bindings-dart/dart/scripts/templates/methods_cpp.template

Issue 918273002: Remove bindings-dart (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 10 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
(Empty)
1 {##############################################################################}
2 {# FIXME: We should return a rejected Promise if an error occurs in this
3 function when ALL methods in this overload return Promise. In order to do so,
4 we must ensure either ALL or NO methods in this overload return Promise #}
5 {% macro overload_resolution_method(method) %}
6 {% set overloads = method.overloads %}
7 {% if method.is_static %}
8 {% set offset = 0 %}
9 {% else %}
10 {% set offset = 1 %}
11 {% endif %}
12 static void {{static_method_name(overloads.name)}}Dispatcher(Dart_NativeArgument s args)
13 {
14 Dart_Handle exception = 0;
15 const int argOffset = {{offset}};
16 int argCount = Dart_GetNativeArgumentCount(args) - argOffset;
17
18 {# First resolve by length #}
19 {# 2. Initialize argcount to be min(maxarg, n). #}
20 switch (std::min({{overloads.maxarg}}, argCount)) {
21 {# 3. Remove from S all entries whose type list is not of length argcount. # }
22 {% for length, tests_methods in overloads.length_tests_methods %}
23 {# 10. If i = d, then: #}
24 case {{length}}:
25 {# Then resolve by testing argument #}
26 {% for test, method in tests_methods %}
27 {% filter runtime_enabled(not overloads.runtime_enabled_function_all and
28 method.runtime_enabled_function) %}
29 if ({{test}}) {
30 {% if method.is_custom %}
31 {{static_method_name(method.name)}}(args);
32 {% else %}
33 {{static_method_name(method.name, method.overload_index)}}(args);
34 {% endif %}
35 return;
36 }
37 {% endfilter %}
38 {% endfor %}
39 break;
40 {% endfor %}
41 default:
42 {# Invalid arity, throw error #}
43 {# Report full list of valid arities if gaps and above minimum #}
44 {% if overloads.valid_arities %}
45 if (argCount >= {{overloads.minarg}}) {
46 const String message = "Wrong arity, expected one of {{overloads.val id_arities}}";
47 exception = DartUtilities::coreArgumentErrorException(message);
48 goto fail;
49 }
50 {% endif %}
51 {# Otherwise just report "not enough arguments" #}
52 {
53 const String message = "Not enough arguments (at least {{overloads.m inarg}} required)";
54 exception = DartUtilities::coreArgumentErrorException(message);
55 goto fail;
56 }
57 return;
58 }
59 {
60 const String message = "No function was found that matched the signature provided.";
61 exception = DartUtilities::coreArgumentErrorException(message);
62 goto fail;
63 }
64 return;
65 fail:
66 Dart_ThrowException(exception);
67 ASSERT_NOT_REACHED();
68 }
69 {% endmacro %}
70
71
72 {##############################################################################}
73 {# arguments_count is normal method.number_of_arguments however for optional #}
74 {# arguments then number_of_required_arguments is passed (sans optional). #}
75 {# overload in the index if overloaded. #}
76 {# interface is specified signals that it's a constructor being called the #}
77 {# delegation to the create is emitted.
78 {##############################################################################}
79 {% macro generate_method(method, arguments_count) %}
80 {% set overload_index = method.overload_index %}
81 static void {{static_method_name(method.name, overload_index)}}(Dart_NativeArgum ents args)
82 {
83 {% if not method.is_static %}
84 {% if cpp_class == "EventTarget" %}
85 {{cpp_class}}* receiver = DartDOMWrapper::receiverToEventTarget(args);
86 {% else %}
87 {{cpp_class}}* /* FIXME(vsm): Remove this. */ ALLOW_UNUSED receiver = DartDO MWrapper::receiver< {{cpp_class}} >(args);
88 {% endif %}
89 {% endif %}
90 {% if method.is_custom_element_callbacks %}
91 CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
92 {% endif %}
93 {% if arguments_count > 0 or
94 method.has_exception_state or
95 method.is_call_with_script_state or
96 method.is_call_with_execution_context or
97 method.is_call_with_script_arguments %}
98 Dart_Handle exception = 0;
99 {% endif %}
100 {
101 {% if method.is_call_with_script_state %}
102 ScriptState* state = DartUtilities::currentScriptState();
103 if (!state) {
104 exception = Dart_NewStringFromCString("Failed to retrieve a script s tate");
105 goto fail;
106 }
107 {% endif %}
108 {% if method.is_call_with_execution_context %}
109 ExecutionContext* context = DartUtilities::scriptExecutionContext();
110 if (!context) {
111 exception = Dart_NewStringFromCString("Failed to retrieve a context" );
112 goto fail;
113 }
114 {% endif %}
115 {% if method.is_call_with_script_arguments %}
116 {# Last parameter is the customArgument #}
117 Dart_Handle customArgument = Dart_GetNativeArgument(args, Dart_GetNative ArgumentCount(args) - 1);
118 RefPtr<ScriptArguments> scriptArguments(DartUtilities::createScriptArgum ents(customArgument, exception));
119 if (!scriptArguments)
120 goto fail;
121 {% endif %}
122 {% if method.number_of_arguments != method.number_of_required_arguments %}
123 int argCount /* FIXME(vsm): Remove this. */ ALLOW_UNUSED = Dart_GetNativ eArgumentCount(args);
124 {% endif %}
125 {% if method.has_exception_state %}
126 DartExceptionState es;
127 {% endif %}
128 {{generate_arguments(method) | indent(8)}}
129 {{callback_return(method, method.dart_set_return_value, method.cpp_value )}}
130 {% if method.has_exception_state %}
131 if (es.hadException()) {
132 exception = es.toDart(args, {{method.auto_scope}});
133 goto fail;
134 }
135
136 {% endif %}
137 return;
138 }
139
140 {% if arguments_count > 0 or method.has_exception_state or method.is_call_with_s cript_state or method.is_call_with_execution_context or method.is_call_with_scri pt_arguments %}
141 fail:
142 Dart_ThrowException(exception);
143 ASSERT_NOT_REACHED();
144 {% endif %}
145 }
146 {% endmacro %}
147
148
149 {##############################################################################}
150 {% macro callback_return(method, dart_set_return_value, cpp_value) %}
151 {%- if method.union_arguments -%}
152 {{callback_union_return(method) | indent(8)}}
153 {%- elif method.idl_type == 'void' -%}
154 {{cpp_value}};
155 {%- elif method.is_constructor -%}
156 {{dart_class}}::returnToDart(args, {{cpp_value}}, {{method.auto_scope}});
157 {%- else -%}
158 {{dart_set_return_value}};
159 {%- endif -%}
160 {% endmacro %}
161
162
163 {##############################################################################}
164 {% macro callback_union_return(method, argument_count) %}
165 {% set type_index = 0 %}
166 {% for union_argument in method.union_arguments %}
167 {{method.cpp_type[type_index]}} {{union_argument}};
168 {% set type_index = type_index + 1 %}
169 {% endfor %}
170 {{method.cpp_value}};
171
172 {% set union_set_result_index = 0 %}
173 {% for union_argument in method.union_arguments %}
174 if ({{union_argument}}) {
175 {{method.dart_set_return_value[union_set_result_index]}};
176 return;
177 }
178 {% set union_set_result_index = union_set_result_index + 1 %}
179 {% endfor %}
180 {% endmacro %}
181
182
183 {##############################################################################}
184 {% macro generate_argument(method, argument) %}
185 {# If sequence result is passed as an argument not as function return value. #}
186 {% if argument.is_optional and
187 not argument.has_default and
188 argument.idl_type != 'Dictionary' and
189 not argument.is_callback_interface %}
190 {# Optional arguments without a default value generate an early call with
191 fewer arguments if they are omitted. #}
192 if (UNLIKELY(argCount <= {{argument.arg_index}})) {
193 {{callback_return(method, argument.dart_set_return_value, argument.cpp_value )}}
194 if (exception)
195 goto fail;
196 return;
197 }
198 {% endif %}
199 {% if argument.is_callback_interface %}
200 {# Callback functions must be functions:
201 http://www.w3.org/TR/WebIDL/#es-callback-function #}
202 {% if argument.is_optional %}
203 {{argument.local_cpp_type}} {{argument.name}};
204 if (argCount > {{argument.arg_index}}) {
205 {{argument.name}} = Dart{{argument.idl_type}}::createWithNullCheck(args, {{a rgument.arg_index}}, exception);
206 }
207 {% else %}{# argument.is_optional #}
208 {{argument.local_cpp_type}} {{argument.name}} = Dart{{argument.idl_type}}::creat eWithNullCheck(args, {{argument.arg_index}}, exception);
209 {% endif %}{# argument.is_optional #}
210 {% else %}{# argument.is_callback_interface #}
211 {% if argument.is_optional and argument.has_default -%}
212 {{argument.local_cpp_type}} {{argument.name}} =
213 (argCount <= {{argument.arg_index}}) ? ({{argument.default_value}}) : {{argu ment.dart_value_to_local_cpp_value}};
214 {% elif argument.is_optional and argument.idl_type == 'Dictionary' %}
215 {{argument.local_cpp_type}} {{argument.name}};
216 if (UNLIKELY(argCount <= {{argument.arg_index}})) {
217 {{argument.name}} = Dictionary();
218 } else {
219 {{argument.name}} = {{argument.dart_value_to_local_cpp_value}};
220 }
221 {% elif argument.is_array_or_sequence_type %}
222 {{argument.local_cpp_type}} {{argument.name}};
223 {{argument.dart_value_to_local_cpp_value}};
224 {% else %}
225 {{argument.local_cpp_type}} {{argument.name}} = {{argument.dart_value_to_local_c pp_value}};
226 {% endif %}
227 {% endif %}{# argument.is_callback_interface #}
228 if (exception)
229 goto fail;
230 {% endmacro %}
231
232
233 {######################################}
234 {% macro generate_arguments(method) %}
235 {%- for argument in method.arguments -%}
236 {{generate_argument(method, argument)}}
237 {%- endfor -%}
238 {% endmacro %}
239
240 {##############################################################################}
241 {% macro static_method_name(name, overload_index) %}
242 {% set name = 'constructor' if not name else name -%}
243 {% if overload_index -%}
244 {{name}}Callback_{{overload_index}}
245 {%- else -%}
246 {{name}}Callback
247 {%- endif %}
248 {% endmacro -%}
249
250
251 {##############################################################################}
252 {% macro generate_resolver_body(dart_class, class_name, method) %}
253 {% for native_entry_group in method.native_entries|groupby('resolver_string') %}
254 {% set uses_script_args = method.is_call_with_script_arguments %}
255 {% if method.overload_index %}
256 {% set method_name = static_method_name(method.name) + "Dispatcher" %}
257 {% else %}
258 {% set method_name = static_method_name(method.name) %}
259 {% endif %}
260 {% set resolver_string = native_entry_group.grouper %}
261 {% if method.is_custom %}
262 // FIXME: we are missing changes from dart.idl so we don't always know how many
263 // args custom methods will take so we ignore that check which could hurt perf
264 // and security but lets us get everything running quicker.
265 if (name == "{{resolver_string}}") {
266 *autoSetupScope = {{method.auto_scope}};
267 return {{dart_class}}Internal::{{method_name}};
268 }
269 {% else %}
270 {% set args_one_based = method.number_of_arguments %}
271 {% set args_required_one_based = method.number_of_required_arguments %}
272 {% if not method.is_static %}
273 {% set args_one_based = args_one_based + 1 %}
274 {% set args_required_one_based = args_required_one_based + 1 %}
275 {% endif %}
276
277 {% if uses_script_args %}
278 {# FIXME(vsm): At least one script argument is expected. Generalize? #}
279 {% set args_one_based = args_one_based + 1 %}
280 {% set args_required_one_based = args_required_one_based + 1 %}
281 {% endif %}
282
283 {% if args_one_based == args_required_one_based %}
284 if (argumentCount == {{args_one_based}} && name == "{{resolver_string}}") {
285 *autoSetupScope = {{method.auto_scope}};
286 return {{dart_class}}Internal::{{method_name}};
287 }
288 {% else %}
289 if (argumentCount >= {{args_required_one_based}} && argumentCount <= {{args_one_ based}} && name == "{{resolver_string}}") {
290 *autoSetupScope = {{method.auto_scope}};
291 return {{dart_class}}Internal::{{method_name}};
292 }
293 {% endif %}
294 {% endif %}
295 {% endfor %}
296 {% endmacro %}
297
298
299 {##############################################################################}
300 {% macro generate_symbolizer_body(dart_class, class_name, method) %}
301 {% for native_entry_group in method.native_entries|groupby('resolver_string') %}
302 {% set uses_script_args = method.is_call_with_script_arguments %}
303 {% if method.overload_index %}
304 {% set method_name = static_method_name(method.name) + "Dispatcher" %}
305 {% else %}
306 {% set method_name = static_method_name(method.name) %}
307 {% endif %}
308 {% set resolver_string = native_entry_group.grouper %}
309 if (nf == {{dart_class}}Internal::{{method_name}}) {
310 return reinterpret_cast<const uint8_t*>("{{resolver_string}}");
311 }
312 {% endfor %}
313 {% endmacro %}
314
315
316 {##############################################################################}
317 {% macro generate_constructor(constructor, arguments_count, overload='') %}
318 {% if overload == '' %}
319 {% set overload_index = constructor.overload_index %}
320 {% else %}
321 {% set overload_index = overload %}
322 {% endif %}
323 static void {{static_method_name(constructor.name, overload_index)}}(Dart_Native Arguments args)
324 {
325 {% if arguments_count > 0 or
326 constructor.has_exception_state or
327 is_constructor_call_with_execution_context or
328 is_constructor_call_with_document or
329 (constructor == named_constructor) %}
330 Dart_Handle exception = 0;
331 {% endif %}
332 {
333 {% if is_constructor_call_with_execution_context %}
334 ExecutionContext* context = DartUtilities::scriptExecutionContext();
335 if (!context) {
336 exception = Dart_NewStringFromCString("Failed to retrieve a context" );
337 goto fail;
338 }
339 {% endif %}
340 {% if is_constructor_call_with_document or (constructor == named_constructor) %}
341 LocalDOMWindow* domWindow = DartUtilities::domWindowForCurrentIsolate();
342 if (!domWindow) {
343 exception = Dart_NewStringFromCString("Failed to fetch domWindow");
344 goto fail;
345 }
346 Document& document = *domWindow->document();
347 {% endif %}
348 int argCount /* FIXME(vsm): Remove this. */ ALLOW_UNUSED = Dart_GetNativ eArgumentCount(args);
349 {% if constructor.has_exception_state %}
350 DartExceptionState es;
351 {% endif %}
352 {{generate_arguments(constructor) | indent(8)}}
353 {{callback_return(constructor, constructor.dart_set_return_value, constr uctor.cpp_value) | indent(8)}}
354 {% if constructor.has_exception_state %}
355 if (es.hadException()) {
356 exception = es.toDart(args, {{constructor.auto_scope}});
357 goto fail;
358 }
359
360 {% endif %}
361 return;
362 }
363
364 {% if arguments_count > 0 or constructor.has_exception_state or is_constructor_c all_with_execution_context or is_constructor_call_with_document or (constructor == named_constructor) %}
365 fail:
366 Dart_ThrowException(exception);
367 ASSERT_NOT_REACHED();
368 {% endif %}
369 }
370 {% endmacro %}
371
372
373 {##############################################################################}
374 {% macro generate_event_constructor() %}
375 template <typename T>
376 static void convert(const String& idlType, Dart_Handle handle, bool nullable, T& value, Dart_Handle& exception)
377 {
378 value = DartUtilities::TypeConvertor<T>()(handle, exception);
379 }
380
381 template <typename T>
382 static void convert(const String& idlType, Dart_Handle handle, bool nullable, Re fPtr<T>& value, Dart_Handle& exception)
383 {
384 if (Dart_IsNull(handle))
385 value = nullptr;
386 else
387 exception = Dart_NewStringFromCString("Event initialization unimplemente d for non-primitive types");
388 }
389
390 template <typename T>
391 static void convert(const String& idlType, Dart_Handle handle, bool nullable, Me mber<T>& value, Dart_Handle& exception)
392 {
393 if (Dart_IsNull(handle))
394 value = nullptr;
395 else
396 exception = Dart_NewStringFromCString("Event initialization unimplemente d for non-primitive types");
397 }
398
399 void initialize{{interface_name}}ForDart({{interface_name}}Init& eventInit, cons t String& type, const HashMap<String, Dart_Handle>& options, Dart_Handle& except ion)
400 {
401 {% if parent_interface %}
402 Dart{{parent_interface}}Internal::initialize{{parent_interface}}ForDart(even tInit, type, options, exception);
403 if (exception)
404 return;
405 {% endif %}
406
407 {% for attribute in attributes
408 if (attribute.is_initialized_by_event_constructor and
409 not attribute.idl_type == 'any')%}
410 {% set is_nullable = 'true' if attribute.is_nullable else 'false' %}
411 if (options.contains("{{attribute.name}}")) {
412 Dart_Handle option = options.get("{{attribute.name}}");
413 convert("{{attribute.idl_type}}", option, {{is_nullable}}, eventInit.{{a ttribute.cpp_name}}, exception);
414 if (exception)
415 return;
416 }
417 {% endfor %}
418 }
419
420 static void eventConstructorCallback(Dart_NativeArguments args)
421 {
422 Dart_Handle exception = 0;
423 {
424 DartStringAdapter type = DartUtilities::dartToString(args, 0, exception) ;
425 if (exception)
426 goto fail;
427
428 Dart_Handle handle = Dart_GetNativeArgument(args, 1);
429 HashMap<String, Dart_Handle> options;
430 DartUtilities::extractMapElements(handle, exception, options);
431 if (exception)
432 goto fail;
433
434 {{interface_name}}Init eventInit;
435 initialize{{interface_name}}ForDart(eventInit, type, options, exception) ;
436 if (exception)
437 goto fail;
438
439 {% if is_constructor_raises_exception %}
440 DartExceptionState es;
441 RefPtrWillBeRawPtr<{{interface_name}}> event = {{interface_name}}::creat e(type, eventInit, es);
442 if (es.hadException()) {
443 exception = es.toDart(args);
444 goto fail;
445 }
446 {% else %}
447 RefPtrWillBeRawPtr<{{interface_name}}> event = {{interface_name}}::creat e(type, eventInit);
448 {% endif %}
449 {{dart_class}}::returnToDart(args, event.get());
450 return;
451 }
452 fail:
453 Dart_ThrowException(exception);
454 ASSERT_NOT_REACHED();
455 }
456 {% endmacro %}
457
458 ,
459 {##############################################################################}
460 {% macro generate_resolver_constructor(dart_class, class_name, constructor) %}
461 {% for native_entry in constructor.native_entries %}
462 {% set resolver_string = native_entry.resolver_string %}
463 {% if constructor.overload_index %}
464 {% set constructor_name = static_method_name(constructor.name) + "Dispatcher" %}
465 {% else %}
466 {% set constructor_name = static_method_name(constructor.name) %}
467 {% endif %}
468 {% if has_custom_constructor %}
469 if (name == "{{resolver_string}}") {
470 {% elif constructor.number_of_arguments == constructor.number_of_required_argume nts %}
471 if (argumentCount == {{constructor.number_of_arguments}} && name == "{{resolver_ string}}") {
472 {% else %}
473 if (argumentCount >= {{constructor.number_of_required_arguments}} && argumentCou nt <= {{constructor.number_of_arguments}} && name == "{{resolver_string}}") {
474 {% endif %}
475 *autoSetupScope = {{constructor.auto_scope}};
476 return {{dart_class}}Internal::{{constructor_name}};
477 }
478 {% endfor %}
479 {% endmacro %}
480
481 {##############################################################################}
482 {% macro generate_resolver_event_constructor(dart_class, class_name) %}
483 {% set resolver_string = interface_name + "_constructorCallback" %}
484 if (argumentCount == 2 && name == "{{resolver_string}}") {
485 *autoSetupScope = 1;
486 return {{dart_class}}Internal::eventConstructorCallback;
487 }
488 {% endmacro %}
489
490 {##############################################################################}
491 {% macro generate_symbolizer_constructor(dart_class, class_name, constructor) %}
492 {% for native_entry in constructor.native_entries %}
493 {% set resolver_string = native_entry.resolver_string %}
494 if (nf == {{dart_class}}Internal::{{static_method_name(constructor.name, constru ctor.overload_index)}}) {
495 return reinterpret_cast<const uint8_t*>("{{resolver_string}}");
496 }
497 {% endfor %}
498 {% endmacro %}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698