Chromium Code Reviews| Index: Source/bindings/scripts/generate_global_constructors.py |
| diff --git a/Source/bindings/scripts/generate_global_constructors.py b/Source/bindings/scripts/generate_global_constructors.py |
| index 3889f132077826bf8919a616c33eef62c38aeb03..b2e48612f8422d659ff40477deabe0573a6acb1d 100755 |
| --- a/Source/bindings/scripts/generate_global_constructors.py |
| +++ b/Source/bindings/scripts/generate_global_constructors.py |
| @@ -17,14 +17,17 @@ http://heycam.github.io/webidl/#Exposed |
| Design document: http://www.chromium.org/developers/design-documents/idl-build |
| """ |
| +import itertools |
| import optparse |
| import os |
| import re |
| import sys |
| +from collections import defaultdict |
| from utilities import get_file_contents, write_file, get_interface_extended_attributes_from_idl, is_callback_interface_from_idl |
| -global_objects = {} |
| +interface_name_to_global_names = defaultdict(list) |
| +global_name_to_constructors = defaultdict(list) |
| HEADER_FORMAT = """ |
| @@ -49,13 +52,35 @@ def parse_options(): |
| return options, args |
| -# Global name: http://heycam.github.io/webidl/#dfn-global-name |
| -# FIXME: We should add support for [Global=xx] extended attribute instead of |
| -# hard-coding this mapping. |
| -def global_name_to_interface_name(global_name): |
| - if global_name.endswith('Worker'): |
| - return global_name + 'GlobalScope' |
| - return global_name |
| +def interface_to_global_names(interface_name, extended_attributes): |
| + """ |
|
Nils Barth (inactive)
2014/05/08 00:37:03
Could you start the docstring with a 1-line summar
Inactive
2014/05/08 01:09:29
Done.
|
| + If the [Global] or [PrimaryGlobal] extended attribute is declared with an |
| + identifier list argument, then those identifiers are the interface's global |
| + names; otherwise, the interface has a single global name, which is the |
| + interface's identifier (http://heycam.github.io/webidl/#Global). |
| + """ |
| + for key in ['Global', 'PrimaryGlobal']: |
| + if key not in extended_attributes: |
| + continue |
| + global_value = extended_attributes[key] |
| + if global_value: |
| + # FIXME: In spec names are comma-separated, but that makes parsing very |
| + # difficult (https://www.w3.org/Bugs/Public/show_bug.cgi?id=24959). |
| + return global_value.split('&') |
| + return [interface_name] |
| + return [] |
| + |
| + |
| +def interface_name_to_constructors(interface_name): |
| + """ |
|
Nils Barth (inactive)
2014/05/08 00:37:03
Summary eg:
"""Returns constructors for an interfa
Inactive
2014/05/08 01:09:29
Done.
|
| + Aggregates the constructors for an interface from |
| + global_name_to_constructors dictionary. |
| + """ |
| + global_names = interface_name_to_global_names[interface_name] |
| + constructors = [] |
|
Nils Barth (inactive)
2014/05/08 00:37:03
A little slicker is an auxiliary flattening functi
Inactive
2014/05/08 01:09:29
Done. But used itertools.chain.from_iterable() ins
Nils Barth (inactive)
2014/05/08 01:13:54
That's fine too, and agreed, a bit clearer.
|
| + for global_name in global_names: |
| + constructors.extend(global_name_to_constructors[global_name]) |
| + return constructors |
| def record_global_constructors(idl_filename): |
| @@ -73,13 +98,20 @@ def record_global_constructors(idl_filename): |
| 'NoInterfaceObject' in extended_attributes): |
| return |
| + # Check if interface has [Global] / [PrimaryGlobal] extended attributes. |
| + interface_name_to_global_names[interface_name] = interface_to_global_names(interface_name, extended_attributes) |
| + |
| + # The [Exposed] extended attribute MUST take an identifier list. Each |
| + # identifier in the list MUST be a global name. An interface or interface |
| + # member the extended attribute applies to will be exposed only on objects |
| + # associated with ECMAScript global environments whose global object |
| + # implements an interface that has a matching global name. |
| # FIXME: In spec names are comma-separated, but that makes parsing very |
| # difficult (https://www.w3.org/Bugs/Public/show_bug.cgi?id=24959). |
| - global_names = extended_attributes.get('Exposed', 'Window').split('&') |
| + exposed_global_names = extended_attributes.get('Exposed', 'Window').split('&') |
| new_constructors_list = generate_global_constructors_list(interface_name, extended_attributes) |
| - for global_name in global_names: |
| - interface_name = global_name_to_interface_name(global_name) |
| - global_objects[interface_name]['constructors'].extend(new_constructors_list) |
| + for exposed_global_name in exposed_global_names: |
| + global_name_to_constructors[exposed_global_name].extend(new_constructors_list) |
| def generate_global_constructors_list(interface_name, extended_attributes): |
| @@ -141,21 +173,25 @@ def main(): |
| # These are passed as pairs of GlobalObjectName, GlobalObject.idl |
| interface_name_idl_filename = [(args[i], args[i + 1]) |
| for i in range(0, len(args), 2)] |
| - global_objects.update( |
| - (interface_name, { |
| - 'idl_filename': idl_filename, |
| - 'constructors': [], |
| - }) |
| - for interface_name, idl_filename in interface_name_idl_filename) |
| for idl_filename in idl_files: |
| record_global_constructors(idl_filename) |
| - for interface_name, global_object in global_objects.iteritems(): |
| + # Print warnings from bad [Exposed] / [Global] mapping. |
|
Nils Barth (inactive)
2014/05/08 00:37:03
Shouldn't we throw an exception instead?
This shou
Inactive
2014/05/08 01:09:29
Done.
|
| + known_global_names = set(itertools.chain.from_iterable(interface_name_to_global_names.values())) |
| + for global_name in global_name_to_constructors: |
|
Nils Barth (inactive)
2014/05/08 00:37:03
A bit simpler as a set difference, no?
Inactive
2014/05/08 01:09:29
Done.
|
| + if global_name not in known_global_names: |
| + print 'WARNING: "%s" used in [Exposed=xxx] but does not match any'\ |
|
Nils Barth (inactive)
2014/05/08 00:37:03
No backslash line continuation; use parentheses an
Inactive
2014/05/08 01:09:29
Done.
|
| + ' [Global] / [PrimaryGlobal]' % global_name |
| + |
| + # Write partial interfaces containing constructor attributes for each |
| + # 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, |
| - global_object['idl_filename'], |
| - global_object['constructors'], |
| + idl_filename, |
| + constructors, |
| options.write_file_only_if_changed) |