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

Side by Side Diff: tools/dom/scripts/systemhtml.py

Issue 1023363002: Make dartj2s do type conversions on constructor arguments (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Line length Created 5 years, 9 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 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 import logging 10 import logging
(...skipping 979 matching lines...) Expand 10 before | Expand all | Expand 10 after
990 '\n' 990 '\n'
991 ' $RENAME$METADATA$MODIFIERS$TYPE $NAME($PARAMS) native;\n', 991 ' $RENAME$METADATA$MODIFIERS$TYPE $NAME($PARAMS) native;\n',
992 RENAME=self._RenamingAnnotation(info.declared_name, html_name), 992 RENAME=self._RenamingAnnotation(info.declared_name, html_name),
993 METADATA=self._Metadata(info.type_name, info.declared_name, 993 METADATA=self._Metadata(info.type_name, info.declared_name,
994 self.SecureOutputType(info.type_name)), 994 self.SecureOutputType(info.type_name)),
995 MODIFIERS='static ' if info.IsStatic() else '', 995 MODIFIERS='static ' if info.IsStatic() else '',
996 TYPE=self.SecureOutputType(info.type_name, False, True), 996 TYPE=self.SecureOutputType(info.type_name, False, True),
997 NAME=html_name, 997 NAME=html_name,
998 PARAMS=info.ParametersAsDeclaration(self._NarrowInputType)) 998 PARAMS=info.ParametersAsDeclaration(self._NarrowInputType))
999 999
1000 def _ConvertArgumentTypes(
1001 self, stmts_emitter, arguments, argument_count, info):
1002 temp_version = [0]
1003 converted_arguments = []
1004 target_parameters = []
1005 for position, arg in enumerate(arguments[:argument_count]):
1006 conversion = self._InputConversion(arg.type.id, info.declared_name)
1007 param_name = arguments[position].id
1008 if conversion:
1009 temp_version[0] += 1
1010 temp_name = '%s_%s' % (param_name, temp_version[0])
1011 temp_type = conversion.output_type
1012 stmts_emitter.Emit(
1013 '$(INDENT)$TYPE $NAME = $CONVERT($ARG);\n',
1014 TYPE=TypeOrVar(temp_type),
1015 NAME=temp_name,
1016 CONVERT=conversion.function_name,
1017 ARG=info.param_infos[position].name)
1018 converted_arguments.append(temp_name)
1019 param_type = temp_type
1020 verified_type = temp_type # verified by assignment in checked mode.
1021 else:
1022 converted_arguments.append(info.param_infos[position].name)
1023 param_type = self._NarrowInputType(arg.type.id)
1024 # Verified by argument checking on entry to the dispatcher.
1025
1026 verified_type = self._InputType(
1027 info.param_infos[position].type_id, info)
1028 # The native method does not need an argument type if we know the type.
1029 # But we do need the native methods to have correct function types, so
1030 # be conservative.
1031 if param_type == verified_type:
1032 if param_type in ['String', 'num', 'int', 'double', 'bool', 'Object']:
1033 param_type = 'dynamic'
1034
1035 target_parameters.append(
1036 '%s%s' % (TypeOrNothing(param_type), param_name))
1037
1038 return target_parameters, converted_arguments
1039
1040 def _InputType(self, type_name, info):
1041 conversion = self._InputConversion(type_name, info.declared_name)
1042 if conversion:
1043 return conversion.input_type
1044 else:
1045 return self._NarrowInputType(type_name) if type_name else 'dynamic'
1046
1000 def _AddOperationWithConversions(self, info, html_name): 1047 def _AddOperationWithConversions(self, info, html_name):
1001 # Assert all operations have same return type. 1048 # Assert all operations have same return type.
1002 assert len(set([op.type.id for op in info.operations])) == 1 1049 assert len(set([op.type.id for op in info.operations])) == 1
1003 output_conversion = self._OutputConversion(info.type_name, 1050 output_conversion = self._OutputConversion(info.type_name,
1004 info.declared_name) 1051 info.declared_name)
1005 if output_conversion: 1052 if output_conversion:
1006 return_type = output_conversion.output_type 1053 return_type = output_conversion.output_type
1007 native_return_type = output_conversion.input_type 1054 native_return_type = output_conversion.input_type
1008 else: 1055 else:
1009 return_type = self._NarrowInputType(info.type_name) 1056 return_type = self._NarrowInputType(info.type_name)
1010 native_return_type = return_type 1057 native_return_type = return_type
1011 1058
1012 def InputType(type_name):
1013 conversion = self._InputConversion(type_name, info.declared_name)
1014 if conversion:
1015 return conversion.input_type
1016 else:
1017 return self._NarrowInputType(type_name) if type_name else 'dynamic'
1018
1019 parameter_names = [param_info.name for param_info in info.param_infos] 1059 parameter_names = [param_info.name for param_info in info.param_infos]
1020 parameter_types = [InputType(param_info.type_id) 1060 parameter_types = [self._InputType(param_info.type_id, info)
1021 for param_info in info.param_infos] 1061 for param_info in info.param_infos]
1022 operations = info.operations 1062 operations = info.operations
1023 1063
1024 temp_version = [0] 1064 def InputType(type_name):
1065 return self._InputType(type_name, info)
terry 2015/03/23 14:45:59 Why does InputType exist can't you just all use _I
Alan Knight 2015/03/23 18:00:12 I have to pass it as a closure, so I needed someth
1025 1066
1026 def GenerateCall( 1067 def GenerateCall(
1027 stmts_emitter, call_emitter, version, operation, argument_count): 1068 stmts_emitter, call_emitter, version, operation, argument_count):
1028 target = '_%s_%d' % ( 1069 target = '_%s_%d' % (
1029 html_name[1:] if html_name.startswith('_') else html_name, version); 1070 html_name[1:] if html_name.startswith('_') else html_name, version);
1030 arguments = []
1031 target_parameters = []
1032 for position, arg in enumerate(operation.arguments[:argument_count]):
1033 conversion = self._InputConversion(arg.type.id, operation.id)
1034 param_name = operation.arguments[position].id
1035 if conversion:
1036 temp_version[0] += 1
1037 temp_name = '%s_%s' % (param_name, temp_version[0])
1038 temp_type = conversion.output_type
1039 stmts_emitter.Emit(
1040 '$(INDENT)$TYPE $NAME = $CONVERT($ARG);\n',
1041 TYPE=TypeOrVar(temp_type),
1042 NAME=temp_name,
1043 CONVERT=conversion.function_name,
1044 ARG=parameter_names[position])
1045 arguments.append(temp_name)
1046 param_type = temp_type
1047 verified_type = temp_type # verified by assignment in checked mode.
1048 else:
1049 arguments.append(parameter_names[position])
1050 param_type = self._NarrowInputType(arg.type.id)
1051 # Verified by argument checking on entry to the dispatcher.
1052 1071
1053 verified_type = InputType(info.param_infos[position].type_id) 1072 (target_parameters, arguments) = self._ConvertArgumentTypes(
1054 # The native method does not need an argument type if we know the type . 1073 stmts_emitter, operation.arguments, argument_count, info)
1055 # But we do need the native methods to have correct function types, so
1056 # be conservative.
1057 if param_type == verified_type:
1058 if param_type in ['String', 'num', 'int', 'double', 'bool', 'Object' ]:
1059 param_type = 'dynamic'
1060
1061 target_parameters.append(
1062 '%s%s' % (TypeOrNothing(param_type), param_name))
1063 1074
1064 argument_list = ', '.join(arguments) 1075 argument_list = ', '.join(arguments)
1065 # TODO(sra): If the native method has zero type checks, we can 'inline' is 1076 # TODO(sra): If the native method has zero type checks, we can 'inline' is
1066 # and call it directly with a JS-expression. 1077 # and call it directly with a JS-expression.
1067 call = '%s(%s)' % (target, argument_list) 1078 call = '%s(%s)' % (target, argument_list)
1068 1079
1069 if output_conversion: 1080 if output_conversion:
1070 call = '%s(%s)' % (output_conversion.function_name, call) 1081 call = '%s(%s)' % (output_conversion.function_name, call)
1071 1082
1072 call_emitter.Emit(call) 1083 call_emitter.Emit(call)
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after
1275 1286
1276 def AddFile(self, basename, library_name, path): 1287 def AddFile(self, basename, library_name, path):
1277 self._libraries[library_name].AddFile(path) 1288 self._libraries[library_name].AddFile(path)
1278 1289
1279 def AddTypeEntry(self, library_name, idl_name, dart_name): 1290 def AddTypeEntry(self, library_name, idl_name, dart_name):
1280 self._libraries[library_name].AddTypeEntry(idl_name, dart_name) 1291 self._libraries[library_name].AddTypeEntry(idl_name, dart_name)
1281 1292
1282 def Emit(self, emitter, auxiliary_dir): 1293 def Emit(self, emitter, auxiliary_dir):
1283 for lib in self._libraries.values(): 1294 for lib in self._libraries.values():
1284 lib.Emit(emitter, auxiliary_dir) 1295 lib.Emit(emitter, auxiliary_dir)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698