OLD | NEW |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | 1 # Copyright (C) 2013 Google Inc. All rights reserved. |
2 # | 2 # |
3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
5 # met: | 5 # met: |
6 # | 6 # |
7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 import jinja2 | 71 import jinja2 |
72 | 72 |
73 import idl_types | 73 import idl_types |
74 from idl_types import IdlType | 74 from idl_types import IdlType |
75 import v8_callback_interface | 75 import v8_callback_interface |
76 import v8_dictionary | 76 import v8_dictionary |
77 from v8_globals import includes, interfaces | 77 from v8_globals import includes, interfaces |
78 import v8_interface | 78 import v8_interface |
79 import v8_types | 79 import v8_types |
80 from v8_utilities import capitalize, cpp_name, conditional_string, v8_class_name | 80 from v8_utilities import capitalize, cpp_name, conditional_string, v8_class_name |
| 81 from utilities import KNOWN_COMPONENTS |
81 | 82 |
82 | 83 |
83 def render_template(interface_info, header_template, cpp_template, | 84 def render_template(interface_info, header_template, cpp_template, |
84 template_context): | 85 template_context): |
85 template_context['code_generator'] = module_pyname | 86 template_context['code_generator'] = module_pyname |
86 | 87 |
87 # Add includes for any dependencies | 88 # Add includes for any dependencies |
88 template_context['header_includes'] = sorted( | 89 template_context['header_includes'] = sorted( |
89 template_context['header_includes']) | 90 template_context['header_includes']) |
90 includes.update(interface_info.get('dependencies_include_paths', [])) | 91 includes.update(interface_info.get('dependencies_include_paths', [])) |
91 template_context['cpp_includes'] = sorted(includes) | 92 template_context['cpp_includes'] = sorted(includes) |
92 | 93 |
93 header_text = header_template.render(template_context) | 94 header_text = header_template.render(template_context) |
94 cpp_text = cpp_template.render(template_context) | 95 cpp_text = cpp_template.render(template_context) |
95 return header_text, cpp_text | 96 return header_text, cpp_text |
96 | 97 |
97 | 98 |
98 class CodeGeneratorV8(object): | 99 class CodeGeneratorBase(object): |
| 100 """Base class for v8 bindings generator and IDL dictionary impl generator""" |
| 101 |
99 def __init__(self, interfaces_info, cache_dir, output_dir): | 102 def __init__(self, interfaces_info, cache_dir, output_dir): |
100 interfaces_info = interfaces_info or {} | 103 interfaces_info = interfaces_info or {} |
101 self.interfaces_info = interfaces_info | 104 self.interfaces_info = interfaces_info |
102 self.jinja_env = initialize_jinja_env(cache_dir) | 105 self.jinja_env = initialize_jinja_env(cache_dir) |
103 self.output_dir = output_dir | 106 self.output_dir = output_dir |
104 | 107 |
105 # Set global type info | 108 # Set global type info |
106 idl_types.set_ancestors(dict( | 109 idl_types.set_ancestors(interfaces_info['ancestors']) |
107 (interface_name, interface_info['ancestors']) | 110 IdlType.set_callback_interfaces(interfaces_info['callback_interfaces']) |
108 for interface_name, interface_info in interfaces_info.iteritems() | 111 IdlType.set_dictionaries(interfaces_info['dictionaries']) |
109 if interface_info['ancestors'])) | 112 IdlType.set_implemented_as_interfaces(interfaces_info['implemented_as_in
terfaces']) |
110 IdlType.set_callback_interfaces(set( | 113 IdlType.set_garbage_collected_types(interfaces_info['garbage_collected_i
nterfaces']) |
111 interface_name | 114 IdlType.set_will_be_garbage_collected_types(interfaces_info['will_be_gar
bage_collected_interfaces']) |
112 for interface_name, interface_info in interfaces_info.iteritems() | 115 v8_types.set_component_dirs(interfaces_info['component_dirs']) |
113 if interface_info['is_callback_interface'])) | |
114 IdlType.set_dictionaries(set( | |
115 dictionary_name | |
116 for dictionary_name, interface_info in interfaces_info.iteritems() | |
117 if interface_info['is_dictionary'])) | |
118 IdlType.set_implemented_as_interfaces(dict( | |
119 (interface_name, interface_info['implemented_as']) | |
120 for interface_name, interface_info in interfaces_info.iteritems() | |
121 if interface_info['implemented_as'])) | |
122 IdlType.set_garbage_collected_types(set( | |
123 interface_name | |
124 for interface_name, interface_info in interfaces_info.iteritems() | |
125 if 'GarbageCollected' in interface_info['inherited_extended_attribut
es'])) | |
126 IdlType.set_will_be_garbage_collected_types(set( | |
127 interface_name | |
128 for interface_name, interface_info in interfaces_info.iteritems() | |
129 if 'WillBeGarbageCollected' in interface_info['inherited_extended_at
tributes'])) | |
130 v8_types.set_component_dirs(dict( | |
131 (interface_name, interface_info['component_dir']) | |
132 for interface_name, interface_info in interfaces_info.iteritems())) | |
133 | 116 |
134 def output_paths_for_bindings(self, definition_name): | 117 def generate_code(self, definitions, definition_name): |
| 118 """Returns .h/.cpp code as ((path, content)...).""" |
| 119 # Set local type info |
| 120 IdlType.set_callback_functions(definitions.callback_functions.keys()) |
| 121 IdlType.set_enums((enum.name, enum.values) |
| 122 for enum in definitions.enumerations.values()) |
| 123 return self.generate_code_internal(definitions, definition_name) |
| 124 |
| 125 def generate_code_internal(self, definitions, definition_name): |
| 126 # This should be implemented in subclasses. |
| 127 raise NotImplementedError() |
| 128 |
| 129 |
| 130 class CodeGeneratorV8(CodeGeneratorBase): |
| 131 def __init__(self, interfaces_info, cache_dir, output_dir): |
| 132 CodeGeneratorBase.__init__(self, interfaces_info, cache_dir, output_dir) |
| 133 |
| 134 def output_paths(self, definition_name): |
135 header_path = posixpath.join(self.output_dir, | 135 header_path = posixpath.join(self.output_dir, |
136 'V8%s.h' % definition_name) | 136 'V8%s.h' % definition_name) |
137 cpp_path = posixpath.join(self.output_dir, 'V8%s.cpp' % definition_name) | 137 cpp_path = posixpath.join(self.output_dir, 'V8%s.cpp' % definition_name) |
138 return header_path, cpp_path | 138 return header_path, cpp_path |
139 | 139 |
140 def output_paths_for_impl(self, definition_name): | 140 def generate_code_internal(self, definitions, definition_name): |
141 header_path = posixpath.join(self.output_dir, '%s.h' % definition_name) | |
142 cpp_path = posixpath.join(self.output_dir, '%s.cpp' % definition_name) | |
143 return header_path, cpp_path | |
144 | |
145 def generate_code(self, definitions, definition_name): | |
146 """Returns .h/.cpp code as (header_text, cpp_text).""" | |
147 # Set local type info | |
148 IdlType.set_callback_functions(definitions.callback_functions.keys()) | |
149 IdlType.set_enums((enum.name, enum.values) | |
150 for enum in definitions.enumerations.values()) | |
151 | |
152 if definition_name in definitions.interfaces: | 141 if definition_name in definitions.interfaces: |
153 return self.generate_interface_code( | 142 return self.generate_interface_code( |
154 definitions, definition_name, | 143 definitions, definition_name, |
155 definitions.interfaces[definition_name]) | 144 definitions.interfaces[definition_name]) |
156 if definition_name in definitions.dictionaries: | 145 if definition_name in definitions.dictionaries: |
157 return self.generate_dictionary_code( | 146 return self.generate_dictionary_code( |
158 definitions, definition_name, | 147 definitions, definition_name, |
159 definitions.dictionaries[definition_name]) | 148 definitions.dictionaries[definition_name]) |
160 raise ValueError('%s is not in IDL definitions' % definition_name) | 149 raise ValueError('%s is not in IDL definitions' % definition_name) |
161 | 150 |
(...skipping 13 matching lines...) Expand all Loading... |
175 header_template = self.jinja_env.get_template(header_template_filename) | 164 header_template = self.jinja_env.get_template(header_template_filename) |
176 cpp_template = self.jinja_env.get_template(cpp_template_filename) | 165 cpp_template = self.jinja_env.get_template(cpp_template_filename) |
177 | 166 |
178 interface_info = self.interfaces_info[interface_name] | 167 interface_info = self.interfaces_info[interface_name] |
179 | 168 |
180 template_context = interface_context(interface) | 169 template_context = interface_context(interface) |
181 # Add the include for interface itself | 170 # Add the include for interface itself |
182 template_context['header_includes'].add(interface_info['include_path']) | 171 template_context['header_includes'].add(interface_info['include_path']) |
183 header_text, cpp_text = render_template( | 172 header_text, cpp_text = render_template( |
184 interface_info, header_template, cpp_template, template_context) | 173 interface_info, header_template, cpp_template, template_context) |
185 header_path, cpp_path = self.output_paths_for_bindings(interface_name) | 174 header_path, cpp_path = self.output_paths(interface_name) |
186 return ( | 175 return ( |
187 (header_path, header_text), | 176 (header_path, header_text), |
188 (cpp_path, cpp_text), | 177 (cpp_path, cpp_text), |
189 ) | 178 ) |
190 | 179 |
191 def generate_dictionary_code(self, definitions, dictionary_name, | 180 def generate_dictionary_code(self, definitions, dictionary_name, |
192 dictionary): | 181 dictionary): |
193 interface_info = self.interfaces_info[dictionary_name] | |
194 bindings_results = self.generate_dictionary_bindings( | |
195 dictionary_name, interface_info, dictionary) | |
196 impl_results = self.generate_dictionary_impl( | |
197 dictionary_name, interface_info, dictionary) | |
198 return bindings_results + impl_results | |
199 | |
200 def generate_dictionary_bindings(self, dictionary_name, | |
201 interface_info, dictionary): | |
202 header_template = self.jinja_env.get_template('dictionary_v8.h') | 182 header_template = self.jinja_env.get_template('dictionary_v8.h') |
203 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') | 183 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') |
204 template_context = v8_dictionary.dictionary_context(dictionary) | 184 template_context = v8_dictionary.dictionary_context(dictionary) |
| 185 interface_info = self.interfaces_info[dictionary_name] |
205 # Add the include for interface itself | 186 # Add the include for interface itself |
206 template_context['header_includes'].add(interface_info['include_path']) | 187 template_context['header_includes'].add(interface_info['include_path']) |
207 header_text, cpp_text = render_template( | 188 header_text, cpp_text = render_template( |
208 interface_info, header_template, cpp_template, template_context) | 189 interface_info, header_template, cpp_template, template_context) |
209 header_path, cpp_path = self.output_paths_for_bindings(dictionary_name) | 190 header_path, cpp_path = self.output_paths(dictionary_name) |
210 return ( | 191 return ( |
211 (header_path, header_text), | 192 (header_path, header_text), |
212 (cpp_path, cpp_text), | 193 (cpp_path, cpp_text), |
213 ) | 194 ) |
214 | 195 |
215 def generate_dictionary_impl(self, dictionary_name, | 196 |
216 interface_info, dictionary): | 197 class CodeGeneratorDictionaryImpl(CodeGeneratorBase): |
| 198 def __init__(self, interfaces_info, cache_dir, output_dir): |
| 199 CodeGeneratorBase.__init__(self, interfaces_info, cache_dir, output_dir) |
| 200 |
| 201 def output_paths(self, definition_name, interface_info): |
| 202 output_dir = posixpath.join(self.output_dir, |
| 203 interface_info['relative_dir']) |
| 204 header_path = posixpath.join(output_dir, '%s.h' % definition_name) |
| 205 cpp_path = posixpath.join(output_dir, '%s.cpp' % definition_name) |
| 206 return header_path, cpp_path |
| 207 |
| 208 def generate_code_internal(self, definitions, definition_name): |
| 209 if not definition_name in definitions.dictionaries: |
| 210 raise ValueError('%s is not an IDL dictionary') |
| 211 dictionary = definitions.dictionaries[definition_name] |
| 212 interface_info = self.interfaces_info[definition_name] |
217 header_template = self.jinja_env.get_template('dictionary_impl.h') | 213 header_template = self.jinja_env.get_template('dictionary_impl.h') |
218 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') | 214 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') |
219 template_context = v8_dictionary.dictionary_impl_context( | 215 template_context = v8_dictionary.dictionary_impl_context( |
220 dictionary, self.interfaces_info) | 216 dictionary, self.interfaces_info) |
221 header_text, cpp_text = render_template( | 217 header_text, cpp_text = render_template( |
222 interface_info, header_template, cpp_template, template_context) | 218 interface_info, header_template, cpp_template, template_context) |
223 header_path, cpp_path = self.output_paths_for_impl(dictionary_name) | 219 header_path, cpp_path = self.output_paths( |
| 220 definition_name, interface_info) |
224 return ( | 221 return ( |
225 (header_path, header_text), | 222 (header_path, header_text), |
226 (cpp_path, cpp_text), | 223 (cpp_path, cpp_text), |
227 ) | 224 ) |
228 | 225 |
229 | 226 |
230 def initialize_jinja_env(cache_dir): | 227 def initialize_jinja_env(cache_dir): |
231 jinja_env = jinja2.Environment( | 228 jinja_env = jinja2.Environment( |
232 loader=jinja2.FileSystemLoader(templates_dir), | 229 loader=jinja2.FileSystemLoader(templates_dir), |
233 # Bytecode cache is not concurrency-safe unless pre-cached: | 230 # Bytecode cache is not concurrency-safe unless pre-cached: |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
306 | 303 |
307 # Create a dummy file as output for the build system, | 304 # Create a dummy file as output for the build system, |
308 # since filenames of individual cache files are unpredictable and opaque | 305 # since filenames of individual cache files are unpredictable and opaque |
309 # (they are hashes of the template path, which varies based on environment) | 306 # (they are hashes of the template path, which varies based on environment) |
310 with open(dummy_filename, 'w') as dummy_file: | 307 with open(dummy_filename, 'w') as dummy_file: |
311 pass # |open| creates or touches the file | 308 pass # |open| creates or touches the file |
312 | 309 |
313 | 310 |
314 if __name__ == '__main__': | 311 if __name__ == '__main__': |
315 sys.exit(main(sys.argv)) | 312 sys.exit(main(sys.argv)) |
OLD | NEW |