OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 """This module provides shared functionality for the system to generate | 6 """This module provides shared functionality for the system to generate |
7 dart:html APIs from the IDL database.""" | 7 dart:html APIs from the IDL database.""" |
8 | 8 |
9 import emitter | 9 import emitter |
10 from generator import AnalyzeOperation, ConstantOutputOrder, \ | 10 from generator import AnalyzeOperation, ConstantOutputOrder, \ |
(...skipping 18 matching lines...) Expand all Loading... |
29 'Location': 'LocationBase', | 29 'Location': 'LocationBase', |
30 'History': 'HistoryBase', | 30 'History': 'HistoryBase', |
31 } | 31 } |
32 | 32 |
33 _custom_factories = [ | 33 _custom_factories = [ |
34 'Notification', | 34 'Notification', |
35 'EventSource', | 35 'EventSource', |
36 ] | 36 ] |
37 | 37 |
38 class HtmlDartGenerator(object): | 38 class HtmlDartGenerator(object): |
39 def __init__(self, interface, options): | 39 def __init__(self, interface, options, dart_use_blink): |
| 40 # This goes away after the Chrome 35 roll (or whenever we commit to the |
| 41 # dart:blink refactor) |
| 42 self._dart_use_blink = dart_use_blink |
40 self._database = options.database | 43 self._database = options.database |
41 self._interface = interface | 44 self._interface = interface |
42 self._type_registry = options.type_registry | 45 self._type_registry = options.type_registry |
43 self._interface_type_info = self._type_registry.TypeInfo(self._interface.id) | 46 self._interface_type_info = self._type_registry.TypeInfo(self._interface.id) |
44 self._renamer = options.renamer | 47 self._renamer = options.renamer |
45 self._metadata = options.metadata | 48 self._metadata = options.metadata |
46 self._library_name = self._renamer.GetLibraryName(self._interface) | 49 self._library_name = self._renamer.GetLibraryName(self._interface) |
47 | 50 |
48 def EmitSupportCheck(self): | 51 def EmitSupportCheck(self): |
49 if self.HasSupportCheck(): | 52 if self.HasSupportCheck(): |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 else: | 311 else: |
309 self.EmitOperation(info, method_name) | 312 self.EmitOperation(info, method_name) |
310 | 313 |
311 def _GenerateOverloadDispatcher(self, | 314 def _GenerateOverloadDispatcher(self, |
312 info, | 315 info, |
313 signatures, | 316 signatures, |
314 is_void, | 317 is_void, |
315 declaration, | 318 declaration, |
316 generate_call, | 319 generate_call, |
317 is_optional, | 320 is_optional, |
| 321 emitter, |
318 can_omit_type_check=lambda type, pos: False): | 322 can_omit_type_check=lambda type, pos: False): |
319 | 323 |
320 parameter_names = [p.name for p in info.param_infos] | 324 parameter_names = [p.name for p in info.param_infos] |
321 number_of_required_in_dart = info.NumberOfRequiredInDart() | 325 number_of_required_in_dart = info.NumberOfRequiredInDart() |
322 | 326 |
323 body_emitter = self._members_emitter.Emit( | 327 body_emitter = emitter.Emit( |
324 '\n' | 328 '\n' |
325 ' $DECLARATION {\n' | 329 ' $DECLARATION {\n' |
326 '$!BODY' | 330 '$!BODY' |
327 ' }\n', | 331 ' }\n', |
328 DECLARATION=declaration) | 332 DECLARATION=declaration) |
329 | 333 |
330 version = [0] | 334 version = [0] |
331 def GenerateCall(signature_index, argument_count, checks): | 335 def GenerateCall(signature_index, argument_count, checks): |
332 if checks: | 336 if checks: |
333 (stmts_emitter, call_emitter) = body_emitter.Emit( | 337 (stmts_emitter, call_emitter) = body_emitter.Emit( |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 | 429 |
426 def GenerateCall( | 430 def GenerateCall( |
427 stmts_emitter, call_emitter, version, signature_index, argument_count): | 431 stmts_emitter, call_emitter, version, signature_index, argument_count): |
428 generate_call( | 432 generate_call( |
429 stmts_emitter, call_emitter, | 433 stmts_emitter, call_emitter, |
430 version, operations[signature_index], argument_count) | 434 version, operations[signature_index], argument_count) |
431 | 435 |
432 def IsOptional(signature_index, argument): | 436 def IsOptional(signature_index, argument): |
433 return is_optional(operations[signature_index], argument) | 437 return is_optional(operations[signature_index], argument) |
434 | 438 |
| 439 emitter = \ |
| 440 self._native_library_emitter if self._dart_use_blink \ |
| 441 else self._members_emitter |
| 442 |
435 self._GenerateOverloadDispatcher( | 443 self._GenerateOverloadDispatcher( |
436 info, | 444 info, |
437 [operation.arguments for operation in operations], | 445 [operation.arguments for operation in operations], |
438 operations[0].type.id == 'void', | 446 operations[0].type.id == 'void', |
439 declaration, | 447 declaration, |
440 GenerateCall, | 448 GenerateCall, |
441 IsOptional, | 449 IsOptional, |
| 450 emitter, |
442 can_omit_type_check) | 451 can_omit_type_check) |
443 | 452 |
444 def AdditionalImplementedInterfaces(self): | 453 def AdditionalImplementedInterfaces(self): |
445 # TODO: Include all implemented interfaces, including other Lists. | 454 # TODO: Include all implemented interfaces, including other Lists. |
446 implements = [] | 455 implements = [] |
447 if self._interface_type_info.list_item_type(): | 456 if self._interface_type_info.list_item_type(): |
448 item_type = self._type_registry.TypeInfo( | 457 item_type = self._type_registry.TypeInfo( |
449 self._interface_type_info.list_item_type()).dart_type() | 458 self._interface_type_info.list_item_type()).dart_type() |
450 implements.append('List<%s>' % item_type) | 459 implements.append('List<%s>' % item_type) |
451 return implements | 460 return implements |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 METADATA=metadata, | 531 METADATA=metadata, |
523 FACTORY=factory_name, | 532 FACTORY=factory_name, |
524 CTOR_FACTORY_NAME=factory_constructor_name, | 533 CTOR_FACTORY_NAME=factory_constructor_name, |
525 PARAMS=constructor_info.ParametersAsDeclaration(self._DartType), | 534 PARAMS=constructor_info.ParametersAsDeclaration(self._DartType), |
526 FACTORY_PARAMS=factory_parameters) | 535 FACTORY_PARAMS=factory_parameters) |
527 | 536 |
528 for index, param_info in enumerate(constructor_info.param_infos): | 537 for index, param_info in enumerate(constructor_info.param_infos): |
529 if param_info.is_optional: | 538 if param_info.is_optional: |
530 inits.Emit(' if ($E != null) e.$E = $E;\n', E=param_info.name) | 539 inits.Emit(' if ($E != null) e.$E = $E;\n', E=param_info.name) |
531 else: | 540 else: |
| 541 custom_factory_ctr = self._interface.id in _custom_factories |
| 542 constructor_full_name = constructor_info._ConstructorFullName( |
| 543 self._DartType) |
| 544 |
532 def GenerateCall( | 545 def GenerateCall( |
533 stmts_emitter, call_emitter, | 546 stmts_emitter, call_emitter, |
534 version, signature_index, argument_count): | 547 version, signature_index, argument_count): |
535 name = emitter.Format('_create_$VERSION', VERSION=version) | 548 name = emitter.Format('_create_$VERSION', VERSION=version) |
536 call_emitter.Emit('$FACTORY.$NAME($FACTORY_PARAMS)', | 549 if self._dart_use_blink: |
537 FACTORY=factory_name, | 550 qualified_name = \ |
538 NAME=name, | 551 "_".join(["Native",self._interface.id, |
| 552 name + 'constructorCallback']) |
| 553 else: |
| 554 qualified_name = emitter.Format( |
| 555 '$FACTORY.$NAME', |
| 556 FACTORY=factory_name, |
| 557 NAME=name) |
| 558 call_emitter.Emit('$FACTORY_NAME($FACTORY_PARAMS)', |
| 559 FACTORY_NAME=qualified_name, |
539 FACTORY_PARAMS= \ | 560 FACTORY_PARAMS= \ |
540 constructor_info.ParametersAsArgumentList(argument_count)) | 561 constructor_info.ParametersAsArgumentList(argument_count)) |
541 self.EmitStaticFactoryOverload( | 562 self.EmitStaticFactoryOverload( |
542 constructor_info, name, | 563 constructor_info, name, |
543 constructor_info.idl_args[signature_index][:argument_count]) | 564 constructor_info.idl_args[signature_index][:argument_count]) |
544 | 565 |
545 def IsOptional(signature_index, argument): | 566 def IsOptional(signature_index, argument): |
546 return self.IsConstructorArgumentOptional(argument) | 567 return self.IsConstructorArgumentOptional(argument) |
547 | 568 |
548 custom_factory_ctr = self._interface.id in _custom_factories | 569 entry_declaration = emitter.Format( |
549 constructor_full_name = constructor_info._ConstructorFullName( | 570 '$(METADATA)$FACTORY_KEYWORD $CTOR($PARAMS)', |
550 self._DartType) | 571 FACTORY_KEYWORD=('factory' if not custom_factory_ctr else |
| 572 'static %s' % constructor_full_name), |
| 573 CTOR=(('' if not custom_factory_ctr else '_factory') |
| 574 + constructor_full_name), |
| 575 METADATA=metadata, |
| 576 PARAMS=constructor_info.ParametersAsDeclaration(self._DartType)) |
| 577 |
| 578 if self._dart_use_blink: |
| 579 overload_emitter = self._native_library_emitter |
| 580 mname = constructor_full_name.replace(".", "_") |
| 581 blink_name = \ |
| 582 "_".join(["Native",self._interface.id, mname]) |
| 583 qual_name = self._native_library_name + "." + blink_name |
| 584 actuals_s = constructor_info.ParametersAsStringOfVariables() |
| 585 self._members_emitter.Emit( |
| 586 '\n' |
| 587 ' $DECLARATION => $NATIVE_NAME($ACTUALS);\n', |
| 588 DECLARATION=entry_declaration, |
| 589 NATIVE_NAME=qual_name, |
| 590 ACTUALS=actuals_s) |
| 591 overload_declaration = emitter.Format( |
| 592 '// Generated overload resolver\n' |
| 593 '$CTOR($PARAMS)', |
| 594 CTOR=blink_name, |
| 595 PARAMS=actuals_s) |
| 596 |
| 597 |
| 598 else: |
| 599 overload_emitter = self._members_emitter |
| 600 overload_declaration = entry_declaration |
| 601 |
551 self._GenerateOverloadDispatcher( | 602 self._GenerateOverloadDispatcher( |
552 constructor_info, | 603 constructor_info, |
553 constructor_info.idl_args, | 604 constructor_info.idl_args, |
554 False, | 605 False, |
555 emitter.Format('$(METADATA)$FACTORY_KEYWORD $CTOR($PARAMS)', | 606 overload_declaration, |
556 FACTORY_KEYWORD=('factory' if not custom_factory_ctr else | |
557 'static %s' % constructor_full_name), | |
558 CTOR=(('' if not custom_factory_ctr else '_factory') | |
559 + constructor_full_name), | |
560 METADATA=metadata, | |
561 PARAMS=constructor_info.ParametersAsDeclaration(self._DartType)), | |
562 GenerateCall, | 607 GenerateCall, |
563 IsOptional) | 608 IsOptional, |
| 609 overload_emitter) |
564 | 610 |
565 def _AddFutureifiedOperation(self, info, html_name): | 611 def _AddFutureifiedOperation(self, info, html_name): |
566 """Given a API function that uses callbacks, convert it to using Futures. | 612 """Given a API function that uses callbacks, convert it to using Futures. |
567 | 613 |
568 This conversion assumes the success callback is always provided before the | 614 This conversion assumes the success callback is always provided before the |
569 error callback (and so far in the DOM API, this is the case).""" | 615 error callback (and so far in the DOM API, this is the case).""" |
570 callback_info = GetCallbackInfo( | 616 callback_info = GetCallbackInfo( |
571 self._database.GetInterface(info.callback_args[0].type_id)) | 617 self._database.GetInterface(info.callback_args[0].type_id)) |
572 | 618 |
573 param_list = info.ParametersAsArgumentList() | 619 param_list = info.ParametersAsArgumentList() |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 | 752 |
707 def SecureBaseName(self, type_name): | 753 def SecureBaseName(self, type_name): |
708 if type_name in _secure_base_types: | 754 if type_name in _secure_base_types: |
709 return _secure_base_types[type_name] | 755 return _secure_base_types[type_name] |
710 | 756 |
711 def _DartType(self, type_name): | 757 def _DartType(self, type_name): |
712 return self._type_registry.DartType(type_name) | 758 return self._type_registry.DartType(type_name) |
713 | 759 |
714 def _TypeInfo(self, type_name): | 760 def _TypeInfo(self, type_name): |
715 return self._type_registry.TypeInfo(type_name) | 761 return self._type_registry.TypeInfo(type_name) |
OLD | NEW |