| 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, \ |
| 11 DartDomNameOfAttribute, FindMatchingAttribute, IsDartCollectionType, \ | 11 DartDomNameOfAttribute, FindMatchingAttribute, IsDartCollectionType, \ |
| 12 IsPureInterface, TypeOrNothing, GetAnnotationsAndComments, \ | 12 IsPureInterface, TypeOrNothing, ConvertToFuture, GetCallbackInfo |
| 13 FormatAnnotationsAndComments, ConvertToFuture, GetCallbackInfo | |
| 14 from htmlrenamer import convert_to_future_members | 13 from htmlrenamer import convert_to_future_members |
| 15 | 14 |
| 16 # Types that are accessible cross-frame in a limited fashion. | 15 # Types that are accessible cross-frame in a limited fashion. |
| 17 # In these cases, the base type (e.g., WindowBase) provides restricted access | 16 # In these cases, the base type (e.g., WindowBase) provides restricted access |
| 18 # while the subtype (e.g., Window) provides full access to the | 17 # while the subtype (e.g., Window) provides full access to the |
| 19 # corresponding objects if there are from the same frame. | 18 # corresponding objects if there are from the same frame. |
| 20 _secure_base_types = { | 19 _secure_base_types = { |
| 21 'Window': 'WindowBase', | 20 'Window': 'WindowBase', |
| 22 'Location': 'LocationBase', | 21 'Location': 'LocationBase', |
| 23 'History': 'HistoryBase', | 22 'History': 'HistoryBase', |
| 24 } | 23 } |
| 25 | 24 |
| 26 _custom_factories = [ | 25 _custom_factories = [ |
| 27 'Notification', | 26 'Notification', |
| 28 'EventSource', | 27 'EventSource', |
| 29 ] | 28 ] |
| 30 | 29 |
| 31 class HtmlDartGenerator(object): | 30 class HtmlDartGenerator(object): |
| 32 def __init__(self, interface, options): | 31 def __init__(self, interface, options): |
| 33 self._database = options.database | 32 self._database = options.database |
| 34 self._interface = interface | 33 self._interface = interface |
| 35 self._type_registry = options.type_registry | 34 self._type_registry = options.type_registry |
| 36 self._interface_type_info = self._type_registry.TypeInfo(self._interface.id) | 35 self._interface_type_info = self._type_registry.TypeInfo(self._interface.id) |
| 37 self._renamer = options.renamer | 36 self._renamer = options.renamer |
| 37 self._metadata = options.metadata |
| 38 self._library_name = self._renamer.GetLibraryName(self._interface) | 38 self._library_name = self._renamer.GetLibraryName(self._interface) |
| 39 | 39 |
| 40 def EmitSupportCheck(self): | 40 def EmitSupportCheck(self): |
| 41 if self.HasSupportCheck(): | 41 if self.HasSupportCheck(): |
| 42 check = self.GetSupportCheck() | 42 check = self.GetSupportCheck() |
| 43 if type(check) != tuple: | 43 if type(check) != tuple: |
| 44 signature = 'get supported' | 44 signature = 'get supported' |
| 45 else: | 45 else: |
| 46 signature = check[0] | 46 signature = check[0] |
| 47 check = check[1] | 47 check = check[1] |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 """ | 338 """ |
| 339 for constructor_info in constructors: | 339 for constructor_info in constructors: |
| 340 self._AddConstructor( | 340 self._AddConstructor( |
| 341 constructor_info, factory_name, factory_constructor_name) | 341 constructor_info, factory_name, factory_constructor_name) |
| 342 | 342 |
| 343 def _AddConstructor(self, | 343 def _AddConstructor(self, |
| 344 constructor_info, factory_name, factory_constructor_name): | 344 constructor_info, factory_name, factory_constructor_name): |
| 345 if self.GenerateCustomFactory(constructor_info): | 345 if self.GenerateCustomFactory(constructor_info): |
| 346 return | 346 return |
| 347 | 347 |
| 348 annotations = FormatAnnotationsAndComments( | 348 metadata = self._metadata.GetFormattedMetadata( |
| 349 GetAnnotationsAndComments(self._library_name, self._interface.id, | 349 self._library_name, self._interface.id, self._interface.id, ' ') |
| 350 self._interface.id), ' ') | |
| 351 | 350 |
| 352 if not factory_constructor_name: | 351 if not factory_constructor_name: |
| 353 factory_constructor_name = '_create' | 352 factory_constructor_name = '_create' |
| 354 factory_parameters = constructor_info.ParametersAsArgumentList() | 353 factory_parameters = constructor_info.ParametersAsArgumentList() |
| 355 has_factory_provider = True | 354 has_factory_provider = True |
| 356 else: | 355 else: |
| 357 factory_parameters = ', '.join(constructor_info.factory_parameters) | 356 factory_parameters = ', '.join(constructor_info.factory_parameters) |
| 358 has_factory_provider = False | 357 has_factory_provider = False |
| 359 | 358 |
| 360 if constructor_info.pure_dart_constructor: | 359 if constructor_info.pure_dart_constructor: |
| 361 # TODO(antonm): use common dispatcher generation for this case as well. | 360 # TODO(antonm): use common dispatcher generation for this case as well. |
| 362 has_optional = any(param_info.is_optional | 361 has_optional = any(param_info.is_optional |
| 363 for param_info in constructor_info.param_infos) | 362 for param_info in constructor_info.param_infos) |
| 364 | 363 |
| 365 if not has_optional: | 364 if not has_optional: |
| 366 self._members_emitter.Emit( | 365 self._members_emitter.Emit( |
| 367 '\n $(ANNOTATIONS)' | 366 '\n $(METADATA)' |
| 368 'factory $CTOR($PARAMS) => ' | 367 'factory $CTOR($PARAMS) => ' |
| 369 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', | 368 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', |
| 370 CTOR=constructor_info._ConstructorFullName(self._DartType), | 369 CTOR=constructor_info._ConstructorFullName(self._DartType), |
| 371 PARAMS=constructor_info.ParametersDeclaration(self._DartType), | 370 PARAMS=constructor_info.ParametersDeclaration(self._DartType), |
| 372 FACTORY=factory_name, | 371 FACTORY=factory_name, |
| 373 ANNOTATIONS=annotations, | 372 METADATA=metadata, |
| 374 CTOR_FACTORY_NAME=factory_constructor_name, | 373 CTOR_FACTORY_NAME=factory_constructor_name, |
| 375 FACTORY_PARAMS=factory_parameters) | 374 FACTORY_PARAMS=factory_parameters) |
| 376 else: | 375 else: |
| 377 if has_factory_provider: | 376 if has_factory_provider: |
| 378 dispatcher_emitter = self._members_emitter.Emit( | 377 dispatcher_emitter = self._members_emitter.Emit( |
| 379 '\n $(ANNOTATIONS)' | 378 '\n $(METADATA)' |
| 380 'factory $CTOR($PARAMS) {\n' | 379 'factory $CTOR($PARAMS) {\n' |
| 381 '$!DISPATCHER' | 380 '$!DISPATCHER' |
| 382 ' return $FACTORY._create($FACTORY_PARAMS);\n' | 381 ' return $FACTORY._create($FACTORY_PARAMS);\n' |
| 383 ' }\n', | 382 ' }\n', |
| 384 CTOR=constructor_info._ConstructorFullName(self._DartType), | 383 CTOR=constructor_info._ConstructorFullName(self._DartType), |
| 385 PARAMS=constructor_info.ParametersDeclaration(self._DartType), | 384 PARAMS=constructor_info.ParametersDeclaration(self._DartType), |
| 386 FACTORY=factory_name, | 385 FACTORY=factory_name, |
| 387 ANNOTATIONS=annotations, | 386 METADATA=metadata, |
| 388 FACTORY_PARAMS=constructor_info.ParametersAsArgumentList()) | 387 FACTORY_PARAMS=constructor_info.ParametersAsArgumentList()) |
| 389 | 388 |
| 390 for index, param_info in enumerate(constructor_info.param_infos): | 389 for index, param_info in enumerate(constructor_info.param_infos): |
| 391 if param_info.is_optional: | 390 if param_info.is_optional: |
| 392 dispatcher_emitter.Emit( | 391 dispatcher_emitter.Emit( |
| 393 ' if (!?$OPT_PARAM_NAME) {\n' | 392 ' if (!?$OPT_PARAM_NAME) {\n' |
| 394 ' return $FACTORY._create($FACTORY_PARAMS);\n' | 393 ' return $FACTORY._create($FACTORY_PARAMS);\n' |
| 395 ' }\n', | 394 ' }\n', |
| 396 OPT_PARAM_NAME=param_info.name, | 395 OPT_PARAM_NAME=param_info.name, |
| 397 FACTORY=factory_name, | 396 FACTORY=factory_name, |
| 398 FACTORY_PARAMS=constructor_info.ParametersAsArgumentList(index)) | 397 FACTORY_PARAMS=constructor_info.ParametersAsArgumentList(index)) |
| 399 else: | 398 else: |
| 400 inits = self._members_emitter.Emit( | 399 inits = self._members_emitter.Emit( |
| 401 '\n $(ANNOTATIONS)' | 400 '\n $(METADATA)' |
| 402 'factory $CONSTRUCTOR($PARAMS) {\n' | 401 'factory $CONSTRUCTOR($PARAMS) {\n' |
| 403 ' var e = $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | 402 ' var e = $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
| 404 '$!INITS' | 403 '$!INITS' |
| 405 ' return e;\n' | 404 ' return e;\n' |
| 406 ' }\n', | 405 ' }\n', |
| 407 CONSTRUCTOR=constructor_info._ConstructorFullName(self._DartType), | 406 CONSTRUCTOR=constructor_info._ConstructorFullName(self._DartType), |
| 408 ANNOTATIONS=annotations, | 407 METADATA=metadata, |
| 409 FACTORY=factory_name, | 408 FACTORY=factory_name, |
| 410 CTOR_FACTORY_NAME=factory_constructor_name, | 409 CTOR_FACTORY_NAME=factory_constructor_name, |
| 411 PARAMS=constructor_info.ParametersDeclaration(self._DartType), | 410 PARAMS=constructor_info.ParametersDeclaration(self._DartType), |
| 412 FACTORY_PARAMS=factory_parameters) | 411 FACTORY_PARAMS=factory_parameters) |
| 413 | 412 |
| 414 for index, param_info in enumerate(constructor_info.param_infos): | 413 for index, param_info in enumerate(constructor_info.param_infos): |
| 415 if param_info.is_optional: | 414 if param_info.is_optional: |
| 416 inits.Emit(' if ($E != null) e.$E = $E;\n', E=param_info.name) | 415 inits.Emit(' if ($E != null) e.$E = $E;\n', E=param_info.name) |
| 417 else: | 416 else: |
| 418 def GenerateCall( | 417 def GenerateCall( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 431 def IsOptional(signature_index, argument): | 430 def IsOptional(signature_index, argument): |
| 432 return self.IsConstructorArgumentOptional(argument) | 431 return self.IsConstructorArgumentOptional(argument) |
| 433 | 432 |
| 434 custom_factory_ctr = self._interface.id in _custom_factories | 433 custom_factory_ctr = self._interface.id in _custom_factories |
| 435 constructor_full_name = constructor_info._ConstructorFullName( | 434 constructor_full_name = constructor_info._ConstructorFullName( |
| 436 self._DartType) | 435 self._DartType) |
| 437 self._GenerateOverloadDispatcher( | 436 self._GenerateOverloadDispatcher( |
| 438 constructor_info.idl_args, | 437 constructor_info.idl_args, |
| 439 False, | 438 False, |
| 440 [info.name for info in constructor_info.param_infos], | 439 [info.name for info in constructor_info.param_infos], |
| 441 emitter.Format('$(ANNOTATIONS)$FACTORY_KEYWORD $CTOR($PARAMS)', | 440 emitter.Format('$(METADATA)$FACTORY_KEYWORD $CTOR($PARAMS)', |
| 442 FACTORY_KEYWORD=('factory' if not custom_factory_ctr else | 441 FACTORY_KEYWORD=('factory' if not custom_factory_ctr else |
| 443 'static %s' % constructor_full_name), | 442 'static %s' % constructor_full_name), |
| 444 CTOR=(('' if not custom_factory_ctr else '_factory') | 443 CTOR=(('' if not custom_factory_ctr else '_factory') |
| 445 + constructor_full_name), | 444 + constructor_full_name), |
| 446 ANNOTATIONS=annotations, | 445 METADATA=metadata, |
| 447 PARAMS=constructor_info.ParametersDeclaration(self._DartType)), | 446 PARAMS=constructor_info.ParametersDeclaration(self._DartType)), |
| 448 GenerateCall, | 447 GenerateCall, |
| 449 IsOptional) | 448 IsOptional) |
| 450 | 449 |
| 451 def _AddFutureifiedOperation(self, info, html_name): | 450 def _AddFutureifiedOperation(self, info, html_name): |
| 452 """Given a API function that uses callbacks, convert it to using Futures. | 451 """Given a API function that uses callbacks, convert it to using Futures. |
| 453 | 452 |
| 454 This conversion assumes the success callback is always provided before the | 453 This conversion assumes the success callback is always provided before the |
| 455 error callback (and so far in the DOM API, this is the case).""" | 454 error callback (and so far in the DOM API, this is the case).""" |
| 456 callback_info = GetCallbackInfo( | 455 callback_info = GetCallbackInfo( |
| 457 self._database.GetInterface(info.callback_args[0].type_id)) | 456 self._database.GetInterface(info.callback_args[0].type_id)) |
| 458 | 457 |
| 459 param_list = info.ParametersAsArgumentList() | 458 param_list = info.ParametersAsArgumentList() |
| 460 annotations = '' | 459 metadata = '' |
| 461 if '_RenamingAnnotation' in dir(self): | 460 if '_RenamingAnnotation' in dir(self): |
| 462 annotations = (self._RenamingAnnotation(info.declared_name, html_name) + | 461 metadata = (self._RenamingAnnotation(info.declared_name, html_name) + |
| 463 self._Annotations(info.type_name, info.declared_name)) | 462 self._Metadata(info.type_name, info.declared_name)) |
| 464 self._members_emitter.Emit( | 463 self._members_emitter.Emit( |
| 465 '\n' | 464 '\n' |
| 466 ' $ANNOTATIONS$MODIFIERS$TYPE$FUTURE_GENERIC $NAME($PARAMS) {\n' | 465 ' $METADATA$MODIFIERS$TYPE$FUTURE_GENERIC $NAME($PARAMS) {\n' |
| 467 ' var completer = new Completer$(FUTURE_GENERIC)();\n' | 466 ' var completer = new Completer$(FUTURE_GENERIC)();\n' |
| 468 ' $ORIGINAL_FUNCTION($PARAMS_LIST\n' | 467 ' $ORIGINAL_FUNCTION($PARAMS_LIST\n' |
| 469 ' $NAMED_PARAM($VARIABLE_NAME) { ' | 468 ' $NAMED_PARAM($VARIABLE_NAME) { ' |
| 470 'completer.complete($VARIABLE_NAME); }' | 469 'completer.complete($VARIABLE_NAME); }' |
| 471 '$ERROR_CALLBACK);\n' | 470 '$ERROR_CALLBACK);\n' |
| 472 ' return completer.future;\n' | 471 ' return completer.future;\n' |
| 473 ' }\n', | 472 ' }\n', |
| 474 ANNOTATIONS=annotations, | 473 METADATA=metadata, |
| 475 MODIFIERS='static ' if info.IsStatic() else '', | 474 MODIFIERS='static ' if info.IsStatic() else '', |
| 476 TYPE=self.SecureOutputType(info.type_name), | 475 TYPE=self.SecureOutputType(info.type_name), |
| 477 NAME=html_name[1:], | 476 NAME=html_name[1:], |
| 478 PARAMS=info.ParametersDeclaration(self._NarrowInputType | 477 PARAMS=info.ParametersDeclaration(self._NarrowInputType |
| 479 if '_NarrowInputType' in dir(self) else self._DartType), | 478 if '_NarrowInputType' in dir(self) else self._DartType), |
| 480 PARAMS_LIST='' if param_list == '' else param_list + ',', | 479 PARAMS_LIST='' if param_list == '' else param_list + ',', |
| 481 NAMED_PARAM=('%s : ' % info.callback_args[0].name | 480 NAMED_PARAM=('%s : ' % info.callback_args[0].name |
| 482 if info.requires_named_arguments and | 481 if info.requires_named_arguments and |
| 483 info.callback_args[0].is_optional else ''), | 482 info.callback_args[0].is_optional else ''), |
| 484 VARIABLE_NAME= '' if len(callback_info.param_infos) == 0 else 'value', | 483 VARIABLE_NAME= '' if len(callback_info.param_infos) == 0 else 'value', |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 walk(interface.parents) | 593 walk(interface.parents) |
| 595 else: | 594 else: |
| 596 walk(interface.parents[1:]) | 595 walk(interface.parents[1:]) |
| 597 return result | 596 return result |
| 598 | 597 |
| 599 def _DartType(self, type_name): | 598 def _DartType(self, type_name): |
| 600 return self._type_registry.DartType(type_name) | 599 return self._type_registry.DartType(type_name) |
| 601 | 600 |
| 602 def _IsPrivate(self, name): | 601 def _IsPrivate(self, name): |
| 603 return name.startswith('_') | 602 return name.startswith('_') |
| OLD | NEW |