| 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 systems to generate | 6 """This module provides shared functionality for the systems to generate |
| 7 native binding from the IDL database.""" | 7 native binding from the IDL database.""" |
| 8 | 8 |
| 9 import emitter | 9 import emitter |
| 10 import os | 10 import os |
| 11 import systembase | 11 import systembase |
| 12 from generator import * | 12 from generator import * |
| 13 | 13 |
| 14 | 14 |
| 15 class NativeImplementationSystem(systembase.System): | 15 class NativeImplementationSystem(systembase.System): |
| 16 | 16 |
| 17 def __init__(self, options): | 17 def __init__(self, options, cpp_library_emitter): |
| 18 super(NativeImplementationSystem, self).__init__(options) | 18 super(NativeImplementationSystem, self).__init__(options) |
| 19 self._cpp_header_files = [] | 19 self._cpp_library_emitter = cpp_library_emitter |
| 20 self._cpp_impl_files = [] | |
| 21 | 20 |
| 22 def ImplementationGenerator(self, interface): | 21 def ImplementationGenerator(self, interface): |
| 23 return NativeImplementationGenerator(self, interface) | 22 return NativeImplementationGenerator(self, interface) |
| 24 | 23 |
| 25 def ProcessCallback(self, interface, info): | 24 def ProcessCallback(self, interface, info): |
| 26 self._interface = interface | 25 self._interface = interface |
| 27 | 26 |
| 28 if IsPureInterface(self._interface.id): | 27 if IsPureInterface(self._interface.id): |
| 29 return None | 28 return None |
| 30 | 29 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 ' DartIsolate::Scope scope(m_callback.isolate());\n' | 63 ' DartIsolate::Scope scope(m_callback.isolate());\n' |
| 65 ' DartApiScope apiScope;\n' | 64 ' DartApiScope apiScope;\n' |
| 66 ' $ARGUMENTS_DECLARATION;\n' | 65 ' $ARGUMENTS_DECLARATION;\n' |
| 67 ' return m_callback.handleEvent($ARGUMENT_COUNT, arguments);\n' | 66 ' return m_callback.handleEvent($ARGUMENT_COUNT, arguments);\n' |
| 68 '}\n', | 67 '}\n', |
| 69 CLASS_NAME=class_name, | 68 CLASS_NAME=class_name, |
| 70 PARAMETERS=', '.join(parameters), | 69 PARAMETERS=', '.join(parameters), |
| 71 ARGUMENTS_DECLARATION=arguments_declaration, | 70 ARGUMENTS_DECLARATION=arguments_declaration, |
| 72 ARGUMENT_COUNT=len(arguments)) | 71 ARGUMENT_COUNT=len(arguments)) |
| 73 | 72 |
| 74 cpp_header_path = self._FilePathForCppHeader(self._interface.id) | 73 cpp_header_emitter = self._cpp_library_emitter.CreateHeaderEmitter(self._int
erface.id, True) |
| 75 cpp_header_emitter = self._emitters.FileEmitter(cpp_header_path) | |
| 76 cpp_header_emitter.Emit( | 74 cpp_header_emitter.Emit( |
| 77 self._templates.Load('cpp_callback_header.template'), | 75 self._templates.Load('cpp_callback_header.template'), |
| 78 INTERFACE=self._interface.id, | 76 INTERFACE=self._interface.id, |
| 79 HANDLERS=cpp_header_handlers_emitter.Fragments()) | 77 HANDLERS=cpp_header_handlers_emitter.Fragments()) |
| 80 | 78 |
| 81 cpp_impl_path = self._FilePathForCppImplementation(self._interface.id) | 79 cpp_impl_emitter = self._cpp_library_emitter.CreateSourceEmitter(self._inter
face.id) |
| 82 self._cpp_impl_files.append(cpp_impl_path) | |
| 83 cpp_impl_emitter = self._emitters.FileEmitter(cpp_impl_path) | |
| 84 cpp_impl_emitter.Emit( | 80 cpp_impl_emitter.Emit( |
| 85 self._templates.Load('cpp_callback_implementation.template'), | 81 self._templates.Load('cpp_callback_implementation.template'), |
| 86 INCLUDES=_GenerateCPPIncludes(cpp_impl_includes), | 82 INCLUDES=_GenerateCPPIncludes(cpp_impl_includes), |
| 87 INTERFACE=self._interface.id, | 83 INTERFACE=self._interface.id, |
| 88 HANDLERS=cpp_impl_handlers_emitter.Fragments()) | 84 HANDLERS=cpp_impl_handlers_emitter.Fragments()) |
| 89 | 85 |
| 90 def GenerateLibraries(self, interface_files): | |
| 91 # Generate dart:html library. | |
| 92 auxiliary_dir = os.path.relpath(self._auxiliary_dir, self._output_dir) | |
| 93 self._GenerateLibFile( | |
| 94 'html_dartium.darttemplate', | |
| 95 os.path.join(self._output_dir, 'html_dartium.dart'), | |
| 96 interface_files, | |
| 97 AUXILIARY_DIR=systembase.MassagePath(auxiliary_dir)) | |
| 98 | |
| 99 # Generate DartDerivedSourcesXX.cpp. | |
| 100 partitions = 20 # FIXME: this should be configurable. | |
| 101 sources_count = len(self._cpp_impl_files) | |
| 102 for i in range(0, partitions): | |
| 103 derived_sources_path = os.path.join(self._output_dir, | |
| 104 'DartDerivedSources%02i.cpp' % (i + 1)) | |
| 105 | |
| 106 includes_emitter = emitter.Emitter() | |
| 107 for impl_file in self._cpp_impl_files[i::partitions]: | |
| 108 path = os.path.relpath(impl_file, os.path.dirname(derived_sources_path
)) | |
| 109 includes_emitter.Emit('#include "$PATH"\n', PATH=path) | |
| 110 | |
| 111 derived_sources_emitter = self._emitters.FileEmitter(derived_sources_path) | |
| 112 derived_sources_emitter.Emit( | |
| 113 self._templates.Load('cpp_derived_sources.template'), | |
| 114 INCLUDES=includes_emitter.Fragments()) | |
| 115 | |
| 116 # Generate DartResolver.cpp. | |
| 117 cpp_resolver_path = os.path.join(self._output_dir, 'DartResolver.cpp') | |
| 118 | |
| 119 includes_emitter = emitter.Emitter() | |
| 120 resolver_body_emitter = emitter.Emitter() | |
| 121 for file in self._cpp_header_files: | |
| 122 path = os.path.relpath(file, os.path.dirname(cpp_resolver_path)) | |
| 123 includes_emitter.Emit('#include "$PATH"\n', PATH=path) | |
| 124 resolver_body_emitter.Emit( | |
| 125 ' if (Dart_NativeFunction func = $CLASS_NAME::resolver(name, argu
mentCount))\n' | |
| 126 ' return func;\n', | |
| 127 CLASS_NAME=os.path.splitext(os.path.basename(path))[0]) | |
| 128 | |
| 129 cpp_resolver_emitter = self._emitters.FileEmitter(cpp_resolver_path) | |
| 130 cpp_resolver_emitter.Emit( | |
| 131 self._templates.Load('cpp_resolver.template'), | |
| 132 INCLUDES=includes_emitter.Fragments(), | |
| 133 RESOLVER_BODY=resolver_body_emitter.Fragments()) | |
| 134 | |
| 135 def Finish(self): | |
| 136 pass | |
| 137 | |
| 138 def _FilePathForCppHeader(self, interface_name): | |
| 139 return os.path.join(self._output_dir, 'cpp', 'Dart%s.h' % interface_name) | |
| 140 | |
| 141 def _FilePathForCppImplementation(self, interface_name): | |
| 142 return os.path.join(self._output_dir, 'cpp', 'Dart%s.cpp' % interface_name) | |
| 143 | |
| 144 | 86 |
| 145 class NativeImplementationGenerator(systembase.BaseGenerator): | 87 class NativeImplementationGenerator(systembase.BaseGenerator): |
| 146 """Generates Dart implementation for one DOM IDL interface.""" | 88 """Generates Dart implementation for one DOM IDL interface.""" |
| 147 | 89 |
| 148 def __init__(self, system, interface): | 90 def __init__(self, system, interface): |
| 149 """Generates Dart and C++ code for the given interface. | 91 """Generates Dart and C++ code for the given interface. |
| 150 | 92 |
| 151 Args: | 93 Args: |
| 152 system: The NativeImplementationSystem. | 94 system: The NativeImplementationSystem. |
| 153 interface: an IDLInterface instance. It is assumed that all types have | 95 interface: an IDLInterface instance. It is assumed that all types have |
| (...skipping 17 matching lines...) Expand all Loading... |
| 171 | 113 |
| 172 def ImplementsMergedMembers(self): | 114 def ImplementsMergedMembers(self): |
| 173 # We could not add merged functions to implementation class because | 115 # We could not add merged functions to implementation class because |
| 174 # underlying c++ object doesn't implement them. Merged functions are | 116 # underlying c++ object doesn't implement them. Merged functions are |
| 175 # generated on merged interface implementation instead. | 117 # generated on merged interface implementation instead. |
| 176 return False | 118 return False |
| 177 | 119 |
| 178 def StartInterface(self): | 120 def StartInterface(self): |
| 179 # Create emitters for c++ implementation. | 121 # Create emitters for c++ implementation. |
| 180 if self.HasImplementation(): | 122 if self.HasImplementation(): |
| 181 cpp_header_path = self._system._FilePathForCppHeader(self._interface.id) | 123 self._cpp_header_emitter = self._system._cpp_library_emitter.CreateHeaderE
mitter(self._interface.id) |
| 182 self._system._cpp_header_files.append(cpp_header_path) | 124 self._cpp_impl_emitter = self._system._cpp_library_emitter.CreateSourceEmi
tter(self._interface.id) |
| 183 self._cpp_header_emitter = self._system._emitters.FileEmitter(cpp_header_p
ath) | |
| 184 cpp_impl_path = self._system._FilePathForCppImplementation(self._interface
.id) | |
| 185 self._system._cpp_impl_files.append(cpp_impl_path) | |
| 186 self._cpp_impl_emitter = self._system._emitters.FileEmitter(cpp_impl_path) | |
| 187 else: | 125 else: |
| 188 self._cpp_header_emitter = emitter.Emitter() | 126 self._cpp_header_emitter = emitter.Emitter() |
| 189 self._cpp_impl_emitter = emitter.Emitter() | 127 self._cpp_impl_emitter = emitter.Emitter() |
| 190 | 128 |
| 191 self._interface_type_info = self._TypeInfo(self._interface.id) | 129 self._interface_type_info = self._TypeInfo(self._interface.id) |
| 192 self._members_emitter = emitter.Emitter() | 130 self._members_emitter = emitter.Emitter() |
| 193 self._cpp_declarations_emitter = emitter.Emitter() | 131 self._cpp_declarations_emitter = emitter.Emitter() |
| 194 self._cpp_impl_includes = set() | 132 self._cpp_impl_includes = set() |
| 195 self._cpp_definitions_emitter = emitter.Emitter() | 133 self._cpp_definitions_emitter = emitter.Emitter() |
| 196 self._cpp_resolver_emitter = emitter.Emitter() | 134 self._cpp_resolver_emitter = emitter.Emitter() |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 | 250 |
| 313 class_name = self._ImplClassName(self._interface.id) | 251 class_name = self._ImplClassName(self._interface.id) |
| 314 members_emitter = self._dart_impl_emitter.Emit( | 252 members_emitter = self._dart_impl_emitter.Emit( |
| 315 template, | 253 template, |
| 316 CLASSNAME=class_name, | 254 CLASSNAME=class_name, |
| 317 EXTENDS=' extends ' + self._BaseClassName(), | 255 EXTENDS=' extends ' + self._BaseClassName(), |
| 318 IMPLEMENTS=' implements ' + self._html_interface_name, | 256 IMPLEMENTS=' implements ' + self._html_interface_name, |
| 319 NATIVESPEC='') | 257 NATIVESPEC='') |
| 320 members_emitter.Emit(''.join(self._members_emitter.Fragments())) | 258 members_emitter.Emit(''.join(self._members_emitter.Fragments())) |
| 321 | 259 |
| 322 self._GenerateCppHeader() | 260 self._GenerateCPPHeader() |
| 323 | 261 |
| 324 self._cpp_impl_emitter.Emit( | 262 self._cpp_impl_emitter.Emit( |
| 325 self._system._templates.Load('cpp_implementation.template'), | 263 self._system._templates.Load('cpp_implementation.template'), |
| 326 INTERFACE=self._interface.id, | 264 INTERFACE=self._interface.id, |
| 327 INCLUDES=_GenerateCPPIncludes(self._cpp_impl_includes), | 265 INCLUDES=_GenerateCPPIncludes(self._cpp_impl_includes), |
| 328 CALLBACKS=self._cpp_definitions_emitter.Fragments(), | 266 CALLBACKS=self._cpp_definitions_emitter.Fragments(), |
| 329 RESOLVER=self._cpp_resolver_emitter.Fragments(), | 267 RESOLVER=self._cpp_resolver_emitter.Fragments(), |
| 330 DART_IMPLEMENTATION_CLASS=class_name) | 268 DART_IMPLEMENTATION_CLASS=class_name) |
| 331 | 269 |
| 332 def _GenerateCppHeader(self): | 270 def _GenerateCPPHeader(self): |
| 333 to_native_emitter = emitter.Emitter() | 271 to_native_emitter = emitter.Emitter() |
| 334 if self._interface_type_info.custom_to_native(): | 272 if self._interface_type_info.custom_to_native(): |
| 335 to_native_emitter.Emit( | 273 to_native_emitter.Emit( |
| 336 ' static PassRefPtr<NativeType> toNative(Dart_Handle handle, Dart_H
andle& exception);\n') | 274 ' static PassRefPtr<NativeType> toNative(Dart_Handle handle, Dart_H
andle& exception);\n') |
| 337 else: | 275 else: |
| 338 to_native_emitter.Emit( | 276 to_native_emitter.Emit( |
| 339 ' static NativeType* toNative(Dart_Handle handle, Dart_Handle& exce
ption)\n' | 277 ' static NativeType* toNative(Dart_Handle handle, Dart_Handle& exce
ption)\n' |
| 340 ' {\n' | 278 ' {\n' |
| 341 ' return DartDOMWrapper::unwrapDartWrapper<Dart$INTERFACE>(hand
le, exception);\n' | 279 ' return DartDOMWrapper::unwrapDartWrapper<Dart$INTERFACE>(hand
le, exception);\n' |
| 342 ' }\n', | 280 ' }\n', |
| (...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 917 return False | 855 return False |
| 918 if operation.id in ['addEventListener', 'removeEventListener'] and argument.
id == 'useCapture': | 856 if operation.id in ['addEventListener', 'removeEventListener'] and argument.
id == 'useCapture': |
| 919 return False | 857 return False |
| 920 # Another option would be to adjust in IDLs, but let's keep it here for now | 858 # Another option would be to adjust in IDLs, but let's keep it here for now |
| 921 # as it's a single instance. | 859 # as it's a single instance. |
| 922 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope
rty' and argument.id == 'priority': | 860 if self._interface.id == 'CSSStyleDeclaration' and operation.id == 'setPrope
rty' and argument.id == 'priority': |
| 923 return False | 861 return False |
| 924 return True | 862 return True |
| 925 | 863 |
| 926 | 864 |
| 865 class CPPLibraryEmitter(): |
| 866 def __init__(self, emitters, cpp_sources_dir): |
| 867 self._emitters = emitters |
| 868 self._cpp_sources_dir = cpp_sources_dir |
| 869 self._headers_list = [] |
| 870 self._sources_list = [] |
| 871 |
| 872 def CreateHeaderEmitter(self, interface_name, is_callback=False): |
| 873 path = os.path.join(self._cpp_sources_dir, 'Dart%s.h' % interface_name) |
| 874 if not is_callback: |
| 875 self._headers_list.append(path) |
| 876 return self._emitters.FileEmitter(path) |
| 877 |
| 878 def CreateSourceEmitter(self, interface_name): |
| 879 path = os.path.join(self._cpp_sources_dir, 'Dart%s.cpp' % interface_name) |
| 880 self._sources_list.append(path) |
| 881 return self._emitters.FileEmitter(path) |
| 882 |
| 883 def EmitDerivedSources(self, template, output_dir): |
| 884 partitions = 20 # FIXME: this should be configurable. |
| 885 sources_count = len(self._sources_list) |
| 886 for i in range(0, partitions): |
| 887 file_path = os.path.join(output_dir, 'DartDerivedSources%02i.cpp' % (i + 1
)) |
| 888 includes_emitter = self._emitters.FileEmitter(file_path).Emit(template) |
| 889 for source_file in self._sources_list[i::partitions]: |
| 890 path = os.path.relpath(source_file, output_dir) |
| 891 includes_emitter.Emit('#include "$PATH"\n', PATH=path) |
| 892 |
| 893 def EmitResolver(self, template, output_dir): |
| 894 file_path = os.path.join(output_dir, 'DartResolver.cpp') |
| 895 includes_emitter, body_emitter = self._emitters.FileEmitter(file_path).Emit(
template) |
| 896 for header_file in self._headers_list: |
| 897 path = os.path.relpath(header_file, output_dir) |
| 898 includes_emitter.Emit('#include "$PATH"\n', PATH=path) |
| 899 body_emitter.Emit( |
| 900 ' if (Dart_NativeFunction func = $CLASS_NAME::resolver(name, argume
ntCount))\n' |
| 901 ' return func;\n', |
| 902 CLASS_NAME=os.path.splitext(os.path.basename(path))[0]) |
| 903 |
| 904 |
| 927 def _GenerateCPPIncludes(includes): | 905 def _GenerateCPPIncludes(includes): |
| 928 return ''.join(['#include %s\n' % include for include in sorted(includes)]) | 906 return ''.join(['#include %s\n' % include for include in sorted(includes)]) |
| 929 | 907 |
| 930 def _FindInHierarchy(database, interface, test): | 908 def _FindInHierarchy(database, interface, test): |
| 931 if test(interface): | 909 if test(interface): |
| 932 return interface | 910 return interface |
| 933 for parent in interface.parents: | 911 for parent in interface.parents: |
| 934 parent_name = parent.type.id | 912 parent_name = parent.type.id |
| 935 if not database.HasInterface(parent.type.id): | 913 if not database.HasInterface(parent.type.id): |
| 936 continue | 914 continue |
| 937 parent_interface = database.GetInterface(parent.type.id) | 915 parent_interface = database.GetInterface(parent.type.id) |
| 938 parent_interface = _FindInHierarchy(database, parent_interface, test) | 916 parent_interface = _FindInHierarchy(database, parent_interface, test) |
| 939 if parent_interface: | 917 if parent_interface: |
| 940 return parent_interface | 918 return parent_interface |
| 941 | 919 |
| 942 def _ToWebKitName(name): | 920 def _ToWebKitName(name): |
| 943 name = name[0].lower() + name[1:] | 921 name = name[0].lower() + name[1:] |
| 944 name = re.sub(r'^(hTML|uRL|jS|xML|xSLT)', lambda s: s.group(1).lower(), | 922 name = re.sub(r'^(hTML|uRL|jS|xML|xSLT)', lambda s: s.group(1).lower(), |
| 945 name) | 923 name) |
| 946 return re.sub(r'^(create|exclusive)', lambda s: 'is' + s.group(1).capitalize()
, | 924 return re.sub(r'^(create|exclusive)', lambda s: 'is' + s.group(1).capitalize()
, |
| 947 name) | 925 name) |
| OLD | NEW |