| 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 302 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 313 operations[0].type.id == 'void', | 313 operations[0].type.id == 'void', |
| 314 parameter_names, | 314 parameter_names, |
| 315 declaration, | 315 declaration, |
| 316 GenerateCall, | 316 GenerateCall, |
| 317 IsOptional, | 317 IsOptional, |
| 318 can_omit_type_check) | 318 can_omit_type_check) |
| 319 | 319 |
| 320 def AdditionalImplementedInterfaces(self): | 320 def AdditionalImplementedInterfaces(self): |
| 321 # TODO: Include all implemented interfaces, including other Lists. | 321 # TODO: Include all implemented interfaces, including other Lists. |
| 322 implements = [] | 322 implements = [] |
| 323 if self._interface_type_info.is_typed_array(): | 323 if self._interface_type_info.list_item_type(): |
| 324 element_type = self._interface_type_info.list_item_type() | |
| 325 implements.append('List<%s>' % element_type) | |
| 326 elif self._interface_type_info.list_item_type(): | |
| 327 item_type_info = self._type_registry.TypeInfo( | 324 item_type_info = self._type_registry.TypeInfo( |
| 328 self._interface_type_info.list_item_type()) | 325 self._interface_type_info.list_item_type()) |
| 329 implements.append('List<%s>' % item_type_info.dart_type()) | 326 implements.append('List<%s>' % item_type_info.dart_type()) |
| 330 return implements | 327 return implements |
| 331 | 328 |
| 332 def AddConstructors(self, | 329 def AddConstructors(self, |
| 333 constructors, factory_name, factory_constructor_name): | 330 constructors, factory_name, factory_constructor_name): |
| 334 """ Adds all of the constructors. | 331 """ Adds all of the constructors. |
| 335 Arguments: | 332 Arguments: |
| 336 constructors - List of the constructors to be added. | 333 constructors - List of the constructors to be added. |
| 337 factory_name - Name of the factory for this class. | 334 factory_name - Name of the factory for this class. |
| 338 factory_constructor_name - The name of the constructor on the | 335 factory_constructor_name - The name of the constructor on the |
| 339 factory_name to call (calls an autogenerated FactoryProvider | 336 factory_name to call (calls an autogenerated FactoryProvider |
| 340 if unspecified) | 337 if unspecified) |
| 341 """ | 338 """ |
| 342 for constructor_info in constructors: | 339 for constructor_info in constructors: |
| 343 self._AddConstructor( | 340 self._AddConstructor( |
| 344 constructor_info, factory_name, factory_constructor_name) | 341 constructor_info, factory_name, factory_constructor_name) |
| 345 | 342 |
| 346 typed_array_type = None | |
| 347 for interface in self._database.Hierarchy(self._interface): | |
| 348 type_info = self._type_registry.TypeInfo(interface.id) | |
| 349 if type_info.is_typed_array(): | |
| 350 typed_array_type = type_info.list_item_type() | |
| 351 break | |
| 352 | |
| 353 annotations = FormatAnnotationsAndComments( | |
| 354 GetAnnotationsAndComments(self._library_name, self._interface.id, | |
| 355 self._interface.id), ' ') | |
| 356 | |
| 357 fromListAnnotations = FormatAnnotationsAndComments( | |
| 358 GetAnnotationsAndComments(self._library_name, self._interface.id, | |
| 359 'fromList'), ' ') | |
| 360 | |
| 361 fromBufferAnnotations = FormatAnnotationsAndComments( | |
| 362 GetAnnotationsAndComments(self._library_name, self._interface.id, | |
| 363 'fromBuffer'), ' ') | |
| 364 | |
| 365 if typed_array_type: | |
| 366 self._members_emitter.Emit( | |
| 367 '\n $(ANNOTATIONS)factory $CTOR(int length) =>\n' | |
| 368 ' $FACTORY.create$(CTOR)(length);\n' | |
| 369 '\n $(LIST_ANNOTATIONS)factory $CTOR.fromList(List<$TYPE> list) =>\n' | |
| 370 ' $FACTORY.create$(CTOR)_fromList(list);\n' | |
| 371 '\n $(BUFFER_ANNOTATIONS)factory $CTOR.view(ByteBuffer buffer, ' | |
| 372 '[int byteOffset, int length]) => \n' | |
| 373 ' $FACTORY.create$(CTOR)_fromBuffer(buffer, byteOffset, length);\n'
, | |
| 374 CTOR=self._renamer.RenameInterface(interface), | |
| 375 ANNOTATIONS=annotations, | |
| 376 LIST_ANNOTATIONS=fromListAnnotations, | |
| 377 BUFFER_ANNOTATIONS=fromBufferAnnotations, | |
| 378 TYPE=self._DartType(typed_array_type), | |
| 379 FACTORY=factory_name) | |
| 380 | |
| 381 def _AddConstructor(self, | 343 def _AddConstructor(self, |
| 382 constructor_info, factory_name, factory_constructor_name): | 344 constructor_info, factory_name, factory_constructor_name): |
| 383 if self.GenerateCustomFactory(constructor_info): | 345 if self.GenerateCustomFactory(constructor_info): |
| 384 return | 346 return |
| 385 | 347 |
| 386 annotations = FormatAnnotationsAndComments( | 348 annotations = FormatAnnotationsAndComments( |
| 387 GetAnnotationsAndComments(self._library_name, self._interface.id, | 349 GetAnnotationsAndComments(self._library_name, self._interface.id, |
| 388 self._interface.id), ' ') | 350 self._interface.id), ' ') |
| 389 | 351 |
| 390 if not factory_constructor_name: | 352 if not factory_constructor_name: |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 PARAMS=operation.ParametersDeclaration(self._DartType)) | 522 PARAMS=operation.ParametersDeclaration(self._DartType)) |
| 561 | 523 |
| 562 def EmitListMixin(self, element_name): | 524 def EmitListMixin(self, element_name): |
| 563 # TODO(sra): Use separate mixins for mutable implementations of List<T>. | 525 # TODO(sra): Use separate mixins for mutable implementations of List<T>. |
| 564 # TODO(sra): Use separate mixins for typed array implementations of List<T>. | 526 # TODO(sra): Use separate mixins for typed array implementations of List<T>. |
| 565 template_file = 'immutable_list_mixin.darttemplate' | 527 template_file = 'immutable_list_mixin.darttemplate' |
| 566 has_contains = any(op.id == 'contains' for op in self._interface.operations) | 528 has_contains = any(op.id == 'contains' for op in self._interface.operations) |
| 567 has_clear = any(op.id == 'clear' for op in self._interface.operations) | 529 has_clear = any(op.id == 'clear' for op in self._interface.operations) |
| 568 has_length = False | 530 has_length = False |
| 569 has_length_setter = False | 531 has_length_setter = False |
| 570 typed_array = self._interface_type_info.is_typed_array() | |
| 571 | 532 |
| 572 for attr in self._interface.attributes: | 533 for attr in self._interface.attributes: |
| 573 if attr.id == 'length': | 534 if attr.id == 'length': |
| 574 has_length = True | 535 has_length = True |
| 575 has_length_setter = not attr.is_read_only | 536 has_length_setter = not attr.is_read_only |
| 576 | 537 |
| 577 has_num_items = any(attr.id == 'numberOfItems' | 538 has_num_items = any(attr.id == 'numberOfItems' |
| 578 for attr in self._interface.attributes) | 539 for attr in self._interface.attributes) |
| 579 | 540 |
| 580 template = self._template_loader.Load( | 541 template = self._template_loader.Load( |
| 581 template_file, | 542 template_file, |
| 582 { | 543 { |
| 583 'DEFINE_CONTAINS': not has_contains, | 544 'DEFINE_CONTAINS': not has_contains, |
| 584 'DEFINE_CLEAR': not has_clear, | 545 'DEFINE_CLEAR': not has_clear, |
| 585 'DEFINE_IMMUTABLE': not typed_array, | |
| 586 'DEFINE_LENGTH_AS_NUM_ITEMS': not has_length and has_num_items, | 546 'DEFINE_LENGTH_AS_NUM_ITEMS': not has_length and has_num_items, |
| 587 'DEFINE_LENGTH_SETTER': not has_length_setter, | 547 'DEFINE_LENGTH_SETTER': not has_length_setter, |
| 588 }) | 548 }) |
| 589 self._members_emitter.Emit(template, E=element_name) | 549 self._members_emitter.Emit(template, E=element_name) |
| 590 | 550 |
| 591 def SecureOutputType(self, type_name, is_dart_type=False): | 551 def SecureOutputType(self, type_name, is_dart_type=False): |
| 592 """ Converts the type name to the secure type name for return types. | 552 """ Converts the type name to the secure type name for return types. |
| 593 """ | 553 """ |
| 594 if is_dart_type: | 554 if is_dart_type: |
| 595 dart_name = type_name | 555 dart_name = type_name |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 walk(interface.parents) | 594 walk(interface.parents) |
| 635 else: | 595 else: |
| 636 walk(interface.parents[1:]) | 596 walk(interface.parents[1:]) |
| 637 return result | 597 return result |
| 638 | 598 |
| 639 def _DartType(self, type_name): | 599 def _DartType(self, type_name): |
| 640 return self._type_registry.DartType(type_name) | 600 return self._type_registry.DartType(type_name) |
| 641 | 601 |
| 642 def _IsPrivate(self, name): | 602 def _IsPrivate(self, name): |
| 643 return name.startswith('_') | 603 return name.startswith('_') |
| OLD | NEW |