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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 66 |
67 # jinja2 is in chromium's third_party directory. | 67 # jinja2 is in chromium's third_party directory. |
68 # Insert at 1 so at front to override system libraries, and | 68 # Insert at 1 so at front to override system libraries, and |
69 # after path[0] == invoking script dir | 69 # after path[0] == invoking script dir |
70 sys.path.insert(1, third_party_dir) | 70 sys.path.insert(1, third_party_dir) |
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 from v8_globals import includes, interfaces | 77 from v8_globals import includes, interfaces |
77 import v8_interface | 78 import v8_interface |
78 import v8_types | 79 import v8_types |
79 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 |
80 | 81 |
81 | 82 |
| 83 def render_template(interface_info, header_template, cpp_template, |
| 84 template_context): |
| 85 template_context['code_generator'] = module_pyname |
| 86 |
| 87 # Add includes for any dependencies |
| 88 template_context['header_includes'] = sorted( |
| 89 template_context['header_includes']) |
| 90 includes.update(interface_info.get('dependencies_include_paths', [])) |
| 91 template_context['cpp_includes'] = sorted(includes) |
| 92 |
| 93 header_text = header_template.render(template_context) |
| 94 cpp_text = cpp_template.render(template_context) |
| 95 return header_text, cpp_text |
| 96 |
| 97 |
82 class CodeGeneratorV8(object): | 98 class CodeGeneratorV8(object): |
83 def __init__(self, interfaces_info, cache_dir): | 99 def __init__(self, interfaces_info, cache_dir): |
84 interfaces_info = interfaces_info or {} | 100 interfaces_info = interfaces_info or {} |
85 self.interfaces_info = interfaces_info | 101 self.interfaces_info = interfaces_info |
86 self.jinja_env = initialize_jinja_env(cache_dir) | 102 self.jinja_env = initialize_jinja_env(cache_dir) |
87 | 103 |
88 # Set global type info | 104 # Set global type info |
89 idl_types.set_ancestors(dict( | 105 idl_types.set_ancestors(dict( |
90 (interface_name, interface_info['ancestors']) | 106 (interface_name, interface_info['ancestors']) |
91 for interface_name, interface_info in interfaces_info.iteritems() | 107 for interface_name, interface_info in interfaces_info.iteritems() |
92 if interface_info['ancestors'])) | 108 if interface_info['ancestors'])) |
93 IdlType.set_callback_interfaces(set( | 109 IdlType.set_callback_interfaces(set( |
94 interface_name | 110 interface_name |
95 for interface_name, interface_info in interfaces_info.iteritems() | 111 for interface_name, interface_info in interfaces_info.iteritems() |
96 if interface_info['is_callback_interface'])) | 112 if interface_info['is_callback_interface'])) |
| 113 IdlType.set_dictionaries(set( |
| 114 dictionary_name |
| 115 for dictionary_name, interface_info in interfaces_info.iteritems() |
| 116 if interface_info['is_dictionary'])) |
97 IdlType.set_implemented_as_interfaces(dict( | 117 IdlType.set_implemented_as_interfaces(dict( |
98 (interface_name, interface_info['implemented_as']) | 118 (interface_name, interface_info['implemented_as']) |
99 for interface_name, interface_info in interfaces_info.iteritems() | 119 for interface_name, interface_info in interfaces_info.iteritems() |
100 if interface_info['implemented_as'])) | 120 if interface_info['implemented_as'])) |
101 IdlType.set_garbage_collected_types(set( | 121 IdlType.set_garbage_collected_types(set( |
102 interface_name | 122 interface_name |
103 for interface_name, interface_info in interfaces_info.iteritems() | 123 for interface_name, interface_info in interfaces_info.iteritems() |
104 if 'GarbageCollected' in interface_info['inherited_extended_attribut
es'])) | 124 if 'GarbageCollected' in interface_info['inherited_extended_attribut
es'])) |
105 IdlType.set_will_be_garbage_collected_types(set( | 125 IdlType.set_will_be_garbage_collected_types(set( |
106 interface_name | 126 interface_name |
107 for interface_name, interface_info in interfaces_info.iteritems() | 127 for interface_name, interface_info in interfaces_info.iteritems() |
108 if 'WillBeGarbageCollected' in interface_info['inherited_extended_at
tributes'])) | 128 if 'WillBeGarbageCollected' in interface_info['inherited_extended_at
tributes'])) |
109 v8_types.set_component_dirs(dict( | 129 v8_types.set_component_dirs(dict( |
110 (interface_name, interface_info['component_dir']) | 130 (interface_name, interface_info['component_dir']) |
111 for interface_name, interface_info in interfaces_info.iteritems())) | 131 for interface_name, interface_info in interfaces_info.iteritems())) |
112 | 132 |
113 def generate_code(self, definitions, interface_name): | 133 def generate_code(self, definitions, definition_name): |
114 """Returns .h/.cpp code as (header_text, cpp_text).""" | 134 """Returns .h/.cpp code as (header_text, cpp_text).""" |
115 try: | |
116 interface = definitions.interfaces[interface_name] | |
117 except KeyError: | |
118 raise Exception('%s not in IDL definitions' % interface_name) | |
119 | |
120 # Store other interfaces for introspection | |
121 interfaces.update(definitions.interfaces) | |
122 | |
123 # Set local type info | 135 # Set local type info |
124 IdlType.set_callback_functions(definitions.callback_functions.keys()) | 136 IdlType.set_callback_functions(definitions.callback_functions.keys()) |
125 IdlType.set_enums((enum.name, enum.values) | 137 IdlType.set_enums((enum.name, enum.values) |
126 for enum in definitions.enumerations.values()) | 138 for enum in definitions.enumerations.values()) |
127 | 139 |
| 140 if definition_name in definitions.interfaces: |
| 141 return self.generate_interface_code( |
| 142 definitions, definition_name, |
| 143 definitions.interfaces[definition_name]) |
| 144 if definition_name in definitions.dictionaries: |
| 145 return self.generate_dictionary_code( |
| 146 definitions, definition_name, |
| 147 definitions.dictionaries[definition_name]) |
| 148 raise ValueError('%s is not in IDL definitions' % definition_name) |
| 149 |
| 150 def generate_interface_code(self, definitions, interface_name, interface): |
| 151 # Store other interfaces for introspection |
| 152 interfaces.update(definitions.interfaces) |
| 153 |
128 # Select appropriate Jinja template and contents function | 154 # Select appropriate Jinja template and contents function |
129 if interface.is_callback: | 155 if interface.is_callback: |
130 header_template_filename = 'callback_interface.h' | 156 header_template_filename = 'callback_interface.h' |
131 cpp_template_filename = 'callback_interface.cpp' | 157 cpp_template_filename = 'callback_interface.cpp' |
132 interface_context = v8_callback_interface.callback_interface_context | 158 interface_context = v8_callback_interface.callback_interface_context |
133 else: | 159 else: |
134 header_template_filename = 'interface.h' | 160 header_template_filename = 'interface.h' |
135 cpp_template_filename = 'interface.cpp' | 161 cpp_template_filename = 'interface.cpp' |
136 interface_context = v8_interface.interface_context | 162 interface_context = v8_interface.interface_context |
137 header_template = self.jinja_env.get_template(header_template_filename) | 163 header_template = self.jinja_env.get_template(header_template_filename) |
138 cpp_template = self.jinja_env.get_template(cpp_template_filename) | 164 cpp_template = self.jinja_env.get_template(cpp_template_filename) |
139 | 165 |
140 # Compute context (input values for Jinja) | 166 interface_info = self.interfaces_info[interface_name] |
| 167 |
141 template_context = interface_context(interface) | 168 template_context = interface_context(interface) |
142 template_context['code_generator'] = module_pyname | 169 # Add the include for interface itself |
| 170 template_context['header_includes'].add(interface_info['include_path']) |
| 171 header_text, cpp_text = render_template( |
| 172 interface_info, header_template, cpp_template, template_context) |
| 173 return header_text, cpp_text |
143 | 174 |
144 # Add includes for interface itself and any dependencies | 175 def generate_dictionary_code(self, definitions, dictionary_name, |
145 interface_info = self.interfaces_info[interface_name] | 176 dictionary): |
| 177 interface_info = self.interfaces_info[dictionary_name] |
| 178 # FIXME: Generate impl class |
| 179 return self.generate_dictionary_bindings( |
| 180 dictionary_name, interface_info, dictionary) |
| 181 |
| 182 def generate_dictionary_bindings(self, dictionary_name, |
| 183 interface_info, dictionary): |
| 184 header_template = self.jinja_env.get_template('dictionary_v8.h') |
| 185 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') |
| 186 template_context = v8_dictionary.dictionary_context(dictionary) |
| 187 # Add the include for interface itself |
146 template_context['header_includes'].add(interface_info['include_path']) | 188 template_context['header_includes'].add(interface_info['include_path']) |
147 template_context['header_includes'] = sorted(template_context['header_in
cludes']) | 189 header_text, cpp_text = render_template( |
148 includes.update(interface_info.get('dependencies_include_paths', [])) | 190 interface_info, header_template, cpp_template, template_context) |
149 template_context['cpp_includes'] = sorted(includes) | |
150 | |
151 # Render Jinja templates | |
152 header_text = header_template.render(template_context) | |
153 cpp_text = cpp_template.render(template_context) | |
154 return header_text, cpp_text | 191 return header_text, cpp_text |
155 | 192 |
156 | 193 |
157 def initialize_jinja_env(cache_dir): | 194 def initialize_jinja_env(cache_dir): |
158 jinja_env = jinja2.Environment( | 195 jinja_env = jinja2.Environment( |
159 loader=jinja2.FileSystemLoader(templates_dir), | 196 loader=jinja2.FileSystemLoader(templates_dir), |
160 # Bytecode cache is not concurrency-safe unless pre-cached: | 197 # Bytecode cache is not concurrency-safe unless pre-cached: |
161 # if pre-cached this is read-only, but writing creates a race condition. | 198 # if pre-cached this is read-only, but writing creates a race condition. |
162 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), | 199 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), |
163 keep_trailing_newline=True, # newline-terminate generated files | 200 keep_trailing_newline=True, # newline-terminate generated files |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 250 |
214 # Create a dummy file as output for the build system, | 251 # Create a dummy file as output for the build system, |
215 # since filenames of individual cache files are unpredictable and opaque | 252 # since filenames of individual cache files are unpredictable and opaque |
216 # (they are hashes of the template path, which varies based on environment) | 253 # (they are hashes of the template path, which varies based on environment) |
217 with open(dummy_filename, 'w') as dummy_file: | 254 with open(dummy_filename, 'w') as dummy_file: |
218 pass # |open| creates or touches the file | 255 pass # |open| creates or touches the file |
219 | 256 |
220 | 257 |
221 if __name__ == '__main__': | 258 if __name__ == '__main__': |
222 sys.exit(main(sys.argv)) | 259 sys.exit(main(sys.argv)) |
OLD | NEW |