| 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 Extends IdlTypeBase type with |enum_validation_expression| property. | |
| 32 | |
| 33 Design doc: http://www.chromium.org/developers/design-documents/idl-compiler | |
| 34 """ | |
| 35 | |
| 36 import re | |
| 37 | |
| 38 from idl_types import IdlTypeBase | |
| 39 import idl_types | |
| 40 from v8_globals import includes | |
| 41 import v8_types | |
| 42 | |
| 43 ACRONYMS = [ | |
| 44 'CSSOM', # must come *before* CSS to match full acronym | |
| 45 'CSS', | |
| 46 'HTML', | |
| 47 'IME', | |
| 48 'JS', | |
| 49 'SVG', | |
| 50 'URL', | |
| 51 'WOFF', | |
| 52 'XML', | |
| 53 'XSLT', | |
| 54 ] | |
| 55 | |
| 56 | |
| 57 ################################################################################ | |
| 58 # Extended attribute parsing | |
| 59 ################################################################################ | |
| 60 | |
| 61 def extended_attribute_value_contains(extended_attribute_value, key): | |
| 62 return (extended_attribute_value == key or | |
| 63 (isinstance(extended_attribute_value, list) and | |
| 64 key in extended_attribute_value)) | |
| 65 | |
| 66 | |
| 67 def has_extended_attribute(definition_or_member, extended_attribute_list): | |
| 68 return any(extended_attribute in definition_or_member.extended_attributes | |
| 69 for extended_attribute in extended_attribute_list) | |
| 70 | |
| 71 | |
| 72 def has_extended_attribute_value(definition_or_member, name, value): | |
| 73 extended_attributes = definition_or_member.extended_attributes | |
| 74 return (name in extended_attributes and | |
| 75 extended_attribute_value_contains(extended_attributes[name], value)) | |
| 76 | |
| 77 | |
| 78 def extended_attribute_value_as_list(definition_or_member, name): | |
| 79 extended_attributes = definition_or_member.extended_attributes | |
| 80 if name not in extended_attributes: | |
| 81 return None | |
| 82 value = extended_attributes[name] | |
| 83 if isinstance(value, list): | |
| 84 return value | |
| 85 return [value] | |
| 86 | |
| 87 | |
| 88 ################################################################################ | |
| 89 # String handling | |
| 90 ################################################################################ | |
| 91 | |
| 92 def capitalize(name): | |
| 93 """Capitalize first letter or initial acronym (used in setter names).""" | |
| 94 for acronym in ACRONYMS: | |
| 95 if name.startswith(acronym.lower()): | |
| 96 return name.replace(acronym.lower(), acronym) | |
| 97 return name[0].upper() + name[1:] | |
| 98 | |
| 99 | |
| 100 def strip_suffix(string, suffix): | |
| 101 if not suffix or not string.endswith(suffix): | |
| 102 return string | |
| 103 return string[:-len(suffix)] | |
| 104 | |
| 105 | |
| 106 def uncapitalize(name): | |
| 107 """Uncapitalizes first letter or initial acronym (used in method names). | |
| 108 | |
| 109 E.g., 'SetURL' becomes 'setURL', but 'URLFoo' becomes 'urlFoo'. | |
| 110 """ | |
| 111 for acronym in ACRONYMS: | |
| 112 if name.startswith(acronym): | |
| 113 return name.replace(acronym, acronym.lower()) | |
| 114 return name[0].lower() + name[1:] | |
| 115 | |
| 116 | |
| 117 ################################################################################ | |
| 118 # C++ | |
| 119 ################################################################################ | |
| 120 | |
| 121 def enum_validation_expression(idl_type): | |
| 122 # FIXME: Add IdlEnumType, move property to derived type, and remove this che
ck | |
| 123 if not idl_type.is_enum: | |
| 124 return None | |
| 125 return ' || '.join(['string == "%s"' % enum_value | |
| 126 for enum_value in idl_type.enum_values]) | |
| 127 IdlTypeBase.enum_validation_expression = property(enum_validation_expression) | |
| 128 | |
| 129 | |
| 130 def scoped_name(interface, definition, base_name): | |
| 131 if 'ImplementedInPrivateScript' in definition.extended_attributes: | |
| 132 return '%s::PrivateScript::%s' % (v8_class_name(interface), base_name) | |
| 133 # partial interfaces are implemented as separate classes, with their members | |
| 134 # implemented as static member functions | |
| 135 partial_interface_implemented_as = definition.extended_attributes.get('Parti
alInterfaceImplementedAs') | |
| 136 if partial_interface_implemented_as: | |
| 137 return '%s::%s' % (partial_interface_implemented_as, base_name) | |
| 138 if (definition.is_static or | |
| 139 definition.name in ('Constructor', 'NamedConstructor')): | |
| 140 return '%s::%s' % (cpp_name(interface), base_name) | |
| 141 return 'impl->%s' % base_name | |
| 142 | |
| 143 | |
| 144 def v8_class_name(interface): | |
| 145 return v8_types.v8_type(interface.name) | |
| 146 | |
| 147 | |
| 148 ################################################################################ | |
| 149 # Specific extended attributes | |
| 150 ################################################################################ | |
| 151 | |
| 152 # [ActivityLogging] | |
| 153 def activity_logging_world_list(member, access_type=''): | |
| 154 """Returns a set of world suffixes for which a definition member has activit
y logging, for specified access type. | |
| 155 | |
| 156 access_type can be 'Getter' or 'Setter' if only checking getting or setting. | |
| 157 """ | |
| 158 extended_attributes = member.extended_attributes | |
| 159 if 'LogActivity' not in extended_attributes: | |
| 160 return set() | |
| 161 log_activity = extended_attributes['LogActivity'] | |
| 162 if log_activity and not log_activity.startswith(access_type): | |
| 163 return set() | |
| 164 | |
| 165 includes.add('bindings/core/v8/V8DOMActivityLogger.h') | |
| 166 if 'LogAllWorlds' in extended_attributes: | |
| 167 return set(['', 'ForMainWorld']) | |
| 168 return set(['']) # At minimum, include isolated worlds. | |
| 169 | |
| 170 | |
| 171 # [ActivityLogging] | |
| 172 def activity_logging_world_check(member): | |
| 173 """Returns if an isolated world check is required when generating activity | |
| 174 logging code. | |
| 175 | |
| 176 The check is required when there is no per-world binding code and logging is | |
| 177 required only for isolated world. | |
| 178 """ | |
| 179 extended_attributes = member.extended_attributes | |
| 180 if 'LogActivity' not in extended_attributes: | |
| 181 return False | |
| 182 if ('PerWorldBindings' not in extended_attributes and | |
| 183 'LogAllWorlds' not in extended_attributes): | |
| 184 return True | |
| 185 return False | |
| 186 | |
| 187 | |
| 188 # [CallWith] | |
| 189 CALL_WITH_ARGUMENTS = { | |
| 190 'ScriptState': 'scriptState', | |
| 191 'ExecutionContext': 'executionContext', | |
| 192 'ScriptArguments': 'scriptArguments.release()', | |
| 193 'ActiveWindow': 'callingDOMWindow(info.GetIsolate())', | |
| 194 'FirstWindow': 'enteredDOMWindow(info.GetIsolate())', | |
| 195 'Document': 'document', | |
| 196 } | |
| 197 # List because key order matters, as we want arguments in deterministic order | |
| 198 CALL_WITH_VALUES = [ | |
| 199 'ScriptState', | |
| 200 'ExecutionContext', | |
| 201 'ScriptArguments', | |
| 202 'ActiveWindow', | |
| 203 'FirstWindow', | |
| 204 'Document', | |
| 205 ] | |
| 206 | |
| 207 | |
| 208 def call_with_arguments(call_with_values): | |
| 209 if not call_with_values: | |
| 210 return [] | |
| 211 return [CALL_WITH_ARGUMENTS[value] | |
| 212 for value in CALL_WITH_VALUES | |
| 213 if extended_attribute_value_contains(call_with_values, value)] | |
| 214 | |
| 215 | |
| 216 # [Conditional] | |
| 217 DELIMITER_TO_OPERATOR = { | |
| 218 '|': '||', | |
| 219 ',': '&&', | |
| 220 } | |
| 221 | |
| 222 | |
| 223 def conditional_string(definition_or_member): | |
| 224 extended_attributes = definition_or_member.extended_attributes | |
| 225 if 'Conditional' not in extended_attributes: | |
| 226 return None | |
| 227 return 'ENABLE(%s)' % extended_attributes['Conditional'] | |
| 228 | |
| 229 | |
| 230 # [DeprecateAs] | |
| 231 def deprecate_as(member): | |
| 232 extended_attributes = member.extended_attributes | |
| 233 if 'DeprecateAs' not in extended_attributes: | |
| 234 return None | |
| 235 return extended_attributes['DeprecateAs'] | |
| 236 | |
| 237 | |
| 238 # [Exposed] | |
| 239 EXPOSED_EXECUTION_CONTEXT_METHOD = { | |
| 240 'Window': 'isDocument', | |
| 241 } | |
| 242 | |
| 243 | |
| 244 def exposed(definition_or_member, interface): | |
| 245 exposure_set = extended_attribute_value_as_list(definition_or_member, 'Expos
ed') | |
| 246 if not exposure_set: | |
| 247 return None | |
| 248 | |
| 249 interface_exposure_set = expanded_exposure_set_for_interface(interface) | |
| 250 | |
| 251 # Methods must not be exposed to a broader scope than their interface. | |
| 252 if not set(exposure_set).issubset(interface_exposure_set): | |
| 253 raise ValueError('Interface members\' exposure sets must be a subset of
the interface\'s.') | |
| 254 | |
| 255 exposure_checks = [] | |
| 256 for environment in exposure_set: | |
| 257 # Methods must be exposed on one of the scopes known to Blink. | |
| 258 if environment not in EXPOSED_EXECUTION_CONTEXT_METHOD: | |
| 259 raise ValueError('Values for the [Exposed] annotation must reflect t
o a valid exposure scope.') | |
| 260 | |
| 261 exposure_checks.append('context->%s()' % EXPOSED_EXECUTION_CONTEXT_METHO
D[environment]) | |
| 262 | |
| 263 return ' || '.join(exposure_checks) | |
| 264 | |
| 265 | |
| 266 def expanded_exposure_set_for_interface(interface): | |
| 267 exposure_set = extended_attribute_value_as_list(interface, 'Exposed') | |
| 268 return sorted(set(exposure_set)) | |
| 269 | |
| 270 | |
| 271 # [ImplementedAs] | |
| 272 def cpp_name(definition_or_member): | |
| 273 extended_attributes = definition_or_member.extended_attributes | |
| 274 if 'ImplementedAs' not in extended_attributes: | |
| 275 return definition_or_member.name | |
| 276 return extended_attributes['ImplementedAs'] | |
| 277 | |
| 278 | |
| 279 # [MeasureAs] | |
| 280 def measure_as(definition_or_member): | |
| 281 extended_attributes = definition_or_member.extended_attributes | |
| 282 if 'MeasureAs' not in extended_attributes: | |
| 283 return None | |
| 284 return extended_attributes['MeasureAs'] | |
| 285 | |
| 286 | |
| 287 # [RuntimeEnabled] | |
| 288 def runtime_enabled_function_name(definition_or_member): | |
| 289 """Returns the name of the RuntimeEnabledFeatures function. | |
| 290 | |
| 291 The returned function checks if a method/attribute is enabled. | |
| 292 Given extended attribute RuntimeEnabled=FeatureName, return: | |
| 293 RuntimeEnabledFeatures::{featureName}Enabled | |
| 294 """ | |
| 295 extended_attributes = definition_or_member.extended_attributes | |
| 296 if 'RuntimeEnabled' not in extended_attributes: | |
| 297 return None | |
| 298 feature_name = extended_attributes['RuntimeEnabled'] | |
| 299 return 'RuntimeEnabledFeatures::%sEnabled' % uncapitalize(feature_name) | |
| OLD | NEW |