Chromium Code Reviews| Index: Source/bindings/scripts/code_generator_v8.py |
| diff --git a/Source/bindings/scripts/code_generator_v8.py b/Source/bindings/scripts/code_generator_v8.py |
| index f7af610ccd9ab3b1a92911d9329d2b8a550d43e8..0acc98604012e9917c820b4de87767f2530abff5 100644 |
| --- a/Source/bindings/scripts/code_generator_v8.py |
| +++ b/Source/bindings/scripts/code_generator_v8.py |
| @@ -73,17 +73,27 @@ import jinja2 |
| import idl_types |
| from idl_types import IdlType |
| import v8_callback_interface |
| +import v8_dictionary |
| from v8_globals import includes, interfaces |
| import v8_interface |
| import v8_types |
| from v8_utilities import capitalize, cpp_name, conditional_string, v8_class_name |
| +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.
|
| + def __init__(self, output_path, output_code): |
| + self.output_path = output_path |
| + self.output_code = output_code |
| + |
| + |
| class CodeGeneratorV8(object): |
| - def __init__(self, interfaces_info, cache_dir): |
| + def __init__(self, interfaces_info, cache_dir, |
| + v8_output_dir, impl_output_dir=None): |
| interfaces_info = interfaces_info or {} |
| self.interfaces_info = interfaces_info |
| self.jinja_env = initialize_jinja_env(cache_dir) |
| + self.v8_output_dir = v8_output_dir |
| + self.impl_output_dir = impl_output_dir |
| # Set global type info |
| idl_types.set_ancestors(dict( |
| @@ -94,6 +104,10 @@ class CodeGeneratorV8(object): |
| interface_name |
| for interface_name, interface_info in interfaces_info.iteritems() |
| if interface_info['is_callback_interface'])) |
| + IdlType.set_dictionaries(set( |
| + dictionary_name |
| + for dictionary_name, interface_info in interfaces_info.iteritems() |
| + if interface_info['is_dictionary'])) |
| IdlType.set_implemented_as_interfaces(dict( |
| (interface_name, interface_info['implemented_as']) |
| for interface_name, interface_info in interfaces_info.iteritems() |
| @@ -110,21 +124,63 @@ class CodeGeneratorV8(object): |
| (interface_name, interface_info['component_dir']) |
| for interface_name, interface_info in interfaces_info.iteritems())) |
| - def generate_code(self, definitions, interface_name): |
| + def generate_code(self, definitions, definition_name): |
| """Returns .h/.cpp code as (header_text, cpp_text).""" |
| - try: |
| - interface = definitions.interfaces[interface_name] |
| - except KeyError: |
| - raise Exception('%s not in IDL definitions' % interface_name) |
| - |
| - # Store other interfaces for introspection |
| - interfaces.update(definitions.interfaces) |
| - |
| # Set local type info |
| IdlType.set_callback_functions(definitions.callback_functions.keys()) |
| IdlType.set_enums((enum.name, enum.values) |
| for enum in definitions.enumerations.values()) |
| + if definition_name in definitions.interfaces: |
| + return self.generate_interface_code( |
| + definitions, definition_name, |
| + definitions.interfaces[definition_name]) |
| + 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.
|
| + return self.generate_dictionary_code( |
| + definitions, definition_name, |
| + definitions.dictionaries[definition_name]) |
| + else: |
|
Nils Barth (inactive)
2014/07/18 21:52:34
No else after return
bashi
2014/07/22 02:33:56
Done.
|
| + 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.
|
| + |
| + 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.
|
| + template_context): |
| + template_context['code_generator'] = module_pyname |
| + |
| + # Add includes for any dependencies |
| + template_context['header_includes'] = sorted(template_context['header_includes']) |
| + includes.update(interface_info.get('dependencies_include_paths', [])) |
| + template_context['cpp_includes'] = sorted(includes) |
| + |
| + header_text = header_template.render(template_context) |
| + cpp_text = cpp_template.render(template_context) |
| + return header_text, cpp_text |
| + |
| + def output_paths_for_bindings(self, definition_name): |
| + header_path = posixpath.join(self.v8_output_dir, |
| + 'V8%s.h' % definition_name) |
| + cpp_path = posixpath.join(self.v8_output_dir, |
| + 'V8%s.cpp' % definition_name) |
| + return header_path, cpp_path |
| + |
| + 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.
|
| + if self.impl_output_dir: |
| + header_path = posixpath.join(self.impl_output_dir, |
| + relative_dir, |
| + '%s.h' % definition_name) |
| + cpp_path = posixpath.join(self.impl_output_dir, |
| + relative_dir, |
| + '%s.cpp' % definition_name) |
| + else: |
| + header_path = posixpath.join(self.v8_output_dir, |
| + '%s.h' % definition_name) |
| + cpp_path = posixpath.join(self.v8_output_dir, |
| + '%s.cpp' % definition_name) |
| + return header_path, cpp_path |
| + |
| + def generate_interface_code(self, definitions, interface_name, interface): |
| + # Store other interfaces for introspection |
| + 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.
|
| + |
| # Select appropriate Jinja template and contents function |
| if interface.is_callback: |
| header_template_filename = 'callback_interface.h' |
| @@ -137,22 +193,57 @@ class CodeGeneratorV8(object): |
| header_template = self.jinja_env.get_template(header_template_filename) |
| cpp_template = self.jinja_env.get_template(cpp_template_filename) |
| - # Compute context (input values for Jinja) |
| - template_context = interface_context(interface) |
| - template_context['code_generator'] = module_pyname |
| - |
| - # Add includes for interface itself and any dependencies |
| interface_info = self.interfaces_info[interface_name] |
| - template_context['header_includes'].add(interface_info['include_path']) |
| - template_context['header_includes'] = sorted(template_context['header_includes']) |
| - includes.update(interface_info.get('dependencies_include_paths', [])) |
| - template_context['cpp_includes'] = sorted(includes) |
| - |
| - # Render Jinja templates |
| - header_text = header_template.render(template_context) |
| - cpp_text = cpp_template.render(template_context) |
| - return header_text, cpp_text |
| + template_context = interface_context(interface) |
| + # Add the include for interface itself |
| + template_context['header_includes'].add(interface_info['include_path']) |
| + header_text, cpp_text = self.render_template( |
| + interface_info, header_template, cpp_template, template_context) |
| + header_path, cpp_path = self.output_paths_for_bindings(interface_name) |
| + 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.
|
| + GenerationResult(header_path, header_text), |
| + GenerationResult(cpp_path, cpp_text), |
| + ] |
| + |
| + def generate_dictionary_code(self, definitions, dictionary_name, |
| + dictionary): |
| + interface_info = self.interfaces_info[dictionary_name] |
| + bindings_results = self.generate_dictionary_bindings( |
| + dictionary_name, interface_info, dictionary) |
| + impl_results = self.generate_dictionary_impl( |
| + dictionary_name, interface_info, dictionary) |
| + return bindings_results + impl_results |
| + |
| + def generate_dictionary_bindings(self, dictionary_name, |
| + interface_info, dictionary): |
| + header_template = self.jinja_env.get_template('dictionary_v8.h') |
| + cpp_template = self.jinja_env.get_template('dictionary_v8.cpp') |
| + template_context = v8_dictionary.dictionary_context_v8(dictionary) |
| + # Add the include for interface itself |
| + template_context['header_includes'].add(interface_info['include_path']) |
| + header_text, cpp_text = self.render_template( |
| + interface_info, header_template, cpp_template, template_context) |
| + header_path, cpp_path = self.output_paths_for_bindings(dictionary_name) |
| + return [ |
|
Nils Barth (inactive)
2014/07/18 21:52:33
ditto
bashi
2014/07/22 02:33:56
Done.
|
| + GenerationResult(header_path, header_text), |
| + GenerationResult(cpp_path, cpp_text), |
| + ] |
| + |
| + def generate_dictionary_impl(self, dictionary_name, |
| + interface_info, dictionary): |
| + header_template = self.jinja_env.get_template('dictionary_impl.h') |
| + cpp_template = self.jinja_env.get_template('dictionary_impl.cpp') |
| + template_context = v8_dictionary.dictionary_context_impl( |
| + dictionary, self.interfaces_info) |
| + header_text, cpp_text = self.render_template( |
| + interface_info, header_template, cpp_template, template_context) |
| + header_path, cpp_path = self.output_paths_for_impl( |
| + dictionary_name, interface_info['relative_dir']) |
| + return [ |
|
Nils Barth (inactive)
2014/07/18 21:52:33
ditto
bashi
2014/07/22 02:33:56
Done.
|
| + GenerationResult(header_path, header_text), |
| + GenerationResult(cpp_path, cpp_text), |
| + ] |
| def initialize_jinja_env(cache_dir): |
| jinja_env = jinja2.Environment( |