Chromium Code Reviews| 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 | |
| 10 | |
| 9 from systemfrog import * | 11 from systemfrog import * |
| 10 from systeminterface import * | 12 from systeminterface import * |
| 11 | 13 |
| 12 _html_strip_webkit_prefix_classes = [ | 14 _html_strip_webkit_prefix_classes = [ |
| 13 'Animation', | 15 'Animation', |
| 14 'AnimationEvent', | 16 'AnimationEvent', |
| 15 'AnimationList', | 17 'AnimationList', |
| 16 'BlobBuilder', | 18 'BlobBuilder', |
| 17 'CSSKeyframeRule', | 19 'CSSKeyframeRule', |
| 18 'CSSKeyframesRule', | 20 'CSSKeyframesRule', |
| (...skipping 749 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 768 class HtmlInterfacesSystem(HtmlSystem): | 770 class HtmlInterfacesSystem(HtmlSystem): |
| 769 | 771 |
| 770 def __init__(self, templates, database, emitters, output_dir, backend): | 772 def __init__(self, templates, database, emitters, output_dir, backend): |
| 771 super(HtmlInterfacesSystem, self).__init__( | 773 super(HtmlInterfacesSystem, self).__init__( |
| 772 templates, database, emitters, output_dir) | 774 templates, database, emitters, output_dir) |
| 773 self._backend = backend | 775 self._backend = backend |
| 774 self._dart_interface_file_paths = [] | 776 self._dart_interface_file_paths = [] |
| 775 self._factory_provider_emitters = {} | 777 self._factory_provider_emitters = {} |
| 776 | 778 |
| 777 def ProcessInterface(self, interface): | 779 def ProcessInterface(self, interface): |
| 778 """.""" | 780 """Generates dart interface and implementation for the DOM IDL interface.""" |
| 779 | 781 HtmlDartInterfaceGenerator(self, interface).Generate() |
| 780 self._backend.ProcessInterface(interface) | |
| 781 | |
| 782 if interface.id in _merged_html_interfaces: | |
| 783 return | |
| 784 | |
| 785 html_interface_name = self._shared._HTMLInterfaceName(interface.id) | |
| 786 dart_interface_file_path = self._FilePathForDartInterface( | |
| 787 html_interface_name) | |
| 788 | |
| 789 self._dart_interface_file_paths.append(dart_interface_file_path) | |
| 790 | |
| 791 dart_interface_code = self._emitters.FileEmitter(dart_interface_file_path) | |
| 792 | |
| 793 template_file = 'interface_%s.darttemplate' % html_interface_name | |
| 794 template = self._templates.TryLoad(template_file) | |
| 795 if not template: | |
| 796 template = self._templates.Load('interface.darttemplate') | |
| 797 | |
| 798 generator = HtmlDartInterfaceGenerator( | |
| 799 self, interface, dart_interface_code, | |
| 800 template, self._shared) | |
| 801 generator.Generate() | |
| 802 | 782 |
| 803 def ProcessCallback(self, interface, info): | 783 def ProcessCallback(self, interface, info): |
| 804 """Generates a typedef for the callback interface.""" | 784 """Generates a typedef for the callback interface.""" |
| 805 interface_name = interface.id | 785 interface_name = interface.id |
| 806 file_path = self._FilePathForDartInterface(interface_name) | 786 file_path = self._FilePathForDartInterface(interface_name) |
| 807 self._ProcessCallback(interface, info, file_path) | 787 self._ProcessCallback(interface, info, file_path) |
| 808 self._backend.ProcessCallback(interface, info) | 788 self._backend.ProcessCallback(interface, info) |
| 809 | 789 |
| 810 def GenerateLibraries(self): | 790 def GenerateLibraries(self): |
| 811 self._backend.GenerateLibraries(self._dart_interface_file_paths) | 791 self._backend.GenerateLibraries(self._dart_interface_file_paths) |
| 812 | 792 |
| 813 def _FilePathForDartInterface(self, interface_name): | 793 def _FilePathForDartInterface(self, interface_name): |
|
Anton Muhin
2012/06/28 13:20:19
does it still belong to HtmlInterfaceSystem, shoul
podivilov
2012/06/29 12:59:28
Dart interfaces are common in dartium and dart2js,
| |
| 814 """Returns the file path of the Dart interface definition.""" | 794 """Returns the file path of the Dart interface definition.""" |
| 815 # TODO(jmesserly): is this the right path | 795 # TODO(jmesserly): is this the right path |
| 816 return os.path.join(self._output_dir, 'html', 'interface', | 796 return os.path.join(self._output_dir, 'html', 'interface', |
| 817 '%s.dart' % interface_name) | 797 '%s.dart' % interface_name) |
| 818 | 798 |
| 819 # ------------------------------------------------------------------------------ | 799 # ------------------------------------------------------------------------------ |
| 820 | 800 |
| 821 # TODO(jmesserly): inheritance is probably not the right way to factor this long | 801 class HtmlDartInterfaceGenerator(BaseGenerator): |
| 822 # term, but it makes merging better for now. | 802 """Generates dart interface and implementation for the DOM IDL interface.""" |
| 823 class HtmlDartInterfaceGenerator(DartInterfaceGenerator): | |
| 824 """Generates Dart Interface definition for one DOM IDL interface.""" | |
| 825 | 803 |
| 826 def __init__(self, system, interface, emitter, template, shared): | 804 def __init__(self, system, interface): |
| 827 super(HtmlDartInterfaceGenerator, self).__init__(system, interface, | 805 super(HtmlDartInterfaceGenerator, self).__init__( |
| 828 emitter, template) | 806 system._database, interface) |
| 829 self._shared = shared | 807 self._system = system |
|
Anton Muhin
2012/06/28 13:20:19
do you need to store system here too (it's passed
podivilov
2012/06/29 12:59:28
No, it's not passed.
Anton Muhin
2012/06/29 13:34:57
Maybe you should pass it, but I am not insisting o
| |
| 808 self._backend = system._backend | |
| 809 self._emitters = system._emitters | |
| 810 self._shared = system._shared | |
| 811 self._templates = system._templates | |
|
Anton Muhin
2012/06/28 13:20:19
why are you caching _backend, _emitters, _shared a
podivilov
2012/06/29 12:59:28
Done.
| |
| 830 self._html_interface_name = self._shared._HTMLInterfaceName( | 812 self._html_interface_name = self._shared._HTMLInterfaceName( |
| 831 self._interface.id) | 813 self._interface.id) |
| 832 | 814 |
| 815 interface_path = self._system._FilePathForDartInterface( | |
|
Anton Muhin
2012/06/28 13:20:19
it looks like interface_path is only used in then
podivilov
2012/06/29 12:59:28
Done.
| |
| 816 self._html_interface_name) | |
| 817 if not interface.id in _merged_html_interfaces: | |
| 818 self._system._dart_interface_file_paths.append(interface_path) | |
|
Anton Muhin
2012/06/28 13:20:19
again, why you need _system here, or are you going
podivilov
2012/06/29 12:59:28
_dart_interface_file_paths is per-library state, l
| |
| 819 self._interface_emitter = self._emitters.FileEmitter(interface_path) | |
| 820 else: | |
| 821 self._interface_emitter = emitter.Emitter() | |
| 822 | |
| 823 template_file = 'interface_%s.darttemplate' % self._html_interface_name | |
| 824 self._interface_template = (self._templates.TryLoad(template_file) or | |
| 825 self._templates.Load('interface.darttemplate')) | |
| 826 | |
| 827 implementation_path = self._backend.FilePathForDartImplementation( | |
|
Anton Muhin
2012/06/28 13:20:19
ditto for implementation_path and if below.
podivilov
2012/06/29 12:59:28
Done.
| |
| 828 self._interface.id) | |
| 829 if self._backend.HasImplementation(self._interface.id): | |
| 830 self._system._dart_interface_file_paths.append(implementation_path) | |
|
Anton Muhin
2012/06/28 13:20:19
that looks slightly odd that you add implementatio
podivilov
2012/06/29 12:59:28
This is slightly complicated to refactor right now
| |
| 831 self._implementation_emitter = self._emitters.FileEmitter( | |
| 832 implementation_path) | |
| 833 else: | |
| 834 self._implementation_emitter = emitter.Emitter() | |
| 835 | |
| 833 def StartInterface(self): | 836 def StartInterface(self): |
| 837 if not IsPureInterface(self._interface.id): | |
|
Anton Muhin
2012/06/28 13:20:19
that might be stupid, but it feels more natural to
Anton Muhin
2012/06/28 13:20:19
should you call into backend if it's not pure or i
podivilov
2012/06/29 12:59:28
Done.
podivilov
2012/06/29 12:59:28
Done.
| |
| 838 self._backend.ProcessInterface( | |
| 839 self._interface, self._implementation_emitter) | |
| 840 | |
| 834 typename = self._html_interface_name | 841 typename = self._html_interface_name |
| 835 | 842 |
| 836 extends = [] | 843 extends = [] |
| 837 suppressed_extends = [] | 844 suppressed_extends = [] |
| 838 | 845 |
| 839 for parent in self._interface.parents: | 846 for parent in self._interface.parents: |
| 840 # TODO(vsm): Remove source_filter. | 847 # TODO(vsm): Remove source_filter. |
| 841 if MatchSourceFilter(parent): | 848 if MatchSourceFilter(parent): |
| 842 # Parent is a DOM type. | 849 # Parent is a DOM type. |
| 843 extends.append(self._shared.DartType(parent.type.id)) | 850 extends.append(self._shared.DartType(parent.type.id)) |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 874 assert factory_provider == info.factory_provider_name | 881 assert factory_provider == info.factory_provider_name |
| 875 else: | 882 else: |
| 876 factory_provider = info.factory_provider_name | 883 factory_provider = info.factory_provider_name |
| 877 | 884 |
| 878 if factory_provider: | 885 if factory_provider: |
| 879 extends_str += ' default ' + factory_provider | 886 extends_str += ' default ' + factory_provider |
| 880 | 887 |
| 881 # TODO(vsm): Add appropriate package / namespace syntax. | 888 # TODO(vsm): Add appropriate package / namespace syntax. |
| 882 (self._type_comment_emitter, | 889 (self._type_comment_emitter, |
| 883 self._members_emitter, | 890 self._members_emitter, |
| 884 self._top_level_emitter) = self._emitter.Emit( | 891 self._top_level_emitter) = self._interface_emitter.Emit( |
| 885 self._template + '$!TOP_LEVEL', | 892 self._interface_template + '$!TOP_LEVEL', |
| 886 ID=typename, | 893 ID=typename, |
| 887 EXTENDS=extends_str) | 894 EXTENDS=extends_str) |
| 888 | 895 |
| 889 self._type_comment_emitter.Emit("/// @domName $DOMNAME", | 896 self._type_comment_emitter.Emit("/// @domName $DOMNAME", |
| 890 DOMNAME=self._interface.doc_js_name) | 897 DOMNAME=self._interface.doc_js_name) |
| 891 | 898 |
| 892 for constructor_info in constructors: | 899 for constructor_info in constructors: |
| 893 self._members_emitter.Emit( | 900 self._members_emitter.Emit( |
| 894 '\n' | 901 '\n' |
| 895 ' $CTOR($PARAMS);\n', | 902 ' $CTOR($PARAMS);\n', |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 975 ' $TYPE $NAME($PARAMS);\n', | 982 ' $TYPE $NAME($PARAMS);\n', |
| 976 TYPE=self._shared.DartType(info.type_name), | 983 TYPE=self._shared.DartType(info.type_name), |
| 977 NAME=html_name, | 984 NAME=html_name, |
| 978 PARAMS=info.ParametersInterfaceDeclaration( | 985 PARAMS=info.ParametersInterfaceDeclaration( |
| 979 self._shared.DartType)) | 986 self._shared.DartType)) |
| 980 | 987 |
| 981 def FinishInterface(self): | 988 def FinishInterface(self): |
| 982 pass | 989 pass |
| 983 | 990 |
| 984 def AddConstant(self, constant): | 991 def AddConstant(self, constant): |
| 985 self._EmitConstant(self._members_emitter, constant) | 992 type = TypeOrNothing(DartType(constant.type.id), constant.type.id) |
| 993 self._members_emitter.Emit('\n static final $TYPE$NAME = $VALUE;\n', | |
| 994 NAME=constant.id, | |
| 995 TYPE=type, | |
| 996 VALUE=constant.value) | |
| 986 | 997 |
| 987 def AddEventAttributes(self, event_attrs): | 998 def AddEventAttributes(self, event_attrs): |
| 988 event_attrs = DomToHtmlEvents(self._html_interface_name, event_attrs) | 999 event_attrs = DomToHtmlEvents(self._html_interface_name, event_attrs) |
| 989 self._shared._event_classes.add(self._interface.id) | 1000 self._shared._event_classes.add(self._interface.id) |
| 990 events_interface = self._html_interface_name + 'Events' | 1001 events_interface = self._html_interface_name + 'Events' |
| 991 self._EmitEventGetter(events_interface) | 1002 self._EmitEventGetter(events_interface) |
| 992 | 1003 |
| 993 events_members = self._emitter.Emit( | 1004 events_members = self._interface_emitter.Emit( |
| 994 '\ninterface $INTERFACE extends $PARENTS {\n$!MEMBERS}\n', | 1005 '\ninterface $INTERFACE extends $PARENTS {\n$!MEMBERS}\n', |
| 995 INTERFACE=events_interface, | 1006 INTERFACE=events_interface, |
| 996 PARENTS=', '.join( | 1007 PARENTS=', '.join( |
| 997 self._shared.GetParentsEventsClasses(self._interface))) | 1008 self._shared.GetParentsEventsClasses(self._interface))) |
| 998 | 1009 |
| 999 for event_name in event_attrs: | 1010 for event_name in event_attrs: |
| 1000 if event_name in _html_event_names: | 1011 if event_name in _html_event_names: |
| 1001 events_members.Emit('\n EventListenerList get $NAME();\n', | 1012 events_members.Emit('\n EventListenerList get $NAME();\n', |
| 1002 NAME=_html_event_names[event_name]) | 1013 NAME=_html_event_names[event_name]) |
| 1003 else: | 1014 else: |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1344 # ------------------------------------------------------------------------------ | 1355 # ------------------------------------------------------------------------------ |
| 1345 | 1356 |
| 1346 class HtmlFrogSystem(HtmlSystem): | 1357 class HtmlFrogSystem(HtmlSystem): |
| 1347 | 1358 |
| 1348 def __init__(self, templates, database, emitters, output_dir): | 1359 def __init__(self, templates, database, emitters, output_dir): |
| 1349 super(HtmlFrogSystem, self).__init__( | 1360 super(HtmlFrogSystem, self).__init__( |
| 1350 templates, database, emitters, output_dir) | 1361 templates, database, emitters, output_dir) |
| 1351 self._dart_frog_file_paths = [] | 1362 self._dart_frog_file_paths = [] |
| 1352 self._factory_provider_emitters = {} | 1363 self._factory_provider_emitters = {} |
| 1353 | 1364 |
| 1354 def ProcessInterface(self, interface): | 1365 def ProcessInterface(self, interface, implementation_emitter): |
| 1355 """.""" | 1366 """.""" |
| 1356 if interface.id in _merged_html_interfaces: | |
| 1357 return None | |
| 1358 | |
| 1359 if IsPureInterface(interface.id): | |
| 1360 return | |
| 1361 | |
| 1362 html_interface_name = self._shared._HTMLInterfaceName(interface.id) | 1367 html_interface_name = self._shared._HTMLInterfaceName(interface.id) |
| 1363 template_file = 'impl_%s.darttemplate' % html_interface_name | 1368 template_file = 'impl_%s.darttemplate' % html_interface_name |
| 1364 template = self._templates.TryLoad(template_file) | 1369 template = self._templates.TryLoad(template_file) |
| 1365 if not template: | 1370 if not template: |
| 1366 template = self._templates.Load('frog_impl.darttemplate') | 1371 template = self._templates.Load('frog_impl.darttemplate') |
| 1367 | 1372 |
| 1368 dart_code = self._ImplFileEmitter(html_interface_name) | |
| 1369 generator = HtmlFrogClassGenerator(self, interface, template, | 1373 generator = HtmlFrogClassGenerator(self, interface, template, |
| 1370 dart_code, self._shared) | 1374 implementation_emitter, self._shared) |
| 1371 generator.Generate() | 1375 generator.Generate() |
| 1372 | 1376 |
| 1373 def GenerateLibraries(self, interface_files): | 1377 def GenerateLibraries(self, interface_files): |
| 1374 self._GenerateLibFile( | 1378 self._GenerateLibFile( |
| 1375 'html_frog.darttemplate', | 1379 'html_frog.darttemplate', |
| 1376 os.path.join(self._output_dir, 'html_frog.dart'), | 1380 os.path.join(self._output_dir, 'html_frog.dart'), |
| 1377 interface_files + self._dart_frog_file_paths) | 1381 interface_files + self._dart_frog_file_paths) |
| 1378 | 1382 |
| 1379 def Finish(self): | 1383 def Finish(self): |
| 1380 pass | 1384 pass |
| 1381 | 1385 |
| 1386 def HasImplementation(self, interface): | |
| 1387 return not (IsPureInterface(interface) or | |
| 1388 interface in _merged_html_interfaces) | |
| 1389 | |
| 1382 def _ImplFileEmitter(self, name): | 1390 def _ImplFileEmitter(self, name): |
| 1383 """Returns the file emitter of the Frog implementation file.""" | 1391 """Returns the file emitter of the Frog implementation file.""" |
| 1384 path = os.path.join(self._output_dir, 'html', 'frog', '%s.dart' % name) | 1392 path = self.FilePathForDartImplementation(name) |
| 1385 self._dart_frog_file_paths.append(path) | 1393 self._dart_frog_file_paths.append(path) |
| 1386 return self._emitters.FileEmitter(path) | 1394 return self._emitters.FileEmitter(path) |
| 1387 | 1395 |
| 1388 def _EmitterForFactoryProviderBody(self, name): | 1396 def _EmitterForFactoryProviderBody(self, name): |
| 1389 if name not in self._factory_provider_emitters: | 1397 if name not in self._factory_provider_emitters: |
| 1390 template = self._templates.Load('factoryprovider_%s.darttemplate' % name) | 1398 template = self._templates.Load('factoryprovider_%s.darttemplate' % name) |
| 1391 file_emitter = self._ImplFileEmitter(name) | 1399 file_emitter = self._ImplFileEmitter(name) |
| 1392 self._factory_provider_emitters[name] = file_emitter.Emit(template) | 1400 self._factory_provider_emitters[name] = file_emitter.Emit(template) |
| 1393 return self._factory_provider_emitters[name] | 1401 return self._factory_provider_emitters[name] |
| 1394 | 1402 |
| 1403 def FilePathForDartImplementation(self, name): | |
| 1404 name = self._shared._HTMLInterfaceName(name) | |
| 1405 return os.path.join(self._output_dir, 'html', 'frog', '%s.dart' % name) | |
| 1406 | |
| 1395 # ----------------------------------------------------------------------------- | 1407 # ----------------------------------------------------------------------------- |
| 1396 | 1408 |
| 1397 def _ComputeInheritanceClosure(database): | 1409 def _ComputeInheritanceClosure(database): |
| 1398 def Collect(interface, seen, collected): | 1410 def Collect(interface, seen, collected): |
| 1399 name = interface.id | 1411 name = interface.id |
| 1400 if '<' in name: | 1412 if '<' in name: |
| 1401 # TODO(sra): Handle parameterized types. | 1413 # TODO(sra): Handle parameterized types. |
| 1402 return | 1414 return |
| 1403 if not name in seen: | 1415 if not name in seen: |
| 1404 seen.add(name) | 1416 seen.add(name) |
| 1405 collected.append(name) | 1417 collected.append(name) |
| 1406 for parent in interface.parents: | 1418 for parent in interface.parents: |
| 1407 # TODO(sra): Handle parameterized types. | 1419 # TODO(sra): Handle parameterized types. |
| 1408 if not '<' in parent.type.id: | 1420 if not '<' in parent.type.id: |
| 1409 if database.HasInterface(parent.type.id): | 1421 if database.HasInterface(parent.type.id): |
| 1410 Collect(database.GetInterface(parent.type.id), | 1422 Collect(database.GetInterface(parent.type.id), |
| 1411 seen, collected) | 1423 seen, collected) |
| 1412 | 1424 |
| 1413 inheritance_closure = {} | 1425 inheritance_closure = {} |
| 1414 for interface in database.GetInterfaces(): | 1426 for interface in database.GetInterfaces(): |
| 1415 seen = set() | 1427 seen = set() |
| 1416 collected = [] | 1428 collected = [] |
| 1417 Collect(interface, seen, collected) | 1429 Collect(interface, seen, collected) |
| 1418 inheritance_closure[interface.id] = collected | 1430 inheritance_closure[interface.id] = collected |
| 1419 return inheritance_closure | 1431 return inheritance_closure |
| OLD | NEW |