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

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

Issue 179423002: Use a class for CodeGeneratorV8 (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Tweaks Created 6 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | Source/bindings/scripts/unstable/idl_compiler.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 sys.path.insert(1, third_party_dir) 72 sys.path.insert(1, third_party_dir)
73 import jinja2 73 import jinja2
74 74
75 import v8_callback_interface 75 import v8_callback_interface
76 from v8_globals import includes, interfaces 76 from v8_globals import includes, interfaces
77 import v8_interface 77 import v8_interface
78 import v8_types 78 import v8_types
79 from v8_utilities import capitalize, cpp_name, conditional_string, v8_class_name 79 from v8_utilities import capitalize, cpp_name, conditional_string, v8_class_name
80 80
81 81
82 def generate_header_and_cpp(definitions, interface_name, interfaces_info, cache_ dir): 82 class CodeGeneratorV8(object):
83 try: 83 def __init__(self, interfaces_info, cache_dir):
84 interface = definitions.interfaces[interface_name] 84 interfaces_info = interfaces_info or {}
85 except KeyError: 85 self.interfaces_info = interfaces_info
86 raise Exception('%s not in IDL definitions' % interface_name) 86 self.jinja_env = initialize_jinja_env(cache_dir)
87 87
88 # Store other interfaces for introspection 88 # Set global type info
89 interfaces.update(definitions.interfaces) 89 v8_types.set_ancestors(dict(
90 (interface_name, interface_info['ancestors'])
91 for interface_name, interface_info in interfaces_info.iteritems()
92 if 'ancestors' in interface_info))
93 v8_types.set_callback_interfaces(set(
94 interface_name
95 for interface_name, interface_info in interfaces_info.iteritems()
96 if interface_info['is_callback_interface']))
97 v8_types.set_implemented_as_interfaces(dict(
98 (interface_name, interface_info['implemented_as'])
99 for interface_name, interface_info in interfaces_info.iteritems()
100 if 'implemented_as' in interface_info))
101 v8_types.set_will_be_garbage_collected_types(set(
102 interface_name
103 for interface_name, interface_info in interfaces_info.iteritems()
104 if 'inherited_extended_attributes' in interface_info and
105 'WillBeGarbageCollected' in interface_info['inherited_extended_a ttributes']))
90 106
91 # Set up Jinja 107 def generate_code(self, definitions, interface_name):
92 jinja_env = initialize_jinja_env(cache_dir) 108 """Returns .h/.cpp code as (header_text, cpp_text)."""
93 if interface.is_callback: 109 try:
94 header_template_filename = 'callback_interface.h' 110 interface = definitions.interfaces[interface_name]
95 cpp_template_filename = 'callback_interface.cpp' 111 except KeyError:
96 generate_contents = v8_callback_interface.generate_callback_interface 112 raise Exception('%s not in IDL definitions' % interface_name)
97 else:
98 header_template_filename = 'interface.h'
99 cpp_template_filename = 'interface.cpp'
100 generate_contents = v8_interface.generate_interface
101 header_template = jinja_env.get_template(header_template_filename)
102 cpp_template = jinja_env.get_template(cpp_template_filename)
103 113
104 # Set type info, both local and global 114 # Store other interfaces for introspection
105 interface_info = interfaces_info[interface_name] 115 interfaces.update(definitions.interfaces)
106 116
107 v8_types.set_callback_functions(definitions.callback_functions.keys()) 117 # Set local type info
108 v8_types.set_enums((enum.name, enum.values) 118 v8_types.set_callback_functions(definitions.callback_functions.keys())
109 for enum in definitions.enumerations.values()) 119 v8_types.set_enums((enum.name, enum.values)
110 v8_types.set_ancestors(dict( 120 for enum in definitions.enumerations.values())
111 (interface_name, interface_info['ancestors'])
112 for interface_name, interface_info in interfaces_info.iteritems()
113 if 'ancestors' in interface_info))
114 v8_types.set_callback_interfaces(set(
115 interface_name
116 for interface_name, interface_info in interfaces_info.iteritems()
117 if interface_info['is_callback_interface']))
118 v8_types.set_implemented_as_interfaces(dict(
119 (interface_name, interface_info['implemented_as'])
120 for interface_name, interface_info in interfaces_info.iteritems()
121 if 'implemented_as' in interface_info))
122 v8_types.set_will_be_garbage_collected_types(set(
123 interface_name
124 for interface_name, interface_info in interfaces_info.iteritems()
125 if 'inherited_extended_attributes' in interface_info and
126 'WillBeGarbageCollected' in interface_info['inherited_extended_attri butes']))
127 121
128 # Generate contents (input parameters for Jinja) 122 # Select appropriate Jinja template and contents function
129 template_contents = generate_contents(interface) 123 if interface.is_callback:
130 template_contents['header_includes'].add(interface_info['include_path']) 124 header_template_filename = 'callback_interface.h'
131 template_contents['header_includes'] = sorted(template_contents['header_incl udes']) 125 cpp_template_filename = 'callback_interface.cpp'
132 includes.update(interface_info.get('dependencies_include_paths', [])) 126 generate_contents = v8_callback_interface.generate_callback_interfac e
133 template_contents['cpp_includes'] = sorted(includes) 127 else:
128 header_template_filename = 'interface.h'
129 cpp_template_filename = 'interface.cpp'
130 generate_contents = v8_interface.generate_interface
131 header_template = self.jinja_env.get_template(header_template_filename)
132 cpp_template = self.jinja_env.get_template(cpp_template_filename)
134 133
135 # Render Jinja templates 134 # Generate contents (input parameters for Jinja)
136 header_text = header_template.render(template_contents) 135 template_contents = generate_contents(interface)
137 cpp_text = cpp_template.render(template_contents) 136
138 return header_text, cpp_text 137 # Add includes for interface itself and any dependencies
138 interface_info = self.interfaces_info[interface_name]
139 template_contents['header_includes'].add(interface_info['include_path'])
140 template_contents['header_includes'] = sorted(template_contents['header_ includes'])
141 includes.update(interface_info.get('dependencies_include_paths', []))
142 template_contents['cpp_includes'] = sorted(includes)
143
144 # Render Jinja templates
145 header_text = header_template.render(template_contents)
146 cpp_text = cpp_template.render(template_contents)
147 return header_text, cpp_text
139 148
140 149
141 def initialize_jinja_env(cache_dir): 150 def initialize_jinja_env(cache_dir):
142 jinja_env = jinja2.Environment( 151 jinja_env = jinja2.Environment(
143 loader=jinja2.FileSystemLoader(templates_dir), 152 loader=jinja2.FileSystemLoader(templates_dir),
144 # Bytecode cache is not concurrency-safe unless pre-cached: 153 # Bytecode cache is not concurrency-safe unless pre-cached:
145 # if pre-cached this is read-only, but writing creates a race condition. 154 # if pre-cached this is read-only, but writing creates a race condition.
146 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), 155 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
147 keep_trailing_newline=True, # newline-terminate generated files 156 keep_trailing_newline=True, # newline-terminate generated files
148 lstrip_blocks=True, # so can indent control flow tags 157 lstrip_blocks=True, # so can indent control flow tags
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 202
194 # Create a dummy file as output for the build system, 203 # Create a dummy file as output for the build system,
195 # since filenames of individual cache files are unpredictable and opaque 204 # since filenames of individual cache files are unpredictable and opaque
196 # (they are hashes of the template path, which varies based on environment) 205 # (they are hashes of the template path, which varies based on environment)
197 with open(dummy_filename, 'w') as dummy_file: 206 with open(dummy_filename, 'w') as dummy_file:
198 pass # |open| creates or touches the file 207 pass # |open| creates or touches the file
199 208
200 209
201 if __name__ == '__main__': 210 if __name__ == '__main__':
202 sys.exit(main(sys.argv)) 211 sys.exit(main(sys.argv))
OLDNEW
« no previous file with comments | « no previous file | Source/bindings/scripts/unstable/idl_compiler.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698