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

Unified Diff: Source/bindings/scripts/code_generator_v8.py

Issue 19607011: Generate binding code for VoidCallback.idl with code generator in python (Closed) Base URL: https://chromium.googlesource.com/chromium/blink@master
Patch Set: nits Created 7 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 side-by-side diff with in-line comments
Download patch
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 d2f933044a5a171b5f1109ef2dc94fa95f660842..14ea604f5e1b8fbc12e65637e02319ce109b2eeb 100644
--- a/Source/bindings/scripts/code_generator_v8.py
+++ b/Source/bindings/scripts/code_generator_v8.py
@@ -30,12 +30,122 @@
Input: An object of class IdlDefinitions, containing an IDL interface X
Output: V8X.h and V8X.cpp
-
-FIXME: Currently a stub, as part of landing the parser and code generator
-incrementally. Only implements generation of dummy .cpp and .h files.
"""
-import os.path
+import os
+import posixpath
+import re
+import sys
+
+import idl_definitions
+
+# jinja2 is in chromium's third_party directory.
+module_path, module_name = os.path.split(__file__)
+third_party = os.path.join(module_path, os.pardir, os.pardir, os.pardir, os.pardir)
+sys.path.append(third_party)
+import jinja2
+
+
+def apply_template(path_to_template, contents):
+ dirname, basename = os.path.split(path_to_template)
+ jinja_env = jinja2.Environment(trim_blocks=True, loader=jinja2.FileSystemLoader([dirname]))
+ template = jinja_env.get_template(basename)
+ return template.render(contents)
+
+
+def v8_class_name(interface):
+ return 'V8' + interface.name
+
+
+def cpp_type(data_type):
+ """Returns the cpp type corresponding to the IDL type."""
+ if data_type == 'boolean':
+ return 'bool'
+ raise Exception('Not supported')
+
+
+def callback_argument_declaration(operation):
+ arguments = ['%s %s' % (cpp_type(argument.data_type), argument.name) for argument in operation.arguments]
+ return ', '.join(arguments)
+
+
+class CodeGeneratorV8:
+ def __init__(self, definitions, interface_name, output_directory, idl_directories, verbose=False):
+ if interface_name not in definitions.interfaces:
+ raise Exception('%s not in IDL definitions' % interface_name)
+ self.idl_definitions = definitions
+ self.interface_name = interface_name
+ self.idl_directories = idl_directories
+ self.output_directory = output_directory
+ self.verbose = verbose
+ self.interface = self.idl_definitions.interfaces[self.interface_name]
+
+ def cpp_class_header_filename(self):
+ """Returns relative path from bindings/ of webcore header of the interface"""
+ # FIXME: parser will prepare posix form relative path from Source/bindings in IdlInterface.file_name
+ idl_filename = self.idl_definitions.file_name
+ idl_rel_path_local = os.path.relpath(idl_filename)
+ idl_rel_path_posix = idl_rel_path_local.replace(os.path.sep, posixpath.sep)
+
+ idl_dir_posix = posixpath.join('bindings', posixpath.dirname(idl_rel_path_posix))
+ return posixpath.join(idl_dir_posix, self.interface.name + '.h')
+
+ def generate_header_and_cpp(self):
+ if self.interface.is_callback:
+ template_contents = {}
+ template_contents.update(self.generate_callback_interface())
+ header_file_text = apply_template('templates/callback.h', template_contents)
+ cpp_file_text = apply_template('templates/callback.cpp', template_contents)
+ else:
+ # FIXME: Implement.
+ header_file_text = ""
+ cpp_file_text = ""
+ self.write_generated_code(header_file_text, cpp_file_text)
+
+ def write_generated_code(self, header_file_text, cpp_file_text):
+ header_filename = os.path.join(self.output_directory, v8_class_name(self.interface) + '.h')
+ with open(header_filename, 'w') as header_file:
+ header_file.write(header_file_text)
+ cpp_filename = os.path.join(self.output_directory, v8_class_name(self.interface) + '.cpp')
+ with open(cpp_filename, 'w') as cpp_file:
+ cpp_file.write(cpp_file_text)
+
+ def generate_callback_interface(self):
+ methods = []
+ cpp_includes = set([
+ 'core/dom/ScriptExecutionContext.h',
+ 'bindings/v8/V8Binding.h',
+ 'bindings/v8/V8Callback.h',
+ 'wtf/Assertions.h',
+ ])
+ header_includes = set([
+ 'bindings/v8/ActiveDOMCallback.h',
+ 'bindings/v8/DOMWrapperWorld.h',
+ 'bindings/v8/ScopedPersistent.h',
+ ])
+ header_includes.add(self.cpp_class_header_filename())
+ for operation in self.interface.operations:
+ method = {}
+ if 'Custom' not in operation.extended_attributes:
+ if operation.data_type != 'boolean':
+ raise Exception("We don't yet support callbacks that return non-boolean values.")
+ if len(operation.arguments):
+ raise Exception('Not supported')
+ method = {
haraken 2013/07/26 10:15:48 You might want to set 'method.arguments': []
+ 'return_type': cpp_type(operation.data_type),
+ 'name': operation.name,
+ 'parameter_declaration': callback_argument_declaration(operation),
haraken 2013/07/26 10:15:48 parameter_declaration => argument_declaration
+ 'custom': None,
+ }
+ methods.append(method)
+ template_contents = {
+ 'cpp_class_name': self.interface.name,
+ 'v8_class_name': v8_class_name(self.interface),
+ 'cpp_includes': sorted(list(cpp_includes)),
+ 'header_includes': sorted(list(header_includes)),
+ 'methods': methods,
+ }
+ return template_contents
def generate_dummy_header_and_cpp(target_interface_name, output_directory):
haraken 2013/07/26 10:15:48 You can put this method inside the CodeGeneratorV8

Powered by Google App Engine
This is Rietveld 408576698