Chromium Code Reviews| 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 generates Dart APIs from the IDL database.""" | 6 """This module generates Dart APIs from the IDL database.""" |
| 7 | 7 |
| 8 import emitter | 8 import emitter |
| 9 import idlnode | 9 import idlnode |
| 10 import logging | 10 import logging |
| (...skipping 2059 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2070 | 2070 |
| 2071 class NativeImplementationSystem(System): | 2071 class NativeImplementationSystem(System): |
| 2072 | 2072 |
| 2073 def __init__(self, templates, database, emitters, auxiliary_dir, output_dir): | 2073 def __init__(self, templates, database, emitters, auxiliary_dir, output_dir): |
| 2074 super(NativeImplementationSystem, self).__init__( | 2074 super(NativeImplementationSystem, self).__init__( |
| 2075 templates, database, emitters, output_dir) | 2075 templates, database, emitters, output_dir) |
| 2076 | 2076 |
| 2077 self._auxiliary_dir = auxiliary_dir | 2077 self._auxiliary_dir = auxiliary_dir |
| 2078 self._dom_public_files = [] | 2078 self._dom_public_files = [] |
| 2079 self._dom_impl_files = [] | 2079 self._dom_impl_files = [] |
| 2080 self._cpp_header_files = [] | |
| 2081 self._cpp_impl_files = [] | |
| 2080 | 2082 |
| 2081 def InterfaceGenerator(self, | 2083 def InterfaceGenerator(self, |
| 2082 interface, | 2084 interface, |
| 2083 common_prefix, | 2085 common_prefix, |
| 2084 super_interface_name, | 2086 super_interface_name, |
| 2085 source_filter): | 2087 source_filter): |
| 2086 interface_name = interface.id | 2088 interface_name = interface.id |
| 2087 | 2089 |
| 2088 dart_interface_path = self._FilePathForDartInterface(interface_name) | 2090 dart_interface_path = self._FilePathForDartInterface(interface_name) |
| 2089 self._dom_public_files.append(dart_interface_path) | 2091 self._dom_public_files.append(dart_interface_path) |
| 2090 | 2092 |
| 2091 dart_impl_path = self._FilePathForDartImplementation(interface_name) | 2093 dart_impl_path = self._FilePathForDartImplementation(interface_name) |
| 2092 self._dom_impl_files.append(dart_impl_path) | 2094 self._dom_impl_files.append(dart_impl_path) |
| 2093 | 2095 |
| 2096 cpp_header_path = os.path.join(self._output_dir, 'cpp', | |
| 2097 'Dart%s.h' % interface_name) | |
| 2098 self._cpp_header_files.append(cpp_header_path) | |
| 2099 | |
| 2100 cpp_impl_path = os.path.join(self._output_dir, 'cpp', | |
|
antonm
2012/02/03 14:03:06
nit: maybe introduce a helper a la _FilePathForDar
podivilov
2012/02/06 13:24:36
Done.
| |
| 2101 'Dart%s.cpp' % interface_name) | |
| 2102 self._cpp_impl_files.append(cpp_impl_path) | |
| 2103 | |
| 2094 return NativeImplementationGenerator(interface, super_interface_name, | 2104 return NativeImplementationGenerator(interface, super_interface_name, |
| 2095 self._emitters.FileEmitter(dart_impl_path), | 2105 self._emitters.FileEmitter(dart_impl_path), |
| 2106 self._emitters.FileEmitter(cpp_header_path), | |
| 2107 self._emitters.FileEmitter(cpp_impl_path), | |
| 2096 self._BaseDefines(interface), | 2108 self._BaseDefines(interface), |
| 2097 self._templates) | 2109 self._templates) |
| 2098 | 2110 |
| 2099 def ProcessCallback(self, interface, info): | 2111 def ProcessCallback(self, interface, info): |
| 2100 self._dom_public_files.append(self._FilePathForDartInterface(interface.id)) | 2112 self._dom_public_files.append(self._FilePathForDartInterface(interface.id)) |
| 2101 | 2113 |
| 2102 def GenerateLibraries(self, lib_dir): | 2114 def GenerateLibraries(self, lib_dir): |
| 2103 auxiliary_dir = os.path.relpath(self._auxiliary_dir, self._output_dir) | 2115 auxiliary_dir = os.path.relpath(self._auxiliary_dir, self._output_dir) |
| 2104 | 2116 |
| 2105 # Generate dom_public.dart. | 2117 # Generate dom_public.dart. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 2121 dom_impl_imports_emitter = emitter.Emitter() | 2133 dom_impl_imports_emitter = emitter.Emitter() |
| 2122 for file in self._dom_impl_files: | 2134 for file in self._dom_impl_files: |
| 2123 path = os.path.relpath(file, os.path.dirname(dom_impl_path)) | 2135 path = os.path.relpath(file, os.path.dirname(dom_impl_path)) |
| 2124 dom_impl_imports_emitter.Emit('#source("$PATH");\n', PATH=path) | 2136 dom_impl_imports_emitter.Emit('#source("$PATH");\n', PATH=path) |
| 2125 | 2137 |
| 2126 dom_impl_emitter = self._emitters.FileEmitter(dom_impl_path) | 2138 dom_impl_emitter = self._emitters.FileEmitter(dom_impl_path) |
| 2127 dom_impl_emitter.Emit(self._templates.Load('dom_impl.darttemplate'), | 2139 dom_impl_emitter.Emit(self._templates.Load('dom_impl.darttemplate'), |
| 2128 AUXILIARY_DIR=auxiliary_dir, | 2140 AUXILIARY_DIR=auxiliary_dir, |
| 2129 SOURCES=dom_impl_imports_emitter.Fragments()) | 2141 SOURCES=dom_impl_imports_emitter.Fragments()) |
| 2130 | 2142 |
| 2143 # Generate DartDerivedSourcesAll.cpp | |
|
antonm
2012/02/03 14:03:06
nit: missing trailing dot.
podivilov
2012/02/06 13:24:36
Done.
| |
| 2144 cpp_all_in_one_path = os.path.join(self._output_dir, | |
| 2145 'DartDerivedSourcesAll.cpp') | |
| 2146 | |
| 2147 includes_emitter = emitter.Emitter() | |
| 2148 for file in self._cpp_impl_files: | |
| 2149 path = os.path.relpath(file, os.path.dirname(cpp_all_in_one_path)) | |
|
antonm
2012/02/03 14:03:06
nit: two-space indent for control flow.
podivilov
2012/02/06 13:24:36
Done.
| |
| 2150 includes_emitter.Emit('#include "$PATH"\n', PATH=path) | |
|
antonm
2012/02/03 14:03:06
what about ENABLE_ features? Do you have filters
podivilov
2012/02/06 13:24:36
Disabled interfaces and operations are removed by
| |
| 2151 | |
| 2152 cpp_all_in_one_emitter = self._emitters.FileEmitter(cpp_all_in_one_path) | |
| 2153 cpp_all_in_one_emitter.Emit( | |
| 2154 self._templates.Load('cpp_all_in_one.template'), | |
|
sra1
2012/02/03 21:30:49
Instead of using Fragments to build and substitute
podivilov
2012/02/06 13:24:36
Not using template holes was a conscious decision
| |
| 2155 INCLUDES=includes_emitter.Fragments()) | |
|
antonm
2012/02/03 14:03:06
just curious, I don't know if the templating syste
sra1
2012/02/03 21:30:49
Lets spend a few minutes talking about the templat
| |
| 2156 | |
| 2157 # Generate DartResolver.cpp | |
| 2158 cpp_resolver_path = os.path.join(self._output_dir, 'DartResolver.cpp') | |
| 2159 | |
| 2160 includes_emitter = emitter.Emitter() | |
| 2161 resolver_body_emitter = emitter.Emitter() | |
| 2162 for file in self._cpp_header_files: | |
|
antonm
2012/02/03 14:03:06
file is a synonym for open in Python and it's usua
podivilov
2012/02/06 13:24:36
Done.
| |
| 2163 path = os.path.relpath(file, os.path.dirname(cpp_resolver_path)) | |
| 2164 includes_emitter.Emit('#include "$PATH"\n', PATH=path) | |
| 2165 resolver_body_emitter.Emit( | |
| 2166 ' if (Dart_NativeFunction func = $CLASS_NAME::resolver(name, argu mentCount))\n' | |
|
antonm
2012/02/03 14:03:06
multiline literal?
podivilov
2012/02/06 13:24:36
Multiline literals break indentation rules and mak
| |
| 2167 ' return func;\n', | |
| 2168 CLASS_NAME=os.path.splitext(os.path.basename(path))[0]) | |
|
antonm
2012/02/03 14:03:06
hrr, do not we have it stored somewhere already?
| |
| 2169 | |
| 2170 cpp_resolver_emitter = self._emitters.FileEmitter(cpp_resolver_path) | |
| 2171 cpp_resolver_emitter.Emit( | |
| 2172 self._templates.Load('cpp_resolver.template'), | |
| 2173 INCLUDES=includes_emitter.Fragments(), | |
| 2174 RESOLVER_BODY=resolver_body_emitter.Fragments()) | |
|
sra1
2012/02/03 21:30:49
See above comment about using 'holes'.
Start reso
| |
| 2175 | |
| 2131 def Finish(self): | 2176 def Finish(self): |
| 2132 pass | 2177 pass |
| 2133 | 2178 |
| 2134 def _FilePathForDartInterface(self, interface_name): | 2179 def _FilePathForDartInterface(self, interface_name): |
| 2135 return os.path.join(self._output_dir, 'src', 'interface', | 2180 return os.path.join(self._output_dir, 'src', 'interface', |
| 2136 '%s.dart' % interface_name) | 2181 '%s.dart' % interface_name) |
| 2137 | 2182 |
| 2138 def _FilePathForDartImplementation(self, interface_name): | 2183 def _FilePathForDartImplementation(self, interface_name): |
| 2139 return os.path.join(self._output_dir, 'dart', | 2184 return os.path.join(self._output_dir, 'dart', |
| 2140 '%sImplementation.dart' % interface_name) | 2185 '%sImplementation.dart' % interface_name) |
| 2141 | 2186 |
| 2142 | 2187 |
| 2143 class NativeImplementationGenerator(WrappingInterfaceGenerator): | 2188 class NativeImplementationGenerator(WrappingInterfaceGenerator): |
| 2144 """Generates Dart implementation for one DOM IDL interface.""" | 2189 """Generates Dart implementation for one DOM IDL interface.""" |
| 2145 | 2190 |
| 2146 def __init__(self, interface, super_interface, dart_impl_emitter, | 2191 def __init__(self, interface, super_interface, |
| 2192 dart_impl_emitter, cpp_header_emitter, cpp_impl_emitter, | |
| 2147 base_members, templates): | 2193 base_members, templates): |
| 2148 """Generates Dart code for the given interface. | 2194 """Generates Dart and c++ code for the given interface. |
| 2149 | 2195 |
| 2150 Args: | 2196 Args: |
| 2151 | 2197 |
| 2152 interface: an IDLInterface instance. It is assumed that all types have | 2198 interface: an IDLInterface instance. It is assumed that all types have |
| 2153 been converted to Dart types (e.g. int, String), unless they are in | 2199 been converted to Dart types (e.g. int, String), unless they are in |
| 2154 the same package as the interface. | 2200 the same package as the interface. |
| 2155 super_interface: A string or None, the name of the common interface that | 2201 super_interface: A string or None, the name of the common interface that |
| 2156 this interface implements, if any. | 2202 this interface implements, if any. |
| 2157 dart_impl_emitter: an Emitter for the file containing the Dart | 2203 dart_impl_emitter: an Emitter for the file containing the Dart |
| 2158 implementation class. | 2204 implementation class. |
| 2205 cpp_header_emitter: an Emitter for the file containing the c++ header. | |
| 2206 cpp_impl_emitter: an Emitter for the file containing the c++ | |
| 2207 implementation. | |
| 2159 base_members: a set of names of members defined in a base class. This is | 2208 base_members: a set of names of members defined in a base class. This is |
| 2160 used to avoid static member 'overriding' in the generated Dart code. | 2209 used to avoid static member 'overriding' in the generated Dart code. |
| 2161 """ | 2210 """ |
| 2162 self._interface = interface | 2211 self._interface = interface |
| 2163 self._super_interface = super_interface | 2212 self._super_interface = super_interface |
| 2164 self._dart_impl_emitter = dart_impl_emitter | 2213 self._dart_impl_emitter = dart_impl_emitter |
| 2214 self._cpp_header_emitter = cpp_header_emitter | |
| 2215 self._cpp_impl_emitter = cpp_impl_emitter | |
| 2165 self._base_members = base_members | 2216 self._base_members = base_members |
| 2166 self._templates = templates | 2217 self._templates = templates |
| 2167 self._current_secondary_parent = None | 2218 self._current_secondary_parent = None |
| 2168 | 2219 |
| 2169 def StartInterface(self): | 2220 def StartInterface(self): |
| 2170 self._class_name = self._ImplClassName(self._interface.id) | 2221 self._class_name = self._ImplClassName(self._interface.id) |
| 2171 self._members_emitter = emitter.Emitter() | 2222 self._members_emitter = emitter.Emitter() |
| 2172 | 2223 |
| 2173 def _ImplClassName(self, type_name): | 2224 def _ImplClassName(self, interface_name): |
| 2174 return type_name + 'Implementation' | 2225 return interface_name + 'Implementation' |
| 2175 | 2226 |
| 2176 def FinishInterface(self): | 2227 def FinishInterface(self): |
| 2177 interface = self._interface | 2228 base = self._BaseClassName(self._interface) |
| 2178 interface_name = interface.id | |
| 2179 | |
| 2180 base = self._BaseClassName(interface) | |
| 2181 self._dart_impl_emitter.Emit( | 2229 self._dart_impl_emitter.Emit( |
| 2182 self._templates.Load('dart_implementation.darttemplate'), | 2230 self._templates.Load('dart_implementation.darttemplate'), |
| 2183 CLASS=self._class_name, BASE=base, INTERFACE=interface_name, | 2231 CLASS=self._class_name, BASE=base, INTERFACE=self._interface.id, |
| 2184 MEMBERS=self._members_emitter.Fragments()) | 2232 MEMBERS=self._members_emitter.Fragments()) |
| 2185 | 2233 |
| 2234 self._cpp_header_emitter.Emit( | |
| 2235 self._templates.Load('cpp_header.template'), | |
| 2236 INTERFACE=self._interface.id) | |
| 2237 | |
| 2238 self._cpp_impl_emitter.Emit( | |
| 2239 self._templates.Load('cpp_implementation.template'), | |
| 2240 INTERFACE=self._interface.id) | |
| 2241 | |
| 2186 def AddGetter(self, attr): | 2242 def AddGetter(self, attr): |
| 2187 self._members_emitter.Emit( | 2243 self._members_emitter.Emit( |
| 2188 '\n' | 2244 '\n' |
| 2189 ' $TYPE get $NAME() native "$(INTERFACE)_$(NAME)_Getter";\n', | 2245 ' $TYPE get $NAME() native "$(INTERFACE)_$(NAME)_Getter";\n', |
| 2190 NAME=attr.id, TYPE=attr.type.id, INTERFACE=self._interface.id) | 2246 NAME=attr.id, TYPE=attr.type.id, INTERFACE=self._interface.id) |
| 2191 | 2247 |
| 2192 def AddSetter(self, attr): | 2248 def AddSetter(self, attr): |
| 2193 self._members_emitter.Emit( | 2249 self._members_emitter.Emit( |
| 2194 '\n' | 2250 '\n' |
| 2195 ' void set $NAME($TYPE) native "$(INTERFACE)_$(NAME)_Setter";\n', | 2251 ' void set $NAME($TYPE) native "$(INTERFACE)_$(NAME)_Setter";\n', |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2277 INDENT=indent, | 2333 INDENT=indent, |
| 2278 NATIVENAME=native_name, | 2334 NATIVENAME=native_name, |
| 2279 ARGS=argument_expressions) | 2335 ARGS=argument_expressions) |
| 2280 | 2336 |
| 2281 self._members_emitter.Emit(' $TYPE $NATIVE_NAME($PARAMS) native ' | 2337 self._members_emitter.Emit(' $TYPE $NATIVE_NAME($PARAMS) native ' |
| 2282 '"$(INTERFACE)$(NATIVE_NAME)_Callback";\n', | 2338 '"$(INTERFACE)$(NATIVE_NAME)_Callback";\n', |
| 2283 NATIVE_NAME=native_name, | 2339 NATIVE_NAME=native_name, |
| 2284 TYPE=info.type_name, | 2340 TYPE=info.type_name, |
| 2285 PARAMS=', '.join(arg_names), | 2341 PARAMS=', '.join(arg_names), |
| 2286 INTERFACE=self._interface.id) | 2342 INTERFACE=self._interface.id) |
| OLD | NEW |