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

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: table 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') | lib/dom/scripts/systemhtml.py » ('J')
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.
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', 'Object', 'ImageData'),
492 'set':
493 Conversion('_convertDartToNative_ImageData', 'ImageData', 'Object')
494 },
495 'Dictionary': {
496 'get':
497 Conversion('_convertNativeToDart_Dictionary', 'Object', 'Map'),
498 'set':
499 Conversion('_convertDartToNative_Dictionary', 'Map', 'Object'),
500 },
501
502 'DOMString[]': {
503 'set':
504 Conversion('_ensureStringArray', 'List<String>', 'Object'),
505 },
506 }
507
508 def FindConversion(idl_type, direction, interface, member):
509 table = dart2js_conversions.get(idl_type)
510 if table:
511 return (table.get('%s %s.%s' % (direction, interface, member)) or
512 table.get('%s %s.*' % (direction, interface)) or
513 table.get(direction))
514 return None
515
516 # ------------------------------------------------------------------------------
517
452 class IDLTypeInfo(object): 518 class IDLTypeInfo(object):
453 def __init__(self, idl_type, data): 519 def __init__(self, idl_type, data):
454 self._idl_type = idl_type 520 self._idl_type = idl_type
455 self._data = data 521 self._data = data
456 522
457 def idl_type(self): 523 def idl_type(self):
458 return self._idl_type 524 return self._idl_type
459 525
460 def dart_type(self): 526 def dart_type(self):
461 return self._data.dart_type or self._idl_type 527 return self._data.dart_type or self._idl_type
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
816 if match: 882 if match:
817 if type_name == 'DOMString[]': 883 if type_name == 'DOMString[]':
818 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt ring')) 884 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt ring'))
819 item_info = self.TypeInfo(match.group(1) or match.group(2)) 885 item_info = self.TypeInfo(match.group(1) or match.group(2))
820 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) 886 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info)
821 if not type_name in _idl_type_registry: 887 if not type_name in _idl_type_registry:
822 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) 888 return InterfaceIDLTypeInfo(type_name, TypeData('Interface'))
823 type_data = _idl_type_registry.get(type_name) 889 type_data = _idl_type_registry.get(type_name)
824 class_name = '%sIDLTypeInfo' % type_data.clazz 890 class_name = '%sIDLTypeInfo' % type_data.clazz
825 return globals()[class_name](type_name, type_data) 891 return globals()[class_name](type_name, type_data)
OLDNEW
« no previous file with comments | « no previous file | lib/dom/scripts/systemhtml.py » ('j') | lib/dom/scripts/systemhtml.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698