OLD | NEW |
| (Empty) |
1 # Copyright (C) 2013 Google Inc. All rights reserved. | |
2 # | |
3 # Redistribution and use in source and binary forms, with or without | |
4 # modification, are permitted provided that the following conditions are | |
5 # met: | |
6 # | |
7 # * Redistributions of source code must retain the above copyright | |
8 # notice, this list of conditions and the following disclaimer. | |
9 # * Redistributions in binary form must reproduce the above | |
10 # copyright notice, this list of conditions and the following disclaimer | |
11 # in the documentation and/or other materials provided with the | |
12 # distribution. | |
13 # * Neither the name of Google Inc. nor the names of its | |
14 # contributors may be used to endorse or promote products derived from | |
15 # this software without specific prior written permission. | |
16 # | |
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
28 | |
29 """Functions shared by various parts of the code generator. | |
30 | |
31 FIXME: Not currently used in build. | |
32 This is a rewrite of the Perl IDL compiler in Python, but is not complete. | |
33 Once it is complete, we will switch all IDL files over to Python at once. | |
34 Until then, please work on the Perl IDL compiler. | |
35 For details, see bug http://crbug.com/239771 | |
36 """ | |
37 | |
38 # FIXME: eliminate this file if possible | |
39 | |
40 import re | |
41 | |
42 from v8_globals import includes | |
43 import v8_types | |
44 | |
45 ACRONYMS = ['CSS', 'HTML', 'IME', 'JS', 'SVG', 'URL', 'WOFF', 'XML', 'XSLT'] | |
46 | |
47 | |
48 ################################################################################ | |
49 # Extended attribute parsing | |
50 ################################################################################ | |
51 | |
52 def extended_attribute_value_contains(extended_attribute_value, value): | |
53 return (extended_attribute_value and | |
54 value in re.split('[|&]', extended_attribute_value)) | |
55 | |
56 | |
57 def has_extended_attribute(definition_or_member, extended_attribute_list): | |
58 return any(extended_attribute in definition_or_member.extended_attributes | |
59 for extended_attribute in extended_attribute_list) | |
60 | |
61 | |
62 def has_extended_attribute_value(definition_or_member, name, value): | |
63 extended_attributes = definition_or_member.extended_attributes | |
64 return (name in extended_attributes and | |
65 extended_attribute_value_contains(extended_attributes[name], value)) | |
66 | |
67 | |
68 ################################################################################ | |
69 # String handling | |
70 ################################################################################ | |
71 | |
72 def capitalize(name): | |
73 """Capitalize first letter or initial acronym (used in setter names).""" | |
74 for acronym in ACRONYMS: | |
75 if name.startswith(acronym.lower()): | |
76 return name.replace(acronym.lower(), acronym) | |
77 return name[0].upper() + name[1:] | |
78 | |
79 | |
80 def strip_suffix(string, suffix): | |
81 if not suffix or not string.endswith(suffix): | |
82 return string | |
83 return string[:-len(suffix)] | |
84 | |
85 | |
86 def uncapitalize(name): | |
87 """Uncapitalizes first letter or initial acronym (used in method names). | |
88 | |
89 E.g., 'SetURL' becomes 'setURL', but 'URLFoo' becomes 'urlFoo'. | |
90 """ | |
91 for acronym in ACRONYMS: | |
92 if name.startswith(acronym): | |
93 return name.replace(acronym, acronym.lower()) | |
94 return name[0].lower() + name[1:] | |
95 | |
96 | |
97 ################################################################################ | |
98 # C++ | |
99 ################################################################################ | |
100 | |
101 def enum_validation_expression(idl_type): | |
102 if not v8_types.is_enum(idl_type): | |
103 return None | |
104 return ' || '.join(['string == "%s"' % enum_value | |
105 for enum_value in v8_types.enum_values(idl_type)]) | |
106 | |
107 | |
108 def scoped_name(interface, definition, base_name): | |
109 implemented_by = definition.extended_attributes.get('ImplementedBy') | |
110 if implemented_by: | |
111 return '%s::%s' % (implemented_by, base_name) | |
112 if definition.is_static: | |
113 return '%s::%s' % (cpp_name(interface), base_name) | |
114 return 'imp->%s' % base_name | |
115 | |
116 | |
117 def v8_class_name(interface): | |
118 return v8_types.v8_type(interface.name) | |
119 | |
120 | |
121 ################################################################################ | |
122 # Specific extended attributes | |
123 ################################################################################ | |
124 | |
125 # [ActivityLogging] | |
126 def activity_logging_world_list(member, access_type=None): | |
127 """Returns a set of world suffixes for which a definition member has activit
y logging, for specified access type. | |
128 | |
129 access_type can be 'Getter' or 'Setter' if only checking getting or setting. | |
130 """ | |
131 if 'ActivityLogging' not in member.extended_attributes: | |
132 return set() | |
133 activity_logging = member.extended_attributes['ActivityLogging'] | |
134 # [ActivityLogging=For*] (no prefix, starts with the worlds suffix) means | |
135 # "log for all use (method)/access (attribute)", otherwise check that value | |
136 # agrees with specified access_type (Getter/Setter). | |
137 has_logging = (activity_logging.startswith('For') or | |
138 (access_type and activity_logging.startswith(access_type))) | |
139 if not has_logging: | |
140 return set() | |
141 includes.add('bindings/v8/V8DOMActivityLogger.h') | |
142 if activity_logging.endswith('ForIsolatedWorlds'): | |
143 return set(['']) | |
144 return set(['', 'ForMainWorld']) # endswith('ForAllWorlds') | |
145 | |
146 | |
147 # [CallWith] | |
148 CALL_WITH_ARGUMENTS = { | |
149 'ScriptState': '&state', | |
150 'ExecutionContext': 'scriptContext', | |
151 'ScriptArguments': 'scriptArguments.release()', | |
152 'ActiveWindow': 'callingDOMWindow(info.GetIsolate())', | |
153 'FirstWindow': 'enteredDOMWindow(info.GetIsolate())', | |
154 } | |
155 # List because key order matters, as we want arguments in deterministic order | |
156 CALL_WITH_VALUES = [ | |
157 'ScriptState', | |
158 'ExecutionContext', | |
159 'ScriptArguments', | |
160 'ActiveWindow', | |
161 'FirstWindow', | |
162 ] | |
163 | |
164 | |
165 def call_with_arguments(member, call_with_values=None): | |
166 # Optional parameter so setter can override with [SetterCallWith] | |
167 call_with_values = call_with_values or member.extended_attributes.get('CallW
ith') | |
168 if not call_with_values: | |
169 return [] | |
170 return [CALL_WITH_ARGUMENTS[value] | |
171 for value in CALL_WITH_VALUES | |
172 if extended_attribute_value_contains(call_with_values, value)] | |
173 | |
174 | |
175 # [Conditional] | |
176 def conditional_string(definition_or_member): | |
177 extended_attributes = definition_or_member.extended_attributes | |
178 if 'Conditional' not in extended_attributes: | |
179 return None | |
180 conditional = extended_attributes['Conditional'] | |
181 for operator in '&|': | |
182 if operator in conditional: | |
183 conditions = conditional.split(operator) | |
184 operator_separator = ' %s%s ' % (operator, operator) | |
185 return operator_separator.join('ENABLE(%s)' % expression for express
ion in sorted(conditions)) | |
186 return 'ENABLE(%s)' % conditional | |
187 | |
188 | |
189 # [DeprecateAs] | |
190 def deprecate_as(member): | |
191 extended_attributes = member.extended_attributes | |
192 if 'DeprecateAs' not in extended_attributes: | |
193 return None | |
194 includes.add('core/frame/UseCounter.h') | |
195 return extended_attributes['DeprecateAs'] | |
196 | |
197 | |
198 # [ImplementedAs] | |
199 def cpp_name(definition_or_member): | |
200 extended_attributes = definition_or_member.extended_attributes | |
201 if 'ImplementedAs' not in extended_attributes: | |
202 return definition_or_member.name | |
203 return extended_attributes['ImplementedAs'] | |
204 | |
205 | |
206 # [MeasureAs] | |
207 def measure_as(definition_or_member): | |
208 extended_attributes = definition_or_member.extended_attributes | |
209 if 'MeasureAs' not in extended_attributes: | |
210 return None | |
211 includes.add('core/frame/UseCounter.h') | |
212 return extended_attributes['MeasureAs'] | |
213 | |
214 | |
215 # [PerContextEnabled] | |
216 def per_context_enabled_function_name(definition_or_member): | |
217 extended_attributes = definition_or_member.extended_attributes | |
218 if 'PerContextEnabled' not in extended_attributes: | |
219 return None | |
220 feature_name = extended_attributes['PerContextEnabled'] | |
221 return 'ContextFeatures::%sEnabled' % uncapitalize(feature_name) | |
222 | |
223 | |
224 # [RuntimeEnabled] | |
225 def runtime_enabled_function_name(definition_or_member): | |
226 """Returns the name of the RuntimeEnabledFeatures function. | |
227 | |
228 The returned function checks if a method/attribute is enabled. | |
229 Given extended attribute RuntimeEnabled=FeatureName, return: | |
230 RuntimeEnabledFeatures::{featureName}Enabled | |
231 """ | |
232 extended_attributes = definition_or_member.extended_attributes | |
233 if 'RuntimeEnabled' not in extended_attributes: | |
234 return None | |
235 feature_name = extended_attributes['RuntimeEnabled'] | |
236 return 'RuntimeEnabledFeatures::%sEnabled' % uncapitalize(feature_name) | |
OLD | NEW |