diff options
Diffstat (limited to 'chromium/third_party/blink/renderer/bindings/scripts/generate_global_constructors.py')
-rwxr-xr-x | chromium/third_party/blink/renderer/bindings/scripts/generate_global_constructors.py | 91 |
1 files changed, 56 insertions, 35 deletions
diff --git a/chromium/third_party/blink/renderer/bindings/scripts/generate_global_constructors.py b/chromium/third_party/blink/renderer/bindings/scripts/generate_global_constructors.py index 57b677409ba..987f737caf2 100755 --- a/chromium/third_party/blink/renderer/bindings/scripts/generate_global_constructors.py +++ b/chromium/third_party/blink/renderer/bindings/scripts/generate_global_constructors.py @@ -3,7 +3,6 @@ # Copyright 2014 The Chromium Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. - """Generates interface properties on global objects. Concretely these are implemented as "constructor attributes", meaning @@ -41,22 +40,26 @@ from v8_utilities import EXPOSED_EXECUTION_CONTEXT_METHOD interface_name_to_global_names = {} global_name_to_constructors = defaultdict(list) - HEADER_FORMAT = """// Stub header file for {{idl_basename}} -// Required because the IDL compiler assumes that a corresponding header file -// exists for each IDL file. +// Required because the IDL compiler assumes that a corresponding header +// file exists for each IDL file. """ + def parse_options(): parser = optparse.OptionParser() parser.add_option('--idl-files-list', help='file listing IDL files') - parser.add_option('--global-objects-file', help='pickle file of global objects') + parser.add_option( + '--global-objects-file', help='pickle file of global objects') options, args = parser.parse_args() if options.idl_files_list is None: - parser.error('Must specify a file listing IDL files using --idl-files-list.') + parser.error( + 'Must specify a file listing IDL files using --idl-files-list.') if options.global_objects_file is None: - parser.error('Must specify a pickle file of global objects using --global-objects-file.') + parser.error( + 'Must specify a pickle file of global objects using --global-objects-file.' + ) return options, args @@ -75,16 +78,17 @@ def interface_name_to_constructors(interface_name): def record_global_constructors(idl_filename): full_path = os.path.realpath(idl_filename) idl_file_contents = get_file_contents(full_path) - extended_attributes = get_interface_extended_attributes_from_idl(idl_file_contents) + extended_attributes = get_interface_extended_attributes_from_idl( + idl_file_contents) interface_name = get_first_interface_name_from_idl(idl_file_contents) # An interface property is produced for every non-callback interface # that does not have [NoInterfaceObject]. # http://heycam.github.io/webidl/#es-interfaces - if (not should_generate_impl_file_from_idl(idl_file_contents) or - is_non_legacy_callback_interface_from_idl(idl_file_contents) or - is_interface_mixin_from_idl(idl_file_contents) or - 'NoInterfaceObject' in extended_attributes): + if (not should_generate_impl_file_from_idl(idl_file_contents) + or is_non_legacy_callback_interface_from_idl(idl_file_contents) + or is_interface_mixin_from_idl(idl_file_contents) + or 'NoInterfaceObject' in extended_attributes): return exposed_arguments = get_interface_exposed_arguments(idl_file_contents) @@ -92,31 +96,40 @@ def record_global_constructors(idl_filename): # Exposed(Arguments) case for argument in exposed_arguments: if 'RuntimeEnabled' in extended_attributes: - raise ValueError('RuntimeEnabled should not be used with Exposed(Arguments)') + raise ValueError( + 'RuntimeEnabled should not be used with Exposed(Arguments)' + ) attributes = extended_attributes.copy() attributes['RuntimeEnabled'] = argument['runtime_enabled'] - new_constructors_list = generate_global_constructors_list(interface_name, attributes) - global_name_to_constructors[argument['exposed']].extend(new_constructors_list) + new_constructors_list = generate_global_constructors_list( + interface_name, attributes) + global_name_to_constructors[argument['exposed']].extend( + new_constructors_list) else: # Exposed=env or Exposed=(env1,...) case exposed_value = extended_attributes.get('Exposed', 'Window') - exposed_global_names = map(str.strip, exposed_value.strip('()').split(',')) - new_constructors_list = generate_global_constructors_list(interface_name, extended_attributes) + exposed_global_names = map(str.strip, + exposed_value.strip('()').split(',')) + new_constructors_list = generate_global_constructors_list( + interface_name, extended_attributes) for name in exposed_global_names: global_name_to_constructors[name].extend(new_constructors_list) def generate_global_constructors_list(interface_name, extended_attributes): extended_attributes_list = [ - name + (('=' + extended_attributes[name]) if extended_attributes[name] else '') - for name in 'RuntimeEnabled', 'ContextEnabled', 'SecureContext' - if name in extended_attributes] + name + (('=' + extended_attributes[name]) + if extended_attributes[name] else '') + for name in ['RuntimeEnabled', 'ContextEnabled', 'SecureContext'] + if name in extended_attributes + ] if extended_attributes_list: extended_string = '[%s] ' % ', '.join(extended_attributes_list) else: extended_string = '' - attribute_string = 'attribute {interface_name}Constructor {interface_name}'.format(interface_name=interface_name) + attribute_string = 'attribute {interface_name}Constructor {interface_name}'.format( + interface_name=interface_name) attributes_list = [extended_string + attribute_string] # In addition to the usual interface property, for every [NamedConstructor] @@ -129,30 +142,36 @@ def generate_global_constructors_list(interface_name, extended_attributes): constructor_name = re.sub(r'\(.*', '', named_constructor) # Note the reduplicated 'ConstructorConstructor' # FIXME: rename to NamedConstructor - attribute_string = 'attribute %sConstructorConstructor %s' % (interface_name, constructor_name) + attribute_string = 'attribute %sConstructorConstructor %s' % ( + interface_name, constructor_name) attributes_list.append(extended_string + attribute_string) return attributes_list -def write_global_constructors_partial_interface(interface_name, idl_filename, constructor_attributes_list): +def write_global_constructors_partial_interface(interface_name, idl_filename, + constructor_attributes_list): idl_basename = os.path.basename(idl_filename) basename = os.path.splitext(idl_basename)[0] # FIXME: replace this with a simple Jinja template - lines = (['[\n', - ' ImplementedAs=%s\n' % basename, - '] partial interface %s {\n' % interface_name] + - [' %s;\n' % constructor_attribute - # FIXME: sort by interface name (not first by extended attributes) - for constructor_attribute in sorted(constructor_attributes_list)] + - ['};\n']) + lines = ([ + '[\n', + ' ImplementedAs=%s\n' % basename, + '] partial interface %s {\n' % interface_name + ] + [ + ' %s;\n' % constructor_attribute + # FIXME: sort by interface name (not first by extended attributes) + for constructor_attribute in sorted(constructor_attributes_list) + ] + ['};\n']) write_file(''.join(lines), idl_filename) header_filename = os.path.splitext(idl_filename)[0] + '.h' - write_file(HEADER_FORMAT.format(idl_basename=idl_basename), header_filename) + write_file( + HEADER_FORMAT.format(idl_basename=idl_basename), header_filename) ################################################################################ + def main(): options, args = parse_options() @@ -167,7 +186,8 @@ def main(): interface_name_idl_filename = [(args[i], args[i + 1]) for i in range(0, len(args), 2)] - interface_name_to_global_names.update(read_pickle_file(options.global_objects_file)) + interface_name_to_global_names.update( + read_pickle_file(options.global_objects_file)) for idl_filename in idl_files: record_global_constructors(idl_filename) @@ -176,7 +196,8 @@ def main(): known_global_names = EXPOSED_EXECUTION_CONTEXT_METHOD.keys() exposed_global_names = frozenset(global_name_to_constructors) if not exposed_global_names.issubset(known_global_names): - unknown_global_names = exposed_global_names.difference(known_global_names) + unknown_global_names = exposed_global_names.difference( + known_global_names) raise ValueError('The following global names were used in ' '[Exposed=xxx] but do not match any global names: %s' % list(unknown_global_names)) @@ -185,8 +206,8 @@ def main(): # global interface. for interface_name, idl_filename in interface_name_idl_filename: constructors = interface_name_to_constructors(interface_name) - write_global_constructors_partial_interface( - interface_name, idl_filename, constructors) + write_global_constructors_partial_interface(interface_name, + idl_filename, constructors) if __name__ == '__main__': |