OLD | NEW |
---|---|
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # | 2 # |
3 # Copyright 2014 The Chromium Authors. All rights reserved. | 3 # Copyright 2014 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Generates interface properties on global objects. | 7 """Generates interface properties on global objects. |
8 | 8 |
9 Concretely these are implemented as "constructor attributes", meaning | 9 Concretely these are implemented as "constructor attributes", meaning |
10 "attributes whose name ends with Constructor" (special-cased by code generator), | 10 "attributes whose name ends with Constructor" (special-cased by code generator), |
11 hence "global constructors" for short. | 11 hence "global constructors" for short. |
12 | 12 |
13 For reference on global objects, see: | 13 For reference on global objects, see: |
14 http://heycam.github.io/webidl/#Global | 14 http://heycam.github.io/webidl/#Global |
15 http://heycam.github.io/webidl/#Exposed | 15 http://heycam.github.io/webidl/#Exposed |
16 | 16 |
17 Design document: http://www.chromium.org/developers/design-documents/idl-build | 17 Design document: http://www.chromium.org/developers/design-documents/idl-build |
18 """ | 18 """ |
19 | 19 |
20 import itertools | |
20 import optparse | 21 import optparse |
21 import os | 22 import os |
22 import re | 23 import re |
23 import sys | 24 import sys |
24 | 25 |
26 from collections import defaultdict | |
25 from utilities import get_file_contents, write_file, get_interface_extended_attr ibutes_from_idl, is_callback_interface_from_idl | 27 from utilities import get_file_contents, write_file, get_interface_extended_attr ibutes_from_idl, is_callback_interface_from_idl |
26 | 28 |
27 global_objects = {} | 29 interface_name_to_global_names = defaultdict(list) |
30 global_name_to_constructors = defaultdict(list) | |
28 | 31 |
29 | 32 |
30 HEADER_FORMAT = """ | 33 HEADER_FORMAT = """ |
31 // Stub header file for {{idl_basename}} | 34 // Stub header file for {{idl_basename}} |
32 // Required because the IDL compiler assumes that a corresponding header file | 35 // Required because the IDL compiler assumes that a corresponding header file |
33 // exists for each IDL file. | 36 // exists for each IDL file. |
34 """ | 37 """ |
35 | 38 |
36 def parse_options(): | 39 def parse_options(): |
37 parser = optparse.OptionParser() | 40 parser = optparse.OptionParser() |
38 parser.add_option('--idl-files-list', help='file listing IDL files') | 41 parser.add_option('--idl-files-list', help='file listing IDL files') |
39 parser.add_option('--write-file-only-if-changed', type='int', help='if true, do not write an output file if it would be identical to the existing one, which avoids unnecessary rebuilds in ninja') | 42 parser.add_option('--write-file-only-if-changed', type='int', help='if true, do not write an output file if it would be identical to the existing one, which avoids unnecessary rebuilds in ninja') |
40 | 43 |
41 options, args = parser.parse_args() | 44 options, args = parser.parse_args() |
42 | 45 |
43 if options.idl_files_list is None: | 46 if options.idl_files_list is None: |
44 parser.error('Must specify a file listing IDL files using --idl-files-li st.') | 47 parser.error('Must specify a file listing IDL files using --idl-files-li st.') |
45 if options.write_file_only_if_changed is None: | 48 if options.write_file_only_if_changed is None: |
46 parser.error('Must specify whether output files are only written if chan ged using --write-file-only-if-changed.') | 49 parser.error('Must specify whether output files are only written if chan ged using --write-file-only-if-changed.') |
47 options.write_file_only_if_changed = bool(options.write_file_only_if_changed ) | 50 options.write_file_only_if_changed = bool(options.write_file_only_if_changed ) |
48 | 51 |
49 return options, args | 52 return options, args |
50 | 53 |
51 | 54 |
52 # Global name: http://heycam.github.io/webidl/#dfn-global-name | 55 def interface_to_global_names(interface_name, extended_attributes): |
53 # FIXME: We should add support for [Global=xx] extended attribute instead of | 56 """ |
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.
| |
54 # hard-coding this mapping. | 57 If the [Global] or [PrimaryGlobal] extended attribute is declared with an |
55 def global_name_to_interface_name(global_name): | 58 identifier list argument, then those identifiers are the interface's global |
56 if global_name.endswith('Worker'): | 59 names; otherwise, the interface has a single global name, which is the |
57 return global_name + 'GlobalScope' | 60 interface's identifier (http://heycam.github.io/webidl/#Global). |
58 return global_name | 61 """ |
62 for key in ['Global', 'PrimaryGlobal']: | |
63 if key not in extended_attributes: | |
64 continue | |
65 global_value = extended_attributes[key] | |
66 if global_value: | |
67 # FIXME: In spec names are comma-separated, but that makes parsing v ery | |
68 # difficult (https://www.w3.org/Bugs/Public/show_bug.cgi?id=24959). | |
69 return global_value.split('&') | |
70 return [interface_name] | |
71 return [] | |
72 | |
73 | |
74 def interface_name_to_constructors(interface_name): | |
75 """ | |
Nils Barth (inactive)
2014/05/08 00:37:03
Summary eg:
"""Returns constructors for an interfa
Inactive
2014/05/08 01:09:29
Done.
| |
76 Aggregates the constructors for an interface from | |
77 global_name_to_constructors dictionary. | |
78 """ | |
79 global_names = interface_name_to_global_names[interface_name] | |
80 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.
| |
81 for global_name in global_names: | |
82 constructors.extend(global_name_to_constructors[global_name]) | |
83 return constructors | |
59 | 84 |
60 | 85 |
61 def record_global_constructors(idl_filename): | 86 def record_global_constructors(idl_filename): |
62 interface_name, _ = os.path.splitext(os.path.basename(idl_filename)) | 87 interface_name, _ = os.path.splitext(os.path.basename(idl_filename)) |
63 full_path = os.path.realpath(idl_filename) | 88 full_path = os.path.realpath(idl_filename) |
64 idl_file_contents = get_file_contents(full_path) | 89 idl_file_contents = get_file_contents(full_path) |
65 extended_attributes = get_interface_extended_attributes_from_idl(idl_file_co ntents) | 90 extended_attributes = get_interface_extended_attributes_from_idl(idl_file_co ntents) |
66 | 91 |
67 # An interface property is produced for every non-callback interface | 92 # An interface property is produced for every non-callback interface |
68 # that does not have [NoInterfaceObject]. | 93 # that does not have [NoInterfaceObject]. |
69 # Callback interfaces with constants also have interface properties, | 94 # Callback interfaces with constants also have interface properties, |
70 # but there are none of these in Blink. | 95 # but there are none of these in Blink. |
71 # http://heycam.github.io/webidl/#es-interfaces | 96 # http://heycam.github.io/webidl/#es-interfaces |
72 if (is_callback_interface_from_idl(idl_file_contents) or | 97 if (is_callback_interface_from_idl(idl_file_contents) or |
73 'NoInterfaceObject' in extended_attributes): | 98 'NoInterfaceObject' in extended_attributes): |
74 return | 99 return |
75 | 100 |
101 # Check if interface has [Global] / [PrimaryGlobal] extended attributes. | |
102 interface_name_to_global_names[interface_name] = interface_to_global_names(i nterface_name, extended_attributes) | |
103 | |
104 # The [Exposed] extended attribute MUST take an identifier list. Each | |
105 # identifier in the list MUST be a global name. An interface or interface | |
106 # member the extended attribute applies to will be exposed only on objects | |
107 # associated with ECMAScript global environments whose global object | |
108 # implements an interface that has a matching global name. | |
76 # FIXME: In spec names are comma-separated, but that makes parsing very | 109 # FIXME: In spec names are comma-separated, but that makes parsing very |
77 # difficult (https://www.w3.org/Bugs/Public/show_bug.cgi?id=24959). | 110 # difficult (https://www.w3.org/Bugs/Public/show_bug.cgi?id=24959). |
78 global_names = extended_attributes.get('Exposed', 'Window').split('&') | 111 exposed_global_names = extended_attributes.get('Exposed', 'Window').split('& ') |
79 new_constructors_list = generate_global_constructors_list(interface_name, ex tended_attributes) | 112 new_constructors_list = generate_global_constructors_list(interface_name, ex tended_attributes) |
80 for global_name in global_names: | 113 for exposed_global_name in exposed_global_names: |
81 interface_name = global_name_to_interface_name(global_name) | 114 global_name_to_constructors[exposed_global_name].extend(new_constructors _list) |
82 global_objects[interface_name]['constructors'].extend(new_constructors_l ist) | |
83 | 115 |
84 | 116 |
85 def generate_global_constructors_list(interface_name, extended_attributes): | 117 def generate_global_constructors_list(interface_name, extended_attributes): |
86 extended_attributes_list = [ | 118 extended_attributes_list = [ |
87 name + '=' + extended_attributes[name] | 119 name + '=' + extended_attributes[name] |
88 for name in 'Conditional', 'PerContextEnabled', 'RuntimeEnabled' | 120 for name in 'Conditional', 'PerContextEnabled', 'RuntimeEnabled' |
89 if name in extended_attributes] | 121 if name in extended_attributes] |
90 if extended_attributes_list: | 122 if extended_attributes_list: |
91 extended_string = '[%s] ' % ', '.join(extended_attributes_list) | 123 extended_string = '[%s] ' % ', '.join(extended_attributes_list) |
92 else: | 124 else: |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 # limits. This is generated at GYP time, which is ok b/c files are static. | 166 # limits. This is generated at GYP time, which is ok b/c files are static. |
135 with open(options.idl_files_list) as idl_files_list: | 167 with open(options.idl_files_list) as idl_files_list: |
136 idl_files = [line.rstrip('\n') for line in idl_files_list] | 168 idl_files = [line.rstrip('\n') for line in idl_files_list] |
137 | 169 |
138 # Output IDL files (to generate) are passed at the command line, since | 170 # Output IDL files (to generate) are passed at the command line, since |
139 # these are in the build directory, which is determined at build time, not | 171 # these are in the build directory, which is determined at build time, not |
140 # GYP time. | 172 # GYP time. |
141 # These are passed as pairs of GlobalObjectName, GlobalObject.idl | 173 # These are passed as pairs of GlobalObjectName, GlobalObject.idl |
142 interface_name_idl_filename = [(args[i], args[i + 1]) | 174 interface_name_idl_filename = [(args[i], args[i + 1]) |
143 for i in range(0, len(args), 2)] | 175 for i in range(0, len(args), 2)] |
144 global_objects.update( | |
145 (interface_name, { | |
146 'idl_filename': idl_filename, | |
147 'constructors': [], | |
148 }) | |
149 for interface_name, idl_filename in interface_name_idl_filename) | |
150 | 176 |
151 for idl_filename in idl_files: | 177 for idl_filename in idl_files: |
152 record_global_constructors(idl_filename) | 178 record_global_constructors(idl_filename) |
153 | 179 |
154 for interface_name, global_object in global_objects.iteritems(): | 180 # 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.
| |
181 known_global_names = set(itertools.chain.from_iterable(interface_name_to_glo bal_names.values())) | |
182 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.
| |
183 if global_name not in known_global_names: | |
184 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.
| |
185 ' [Global] / [PrimaryGlobal]' % global_name | |
186 | |
187 # Write partial interfaces containing constructor attributes for each | |
188 # global interface. | |
189 for interface_name, idl_filename in interface_name_idl_filename: | |
190 constructors = interface_name_to_constructors(interface_name) | |
155 write_global_constructors_partial_interface( | 191 write_global_constructors_partial_interface( |
156 interface_name, | 192 interface_name, |
157 global_object['idl_filename'], | 193 idl_filename, |
158 global_object['constructors'], | 194 constructors, |
159 options.write_file_only_if_changed) | 195 options.write_file_only_if_changed) |
160 | 196 |
161 | 197 |
162 if __name__ == '__main__': | 198 if __name__ == '__main__': |
163 sys.exit(main()) | 199 sys.exit(main()) |
OLD | NEW |