| 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 the systems to generate | 6 """This module provides shared functionality for the systems to generate |
| 7 native binding from the IDL database.""" | 7 native binding from the IDL database.""" |
| 8 | 8 |
| 9 import emitter | 9 import emitter |
| 10 import os | 10 import os |
| (...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 if interface_name == self._interface.id or not self._database.HasInterface(i
nterface_name): | 577 if interface_name == self._interface.id or not self._database.HasInterface(i
nterface_name): |
| 578 template_file = 'impl_%s.darttemplate' % interface_name | 578 template_file = 'impl_%s.darttemplate' % interface_name |
| 579 template = self._template_loader.TryLoad(template_file) | 579 template = self._template_loader.TryLoad(template_file) |
| 580 if not template: | 580 if not template: |
| 581 template = self._template_loader.Load('dart_implementation.darttemplate') | 581 template = self._template_loader.Load('dart_implementation.darttemplate') |
| 582 return template | 582 return template |
| 583 | 583 |
| 584 def RootClassName(self): | 584 def RootClassName(self): |
| 585 return 'NativeFieldWrapperClass2' | 585 return 'NativeFieldWrapperClass2' |
| 586 | 586 |
| 587 def DeriveNativeEntry(self, operation_id, native_suffix, type_ids, skip_types)
: | 587 # This code matches up with the _generate_native_entry code in |
| 588 # dart_utilities.py in the dartium repository. Any changes to this |
| 589 # should have matching changes on that end. |
| 590 def DeriveNativeEntry(self, name, kind, count): |
| 588 interface_id = self._interface.id | 591 interface_id = self._interface.id |
| 589 database = self._database | 592 database = self._database |
| 590 type_ids = map(lambda type_id : TypeIdToBlinkName(type_id, database), | 593 tag = "" |
| 591 type_ids) | 594 if kind == 'Getter': |
| 592 encoded_type_ids = map(EncodeType, type_ids) | 595 tag = "%s_Getter" % name |
| 593 if native_suffix: | 596 blink_entry = tag |
| 594 operation_id = "%s_%s" % (operation_id, native_suffix) | 597 elif kind == 'Setter': |
| 595 interface_id = TypeIdToBlinkName(interface_id, database) | 598 tag = "%s_Setter" % name |
| 599 blink_entry = tag |
| 600 elif kind == 'Constructor': |
| 601 tag = "constructorCallback" |
| 602 blink_entry = tag |
| 603 elif kind == 'Method': |
| 604 tag = "%s_Callback" % name |
| 605 blink_entry = tag |
| 596 | 606 |
| 597 def DeriveString(components, types, use_types): | 607 interface_id = TypeIdToBlinkName(interface_id, database) |
| 598 if use_types: | |
| 599 components.extend(types) | |
| 600 full_name = "_".join(components) | |
| 601 return full_name | |
| 602 | 608 |
| 603 def mkPublic(s): | 609 def mkPublic(s): |
| 604 if s.startswith("_") or s.startswith("$"): | 610 if s.startswith("_") or s.startswith("$"): |
| 605 return "$" + s | 611 return "$" + s |
| 606 return s | 612 return s |
| 607 | 613 |
| 608 dart_name = mkPublic(DeriveString([operation_id], encoded_type_ids, True)) | 614 if count: |
| 609 resolver_string = DeriveString([interface_id, operation_id], type_ids, | 615 arity = str(count) |
| 610 not skip_types) | 616 dart_name = mkPublic("_".join([tag, arity])) |
| 617 else: |
| 618 dart_name = mkPublic(tag) |
| 619 resolver_string = "_".join([interface_id, tag]) |
| 620 |
| 611 return (dart_name, resolver_string) | 621 return (dart_name, resolver_string) |
| 612 | 622 |
| 613 | 623 |
| 614 def DeriveNativeName(self, name, suffix=""): | 624 def DeriveNativeName(self, name, suffix=""): |
| 615 fields = ['$' + name] | 625 fields = ['$' + name] |
| 616 if suffix != "": | 626 if suffix != "": |
| 617 fields.append(suffix) | 627 fields.append(suffix) |
| 618 return "_".join(fields) | 628 return "_".join(fields) |
| 619 | 629 |
| 620 def DeriveQualifiedBlinkName(self, interface_name, name): | 630 def DeriveQualifiedBlinkName(self, interface_name, name): |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 696 if arguments is None: | 706 if arguments is None: |
| 697 arguments = constructor_info.idl_args[0] | 707 arguments = constructor_info.idl_args[0] |
| 698 argument_count = len(arguments) | 708 argument_count = len(arguments) |
| 699 else: | 709 else: |
| 700 argument_count = len(arguments) | 710 argument_count = len(arguments) |
| 701 | 711 |
| 702 typed_formals = constructor_info.ParametersAsArgumentList(argument_count) | 712 typed_formals = constructor_info.ParametersAsArgumentList(argument_count) |
| 703 parameters = constructor_info.ParametersAsStringOfVariables(argument_count) | 713 parameters = constructor_info.ParametersAsStringOfVariables(argument_count) |
| 704 interface_name = self._interface_type_info.interface_name() | 714 interface_name = self._interface_type_info.interface_name() |
| 705 | 715 |
| 706 type_ids = [p.type.id for p in arguments[:argument_count]] | |
| 707 dart_native_name, constructor_callback_id = \ | 716 dart_native_name, constructor_callback_id = \ |
| 708 self.DeriveNativeEntry(cpp_suffix, None, type_ids, is_custom) | 717 self.DeriveNativeEntry(cpp_suffix, 'Constructor', argument_count) |
| 709 if constructor_callback_id in _cpp_resolver_string_map: | 718 if constructor_callback_id in _cpp_resolver_string_map: |
| 710 constructor_callback_id = \ | 719 constructor_callback_id = \ |
| 711 _cpp_resolver_string_map[constructor_callback_id] | 720 _cpp_resolver_string_map[constructor_callback_id] |
| 712 if dart_native_name not in self._blink_entries: | 721 if dart_native_name not in self._blink_entries: |
| 713 self._blink_entries.add(dart_native_name) | 722 self._blink_entries.add(dart_native_name) |
| 714 self._native_class_emitter.Emit( | 723 self._native_class_emitter.Emit( |
| 715 '\n' | 724 '\n' |
| 716 ' static $FACTORY_METHOD_NAME($PARAMETERS) native "$ID";\n', | 725 ' static $FACTORY_METHOD_NAME($PARAMETERS) native "$ID";\n', |
| 717 FACTORY_METHOD_NAME=dart_native_name, | 726 FACTORY_METHOD_NAME=dart_native_name, |
| 718 PARAMETERS=parameters, | 727 PARAMETERS=parameters, |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 998 parameters = [] | 1007 parameters = [] |
| 999 dart_declaration = '%s get %s' % (return_type, html_name) | 1008 dart_declaration = '%s get %s' % (return_type, html_name) |
| 1000 is_custom = _IsCustom(attr) and (_IsCustomValue(attr, None) or | 1009 is_custom = _IsCustom(attr) and (_IsCustomValue(attr, None) or |
| 1001 _IsCustomValue(attr, 'Getter')) | 1010 _IsCustomValue(attr, 'Getter')) |
| 1002 # This seems to have been replaced with Custom=Getter (see above), but | 1011 # This seems to have been replaced with Custom=Getter (see above), but |
| 1003 # check to be sure we don't see the old syntax | 1012 # check to be sure we don't see the old syntax |
| 1004 assert(not ('CustomGetter' in attr.ext_attrs)) | 1013 assert(not ('CustomGetter' in attr.ext_attrs)) |
| 1005 native_suffix = 'Getter' | 1014 native_suffix = 'Getter' |
| 1006 auto_scope_setup = self._GenerateAutoSetupScope(attr.id, native_suffix) | 1015 auto_scope_setup = self._GenerateAutoSetupScope(attr.id, native_suffix) |
| 1007 native_entry = \ | 1016 native_entry = \ |
| 1008 self.DeriveNativeEntry(attr.id, native_suffix, [], True) | 1017 self.DeriveNativeEntry(attr.id, 'Getter', None) |
| 1009 cpp_callback_name = self._GenerateNativeBinding(attr.id, 1, | 1018 cpp_callback_name = self._GenerateNativeBinding(attr.id, 1, |
| 1010 dart_declaration, False, return_type, parameters, | 1019 dart_declaration, False, return_type, parameters, |
| 1011 native_suffix, is_custom, auto_scope_setup, native_entry=native_entry) | 1020 native_suffix, is_custom, auto_scope_setup, native_entry=native_entry) |
| 1012 if is_custom: | 1021 if is_custom: |
| 1013 return | 1022 return |
| 1014 | 1023 |
| 1015 if 'Reflect' in attr.ext_attrs: | 1024 if 'Reflect' in attr.ext_attrs: |
| 1016 webcore_function_name = self._TypeInfo(attr.type.id).webcore_getter_name() | 1025 webcore_function_name = self._TypeInfo(attr.type.id).webcore_getter_name() |
| 1017 if 'URL' in attr.ext_attrs: | 1026 if 'URL' in attr.ext_attrs: |
| 1018 if 'NonEmpty' in attr.ext_attrs: | 1027 if 'NonEmpty' in attr.ext_attrs: |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1053 dart_declaration = 'void set %s(%s value)' % (html_name, ptype) | 1062 dart_declaration = 'void set %s(%s value)' % (html_name, ptype) |
| 1054 is_custom = _IsCustom(attr) and (_IsCustomValue(attr, None) or | 1063 is_custom = _IsCustom(attr) and (_IsCustomValue(attr, None) or |
| 1055 _IsCustomValue(attr, 'Setter')) | 1064 _IsCustomValue(attr, 'Setter')) |
| 1056 # This seems to have been replaced with Custom=Setter (see above), but | 1065 # This seems to have been replaced with Custom=Setter (see above), but |
| 1057 # check to be sure we don't see the old syntax | 1066 # check to be sure we don't see the old syntax |
| 1058 assert(not ('CustomSetter' in attr.ext_attrs)) | 1067 assert(not ('CustomSetter' in attr.ext_attrs)) |
| 1059 assert(not ('V8CustomSetter' in attr.ext_attrs)) | 1068 assert(not ('V8CustomSetter' in attr.ext_attrs)) |
| 1060 native_suffix = 'Setter' | 1069 native_suffix = 'Setter' |
| 1061 auto_scope_setup = self._GenerateAutoSetupScope(attr.id, native_suffix) | 1070 auto_scope_setup = self._GenerateAutoSetupScope(attr.id, native_suffix) |
| 1062 native_entry = \ | 1071 native_entry = \ |
| 1063 self.DeriveNativeEntry(attr.id, native_suffix, [attr.type.id], True) | 1072 self.DeriveNativeEntry(attr.id, 'Setter', None) |
| 1064 cpp_callback_name = self._GenerateNativeBinding(attr.id, 2, | 1073 cpp_callback_name = self._GenerateNativeBinding(attr.id, 2, |
| 1065 dart_declaration, False, return_type, parameters, | 1074 dart_declaration, False, return_type, parameters, |
| 1066 native_suffix, is_custom, auto_scope_setup, native_entry=native_entry) | 1075 native_suffix, is_custom, auto_scope_setup, native_entry=native_entry) |
| 1067 if is_custom: | 1076 if is_custom: |
| 1068 return | 1077 return |
| 1069 | 1078 |
| 1070 if 'Reflect' in attr.ext_attrs: | 1079 if 'Reflect' in attr.ext_attrs: |
| 1071 webcore_function_name = self._TypeInfo(attr.type.id).webcore_setter_name() | 1080 webcore_function_name = self._TypeInfo(attr.type.id).webcore_setter_name() |
| 1072 else: | 1081 else: |
| 1073 if 'ImplementedAs' in attr.ext_attrs: | 1082 if 'ImplementedAs' in attr.ext_attrs: |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1116 dart_element_type = self._DartType(element_type) | 1125 dart_element_type = self._DartType(element_type) |
| 1117 if self._HasNativeIndexGetter(): | 1126 if self._HasNativeIndexGetter(): |
| 1118 self._EmitNativeIndexGetter(dart_element_type) | 1127 self._EmitNativeIndexGetter(dart_element_type) |
| 1119 elif self._HasExplicitIndexedGetter(): | 1128 elif self._HasExplicitIndexedGetter(): |
| 1120 self._EmitExplicitIndexedGetter(dart_element_type) | 1129 self._EmitExplicitIndexedGetter(dart_element_type) |
| 1121 else: | 1130 else: |
| 1122 is_custom = any((op.id == 'item' and _IsCustom(op)) for op in self._interf
ace.operations) | 1131 is_custom = any((op.id == 'item' and _IsCustom(op)) for op in self._interf
ace.operations) |
| 1123 # First emit a toplevel function to do the native call | 1132 # First emit a toplevel function to do the native call |
| 1124 # Calls to this are emitted elsewhere, | 1133 # Calls to this are emitted elsewhere, |
| 1125 dart_native_name, resolver_string = \ | 1134 dart_native_name, resolver_string = \ |
| 1126 self.DeriveNativeEntry("item", "Callback", ["unsigned long"], | 1135 self.DeriveNativeEntry("item", 'Method', 1) |
| 1127 is_custom) | |
| 1128 if resolver_string in _cpp_resolver_string_map: | 1136 if resolver_string in _cpp_resolver_string_map: |
| 1129 resolver_string = \ | 1137 resolver_string = \ |
| 1130 _cpp_resolver_string_map[resolver_string] | 1138 _cpp_resolver_string_map[resolver_string] |
| 1131 | 1139 |
| 1132 # Emit the method which calls the toplevel function, along with | 1140 # Emit the method which calls the toplevel function, along with |
| 1133 # the [] operator. | 1141 # the [] operator. |
| 1134 dart_qualified_name = \ | 1142 dart_qualified_name = \ |
| 1135 self.DeriveQualifiedBlinkName(self._interface.id, | 1143 self.DeriveQualifiedBlinkName(self._interface.id, |
| 1136 dart_native_name) | 1144 dart_native_name) |
| 1137 self._members_emitter.Emit( | 1145 self._members_emitter.Emit( |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1223 formals = info.ParametersAsDeclaration(self._DartType) | 1231 formals = info.ParametersAsDeclaration(self._DartType) |
| 1224 parameters = info.ParametersAsListOfVariables() | 1232 parameters = info.ParametersAsListOfVariables() |
| 1225 dart_declaration = '%s%s %s(%s)' % ( | 1233 dart_declaration = '%s%s %s(%s)' % ( |
| 1226 'static ' if info.IsStatic() else '', | 1234 'static ' if info.IsStatic() else '', |
| 1227 return_type, | 1235 return_type, |
| 1228 html_name, | 1236 html_name, |
| 1229 formals) | 1237 formals) |
| 1230 | 1238 |
| 1231 operation = info.operations[0] | 1239 operation = info.operations[0] |
| 1232 is_custom = _IsCustom(operation) | 1240 is_custom = _IsCustom(operation) |
| 1233 has_optional_arguments = any(self._IsArgumentOptionalInWebCore(operation, ar
gument) for argument in operation.arguments) | 1241 has_optional_arguments = any(IsOptional(argument) for argument in operation.
arguments) |
| 1234 needs_dispatcher = not is_custom and (len(info.operations) > 1 or has_option
al_arguments) | 1242 needs_dispatcher = not is_custom and (len(info.operations) > 1 or has_option
al_arguments) |
| 1235 | 1243 |
| 1236 if info.callback_args: | 1244 if info.callback_args: |
| 1237 self._AddFutureifiedOperation(info, html_name) | 1245 self._AddFutureifiedOperation(info, html_name) |
| 1238 elif not needs_dispatcher: | 1246 elif not needs_dispatcher: |
| 1239 # Bind directly to native implementation | 1247 # Bind directly to native implementation |
| 1240 argument_count = (0 if info.IsStatic() else 1) + len(info.param_infos) | 1248 argument_count = (0 if info.IsStatic() else 1) + len(info.param_infos) |
| 1241 native_suffix = 'Callback' | 1249 native_suffix = 'Callback' |
| 1242 auto_scope_setup = self._GenerateAutoSetupScope(info.name, native_suffix) | 1250 auto_scope_setup = self._GenerateAutoSetupScope(info.name, native_suffix) |
| 1243 type_ids = [argument.type.id | |
| 1244 for argument in operation.arguments[:len(info.param_infos)]] | |
| 1245 native_entry = \ | 1251 native_entry = \ |
| 1246 self.DeriveNativeEntry(operation.id, native_suffix, type_ids, | 1252 self.DeriveNativeEntry(operation.id, 'Method', len(info.param_infos)) |
| 1247 is_custom) | |
| 1248 cpp_callback_name = self._GenerateNativeBinding( | 1253 cpp_callback_name = self._GenerateNativeBinding( |
| 1249 info.name, argument_count, dart_declaration, | 1254 info.name, argument_count, dart_declaration, |
| 1250 info.IsStatic(), return_type, parameters, | 1255 info.IsStatic(), return_type, parameters, |
| 1251 native_suffix, is_custom, auto_scope_setup, | 1256 native_suffix, is_custom, auto_scope_setup, |
| 1252 native_entry=native_entry) | 1257 native_entry=native_entry) |
| 1253 if not is_custom: | 1258 if not is_custom: |
| 1254 self._GenerateOperationNativeCallback(operation, operation.arguments, cp
p_callback_name, auto_scope_setup) | 1259 self._GenerateOperationNativeCallback(operation, operation.arguments, cp
p_callback_name, auto_scope_setup) |
| 1255 else: | 1260 else: |
| 1256 self._GenerateDispatcher(info, info.operations, dart_declaration, html_nam
e) | 1261 self._GenerateDispatcher(info, info.operations, dart_declaration, html_nam
e) |
| 1257 | 1262 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 1268 is_custom = _IsCustom(operation) | 1273 is_custom = _IsCustom(operation) |
| 1269 base_name = '_%s_%s' % (operation.id, version) | 1274 base_name = '_%s_%s' % (operation.id, version) |
| 1270 static = True | 1275 static = True |
| 1271 if not operation.is_static: | 1276 if not operation.is_static: |
| 1272 actuals = ['this'] + actuals | 1277 actuals = ['this'] + actuals |
| 1273 formals = ['mthis'] + formals | 1278 formals = ['mthis'] + formals |
| 1274 actuals_s = ", ".join(actuals) | 1279 actuals_s = ", ".join(actuals) |
| 1275 formals_s = ", ".join(formals) | 1280 formals_s = ", ".join(formals) |
| 1276 dart_declaration = '%s(%s)' % ( | 1281 dart_declaration = '%s(%s)' % ( |
| 1277 base_name, formals_s) | 1282 base_name, formals_s) |
| 1278 type_ids = [argument.type.id | |
| 1279 for argument in operation.arguments[:argument_count]] | |
| 1280 native_entry = \ | 1283 native_entry = \ |
| 1281 self.DeriveNativeEntry(operation.id, native_suffix, type_ids, | 1284 self.DeriveNativeEntry(operation.id, 'Method', argument_count) |
| 1282 is_custom) | |
| 1283 overload_base_name = native_entry[0] | 1285 overload_base_name = native_entry[0] |
| 1284 overload_name = \ | 1286 overload_name = \ |
| 1285 self.DeriveQualifiedBlinkName(self._interface.id, | 1287 self.DeriveQualifiedBlinkName(self._interface.id, |
| 1286 overload_base_name) | 1288 overload_base_name) |
| 1287 call_emitter.Emit('$NAME($ARGS)', NAME=overload_name, ARGS=actuals_s) | 1289 call_emitter.Emit('$NAME($ARGS)', NAME=overload_name, ARGS=actuals_s) |
| 1288 auto_scope_setup = \ | 1290 auto_scope_setup = \ |
| 1289 self._GenerateAutoSetupScope(base_name, native_suffix) | 1291 self._GenerateAutoSetupScope(base_name, native_suffix) |
| 1290 cpp_callback_name = self._GenerateNativeBinding( | 1292 cpp_callback_name = self._GenerateNativeBinding( |
| 1291 base_name, (0 if static else 1) + argument_count, | 1293 base_name, (0 if static else 1) + argument_count, |
| 1292 dart_declaration, static, return_type, formals, | 1294 dart_declaration, static, return_type, formals, |
| 1293 native_suffix, is_custom, auto_scope_setup, emit_metadata=False, | 1295 native_suffix, is_custom, auto_scope_setup, emit_metadata=False, |
| 1294 emit_to_native=True, native_entry=native_entry) | 1296 emit_to_native=True, native_entry=native_entry) |
| 1295 if not is_custom: | 1297 if not is_custom: |
| 1296 self._GenerateOperationNativeCallback(operation, | 1298 self._GenerateOperationNativeCallback(operation, |
| 1297 operation.arguments[:argument_count], cpp_callback_name, | 1299 operation.arguments[:argument_count], cpp_callback_name, |
| 1298 auto_scope_setup) | 1300 auto_scope_setup) |
| 1299 | 1301 |
| 1300 | 1302 |
| 1301 self._GenerateDispatcherBody( | 1303 self._GenerateDispatcherBody( |
| 1302 info, | 1304 info, |
| 1303 operations, | 1305 operations, |
| 1304 dart_declaration, | 1306 dart_declaration, |
| 1305 GenerateCall, | 1307 GenerateCall, |
| 1306 self._IsArgumentOptionalInWebCore) | 1308 IsOptional) |
| 1307 | 1309 |
| 1308 def SecondaryContext(self, interface): | 1310 def SecondaryContext(self, interface): |
| 1309 pass | 1311 pass |
| 1310 | 1312 |
| 1311 def _GenerateOperationNativeCallback(self, operation, arguments, cpp_callback_
name, auto_scope_setup=True): | 1313 def _GenerateOperationNativeCallback(self, operation, arguments, cpp_callback_
name, auto_scope_setup=True): |
| 1312 webcore_function_name = operation.ext_attrs.get('ImplementedAs', operation.i
d) | 1314 webcore_function_name = operation.ext_attrs.get('ImplementedAs', operation.i
d) |
| 1313 | 1315 |
| 1314 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi
on_name, operation, cpp_callback_name) | 1316 function_expression = self._GenerateWebCoreFunctionExpression(webcore_functi
on_name, operation, cpp_callback_name) |
| 1315 self._GenerateNativeCallback( | 1317 self._GenerateNativeCallback( |
| 1316 cpp_callback_name, | 1318 cpp_callback_name, |
| (...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2007 | 2009 |
| 2008 def _IsCustom(op_or_attr): | 2010 def _IsCustom(op_or_attr): |
| 2009 assert(isinstance(op_or_attr, IDLMember)) | 2011 assert(isinstance(op_or_attr, IDLMember)) |
| 2010 return 'Custom' in op_or_attr.ext_attrs or 'DartCustom' in op_or_attr.ext_attr
s | 2012 return 'Custom' in op_or_attr.ext_attrs or 'DartCustom' in op_or_attr.ext_attr
s |
| 2011 | 2013 |
| 2012 def _IsCustomValue(op_or_attr, value): | 2014 def _IsCustomValue(op_or_attr, value): |
| 2013 if _IsCustom(op_or_attr): | 2015 if _IsCustom(op_or_attr): |
| 2014 return op_or_attr.ext_attrs.get('Custom') == value \ | 2016 return op_or_attr.ext_attrs.get('Custom') == value \ |
| 2015 or op_or_attr.ext_attrs.get('DartCustom') == value | 2017 or op_or_attr.ext_attrs.get('DartCustom') == value |
| 2016 return False | 2018 return False |
| OLD | NEW |