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

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: sequence and array support 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 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,
100 v8_output_dir, impl_output_dir=None):
84 interfaces_info = interfaces_info or {} 101 interfaces_info = interfaces_info or {}
85 self.interfaces_info = interfaces_info 102 self.interfaces_info = interfaces_info
86 self.jinja_env = initialize_jinja_env(cache_dir) 103 self.jinja_env = initialize_jinja_env(cache_dir)
104 self.v8_output_dir = v8_output_dir
105 self.impl_output_dir = impl_output_dir
87 106
88 # Set global type info 107 # Set global type info
89 idl_types.set_ancestors(dict( 108 idl_types.set_ancestors(dict(
90 (interface_name, interface_info['ancestors']) 109 (interface_name, interface_info['ancestors'])
91 for interface_name, interface_info in interfaces_info.iteritems() 110 for interface_name, interface_info in interfaces_info.iteritems()
92 if interface_info['ancestors'])) 111 if interface_info['ancestors']))
93 IdlType.set_callback_interfaces(set( 112 IdlType.set_callback_interfaces(set(
94 interface_name 113 interface_name
95 for interface_name, interface_info in interfaces_info.iteritems() 114 for interface_name, interface_info in interfaces_info.iteritems()
96 if interface_info['is_callback_interface'])) 115 if interface_info['is_callback_interface']))
116 IdlType.set_dictionaries(set(
117 dictionary_name
118 for dictionary_name, interface_info in interfaces_info.iteritems()
119 if interface_info['is_dictionary']))
97 IdlType.set_implemented_as_interfaces(dict( 120 IdlType.set_implemented_as_interfaces(dict(
98 (interface_name, interface_info['implemented_as']) 121 (interface_name, interface_info['implemented_as'])
99 for interface_name, interface_info in interfaces_info.iteritems() 122 for interface_name, interface_info in interfaces_info.iteritems()
100 if interface_info['implemented_as'])) 123 if interface_info['implemented_as']))
101 IdlType.set_garbage_collected_types(set( 124 IdlType.set_garbage_collected_types(set(
102 interface_name 125 interface_name
103 for interface_name, interface_info in interfaces_info.iteritems() 126 for interface_name, interface_info in interfaces_info.iteritems()
104 if 'GarbageCollected' in interface_info['inherited_extended_attribut es'])) 127 if 'GarbageCollected' in interface_info['inherited_extended_attribut es']))
105 IdlType.set_will_be_garbage_collected_types(set( 128 IdlType.set_will_be_garbage_collected_types(set(
106 interface_name 129 interface_name
107 for interface_name, interface_info in interfaces_info.iteritems() 130 for interface_name, interface_info in interfaces_info.iteritems()
108 if 'WillBeGarbageCollected' in interface_info['inherited_extended_at tributes'])) 131 if 'WillBeGarbageCollected' in interface_info['inherited_extended_at tributes']))
109 v8_types.set_component_dirs(dict( 132 v8_types.set_component_dirs(dict(
110 (interface_name, interface_info['component_dir']) 133 (interface_name, interface_info['component_dir'])
111 for interface_name, interface_info in interfaces_info.iteritems())) 134 for interface_name, interface_info in interfaces_info.iteritems()))
112 135
113 def generate_code(self, definitions, interface_name): 136 def generate_code(self, definitions, definition_name):
114 """Returns .h/.cpp code as (header_text, cpp_text).""" 137 """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 138 # Set local type info
124 IdlType.set_callback_functions(definitions.callback_functions.keys()) 139 IdlType.set_callback_functions(definitions.callback_functions.keys())
125 IdlType.set_enums((enum.name, enum.values) 140 IdlType.set_enums((enum.name, enum.values)
126 for enum in definitions.enumerations.values()) 141 for enum in definitions.enumerations.values())
127 142
143 if definition_name in definitions.interfaces:
144 return self.generate_interface_code(
145 definitions, definition_name,
146 definitions.interfaces[definition_name])
147 if definition_name in definitions.dictionaries:
148 return self.generate_dictionary_code(
149 definitions, definition_name,
150 definitions.dictionaries[definition_name])
151 raise ValueError('%s is not in IDL definitions' % definition_name)
152
153 def output_paths_for_bindings(self, definition_name):
154 header_path = posixpath.join(self.v8_output_dir,
155 'V8%s.h' % definition_name)
156 cpp_path = posixpath.join(self.v8_output_dir,
157 'V8%s.cpp' % definition_name)
158 return header_path, cpp_path
159
160 def output_paths_for_impl(self, definition_name, relative_dir):
161 output_dir = (posixpath.join(self.impl_output_dir, relative_dir) if
162 self.impl_output_dir else self.v8_output_dir)
163 header_path = posixpath.join(output_dir, '%s.h' % definition_name)
164 cpp_path = posixpath.join(output_dir, '%s.cpp' % definition_name)
165 return header_path, cpp_path
166
167 def generate_interface_code(self, definitions, interface_name, interface):
168 # Store other interfaces for introspection
169 interfaces.update(definitions.interfaces)
170
128 # Select appropriate Jinja template and contents function 171 # Select appropriate Jinja template and contents function
129 if interface.is_callback: 172 if interface.is_callback:
130 header_template_filename = 'callback_interface.h' 173 header_template_filename = 'callback_interface.h'
131 cpp_template_filename = 'callback_interface.cpp' 174 cpp_template_filename = 'callback_interface.cpp'
132 interface_context = v8_callback_interface.callback_interface_context 175 interface_context = v8_callback_interface.callback_interface_context
133 else: 176 else:
134 header_template_filename = 'interface.h' 177 header_template_filename = 'interface.h'
135 cpp_template_filename = 'interface.cpp' 178 cpp_template_filename = 'interface.cpp'
136 interface_context = v8_interface.interface_context 179 interface_context = v8_interface.interface_context
137 header_template = self.jinja_env.get_template(header_template_filename) 180 header_template = self.jinja_env.get_template(header_template_filename)
138 cpp_template = self.jinja_env.get_template(cpp_template_filename) 181 cpp_template = self.jinja_env.get_template(cpp_template_filename)
139 182
140 # Compute context (input values for Jinja) 183 interface_info = self.interfaces_info[interface_name]
184
141 template_context = interface_context(interface) 185 template_context = interface_context(interface)
142 template_context['code_generator'] = module_pyname 186 # Add the include for interface itself
187 template_context['header_includes'].add(interface_info['include_path'])
188 header_text, cpp_text = render_template(
189 interface_info, header_template, cpp_template, template_context)
190 header_path, cpp_path = self.output_paths_for_bindings(interface_name)
191 return (
192 (header_path, header_text),
193 (cpp_path, cpp_text),
194 )
143 195
144 # Add includes for interface itself and any dependencies 196 def generate_dictionary_code(self, definitions, dictionary_name,
145 interface_info = self.interfaces_info[interface_name] 197 dictionary):
198 interface_info = self.interfaces_info[dictionary_name]
199 bindings_results = self.generate_dictionary_bindings(
200 dictionary_name, interface_info, dictionary)
201 impl_results = self.generate_dictionary_impl(
202 dictionary_name, interface_info, dictionary)
203 return bindings_results + impl_results
204
205 def generate_dictionary_bindings(self, dictionary_name,
206 interface_info, dictionary):
207 header_template = self.jinja_env.get_template('dictionary_v8.h')
208 cpp_template = self.jinja_env.get_template('dictionary_v8.cpp')
209 template_context = v8_dictionary.dictionary_context_v8(dictionary)
210 # Add the include for interface itself
146 template_context['header_includes'].add(interface_info['include_path']) 211 template_context['header_includes'].add(interface_info['include_path'])
147 template_context['header_includes'] = sorted(template_context['header_in cludes']) 212 header_text, cpp_text = render_template(
148 includes.update(interface_info.get('dependencies_include_paths', [])) 213 interface_info, header_template, cpp_template, template_context)
149 template_context['cpp_includes'] = sorted(includes) 214 header_path, cpp_path = self.output_paths_for_bindings(dictionary_name)
215 return (
216 (header_path, header_text),
217 (cpp_path, cpp_text),
218 )
150 219
151 # Render Jinja templates 220 def generate_dictionary_impl(self, dictionary_name,
152 header_text = header_template.render(template_context) 221 interface_info, dictionary):
153 cpp_text = cpp_template.render(template_context) 222 header_template = self.jinja_env.get_template('dictionary_impl.h')
154 return header_text, cpp_text 223 cpp_template = self.jinja_env.get_template('dictionary_impl.cpp')
155 224 template_context = v8_dictionary.dictionary_context_impl(
225 dictionary, self.interfaces_info)
226 header_text, cpp_text = render_template(
227 interface_info, header_template, cpp_template, template_context)
228 header_path, cpp_path = self.output_paths_for_impl(
229 dictionary_name, interface_info['relative_dir'])
230 return (
231 (header_path, header_text),
232 (cpp_path, cpp_text),
233 )
156 234
157 def initialize_jinja_env(cache_dir): 235 def initialize_jinja_env(cache_dir):
158 jinja_env = jinja2.Environment( 236 jinja_env = jinja2.Environment(
159 loader=jinja2.FileSystemLoader(templates_dir), 237 loader=jinja2.FileSystemLoader(templates_dir),
160 # Bytecode cache is not concurrency-safe unless pre-cached: 238 # Bytecode cache is not concurrency-safe unless pre-cached:
161 # if pre-cached this is read-only, but writing creates a race condition. 239 # if pre-cached this is read-only, but writing creates a race condition.
162 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), 240 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
163 keep_trailing_newline=True, # newline-terminate generated files 241 keep_trailing_newline=True, # newline-terminate generated files
164 lstrip_blocks=True, # so can indent control flow tags 242 lstrip_blocks=True, # so can indent control flow tags
165 trim_blocks=True) 243 trim_blocks=True)
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 291
214 # Create a dummy file as output for the build system, 292 # Create a dummy file as output for the build system,
215 # since filenames of individual cache files are unpredictable and opaque 293 # since filenames of individual cache files are unpredictable and opaque
216 # (they are hashes of the template path, which varies based on environment) 294 # (they are hashes of the template path, which varies based on environment)
217 with open(dummy_filename, 'w') as dummy_file: 295 with open(dummy_filename, 'w') as dummy_file:
218 pass # |open| creates or touches the file 296 pass # |open| creates or touches the file
219 297
220 298
221 if __name__ == '__main__': 299 if __name__ == '__main__':
222 sys.exit(main(sys.argv)) 300 sys.exit(main(sys.argv))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698