Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(258)

Side by Side Diff: Source/bindings/scripts/code_generator_v8.py

Issue 386963003: [WIP][NotForLand] IDL dictionary support (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 class GenerationResult(object):
Nils Barth (inactive) 2014/07/18 21:52:33 collections.namedtuple (or just use a 2-tuple?)
bashi 2014/07/22 02:33:56 Removed and used 2-tuples.
84 def __init__(self, output_path, output_code):
85 self.output_path = output_path
86 self.output_code = output_code
87
88
82 class CodeGeneratorV8(object): 89 class CodeGeneratorV8(object):
83 def __init__(self, interfaces_info, cache_dir): 90 def __init__(self, interfaces_info, cache_dir,
91 v8_output_dir, impl_output_dir=None):
84 interfaces_info = interfaces_info or {} 92 interfaces_info = interfaces_info or {}
85 self.interfaces_info = interfaces_info 93 self.interfaces_info = interfaces_info
86 self.jinja_env = initialize_jinja_env(cache_dir) 94 self.jinja_env = initialize_jinja_env(cache_dir)
95 self.v8_output_dir = v8_output_dir
96 self.impl_output_dir = impl_output_dir
87 97
88 # Set global type info 98 # Set global type info
89 idl_types.set_ancestors(dict( 99 idl_types.set_ancestors(dict(
90 (interface_name, interface_info['ancestors']) 100 (interface_name, interface_info['ancestors'])
91 for interface_name, interface_info in interfaces_info.iteritems() 101 for interface_name, interface_info in interfaces_info.iteritems()
92 if interface_info['ancestors'])) 102 if interface_info['ancestors']))
93 IdlType.set_callback_interfaces(set( 103 IdlType.set_callback_interfaces(set(
94 interface_name 104 interface_name
95 for interface_name, interface_info in interfaces_info.iteritems() 105 for interface_name, interface_info in interfaces_info.iteritems()
96 if interface_info['is_callback_interface'])) 106 if interface_info['is_callback_interface']))
107 IdlType.set_dictionaries(set(
108 dictionary_name
109 for dictionary_name, interface_info in interfaces_info.iteritems()
110 if interface_info['is_dictionary']))
97 IdlType.set_implemented_as_interfaces(dict( 111 IdlType.set_implemented_as_interfaces(dict(
98 (interface_name, interface_info['implemented_as']) 112 (interface_name, interface_info['implemented_as'])
99 for interface_name, interface_info in interfaces_info.iteritems() 113 for interface_name, interface_info in interfaces_info.iteritems()
100 if interface_info['implemented_as'])) 114 if interface_info['implemented_as']))
101 IdlType.set_garbage_collected_types(set( 115 IdlType.set_garbage_collected_types(set(
102 interface_name 116 interface_name
103 for interface_name, interface_info in interfaces_info.iteritems() 117 for interface_name, interface_info in interfaces_info.iteritems()
104 if 'GarbageCollected' in interface_info['inherited_extended_attribut es'])) 118 if 'GarbageCollected' in interface_info['inherited_extended_attribut es']))
105 IdlType.set_will_be_garbage_collected_types(set( 119 IdlType.set_will_be_garbage_collected_types(set(
106 interface_name 120 interface_name
107 for interface_name, interface_info in interfaces_info.iteritems() 121 for interface_name, interface_info in interfaces_info.iteritems()
108 if 'WillBeGarbageCollected' in interface_info['inherited_extended_at tributes'])) 122 if 'WillBeGarbageCollected' in interface_info['inherited_extended_at tributes']))
109 v8_types.set_component_dirs(dict( 123 v8_types.set_component_dirs(dict(
110 (interface_name, interface_info['component_dir']) 124 (interface_name, interface_info['component_dir'])
111 for interface_name, interface_info in interfaces_info.iteritems())) 125 for interface_name, interface_info in interfaces_info.iteritems()))
112 126
113 def generate_code(self, definitions, interface_name): 127 def generate_code(self, definitions, definition_name):
114 """Returns .h/.cpp code as (header_text, cpp_text).""" 128 """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 129 # Set local type info
124 IdlType.set_callback_functions(definitions.callback_functions.keys()) 130 IdlType.set_callback_functions(definitions.callback_functions.keys())
125 IdlType.set_enums((enum.name, enum.values) 131 IdlType.set_enums((enum.name, enum.values)
126 for enum in definitions.enumerations.values()) 132 for enum in definitions.enumerations.values())
127 133
134 if definition_name in definitions.interfaces:
135 return self.generate_interface_code(
136 definitions, definition_name,
137 definitions.interfaces[definition_name])
138 elif definition_name in definitions.dictionaries:
Nils Barth (inactive) 2014/07/18 21:52:33 No else after return (if, not elif).
bashi 2014/07/22 02:33:56 Done.
139 return self.generate_dictionary_code(
140 definitions, definition_name,
141 definitions.dictionaries[definition_name])
142 else:
Nils Barth (inactive) 2014/07/18 21:52:34 No else after return
bashi 2014/07/22 02:33:56 Done.
143 raise Exception('%s is not in IDL definitions' % definition_name)
Nils Barth (inactive) 2014/07/18 21:52:33 Exception => ValueError (similar to key missing)
bashi 2014/07/22 02:33:56 Done.
144
145 def render_template(self, interface_info, header_template, cpp_template,
Nils Barth (inactive) 2014/07/18 21:52:33 This can be a function, not a method, right?
bashi 2014/07/22 02:33:56 Yes. Done.
146 template_context):
147 template_context['code_generator'] = module_pyname
148
149 # Add includes for any dependencies
150 template_context['header_includes'] = sorted(template_context['header_in cludes'])
151 includes.update(interface_info.get('dependencies_include_paths', []))
152 template_context['cpp_includes'] = sorted(includes)
153
154 header_text = header_template.render(template_context)
155 cpp_text = cpp_template.render(template_context)
156 return header_text, cpp_text
157
158 def output_paths_for_bindings(self, definition_name):
159 header_path = posixpath.join(self.v8_output_dir,
160 'V8%s.h' % definition_name)
161 cpp_path = posixpath.join(self.v8_output_dir,
162 'V8%s.cpp' % definition_name)
163 return header_path, cpp_path
164
165 def output_paths_for_impl(self, definition_name, relative_dir):
Nils Barth (inactive) 2014/07/18 21:52:33 output_dir = posixpath.join(self.impl_output_dir,
bashi 2014/07/22 02:33:57 Done.
166 if self.impl_output_dir:
167 header_path = posixpath.join(self.impl_output_dir,
168 relative_dir,
169 '%s.h' % definition_name)
170 cpp_path = posixpath.join(self.impl_output_dir,
171 relative_dir,
172 '%s.cpp' % definition_name)
173 else:
174 header_path = posixpath.join(self.v8_output_dir,
175 '%s.h' % definition_name)
176 cpp_path = posixpath.join(self.v8_output_dir,
177 '%s.cpp' % definition_name)
178 return header_path, cpp_path
179
180 def generate_interface_code(self, definitions, interface_name, interface):
181 # Store other interfaces for introspection
182 interfaces.update(definitions.interfaces)
Nils Barth (inactive) 2014/07/18 21:52:33 *self.*interfaces ?
bashi 2014/07/22 02:33:56 No. it comes from v8_globals.
183
128 # Select appropriate Jinja template and contents function 184 # Select appropriate Jinja template and contents function
129 if interface.is_callback: 185 if interface.is_callback:
130 header_template_filename = 'callback_interface.h' 186 header_template_filename = 'callback_interface.h'
131 cpp_template_filename = 'callback_interface.cpp' 187 cpp_template_filename = 'callback_interface.cpp'
132 interface_context = v8_callback_interface.callback_interface_context 188 interface_context = v8_callback_interface.callback_interface_context
133 else: 189 else:
134 header_template_filename = 'interface.h' 190 header_template_filename = 'interface.h'
135 cpp_template_filename = 'interface.cpp' 191 cpp_template_filename = 'interface.cpp'
136 interface_context = v8_interface.interface_context 192 interface_context = v8_interface.interface_context
137 header_template = self.jinja_env.get_template(header_template_filename) 193 header_template = self.jinja_env.get_template(header_template_filename)
138 cpp_template = self.jinja_env.get_template(cpp_template_filename) 194 cpp_template = self.jinja_env.get_template(cpp_template_filename)
139 195
140 # Compute context (input values for Jinja) 196 interface_info = self.interfaces_info[interface_name]
197
141 template_context = interface_context(interface) 198 template_context = interface_context(interface)
142 template_context['code_generator'] = module_pyname 199 # Add the include for interface itself
200 template_context['header_includes'].add(interface_info['include_path'])
201 header_text, cpp_text = self.render_template(
202 interface_info, header_template, cpp_template, template_context)
203 header_path, cpp_path = self.output_paths_for_bindings(interface_name)
204 return [
Nils Barth (inactive) 2014/07/18 21:52:33 nit: () instead of [] (fixed return value: you're
bashi 2014/07/22 02:33:56 Done.
205 GenerationResult(header_path, header_text),
206 GenerationResult(cpp_path, cpp_text),
207 ]
143 208
144 # Add includes for interface itself and any dependencies 209 def generate_dictionary_code(self, definitions, dictionary_name,
145 interface_info = self.interfaces_info[interface_name] 210 dictionary):
211 interface_info = self.interfaces_info[dictionary_name]
212 bindings_results = self.generate_dictionary_bindings(
213 dictionary_name, interface_info, dictionary)
214 impl_results = self.generate_dictionary_impl(
215 dictionary_name, interface_info, dictionary)
216 return bindings_results + impl_results
217
218 def generate_dictionary_bindings(self, dictionary_name,
219 interface_info, dictionary):
220 header_template = self.jinja_env.get_template('dictionary_v8.h')
221 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp')
222 template_context = v8_dictionary.dictionary_context_v8(dictionary)
223 # Add the include for interface itself
146 template_context['header_includes'].add(interface_info['include_path']) 224 template_context['header_includes'].add(interface_info['include_path'])
147 template_context['header_includes'] = sorted(template_context['header_in cludes']) 225 header_text, cpp_text = self.render_template(
148 includes.update(interface_info.get('dependencies_include_paths', [])) 226 interface_info, header_template, cpp_template, template_context)
149 template_context['cpp_includes'] = sorted(includes) 227 header_path, cpp_path = self.output_paths_for_bindings(dictionary_name)
228 return [
Nils Barth (inactive) 2014/07/18 21:52:33 ditto
bashi 2014/07/22 02:33:56 Done.
229 GenerationResult(header_path, header_text),
230 GenerationResult(cpp_path, cpp_text),
231 ]
150 232
151 # Render Jinja templates 233 def generate_dictionary_impl(self, dictionary_name,
152 header_text = header_template.render(template_context) 234 interface_info, dictionary):
153 cpp_text = cpp_template.render(template_context) 235 header_template = self.jinja_env.get_template('dictionary_impl.h')
154 return header_text, cpp_text 236 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp')
155 237 template_context = v8_dictionary.dictionary_context_impl(
238 dictionary, self.interfaces_info)
239 header_text, cpp_text = self.render_template(
240 interface_info, header_template, cpp_template, template_context)
241 header_path, cpp_path = self.output_paths_for_impl(
242 dictionary_name, interface_info['relative_dir'])
243 return [
Nils Barth (inactive) 2014/07/18 21:52:33 ditto
bashi 2014/07/22 02:33:56 Done.
244 GenerationResult(header_path, header_text),
245 GenerationResult(cpp_path, cpp_text),
246 ]
156 247
157 def initialize_jinja_env(cache_dir): 248 def initialize_jinja_env(cache_dir):
158 jinja_env = jinja2.Environment( 249 jinja_env = jinja2.Environment(
159 loader=jinja2.FileSystemLoader(templates_dir), 250 loader=jinja2.FileSystemLoader(templates_dir),
160 # Bytecode cache is not concurrency-safe unless pre-cached: 251 # Bytecode cache is not concurrency-safe unless pre-cached:
161 # if pre-cached this is read-only, but writing creates a race condition. 252 # if pre-cached this is read-only, but writing creates a race condition.
162 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), 253 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
163 keep_trailing_newline=True, # newline-terminate generated files 254 keep_trailing_newline=True, # newline-terminate generated files
164 lstrip_blocks=True, # so can indent control flow tags 255 lstrip_blocks=True, # so can indent control flow tags
165 trim_blocks=True) 256 trim_blocks=True)
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 304
214 # Create a dummy file as output for the build system, 305 # Create a dummy file as output for the build system,
215 # since filenames of individual cache files are unpredictable and opaque 306 # since filenames of individual cache files are unpredictable and opaque
216 # (they are hashes of the template path, which varies based on environment) 307 # (they are hashes of the template path, which varies based on environment)
217 with open(dummy_filename, 'w') as dummy_file: 308 with open(dummy_filename, 'w') as dummy_file:
218 pass # |open| creates or touches the file 309 pass # |open| creates or touches the file
219 310
220 311
221 if __name__ == '__main__': 312 if __name__ == '__main__':
222 sys.exit(main(sys.argv)) 313 sys.exit(main(sys.argv))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698