Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1109)

Side by Side Diff: lib/dom/scripts/generator.py

Issue 10827428: Dispatch with conversion hooks (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | lib/dom/scripts/systemhtml.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 systems to generate 6 """This module provides shared functionality for systems to generate
7 Dart APIs from the IDL database.""" 7 Dart APIs from the IDL database."""
8 8
9 import copy 9 import copy
10 import re 10 import re
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 type_id: Original type id. None for merged types. 162 type_id: Original type id. None for merged types.
163 dart_type: DartType of parameter. 163 dart_type: DartType of parameter.
164 is_optional: Parameter optionality. 164 is_optional: Parameter optionality.
165 """ 165 """
166 def __init__(self, name, type_id, dart_type, is_optional): 166 def __init__(self, name, type_id, dart_type, is_optional):
167 self.name = name 167 self.name = name
168 self.type_id = type_id 168 self.type_id = type_id
169 self.dart_type = dart_type 169 self.dart_type = dart_type
170 self.is_optional = is_optional 170 self.is_optional = is_optional
171 171
172 def Copy(self):
173 return ParamInfo(self.name, self.type_id, self.dart_type, self.is_optional)
174
172 def __repr__(self): 175 def __repr__(self):
173 content = 'name = %s, type_id = %s, dart_type = %s, is_optional = %s' % ( 176 content = 'name = %s, type_id = %s, dart_type = %s, is_optional = %s' % (
174 self.name, self.type_id, self.dart_type, self.is_optional) 177 self.name, self.type_id, self.dart_type, self.is_optional)
175 return '<ParamInfo(%s)>' % content 178 return '<ParamInfo(%s)>' % content
176 179
177 180
178 # Given a list of overloaded arguments, render a dart argument. 181 # Given a list of overloaded arguments, render a dart argument.
179 def _DartArg(args, interface, constructor=False): 182 def _DartArg(args, interface, constructor=False):
180 # Given a list of overloaded arguments, choose a suitable name. 183 # Given a list of overloaded arguments, choose a suitable name.
181 def OverloadedName(args): 184 def OverloadedName(args):
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 is_static = self.overloads[0].is_static 409 is_static = self.overloads[0].is_static
407 assert any([is_static == o.is_static for o in self.overloads]) 410 assert any([is_static == o.is_static for o in self.overloads])
408 return is_static 411 return is_static
409 412
410 def ConstructorFullName(self): 413 def ConstructorFullName(self):
411 if self.constructor_name: 414 if self.constructor_name:
412 return self.type_name + '.' + self.constructor_name 415 return self.type_name + '.' + self.constructor_name
413 else: 416 else:
414 return self.type_name 417 return self.type_name
415 418
419 def CopyAndWidenDefaultParameters(self):
420 """Returns equivalent OperationInfo, but default parameters are Dynamic."""
421 info = copy.copy(self)
422 info.param_infos = [param.Copy() for param in self.param_infos]
423 for param in info.param_infos:
424 if param.is_optional:
425 param.dart_type = 'Dynamic'
426 return info
427
416 428
417 def ConstantOutputOrder(a, b): 429 def ConstantOutputOrder(a, b):
418 """Canonical output ordering for constants.""" 430 """Canonical output ordering for constants."""
419 if a.id < b.id: return -1 431 if a.id < b.id: return -1
420 if a.id > b.id: return 1 432 if a.id > b.id: return 1
421 return 0 433 return 0
422 434
423 435
424 def _FormatNameList(names): 436 def _FormatNameList(names):
425 """Returns JavaScript array literal expression with one name per line.""" 437 """Returns JavaScript array literal expression with one name per line."""
(...skipping 16 matching lines...) Expand all
442 return ''.join(FormatLine(line) for line in text.split('\n')) 454 return ''.join(FormatLine(line) for line in text.split('\n'))
443 455
444 # Given a sorted sequence of type identifiers, return an appropriate type 456 # Given a sorted sequence of type identifiers, return an appropriate type
445 # name 457 # name
446 def TypeName(type_ids, interface): 458 def TypeName(type_ids, interface):
447 # Dynamically type this field for now. 459 # Dynamically type this field for now.
448 return 'Dynamic' 460 return 'Dynamic'
449 461
450 # ------------------------------------------------------------------------------ 462 # ------------------------------------------------------------------------------
451 463
464 class Conversion(object):
465 """Represents a way of converting between types."""
466 def __init__(self, name, input_type, output_type):
467 # input_type is the type of the API input (and the argument type of the
468 # conversion function)
469 # output_type is the type of the API output (and the result type of the
470 # conversion function)
471 self.function_name = name
472 self.input_type = input_type
473 self.output_type = output_type
474
475 # TYPE -> "DIRECTION INTERFACE.MEMBER" -> conversion
476 # TYPE -> "DIRECTION INTERFACE.*" -> conversion
477 # TYPE -> "DIRECTION" -> conversion
478 #
479 # where DIRECTION is 'get' for getters and operation return values, 'set' for
480 # setters and operation arguments. INTERFACE and MEMBER are the idl names.
481 #
482 dart2js_conversions = {
483 'IDBKey': {
484 'get':
485 Conversion('_convertNativeToDart_IDBKey', 'Dynamic', 'Dynamic'),
486 'set':
487 Conversion('_convertDartToNative_IDBKey', 'Dynamic', 'Dynamic'),
488 },
489 'ImageData': {
490 'get':
491 Conversion('_convertNativeToDart_ImageData', 'Dynamic', 'ImageData'),
492 'set':
493 Conversion('_convertDartToNative_ImageData', 'ImageData', 'Dynamic')
494 },
495 'Dictionary': {
496 'get':
497 Conversion('_convertNativeToDart_Dictionary', 'Dynamic', 'Map'),
498 'set':
499 Conversion('_convertDartToNative_Dictionary', 'Map', 'Dynamic'),
500 },
501
502 'DOMString[]': {
503 'set':
504 Conversion(
505 '_convertDartToNative_StringArray', 'List<String>', 'List'),
506 },
507 }
508
509 def FindConversion(idl_type, direction, interface, member):
510 table = dart2js_conversions.get(idl_type)
511 if table:
512 return (table.get('%s %s.%s' % (direction, interface, member)) or
513 table.get('%s %s.*' % (direction, interface)) or
514 table.get(direction))
515 return None
516
517 # ------------------------------------------------------------------------------
518
452 class IDLTypeInfo(object): 519 class IDLTypeInfo(object):
453 def __init__(self, idl_type, data): 520 def __init__(self, idl_type, data):
454 self._idl_type = idl_type 521 self._idl_type = idl_type
455 self._data = data 522 self._data = data
456 523
457 def idl_type(self): 524 def idl_type(self):
458 return self._idl_type 525 return self._idl_type
459 526
460 def dart_type(self): 527 def dart_type(self):
461 return self._data.dart_type or self._idl_type 528 return self._data.dart_type or self._idl_type
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
780 if match: 847 if match:
781 if type_name == 'DOMString[]': 848 if type_name == 'DOMString[]':
782 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt ring')) 849 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt ring'))
783 item_info = self.TypeInfo(match.group(1) or match.group(2)) 850 item_info = self.TypeInfo(match.group(1) or match.group(2))
784 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) 851 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info)
785 if not type_name in _idl_type_registry: 852 if not type_name in _idl_type_registry:
786 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) 853 return InterfaceIDLTypeInfo(type_name, TypeData('Interface'))
787 type_data = _idl_type_registry.get(type_name) 854 type_data = _idl_type_registry.get(type_name)
788 class_name = '%sIDLTypeInfo' % type_data.clazz 855 class_name = '%sIDLTypeInfo' % type_data.clazz
789 return globals()[class_name](type_name, type_data) 856 return globals()[class_name](type_name, type_data)
OLDNEW
« no previous file with comments | « no previous file | lib/dom/scripts/systemhtml.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698