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 |
11 from generator import * | 11 from generator import * |
12 from systemhtml import SecureOutputType | 12 from systemhtml import SecureOutputType |
| 13 from htmldartgenerator import * |
13 | 14 |
14 class DartiumBackend(object): | 15 class DartiumBackend(HtmlDartGenerator): |
15 """Generates Dart implementation for one DOM IDL interface.""" | 16 """Generates Dart implementation for one DOM IDL interface.""" |
16 | 17 |
17 def __init__(self, interface, cpp_library_emitter, options): | 18 def __init__(self, interface, cpp_library_emitter, options): |
| 19 super(DartiumBackend, self).__init__(interface, options) |
| 20 |
18 self._interface = interface | 21 self._interface = interface |
19 self._cpp_library_emitter = cpp_library_emitter | 22 self._cpp_library_emitter = cpp_library_emitter |
20 self._database = options.database | 23 self._database = options.database |
21 self._template_loader = options.templates | 24 self._template_loader = options.templates |
22 self._type_registry = options.type_registry | 25 self._type_registry = options.type_registry |
23 self._interface_type_info = self._type_registry.TypeInfo(self._interface.id) | 26 self._interface_type_info = self._type_registry.TypeInfo(self._interface.id) |
24 | 27 |
25 def ImplementsMergedMembers(self): | 28 def ImplementsMergedMembers(self): |
26 # We could not add merged functions to implementation class because | 29 # We could not add merged functions to implementation class because |
27 # underlying c++ object doesn't implement them. Merged functions are | 30 # underlying c++ object doesn't implement them. Merged functions are |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 if interface_name == self._interface.id or not self._database.HasInterface(i
nterface_name): | 100 if interface_name == self._interface.id or not self._database.HasInterface(i
nterface_name): |
98 template_file = 'impl_%s.darttemplate' % interface_name | 101 template_file = 'impl_%s.darttemplate' % interface_name |
99 template = self._template_loader.TryLoad(template_file) | 102 template = self._template_loader.TryLoad(template_file) |
100 if not template: | 103 if not template: |
101 template = self._template_loader.Load('dart_implementation.darttemplate') | 104 template = self._template_loader.Load('dart_implementation.darttemplate') |
102 return template | 105 return template |
103 | 106 |
104 def RootClassName(self): | 107 def RootClassName(self): |
105 return 'NativeFieldWrapperClass1' | 108 return 'NativeFieldWrapperClass1' |
106 | 109 |
107 def AdditionalImplementedInterfaces(self): | |
108 return [] | |
109 | |
110 def NativeSpec(self): | 110 def NativeSpec(self): |
111 return '' | 111 return '' |
112 | 112 |
113 def StartInterface(self, memebers_emitter): | 113 def StartInterface(self, memebers_emitter): |
114 # Create emitters for c++ implementation. | 114 # Create emitters for c++ implementation. |
115 if not IsPureInterface(self._interface.id): | 115 if not IsPureInterface(self._interface.id): |
116 self._cpp_header_emitter = self._cpp_library_emitter.CreateHeaderEmitter(s
elf._interface.id) | 116 self._cpp_header_emitter = self._cpp_library_emitter.CreateHeaderEmitter(s
elf._interface.id) |
117 self._cpp_impl_emitter = self._cpp_library_emitter.CreateSourceEmitter(sel
f._interface.id) | 117 self._cpp_impl_emitter = self._cpp_library_emitter.CreateSourceEmitter(sel
f._interface.id) |
118 else: | 118 else: |
119 self._cpp_header_emitter = emitter.Emitter() | 119 self._cpp_header_emitter = emitter.Emitter() |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 | 202 |
203 native_binding = '%s_constructor_Callback' % self._interface.id | 203 native_binding = '%s_constructor_Callback' % self._interface.id |
204 emitter.Emit( | 204 emitter.Emit( |
205 template, | 205 template, |
206 FACTORYPROVIDER=factory_provider, | 206 FACTORYPROVIDER=factory_provider, |
207 INTERFACE=interface_name, | 207 INTERFACE=interface_name, |
208 PARAMETERS=constructor_info.ParametersDeclaration(self._DartType), | 208 PARAMETERS=constructor_info.ParametersDeclaration(self._DartType), |
209 ARGUMENTS=constructor_info.ParametersAsArgumentList(), | 209 ARGUMENTS=constructor_info.ParametersAsArgumentList(), |
210 NATIVE_NAME=native_binding) | 210 NATIVE_NAME=native_binding) |
211 | 211 |
| 212 def AddConstructors(self, constructors, factory_provider, class_name, |
| 213 base_class): |
| 214 super(DartiumBackend, self).AddConstructors(constructors, factory_provider, |
| 215 class_name, base_class) |
| 216 |
| 217 super_constructor = '' |
| 218 if base_class and base_class != 'NativeFieldWrapperClass1': |
| 219 super_constructor = ': super.internal()' |
| 220 |
| 221 self._members_emitter.Emit( |
| 222 ' $CLASSNAME.internal()$SUPERCONSTRUCTOR;\n', |
| 223 CLASSNAME=class_name, |
| 224 SUPERCONSTRUCTOR=super_constructor) |
| 225 |
212 def FinishInterface(self): | 226 def FinishInterface(self): |
213 self._GenerateCPPHeader() | 227 self._GenerateCPPHeader() |
214 | 228 |
215 self._cpp_impl_emitter.Emit( | 229 self._cpp_impl_emitter.Emit( |
216 self._template_loader.Load('cpp_implementation.template'), | 230 self._template_loader.Load('cpp_implementation.template'), |
217 INTERFACE=self._interface.id, | 231 INTERFACE=self._interface.id, |
218 INCLUDES=self._GenerateCPPIncludes(self._cpp_impl_includes), | 232 INCLUDES=self._GenerateCPPIncludes(self._cpp_impl_includes), |
219 CALLBACKS=self._cpp_definitions_emitter.Fragments(), | 233 CALLBACKS=self._cpp_definitions_emitter.Fragments(), |
220 RESOLVER=self._cpp_resolver_emitter.Fragments(), | 234 RESOLVER=self._cpp_resolver_emitter.Fragments(), |
221 DART_IMPLEMENTATION_CLASS=self._interface_type_info.implementation_name(
)) | 235 DART_IMPLEMENTATION_CLASS=self._interface_type_info.implementation_name(
)) |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 if not needs_dispatcher: | 469 if not needs_dispatcher: |
456 # Bind directly to native implementation | 470 # Bind directly to native implementation |
457 argument_count = (0 if info.IsStatic() else 1) + len(info.param_infos) | 471 argument_count = (0 if info.IsStatic() else 1) + len(info.param_infos) |
458 cpp_callback_name = self._GenerateNativeBinding( | 472 cpp_callback_name = self._GenerateNativeBinding( |
459 info.name, argument_count, dart_declaration, 'Callback', is_custom) | 473 info.name, argument_count, dart_declaration, 'Callback', is_custom) |
460 if not is_custom: | 474 if not is_custom: |
461 self._GenerateOperationNativeCallback(operation, operation.arguments, cp
p_callback_name) | 475 self._GenerateOperationNativeCallback(operation, operation.arguments, cp
p_callback_name) |
462 else: | 476 else: |
463 self._GenerateDispatcher(info.operations, dart_declaration, [info.name for
info in info.param_infos]) | 477 self._GenerateDispatcher(info.operations, dart_declaration, [info.name for
info in info.param_infos]) |
464 | 478 |
| 479 def AddConstant(self, constant): |
| 480 type = TypeOrNothing(self._DartType(constant.type.id), constant.type.id) |
| 481 self._members_emitter.Emit('\n static const $TYPE$NAME = $VALUE;\n', |
| 482 NAME=constant.id, |
| 483 TYPE=type, |
| 484 VALUE=constant.value) |
| 485 |
465 def _GenerateDispatcher(self, operations, dart_declaration, argument_names): | 486 def _GenerateDispatcher(self, operations, dart_declaration, argument_names): |
466 | 487 |
467 body = self._members_emitter.Emit( | 488 body = self._members_emitter.Emit( |
468 '\n' | 489 '\n' |
469 ' $DECLARATION {\n' | 490 ' $DECLARATION {\n' |
470 '$!BODY' | 491 '$!BODY' |
471 ' }\n', | 492 ' }\n', |
472 DECLARATION=dart_declaration) | 493 DECLARATION=dart_declaration) |
473 | 494 |
474 version = [1] | 495 version = [1] |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
750 ' Dart_Handle returnValue = $TO_DART_CONVERSION;\n' | 771 ' Dart_Handle returnValue = $TO_DART_CONVERSION;\n' |
751 ' if (returnValue)\n' | 772 ' if (returnValue)\n' |
752 ' Dart_SetReturnValue(args, returnValue);\n', | 773 ' Dart_SetReturnValue(args, returnValue);\n', |
753 TO_DART_CONVERSION=to_dart_conversion) | 774 TO_DART_CONVERSION=to_dart_conversion) |
754 | 775 |
755 def _GenerateNativeBinding(self, idl_name, argument_count, dart_declaration, | 776 def _GenerateNativeBinding(self, idl_name, argument_count, dart_declaration, |
756 native_suffix, is_custom): | 777 native_suffix, is_custom): |
757 native_binding = '%s_%s_%s' % (self._interface.id, idl_name, native_suffix) | 778 native_binding = '%s_%s_%s' % (self._interface.id, idl_name, native_suffix) |
758 self._members_emitter.Emit( | 779 self._members_emitter.Emit( |
759 '\n' | 780 '\n' |
760 ' $DART_DECLARATION native "$NATIVE_BINDING";\n', | 781 '\n /** @domName $DOMINTERFACE.$DOMNAME */' |
761 DART_DECLARATION=dart_declaration, NATIVE_BINDING=native_binding) | 782 '\n $DART_DECLARATION native "$NATIVE_BINDING";\n', |
| 783 DOMINTERFACE=self._interface.id, |
| 784 DOMNAME=idl_name, |
| 785 DART_DECLARATION=dart_declaration, |
| 786 NATIVE_BINDING=native_binding) |
762 | 787 |
763 cpp_callback_name = '%s%s' % (idl_name, native_suffix) | 788 cpp_callback_name = '%s%s' % (idl_name, native_suffix) |
764 self._cpp_resolver_emitter.Emit( | 789 self._cpp_resolver_emitter.Emit( |
765 ' if (argumentCount == $ARGC && name == "$NATIVE_BINDING")\n' | 790 ' if (argumentCount == $ARGC && name == "$NATIVE_BINDING")\n' |
766 ' return Dart$(INTERFACE_NAME)Internal::$CPP_CALLBACK_NAME;\n', | 791 ' return Dart$(INTERFACE_NAME)Internal::$CPP_CALLBACK_NAME;\n', |
767 ARGC=argument_count, | 792 ARGC=argument_count, |
768 NATIVE_BINDING=native_binding, | 793 NATIVE_BINDING=native_binding, |
769 INTERFACE_NAME=self._interface.id, | 794 INTERFACE_NAME=self._interface.id, |
770 CPP_CALLBACK_NAME=cpp_callback_name) | 795 CPP_CALLBACK_NAME=cpp_callback_name) |
771 | 796 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
860 def EmitResolver(self, template, output_dir): | 885 def EmitResolver(self, template, output_dir): |
861 file_path = os.path.join(output_dir, 'DartResolver.cpp') | 886 file_path = os.path.join(output_dir, 'DartResolver.cpp') |
862 includes_emitter, body_emitter = self._emitters.FileEmitter(file_path).Emit(
template) | 887 includes_emitter, body_emitter = self._emitters.FileEmitter(file_path).Emit(
template) |
863 for header_file in self._headers_list: | 888 for header_file in self._headers_list: |
864 path = os.path.relpath(header_file, output_dir) | 889 path = os.path.relpath(header_file, output_dir) |
865 includes_emitter.Emit('#include "$PATH"\n', PATH=path) | 890 includes_emitter.Emit('#include "$PATH"\n', PATH=path) |
866 body_emitter.Emit( | 891 body_emitter.Emit( |
867 ' if (Dart_NativeFunction func = $CLASS_NAME::resolver(name, argume
ntCount))\n' | 892 ' if (Dart_NativeFunction func = $CLASS_NAME::resolver(name, argume
ntCount))\n' |
868 ' return func;\n', | 893 ' return func;\n', |
869 CLASS_NAME=os.path.splitext(os.path.basename(path))[0]) | 894 CLASS_NAME=os.path.splitext(os.path.basename(path))[0]) |
OLD | NEW |