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

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

Issue 10915245: Removing interfaces from dart:html (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Further fixes to dartium. Created 8 years, 3 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
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 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 implementation. 355 implementation.
356 356
357 Args: 357 Args:
358 rename_type: A function that allows the types to be renamed. 358 rename_type: A function that allows the types to be renamed.
359 The function is applied to the parameter's dart_type. 359 The function is applied to the parameter's dart_type.
360 """ 360 """
361 return self._FormatParams( 361 return self._FormatParams(
362 self.param_infos, 'null', 362 self.param_infos, 'null',
363 lambda param: TypeOrNothing(rename_type(param.dart_type))) 363 lambda param: TypeOrNothing(rename_type(param.dart_type)))
364 364
365 def ParametersAsArgumentList(self): 365 def ParametersAsArgumentList(self, parameter_count = None):
366 """Returns a string of the parameter names suitable for passing the 366 """Returns a string of the parameter names suitable for passing the
367 parameters as arguments. 367 parameters as arguments.
368 """ 368 """
369 return ', '.join(map(lambda param_info: param_info.name, self.param_infos)) 369 if parameter_count is None:
Anton Muhin 2012/09/17 10:11:28 please, do not duplicate common logic. Instead, s
blois 2012/09/17 19:48:22 Done.
370 return ', '.join(map(lambda param_info: param_info.name, self.param_infos) )
371 return ', '.join(map(
372 lambda param_info: param_info.name,
373 self.param_infos[:parameter_count]))
370 374
371 def _FormatParams(self, params, default_value, type_fn): 375 def _FormatParams(self, params, default_value, type_fn):
372 def FormatParam(param): 376 def FormatParam(param):
373 """Returns a parameter declaration fragment for an ParamInfo.""" 377 """Returns a parameter declaration fragment for an ParamInfo."""
374 type = type_fn(param) 378 type = type_fn(param)
375 if param.is_optional and default_value and default_value != 'null': 379 if param.is_optional and default_value and default_value != 'null':
376 return '%s%s = %s' % (type, param.name, default_value) 380 return '%s%s = %s' % (type, param.name, default_value)
377 return '%s%s' % (type, param.name) 381 return '%s%s' % (type, param.name)
378 382
379 required = [] 383 required = []
380 optional = [] 384 optional = []
381 for param_info in params: 385 for param_info in params:
382 if param_info.is_optional: 386 if param_info.is_optional:
383 optional.append(param_info) 387 optional.append(param_info)
384 else: 388 else:
385 if optional: 389 if optional:
386 raise Exception('Optional parameters cannot precede required ones: ' 390 raise Exception('Optional parameters cannot precede required ones: '
387 + str(params)) 391 + str(params))
388 required.append(param_info) 392 required.append(param_info)
389 argtexts = map(FormatParam, required) 393 argtexts = map(FormatParam, required)
390 if optional: 394 if optional:
391 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']') 395 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']')
392 return ', '.join(argtexts) 396 return ', '.join(argtexts)
393 397
394 def IsStatic(self): 398 def IsStatic(self):
395 is_static = self.overloads[0].is_static 399 is_static = self.overloads[0].is_static
396 assert any([is_static == o.is_static for o in self.overloads]) 400 assert any([is_static == o.is_static for o in self.overloads])
397 return is_static 401 return is_static
398 402
399 def ConstructorFullName(self): 403 def ConstructorFullName(self):
Anton Muhin 2012/09/17 10:11:28 with your change, are there any other references t
blois 2012/09/17 19:48:22 Only within this file, made it private.
400 if self.constructor_name: 404 if self.constructor_name:
401 return self.type_name + '.' + self.constructor_name 405 return self.type_name + '.' + self.constructor_name
402 else: 406 else:
403 return self.type_name 407 return self.type_name
404 408
409 def ConstructorFactoryName(self, rename_type):
410 type_name = rename_type(self.type_name)
Anton Muhin 2012/09/17 10:11:28 why you rename type here, but it's not renamed in
blois 2012/09/17 19:48:22 The rename_type here converts between DOM names an
411 if self.constructor_name:
412 return 'create' + type_name + '_' + self.constructor_name
413 else:
414 return 'create' + type_name
415
416 def GenerateFactoryInvocation(self, rename_type, emitter, factory_provider):
417 has_optional = False
Anton Muhin 2012/09/17 10:11:28 has_optional = any(param_info.is_optional for para
blois 2012/09/17 19:48:22 Done.
418 for param_info in self.param_infos:
419 if param_info.is_optional:
420 has_optional = True
421
422 factory_name = self.ConstructorFactoryName(rename_type)
423 if not has_optional:
424 emitter.Emit(
425 '\n'
426 ' factory $CTOR($PARAMS) => $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARA MS);\n',
427 CTOR=rename_type(self.ConstructorFullName()),
428 PARAMS=self.ParametersInterfaceDeclaration(rename_type),
429 FACTORY=factory_provider,
430 CTOR_FACTORY_NAME=factory_name,
431 FACTORY_PARAMS=self.ParametersAsArgumentList())
432 return
433
434 # If we have optional parameters, check to see if they are set
435 # and call the appropriate factory method.
436 def EmitOptionalParameterInvocation(index):
437 emitter.Emit(
438 ' if (!?$(OPT_PARAM_NAME)) {\n'
Anton Muhin 2012/09/17 10:11:28 nit: why OPT_PARAM_NAME is in parentheses?
blois 2012/09/17 19:48:22 Probably leftover from something else. Converted.
439 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n'
440 ' }\n',
441 OPT_PARAM_NAME=self.param_infos[index].name,
442 FACTORY=factory_provider,
443 CTOR_FACTORY_NAME=factory_name,
444 FACTORY_PARAMS=self.ParametersAsArgumentList(index))
445
446 emitter.Emit(
Anton Muhin 2012/09/17 10:11:28 I'd rather do something like: dispatcher_emitter
blois 2012/09/17 19:48:22 Done. Wasn't aware of that trick! On 2012/09/17 1
447 '\n factory $CTOR($PARAMS) {\n',
448 CTOR=rename_type(self.ConstructorFullName()),
449 PARAMS=self.ParametersInterfaceDeclaration(rename_type))
450
451 index = 0
452 for param_info in self.param_infos:
Anton Muhin 2012/09/17 10:11:28 for index, param_info in enumerate(self.param_info
blois 2012/09/17 19:48:22 Done.
453 if param_info.is_optional:
454 EmitOptionalParameterInvocation(index)
455 index += 1
456
457 emitter.Emit(
458 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n',
459 FACTORY=factory_provider,
460 CTOR_FACTORY_NAME=factory_name,
461 FACTORY_PARAMS=self.ParametersAsArgumentList())
462
463 emitter.Emit(' }\n')
464
465
405 def CopyAndWidenDefaultParameters(self): 466 def CopyAndWidenDefaultParameters(self):
406 """Returns equivalent OperationInfo, but default parameters are Dynamic.""" 467 """Returns equivalent OperationInfo, but default parameters are Dynamic."""
407 info = copy.copy(self) 468 info = copy.copy(self)
408 info.param_infos = [param.Copy() for param in self.param_infos] 469 info.param_infos = [param.Copy() for param in self.param_infos]
409 for param in info.param_infos: 470 for param in info.param_infos:
410 if param.is_optional: 471 if param.is_optional:
411 param.dart_type = 'Dynamic' 472 param.dart_type = 'Dynamic'
412 return info 473 return info
413 474
414 475
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 if match: 947 if match:
887 if type_name == 'DOMString[]': 948 if type_name == 'DOMString[]':
888 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt ring')) 949 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt ring'))
889 item_info = self.TypeInfo(match.group(1) or match.group(2)) 950 item_info = self.TypeInfo(match.group(1) or match.group(2))
890 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) 951 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info)
891 if not type_name in _idl_type_registry: 952 if not type_name in _idl_type_registry:
892 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) 953 return InterfaceIDLTypeInfo(type_name, TypeData('Interface'))
893 type_data = _idl_type_registry.get(type_name) 954 type_data = _idl_type_registry.get(type_name)
894 class_name = '%sIDLTypeInfo' % type_data.clazz 955 class_name = '%sIDLTypeInfo' % type_data.clazz
895 return globals()[class_name](type_name, type_data) 956 return globals()[class_name](type_name, type_data)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698