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 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 json | 10 import json |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 optional is a list of parameter declarations corresponding to the | 450 optional is a list of parameter declarations corresponding to the |
451 optional parameters | 451 optional parameters |
452 named is a boolean which is true if the optional parameters should | 452 named is a boolean which is true if the optional parameters should |
453 be named | 453 be named |
454 A parameter declaration is a tuple (dec, var) where var is the | 454 A parameter declaration is a tuple (dec, var) where var is the |
455 variable name, and dec is a string suitable for declaring the | 455 variable name, and dec is a string suitable for declaring the |
456 variable in a parameter list. That is, dec + var is a valid | 456 variable in a parameter list. That is, dec + var is a valid |
457 parameter declaration. | 457 parameter declaration. |
458 """ | 458 """ |
459 def FormatParam(param): | 459 def FormatParam(param): |
460 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' | 460 # Is the type a typedef if so it's a union so it's dynamic. |
| 461 # TODO(terry): This may have to change for dart2js for code shaking the |
| 462 # return types (unions) needs to be emitted with @create |
| 463 # annotations and/or with JS('type1|type2',...) |
| 464 if hasattr(rename_type, 'im_self') and rename_type.im_self._database.HasTy
peDef(param.type_id): |
| 465 dart_type = 'dynamic' |
| 466 else: |
| 467 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' |
461 return (TypeOrNothing(dart_type, param.type_id), param.name) | 468 return (TypeOrNothing(dart_type, param.type_id), param.name) |
462 required = [] | 469 required = [] |
463 optional = [] | 470 optional = [] |
464 for param_info in self.param_infos: | 471 for param_info in self.param_infos: |
465 if param_info.is_optional: | 472 if param_info.is_optional: |
466 optional.append(FormatParam(param_info)) | 473 optional.append(FormatParam(param_info)) |
467 else: | 474 else: |
468 if optional: | 475 if optional: |
469 raise Exception('Optional parameters cannot precede required ones: ' | 476 raise Exception('Optional parameters cannot precede required ones: ' |
470 + str(param_info)) | 477 + str(param_info)) |
(...skipping 889 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1360 | 1367 |
1361 class TypeRegistry(object): | 1368 class TypeRegistry(object): |
1362 def __init__(self, database, renamer=None): | 1369 def __init__(self, database, renamer=None): |
1363 self._database = database | 1370 self._database = database |
1364 self._renamer = renamer | 1371 self._renamer = renamer |
1365 self._cache = {} | 1372 self._cache = {} |
1366 | 1373 |
1367 def HasInterface(self, type_name): | 1374 def HasInterface(self, type_name): |
1368 return self._database.HasInterface(type_name) | 1375 return self._database.HasInterface(type_name) |
1369 | 1376 |
| 1377 def HasTypeDef(self, type_def_name): |
| 1378 return self._database.HasTypeDef(type_def_name) |
| 1379 |
1370 def TypeInfo(self, type_name): | 1380 def TypeInfo(self, type_name): |
1371 if not type_name in self._cache: | 1381 if not type_name in self._cache: |
1372 self._cache[type_name] = self._TypeInfo(type_name) | 1382 self._cache[type_name] = self._TypeInfo(type_name) |
1373 return self._cache[type_name] | 1383 return self._cache[type_name] |
1374 | 1384 |
1375 def DartType(self, type_name): | 1385 def DartType(self, type_name): |
1376 return self.TypeInfo(type_name).dart_type() | 1386 return self.TypeInfo(type_name).dart_type() |
1377 | 1387 |
1378 def _TypeInfo(self, type_name): | 1388 def _TypeInfo(self, type_name): |
1379 match = re.match(r'(?:sequence<([\w ]+)>|(\w+)\[\])$', type_name) | 1389 match = re.match(r'(?:sequence<([\w ]+)>|(\w+)\[\])$', type_name) |
1380 if match: | 1390 if match: |
1381 type_data = TypeData('Sequence') | 1391 type_data = TypeData('Sequence') |
1382 item_info = self.TypeInfo(match.group(1) or match.group(2)) | 1392 if self.HasTypeDef(match.group(1) or match.group(2)): |
| 1393 # It's a typedef (union) |
| 1394 item_info = self.TypeInfo('any') |
| 1395 else: |
| 1396 item_info = self.TypeInfo(match.group(1) or match.group(2)) |
1383 # TODO(vsm): Generalize this code. | 1397 # TODO(vsm): Generalize this code. |
1384 if 'SourceInfo' in type_name: | 1398 if 'SourceInfo' in type_name: |
1385 type_data.native_type = 'const Vector<RefPtr<SourceInfo> >& ' | 1399 type_data.native_type = 'const Vector<RefPtr<SourceInfo> >& ' |
1386 return SequenceIDLTypeInfo(type_name, type_data, item_info) | 1400 return SequenceIDLTypeInfo(type_name, type_data, item_info) |
1387 | 1401 |
1388 if not type_name in _idl_type_registry: | 1402 if not type_name in _idl_type_registry: |
1389 if self._database.HasEnum(type_name): | 1403 if self._database.HasEnum(type_name): |
1390 return PrimitiveIDLTypeInfo( | 1404 return PrimitiveIDLTypeInfo( |
1391 type_name, | 1405 type_name, |
1392 TypeData(clazz='Primitive', dart_type='String', native_type='String'
)) | 1406 TypeData(clazz='Primitive', dart_type='String', native_type='String'
)) |
1393 | 1407 |
1394 if self._database.HasInterface(type_name): | 1408 if self._database.HasInterface(type_name): |
1395 interface = self._database.GetInterface(type_name) | 1409 interface = self._database.GetInterface(type_name) |
1396 else: | 1410 elif self._database.HasDictionary(type_name): |
1397 interface = self._database.GetDictionary(type_name) | 1411 interface = self._database.GetDictionary(type_name) |
| 1412 elif type_name.startswith('sequence<('): |
| 1413 if type_name.find(' or ') != -1: |
| 1414 # Union type of sequence is an any type (no type). |
| 1415 type_data = TypeData('Sequence') |
| 1416 item_info = self.TypeInfo('any') |
| 1417 return SequenceIDLTypeInfo(type_name, type_data, item_info) |
| 1418 elif type_name.startswith('sequence<sequence<'): |
| 1419 # TODO(terry): Cleanup up list of list, etc. |
| 1420 type_data = TypeData('Sequence') |
| 1421 item_info = self.TypeInfo('any') |
| 1422 return SequenceIDLTypeInfo(type_name, type_data, item_info) |
| 1423 elif self.HasTypeDef(type_name): |
| 1424 # It's a typedef (implied union) |
| 1425 return self.TypeInfo('any') |
| 1426 |
1398 if 'Callback' in interface.ext_attrs: | 1427 if 'Callback' in interface.ext_attrs: |
1399 return CallbackIDLTypeInfo(type_name, TypeData('Callback', | 1428 return CallbackIDLTypeInfo(type_name, TypeData('Callback', |
1400 self._renamer.DartifyTypeName(type_name))) | 1429 self._renamer.DartifyTypeName(type_name))) |
1401 return InterfaceIDLTypeInfo( | 1430 return InterfaceIDLTypeInfo( |
1402 type_name, | 1431 type_name, |
1403 TypeData('Interface'), | 1432 TypeData('Interface'), |
1404 self._renamer.RenameInterface(interface), | 1433 self._renamer.RenameInterface(interface), |
1405 self) | 1434 self) |
1406 | 1435 |
1407 type_data = _idl_type_registry.get(type_name) | 1436 type_data = _idl_type_registry.get(type_name) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1462 return_type == 'Rectangle') | 1491 return_type == 'Rectangle') |
1463 | 1492 |
1464 def wrap_return_type_blink(return_type, type_name, type_registry): | 1493 def wrap_return_type_blink(return_type, type_name, type_registry): |
1465 """Returns True if we should wrap the returned value. This checks | 1494 """Returns True if we should wrap the returned value. This checks |
1466 a number of different variations, calling the more basic functions | 1495 a number of different variations, calling the more basic functions |
1467 above.""" | 1496 above.""" |
1468 return (wrap_unwrap_type_blink(return_type, type_registry) or | 1497 return (wrap_unwrap_type_blink(return_type, type_registry) or |
1469 wrap_unwrap_type_blink(type_name, type_registry) or | 1498 wrap_unwrap_type_blink(type_name, type_registry) or |
1470 wrap_type_blink(return_type, type_registry) or | 1499 wrap_type_blink(return_type, type_registry) or |
1471 wrap_unwrap_list_blink(return_type, type_registry)) | 1500 wrap_unwrap_list_blink(return_type, type_registry)) |
OLD | NEW |