Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # pylint: disable=import-error,print-statement,relative-import | 5 # pylint: disable=import-error,print-statement,relative-import |
| 6 | 6 |
| 7 """Generates Blink Web Module bindings. | 7 """Generates Blink Web Module bindings. |
| 8 | 8 |
| 9 The Blink Web Module bindings provide a stable, IDL-generated interface for the | 9 The Blink Web Module bindings provide a stable, IDL-generated interface for the |
| 10 Web Modules. | 10 Web Modules. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 31 WEB_MODULE_IDL_ATTRIBUTE = 'WebModuleAPI' | 31 WEB_MODULE_IDL_ATTRIBUTE = 'WebModuleAPI' |
| 32 | 32 |
| 33 | 33 |
| 34 def includes_for_type(idl_type): | 34 def includes_for_type(idl_type): |
| 35 # TODO(dglazkov): Make this actually work. | 35 # TODO(dglazkov): Make this actually work. |
| 36 name = idl_type.preprocessed_type.base_type | 36 name = idl_type.preprocessed_type.base_type |
| 37 return set([name]) | 37 return set([name]) |
| 38 | 38 |
| 39 | 39 |
| 40 def interface_context(idl_interface): | 40 def interface_context(idl_interface): |
| 41 attributes = [] | 41 builder = InterfaceContextBuilder(MODULE_PYNAME) |
| 42 methods = [] | 42 builder.set_class_name(idl_interface.name) |
| 43 includes = set() | 43 |
| 44 for idl_attribute in idl_interface.attributes: | 44 for idl_attribute in idl_interface.attributes: |
| 45 attributes.append(Attribute.create(idl_attribute)) | 45 builder.add_attribute(idl_attribute) |
| 46 includes.update(includes_for_type(idl_attribute.idl_type)) | 46 |
| 47 for idl_operation in idl_interface.operations: | 47 for idl_operation in idl_interface.operations: |
| 48 if idl_operation.name: | 48 builder.add_operation(idl_operation) |
| 49 methods.append(Method.create(idl_operation)) | 49 |
| 50 return { | 50 return builder.build() |
| 51 'code_generator': MODULE_PYNAME, | |
| 52 'class_name': idl_interface.name, | |
| 53 'cpp_includes': includes, | |
| 54 'attributes': attributes, | |
| 55 'methods': methods, | |
| 56 } | |
| 57 | 51 |
| 58 | 52 |
| 59 class Attribute(object): | 53 class InterfaceContextBuilder(object): |
| 60 def __init__(self, name, return_type): | 54 def __init__(self, code_generator): |
| 61 self.name = name | 55 self.result = {'code_generator': code_generator} |
| 62 self.return_type = return_type | |
| 63 | 56 |
| 64 @staticmethod | 57 def set_class_name(self, class_name): |
| 65 def create(idl_attribute): | 58 self.result['class_name'] = class_name |
| 59 | |
| 60 def _ensure_set(self, name): | |
| 61 return self.result.setdefault(name, set()) | |
| 62 | |
| 63 def _ensure_list(self, name): | |
| 64 return self.result.setdefault(name, []) | |
| 65 | |
| 66 def add_attribute(self, idl_attribute): | |
| 67 self._ensure_list('attributes').append( | |
| 68 self.create_attribute(idl_attribute)) | |
| 69 self._ensure_set('cpp_includes').update( | |
| 70 includes_for_type(idl_attribute.idl_type)) | |
| 71 | |
| 72 def add_operation(self, idl_operation): | |
| 73 if idl_operation.name: | |
| 74 self._ensure_list('methods').append( | |
| 75 self.create_method(idl_operation)) | |
| 76 self._ensure_set('cpp_includes').update( | |
| 77 includes_for_type(idl_operation.idl_type)) | |
| 78 | |
| 79 def create_method(self, idl_operation): | |
| 80 name = idl_operation.name | |
| 81 return_type = idl_operation.idl_type.preprocessed_type.base_type | |
|
bashi
2016/12/12 05:04:38
Not directly related to this CL but do we need pre
| |
| 82 return { | |
| 83 'name': name, | |
| 84 'return_type': return_type | |
| 85 } | |
| 86 | |
| 87 def create_attribute(self, idl_attribute): | |
| 66 name = idl_attribute.name | 88 name = idl_attribute.name |
| 67 return_type = idl_attribute.idl_type.preprocessed_type.base_type | 89 return_type = idl_attribute.idl_type.preprocessed_type.base_type |
| 68 return Attribute(name, return_type) | 90 return { |
| 91 'name': name, | |
| 92 'return_type': return_type | |
| 93 } | |
| 69 | 94 |
| 70 | 95 def build(self): |
| 71 class Method(object): | 96 return self.result |
| 72 def __init__(self, name, return_type): | |
| 73 self.name = name | |
| 74 self.return_type = return_type | |
| 75 | |
| 76 @staticmethod | |
| 77 def create(idl_operation): | |
| 78 name = idl_operation.name | |
| 79 return_type = idl_operation.idl_type.preprocessed_type.base_type | |
| 80 return Method(name, return_type) | |
| 81 | 97 |
| 82 | 98 |
| 83 class CodeGeneratorWebModule(CodeGeneratorBase): | 99 class CodeGeneratorWebModule(CodeGeneratorBase): |
| 84 def __init__(self, info_provider, cache_dir, output_dir): | 100 def __init__(self, info_provider, cache_dir, output_dir): |
| 85 CodeGeneratorBase.__init__(self, MODULE_PYNAME, info_provider, | 101 CodeGeneratorBase.__init__(self, MODULE_PYNAME, info_provider, |
| 86 cache_dir, output_dir) | 102 cache_dir, output_dir) |
| 87 self.typedef_resolver = TypedefResolver(info_provider) | 103 self.typedef_resolver = TypedefResolver(info_provider) |
| 88 | 104 |
| 89 def get_template(self, file_extension): | 105 def get_template(self, file_extension): |
| 90 template_filename = 'web_module_interface.%s.tmpl' % file_extension | 106 template_filename = 'web_module_interface.%s.tmpl' % file_extension |
| 91 return self.jinja_env.get_template(template_filename) | 107 return self.jinja_env.get_template(template_filename) |
| 92 | 108 |
| 93 # TODO(dglazkov): Move to CodeGeneratorBase. | 109 # TODO(dglazkov): Move to CodeGeneratorBase. |
| 94 def output_paths(self, definition_name): | 110 def output_paths(self, definition_name): |
| 95 header_path = posixpath.join(self.output_dir, | 111 header_path = posixpath.join(self.output_dir, |
| 96 'Web%s.h' % definition_name) | 112 'Web%s.h' % definition_name) |
| 97 cpp_path = posixpath.join(self.output_dir, | 113 cpp_path = posixpath.join(self.output_dir, |
| 98 'Web%s.cpp' % definition_name) | 114 'Web%s.cpp' % definition_name) |
| 99 return header_path, cpp_path | 115 return header_path, cpp_path |
| 100 | 116 |
| 101 def generate_interface_code(self, interface): | 117 def generate_interface_code(self, interface): |
| 102 # TODO(dglazkov): Implement callback interfaces. | 118 # TODO(dglazkov): Implement callback interfaces. |
| 103 # TODO(dglazkov): Make sure partial interfaces are handled. | 119 # TODO(dglazkov): Make sure partial interfaces are handled. |
| 104 if interface.is_callback or interface.is_partial: | 120 if interface.is_callback or interface.is_partial: |
| 105 raise ValueError("Partial or callback interfaces are not supported") | 121 raise ValueError('Partial or callback interfaces are not supported') |
| 106 | 122 |
| 107 template_context = interface_context(interface) | 123 template_context = interface_context(interface) |
| 108 | 124 |
| 109 cpp_template = self.get_template('cpp') | 125 cpp_template = self.get_template('cpp') |
| 110 header_template = self.get_template('h') | 126 header_template = self.get_template('h') |
| 111 cpp_text = cpp_template.render(template_context) | 127 cpp_text = cpp_template.render(template_context) |
| 112 header_text = header_template.render(template_context) | 128 header_text = header_template.render(template_context) |
| 113 header_path, cpp_path = self.output_paths(interface.name) | 129 header_path, cpp_path = self.output_paths(interface.name) |
| 114 | 130 |
| 115 return ( | 131 return ( |
| 116 (header_path, header_text), | 132 (header_path, header_text), |
| 117 (cpp_path, cpp_text) | 133 (cpp_path, cpp_text) |
| 118 ) | 134 ) |
| 119 | 135 |
| 120 def generate_code(self, definitions, definition_name): | 136 def generate_code(self, definitions, definition_name): |
| 121 self.typedef_resolver.resolve(definitions, definition_name) | 137 self.typedef_resolver.resolve(definitions, definition_name) |
| 122 header_path, cpp_path = self.output_paths(definition_name) | 138 header_path, cpp_path = self.output_paths(definition_name) |
| 123 | 139 |
| 124 template_context = {} | 140 template_context = {} |
| 125 # TODO(dglazkov): Implement dictionaries | 141 # TODO(dglazkov): Implement dictionaries |
| 126 if definition_name not in definitions.interfaces: | 142 if definition_name not in definitions.interfaces: |
| 127 return None | 143 return None |
| 128 | 144 |
| 129 interface = definitions.interfaces[definition_name] | 145 interface = definitions.interfaces[definition_name] |
| 130 if WEB_MODULE_IDL_ATTRIBUTE not in interface.extended_attributes: | 146 if WEB_MODULE_IDL_ATTRIBUTE not in interface.extended_attributes: |
| 131 return None | 147 return None |
| 132 | 148 |
| 133 return self.generate_interface_code(interface) | 149 return self.generate_interface_code(interface) |
| OLD | NEW |