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 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 from generator import * | 11 from generator import * |
| 12 from systembase import * | 12 from systembase import * |
| 13 from systemhtml import DomToHtmlEvent, DomToHtmlEvents | |
| 13 | 14 |
| 14 class NativeImplementationSystem(System): | 15 class NativeImplementationSystem(System): |
| 15 | 16 |
| 16 def __init__(self, templates, database, emitters, auxiliary_dir, output_dir): | 17 def __init__(self, templates, database, html_renames, emitters, auxiliary_dir, |
| 18 output_dir): | |
| 17 super(NativeImplementationSystem, self).__init__( | 19 super(NativeImplementationSystem, self).__init__( |
| 18 templates, database, emitters, output_dir) | 20 templates, database, emitters, output_dir) |
| 19 | 21 |
| 22 self._html_renames = html_renames | |
| 20 self._auxiliary_dir = auxiliary_dir | 23 self._auxiliary_dir = auxiliary_dir |
| 21 self._dom_public_files = [] | 24 self._dom_public_files = [] |
| 22 self._dom_impl_files = [] | 25 self._dom_impl_files = [] |
| 23 self._cpp_header_files = [] | 26 self._cpp_header_files = [] |
| 24 self._cpp_impl_files = [] | 27 self._cpp_impl_files = [] |
| 25 | 28 |
| 26 def InterfaceGenerator(self, | 29 def InterfaceGenerator(self, |
| 27 interface, | 30 interface, |
| 28 common_prefix, | 31 common_prefix, |
| 29 super_interface_name, | 32 super_interface_name, |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 def StartInterface(self): | 241 def StartInterface(self): |
| 239 self._class_name = self._ImplClassName(self._interface.id) | 242 self._class_name = self._ImplClassName(self._interface.id) |
| 240 self._interface_type_info = GetIDLTypeInfo(self._interface.id) | 243 self._interface_type_info = GetIDLTypeInfo(self._interface.id) |
| 241 self._members_emitter = emitter.Emitter() | 244 self._members_emitter = emitter.Emitter() |
| 242 self._cpp_declarations_emitter = emitter.Emitter() | 245 self._cpp_declarations_emitter = emitter.Emitter() |
| 243 self._cpp_impl_includes = set() | 246 self._cpp_impl_includes = set() |
| 244 self._cpp_definitions_emitter = emitter.Emitter() | 247 self._cpp_definitions_emitter = emitter.Emitter() |
| 245 self._cpp_resolver_emitter = emitter.Emitter() | 248 self._cpp_resolver_emitter = emitter.Emitter() |
| 246 | 249 |
| 247 self._GenerateConstructors() | 250 self._GenerateConstructors() |
| 251 self._GenerateEvents() | |
| 248 | 252 |
| 249 def _GenerateConstructors(self): | 253 def _GenerateConstructors(self): |
| 250 if not self._IsConstructable(): | 254 if not self._IsConstructable(): |
| 251 return | 255 return |
| 252 | 256 |
| 253 # TODO(antonm): currently we don't have information about number of argument s expected by | 257 # TODO(antonm): currently we don't have information about number of argument s expected by |
| 254 # the constructor, so name only dispatch. | 258 # the constructor, so name only dispatch. |
| 255 self._cpp_resolver_emitter.Emit( | 259 self._cpp_resolver_emitter.Emit( |
| 256 ' if (name == "$(INTERFACE_NAME)_constructor_Callback")\n' | 260 ' if (name == "$(INTERFACE_NAME)_constructor_Callback")\n' |
| 257 ' return Dart$(INTERFACE_NAME)Internal::constructorCallback;\n', | 261 ' return Dart$(INTERFACE_NAME)Internal::constructorCallback;\n', |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307 arguments.append(argument_expression) | 311 arguments.append(argument_expression) |
| 308 | 312 |
| 309 function_expression = '%s::%s' % (self._interface_type_info.native_type(), c reate_function) | 313 function_expression = '%s::%s' % (self._interface_type_info.native_type(), c reate_function) |
| 310 invocation = self._GenerateWebCoreInvocation(function_expression, arguments, | 314 invocation = self._GenerateWebCoreInvocation(function_expression, arguments, |
| 311 self._interface.id, self._interface.ext_attrs, raises_dom_exceptions) | 315 self._interface.id, self._interface.ext_attrs, raises_dom_exceptions) |
| 312 self._GenerateNativeCallback(callback_name='constructorCallback', | 316 self._GenerateNativeCallback(callback_name='constructorCallback', |
| 313 parameter_definitions=parameter_definitions_emitter.Fragments(), | 317 parameter_definitions=parameter_definitions_emitter.Fragments(), |
| 314 needs_receiver=False, invocation=invocation, | 318 needs_receiver=False, invocation=invocation, |
| 315 raises_exceptions=raises_exceptions) | 319 raises_exceptions=raises_exceptions) |
| 316 | 320 |
| 321 def _GenerateEvents(self): | |
| 322 if self._interface.id == 'DocumentFragment': | |
|
Anton Muhin
2012/05/16 13:17:57
may you add a comment why this is special-cased?
podivilov
2012/05/16 15:19:44
Done.
| |
| 323 self._EmitEventGetter('ElementEventsImplementation') | |
| 324 return | |
| 325 | |
| 326 events_attributes = [attr for attr in self._interface.attributes | |
| 327 if attr.type.id == 'EventListener'] | |
| 328 if not 'EventTarget' in self._interface.ext_attrs and not events_attributes: | |
| 329 return | |
| 330 | |
|
Anton Muhin
2012/05/16 13:17:57
nit: intentional two blanks?
podivilov
2012/05/16 15:19:44
Done.
| |
| 331 | |
| 332 def is_event_target(interface): | |
|
Anton Muhin
2012/05/16 13:17:57
q: is it a correct style for nest functions? just
podivilov
2012/05/16 15:19:44
Done.
| |
| 333 return ('EventTarget' in interface.ext_attrs and | |
| 334 interface.id != 'EventTarget') | |
| 335 is_root = not _FindParent(self._interface, self._system._database, is_event_ target) | |
| 336 if is_root: | |
| 337 self._members_emitter.Emit(' EventsImplementation _on;\n') | |
| 338 | |
| 339 if not events_attributes: | |
| 340 if is_root: | |
| 341 self._EmitEventGetter('EventsImplementation') | |
| 342 return | |
| 343 | |
| 344 events_class = '%sEventsImplementation' % self._interface.id | |
| 345 self._EmitEventGetter(events_class) | |
| 346 | |
| 347 def has_event_attributes(interface): | |
| 348 for attribute in interface.attributes: | |
|
Anton Muhin
2012/05/16 13:17:57
as a variant: return any((attr.type.id == 'EventLi
podivilov
2012/05/16 15:19:44
Done.
| |
| 349 if attribute.type.id == 'EventListener': | |
| 350 return True | |
| 351 return False | |
| 352 parent = _FindParent(self._interface, self._system._database, has_event_attr ibutes) | |
| 353 if parent: | |
| 354 parent_events_class = '%sEventsImplementation' % parent.id | |
| 355 else: | |
| 356 parent_events_class = 'EventsImplementation' | |
| 357 html_inteface = self._system._html_renames.get(self._interface.id, self._int erface.id) | |
| 358 events_members = self._dart_impl_emitter.Emit( | |
| 359 '\n' | |
| 360 'class $EVENTS_CLASS extends $PARENT_EVENTS_CLASS implements $EVENTS_INT ERFACE {\n' | |
| 361 ' $EVENTS_CLASS(_ptr) : super(_ptr);\n' | |
| 362 '$!MEMBERS\n' | |
| 363 '}\n', | |
| 364 EVENTS_CLASS=events_class, | |
| 365 PARENT_EVENTS_CLASS=parent_events_class, | |
| 366 EVENTS_INTERFACE='html.%sEvents' % html_inteface) | |
| 367 | |
| 368 events_attributes = DomToHtmlEvents(self._interface.id, events_attributes) | |
| 369 for event_name in events_attributes: | |
| 370 events_members.Emit( | |
| 371 ' EventListenerList get $HTML_NAME() => _get("$DOM_NAME");\n', | |
| 372 HTML_NAME=DomToHtmlEvent(event_name), | |
| 373 DOM_NAME=event_name) | |
| 374 | |
| 375 def _EmitEventGetter(self, events_class): | |
| 376 self._members_emitter.Emit( | |
| 377 '\n' | |
| 378 ' $EVENTS_CLASS get on() {\n' | |
| 379 ' if (_on === null) _on = new $EVENTS_CLASS(this);\n' | |
| 380 ' return _on;\n' | |
| 381 ' }\n', | |
| 382 EVENTS_CLASS=events_class) | |
| 383 | |
| 317 def _ImplClassName(self, interface_name): | 384 def _ImplClassName(self, interface_name): |
| 318 return interface_name + 'Implementation' | 385 return interface_name + 'Implementation' |
| 319 | 386 |
| 320 def _BaseClassName(self): | 387 def _BaseClassName(self): |
| 321 if not self._interface.parents: | 388 if not self._interface.parents: |
| 322 return 'DOMWrapperBase' | 389 return 'DOMWrapperBase' |
| 323 | 390 |
| 324 supertype = self._interface.parents[0].type.id | 391 supertype = self._interface.parents[0].type.id |
| 325 | 392 |
| 326 # FIXME: We're currently injecting List<..> and EventTarget as | 393 # FIXME: We're currently injecting List<..> and EventTarget as |
| (...skipping 800 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1127 FUNCTION_CALL='%s(%s)' % (function_expression, ', '.join(arguments))) | 1194 FUNCTION_CALL='%s(%s)' % (function_expression, ', '.join(arguments))) |
| 1128 | 1195 |
| 1129 def _GenerateCPPIncludes(includes): | 1196 def _GenerateCPPIncludes(includes): |
| 1130 return ''.join(['#include %s\n' % include for include in includes]) | 1197 return ''.join(['#include %s\n' % include for include in includes]) |
| 1131 | 1198 |
| 1132 def _DOMWrapperType(database, interface): | 1199 def _DOMWrapperType(database, interface): |
| 1133 if interface.id == 'MessagePort': | 1200 if interface.id == 'MessagePort': |
| 1134 return 'MessagePort' | 1201 return 'MessagePort' |
| 1135 | 1202 |
| 1136 type = 'Object' | 1203 type = 'Object' |
| 1137 if _InstanceOfNode(database, interface): | 1204 def is_node(interface): |
| 1205 return interface.id == 'Node' | |
| 1206 if is_node(interface) or _FindParent(interface, database, is_node): | |
| 1138 type = 'Node' | 1207 type = 'Node' |
| 1139 if 'ActiveDOMObject' in interface.ext_attrs: | 1208 if 'ActiveDOMObject' in interface.ext_attrs: |
| 1140 type = 'Active%s' % type | 1209 type = 'Active%s' % type |
| 1141 return type | 1210 return type |
| 1142 | 1211 |
| 1143 def _InstanceOfNode(database, interface): | 1212 def _FindParent(interface, database, callback): |
| 1144 if interface.id == 'Node': | |
| 1145 return True | |
| 1146 for parent in interface.parents: | 1213 for parent in interface.parents: |
| 1214 parent_name = parent.type.id | |
| 1147 if not database.HasInterface(parent.type.id): | 1215 if not database.HasInterface(parent.type.id): |
| 1148 continue | 1216 continue |
| 1149 parent_interface = database.GetInterface(parent.type.id) | 1217 parent_interface = database.GetInterface(parent.type.id) |
| 1150 if _InstanceOfNode(database, parent_interface): | 1218 if callback(parent_interface): |
| 1151 return True | 1219 return parent_interface |
| 1152 return False | 1220 parent_interface = _FindParent(parent_interface, database, callback) |
| 1221 if parent_interface: | |
| 1222 return parent_interface | |
| OLD | NEW |