| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 """IDL type handling. | 4 """IDL type handling. |
| 5 | 5 |
| 6 Classes: | 6 Classes: |
| 7 IdlTypeBase | 7 IdlTypeBase |
| 8 IdlType | 8 IdlType |
| 9 IdlUnionType | 9 IdlUnionType |
| 10 IdlArrayOrSequenceType |
| 11 IdlArrayType |
| 12 IdlSequenceType |
| 10 """ | 13 """ |
| 11 | 14 |
| 12 from collections import defaultdict | 15 from collections import defaultdict |
| 13 | 16 |
| 14 | 17 |
| 15 ################################################################################ | 18 ################################################################################ |
| 16 # IDL types | 19 # IDL types |
| 17 ################################################################################ | 20 ################################################################################ |
| 18 | 21 |
| 19 INTEGER_TYPES = frozenset([ | 22 INTEGER_TYPES = frozenset([ |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 def inherits_interface(interface_name, ancestor_name): | 92 def inherits_interface(interface_name, ancestor_name): |
| 90 return (interface_name == ancestor_name or | 93 return (interface_name == ancestor_name or |
| 91 ancestor_name in ancestors[interface_name]) | 94 ancestor_name in ancestors[interface_name]) |
| 92 | 95 |
| 93 | 96 |
| 94 def set_ancestors(new_ancestors): | 97 def set_ancestors(new_ancestors): |
| 95 ancestors.update(new_ancestors) | 98 ancestors.update(new_ancestors) |
| 96 | 99 |
| 97 | 100 |
| 98 class IdlTypeBase(object): | 101 class IdlTypeBase(object): |
| 99 """Base class for IdlType and IdlUnionType.""" | 102 """Base class for IdlType, IdlUnionType and IdlArrayOrSequenceType.""" |
| 100 | 103 |
| 101 def __init__(self, is_array=False, is_sequence=False, is_nullable=False): | 104 def __init__(self, is_nullable): |
| 102 self.base_type = None | 105 self.base_type = None |
| 103 self.is_array = is_array | |
| 104 self.is_sequence = is_sequence | |
| 105 self.is_nullable = is_nullable | 106 self.is_nullable = is_nullable |
| 106 | 107 |
| 107 @property | 108 def __str__(self): |
| 108 def native_array_element_type(self): | 109 inner_string = self.inner_string |
| 109 return None | 110 if self.is_nullable: |
| 111 # FIXME: Dictionary::ConversionContext::setConversionType can't |
| 112 # handle the '?' in nullable types (passes nullability separately). |
| 113 # Update that function to handle nullability from the type name, |
| 114 # simplifying its signature. |
| 115 # return inner_string + '?' |
| 116 return inner_string |
| 117 return inner_string |
| 110 | 118 |
| 111 @property | 119 @property |
| 112 def array_element_type(self): | 120 def inner_string(self): |
| 113 return None | 121 raise NotImplementedError( |
| 122 'inner_string property should be defined in subclasses') |
| 114 | 123 |
| 115 @property | 124 @property |
| 116 def is_basic_type(self): | 125 def is_basic_type(self): |
| 117 return False | 126 return False |
| 118 | 127 |
| 119 @property | 128 @property |
| 120 def is_callback_function(self): | 129 def is_callback_function(self): |
| 121 return False | 130 return False |
| 122 | 131 |
| 123 @property | 132 @property |
| 133 def is_callback_interface(self): |
| 134 return False |
| 135 |
| 136 @property |
| 124 def is_dictionary(self): | 137 def is_dictionary(self): |
| 125 return False | 138 return False |
| 126 | 139 |
| 127 @property | 140 @property |
| 128 def is_enum(self): | 141 def is_enum(self): |
| 129 return False | 142 return False |
| 130 | 143 |
| 131 @property | 144 @property |
| 132 def is_integer_type(self): | 145 def is_integer_type(self): |
| 133 return False | 146 return False |
| 134 | 147 |
| 135 @property | 148 @property |
| 136 def is_numeric_type(self): | 149 def is_numeric_type(self): |
| 137 return False | 150 return False |
| 138 | 151 |
| 139 @property | 152 @property |
| 140 def is_primitivee_type(self): | 153 def is_primitive_type(self): |
| 141 return False | 154 return False |
| 142 | 155 |
| 143 @property | 156 @property |
| 157 def is_interface_type(self): |
| 158 return False |
| 159 |
| 160 @property |
| 144 def is_string_type(self): | 161 def is_string_type(self): |
| 145 return False | 162 return False |
| 146 | 163 |
| 147 @property | 164 @property |
| 148 def is_union_type(self): | 165 def is_union_type(self): |
| 149 return False | 166 return False |
| 150 | 167 |
| 151 @property | 168 @property |
| 152 def may_raise_exception_on_conversion(self): | 169 def may_raise_exception_on_conversion(self): |
| 153 return False | 170 return False |
| 154 | 171 |
| 155 @property | 172 @property |
| 156 def name(self): | 173 def name(self): |
| 174 if self.is_nullable: |
| 175 return self.inner_name + 'OrNull' |
| 176 return self.inner_name |
| 177 |
| 178 @property |
| 179 def inner_name(self): |
| 157 raise NotImplementedError( | 180 raise NotImplementedError( |
| 158 'name property should be defined in subclasses') | 181 'inner_name property should be defined in subclasses') |
| 159 | 182 |
| 160 def resolve_typedefs(self, typedefs): | 183 def resolve_typedefs(self, typedefs): |
| 161 raise NotImplementedError( | 184 raise NotImplementedError( |
| 162 'resolve_typedefs should be defined in subclasses') | 185 'resolve_typedefs should be defined in subclasses') |
| 163 | 186 |
| 164 | 187 |
| 165 ################################################################################ | 188 ################################################################################ |
| 166 # IdlType | 189 # IdlType |
| 167 ################################################################################ | 190 ################################################################################ |
| 168 | 191 |
| 169 class IdlType(IdlTypeBase): | 192 class IdlType(IdlTypeBase): |
| 170 # FIXME: incorporate Nullable, etc. | 193 # FIXME: incorporate Nullable, etc. |
| 171 # FIXME: use nested types: IdlArrayType, IdlNullableType, IdlSequenceType | |
| 172 # to support types like short?[] vs. short[]?, instead of treating these | 194 # to support types like short?[] vs. short[]?, instead of treating these |
| 173 # as orthogonal properties (via flags). | 195 # as orthogonal properties (via flags). |
| 174 callback_functions = set() | 196 callback_functions = set() |
| 175 callback_interfaces = set() | 197 callback_interfaces = set() |
| 176 dictionaries = set() | 198 dictionaries = set() |
| 177 enums = {} # name -> values | 199 enums = {} # name -> values |
| 178 | 200 |
| 179 def __init__(self, base_type, is_array=False, is_sequence=False, is_nullable
=False, is_unrestricted=False): | 201 def __init__(self, base_type, is_nullable=False, is_unrestricted=False): |
| 180 super(IdlType, self).__init__(is_array=is_array, is_sequence=is_sequence
, is_nullable=is_nullable) | 202 super(IdlType, self).__init__(is_nullable) |
| 181 if is_array and is_sequence: | |
| 182 raise ValueError('Array of Sequences are not allowed.') | |
| 183 if is_unrestricted: | 203 if is_unrestricted: |
| 184 self.base_type = 'unrestricted %s' % base_type | 204 self.base_type = 'unrestricted %s' % base_type |
| 185 else: | 205 else: |
| 186 self.base_type = base_type | 206 self.base_type = base_type |
| 187 | 207 |
| 188 def __str__(self): | |
| 189 type_string = self.base_type | |
| 190 if self.is_array: | |
| 191 return type_string + '[]' | |
| 192 if self.is_sequence: | |
| 193 return 'sequence<%s>' % type_string | |
| 194 if self.is_nullable: | |
| 195 # FIXME: Dictionary::ConversionContext::setConversionType can't | |
| 196 # handle the '?' in nullable types (passes nullability separately). | |
| 197 # Update that function to handle nullability from the type name, | |
| 198 # simplifying its signature. | |
| 199 # return type_string + '?' | |
| 200 return type_string | |
| 201 return type_string | |
| 202 | |
| 203 # FIXME: move to v8_types.py | |
| 204 @property | 208 @property |
| 205 def native_array_element_type(self): | 209 def inner_string(self): |
| 206 return self.array_element_type or self.sequence_element_type | 210 return self.base_type |
| 207 | |
| 208 @property | |
| 209 def array_element_type(self): | |
| 210 return self.is_array and IdlType(self.base_type) | |
| 211 | |
| 212 @property | |
| 213 def sequence_element_type(self): | |
| 214 return self.is_sequence and IdlType(self.base_type) | |
| 215 | 211 |
| 216 @property | 212 @property |
| 217 def is_basic_type(self): | 213 def is_basic_type(self): |
| 218 return self.base_type in BASIC_TYPES and not self.native_array_element_t
ype | 214 return self.base_type in BASIC_TYPES |
| 219 | 215 |
| 220 @property | 216 @property |
| 221 def is_callback_function(self): | 217 def is_callback_function(self): |
| 222 return self.base_type in IdlType.callback_functions | 218 return self.base_type in IdlType.callback_functions |
| 223 | 219 |
| 224 @property | 220 @property |
| 225 def is_callback_interface(self): | 221 def is_callback_interface(self): |
| 226 return self.base_type in IdlType.callback_interfaces | 222 return self.base_type in IdlType.callback_interfaces |
| 227 | 223 |
| 228 @property | 224 @property |
| 229 def is_dictionary(self): | 225 def is_dictionary(self): |
| 230 return self.base_type in IdlType.dictionaries | 226 return self.base_type in IdlType.dictionaries |
| 231 | 227 |
| 232 @property | 228 @property |
| 233 def is_composite_type(self): | |
| 234 return (self.name == 'Any' or | |
| 235 self.array_element_type or | |
| 236 self.sequence_element_type or | |
| 237 self.is_union_type) | |
| 238 | |
| 239 @property | |
| 240 def is_enum(self): | 229 def is_enum(self): |
| 241 # FIXME: add an IdlEnumType class and a resolve_enums step at end of | 230 # FIXME: add an IdlEnumType class and a resolve_enums step at end of |
| 242 # IdlDefinitions constructor | 231 # IdlDefinitions constructor |
| 243 return self.name in IdlType.enums | 232 return self.name in IdlType.enums |
| 244 | 233 |
| 245 @property | 234 @property |
| 246 def enum_values(self): | 235 def enum_values(self): |
| 247 return IdlType.enums[self.name] | 236 return IdlType.enums[self.name] |
| 248 | 237 |
| 249 @property | 238 @property |
| 250 def is_integer_type(self): | 239 def is_integer_type(self): |
| 251 return self.base_type in INTEGER_TYPES and not self.native_array_element
_type | 240 return self.base_type in INTEGER_TYPES |
| 252 | 241 |
| 253 @property | 242 @property |
| 254 def is_numeric_type(self): | 243 def is_numeric_type(self): |
| 255 return self.base_type in NUMERIC_TYPES and not self.native_array_element
_type | 244 return self.base_type in NUMERIC_TYPES |
| 256 | 245 |
| 257 @property | 246 @property |
| 258 def is_primitive_type(self): | 247 def is_primitive_type(self): |
| 259 return self.base_type in PRIMITIVE_TYPES and not self.native_array_eleme
nt_type | 248 return self.base_type in PRIMITIVE_TYPES |
| 260 | 249 |
| 261 @property | 250 @property |
| 262 def is_interface_type(self): | 251 def is_interface_type(self): |
| 263 # Anything that is not another type is an interface type. | 252 # Anything that is not another type is an interface type. |
| 264 # http://www.w3.org/TR/WebIDL/#idl-types | 253 # http://www.w3.org/TR/WebIDL/#idl-types |
| 265 # http://www.w3.org/TR/WebIDL/#idl-interface | 254 # http://www.w3.org/TR/WebIDL/#idl-interface |
| 266 # In C++ these are RefPtr or PassRefPtr types. | 255 # In C++ these are RefPtr or PassRefPtr types. |
| 267 return not(self.is_basic_type or | 256 return not(self.is_basic_type or |
| 268 self.is_composite_type or | |
| 269 self.is_callback_function or | 257 self.is_callback_function or |
| 270 self.is_dictionary or | 258 self.is_dictionary or |
| 271 self.is_enum or | 259 self.is_enum or |
| 260 self.name == 'Any' or |
| 272 self.name == 'Object' or | 261 self.name == 'Object' or |
| 273 self.name == 'Promise') # Promise will be basic in future | 262 self.name == 'Promise') # Promise will be basic in future |
| 274 | 263 |
| 275 @property | 264 @property |
| 276 def is_string_type(self): | 265 def is_string_type(self): |
| 277 return self.base_type_name in STRING_TYPES | 266 return self.inner_name in STRING_TYPES |
| 278 | 267 |
| 279 @property | 268 @property |
| 280 def may_raise_exception_on_conversion(self): | 269 def may_raise_exception_on_conversion(self): |
| 281 return (self.is_integer_type or | 270 return (self.is_integer_type or |
| 282 self.name in ('ByteString', 'ScalarValueString')) | 271 self.name in ('ByteString', 'ScalarValueString')) |
| 283 | 272 |
| 284 @property | 273 @property |
| 285 def is_union_type(self): | 274 def is_union_type(self): |
| 286 return isinstance(self, IdlUnionType) | 275 return isinstance(self, IdlUnionType) |
| 287 | 276 |
| 288 @property | 277 @property |
| 289 def base_type_name(self): | 278 def inner_name(self): |
| 279 """Return type name (or inner type name if nullable) |
| 280 |
| 281 http://heycam.github.io/webidl/#dfn-type-name |
| 282 """ |
| 290 base_type = self.base_type | 283 base_type = self.base_type |
| 291 return TYPE_NAMES.get(base_type, base_type) | 284 return TYPE_NAMES.get(base_type, base_type) |
| 292 | 285 |
| 293 @property | |
| 294 def name(self): | |
| 295 """Return type name. | |
| 296 | |
| 297 http://heycam.github.io/webidl/#dfn-type-name | |
| 298 """ | |
| 299 base_type_name = self.base_type_name | |
| 300 if self.is_array: | |
| 301 return base_type_name + 'Array' | |
| 302 if self.is_sequence: | |
| 303 return base_type_name + 'Sequence' | |
| 304 if self.is_nullable: | |
| 305 return base_type_name + 'OrNull' | |
| 306 return base_type_name | |
| 307 | |
| 308 @classmethod | 286 @classmethod |
| 309 def set_callback_functions(cls, new_callback_functions): | 287 def set_callback_functions(cls, new_callback_functions): |
| 310 cls.callback_functions.update(new_callback_functions) | 288 cls.callback_functions.update(new_callback_functions) |
| 311 | 289 |
| 312 @classmethod | 290 @classmethod |
| 313 def set_callback_interfaces(cls, new_callback_interfaces): | 291 def set_callback_interfaces(cls, new_callback_interfaces): |
| 314 cls.callback_interfaces.update(new_callback_interfaces) | 292 cls.callback_interfaces.update(new_callback_interfaces) |
| 315 | 293 |
| 316 @classmethod | 294 @classmethod |
| 317 def set_dictionaries(cls, new_dictionaries): | 295 def set_dictionaries(cls, new_dictionaries): |
| 318 cls.dictionaries.update(new_dictionaries) | 296 cls.dictionaries.update(new_dictionaries) |
| 319 | 297 |
| 320 @classmethod | 298 @classmethod |
| 321 def set_enums(cls, new_enums): | 299 def set_enums(cls, new_enums): |
| 322 cls.enums.update(new_enums) | 300 cls.enums.update(new_enums) |
| 323 | 301 |
| 324 def resolve_typedefs(self, typedefs): | 302 def resolve_typedefs(self, typedefs): |
| 303 # This function either returns |self|, possibly mutated, or leaves this |
| 304 # object unmodified and returns a different object. |
| 305 # FIXME: Change to never mutate |self|, and rename typedefs_resolved(). |
| 325 if self.base_type not in typedefs: | 306 if self.base_type not in typedefs: |
| 326 return self | 307 return self |
| 327 new_type = typedefs[self.base_type] | 308 new_type = typedefs[self.base_type] |
| 328 if type(new_type) != type(self): | 309 if type(new_type) != type(self): |
| 329 # If type changes, need to return a different object, | 310 # If type changes, need to return a different object, |
| 330 # since can't change type(self) | 311 # since can't change type(self) |
| 331 return new_type | 312 return new_type |
| 332 # If type doesn't change, just mutate self to avoid a new object | 313 # If type doesn't change, just mutate self to avoid a new object |
| 333 # FIXME: a bit ugly; use __init__ instead of setting flags | 314 self.__init__(new_type.base_type, self.is_nullable or new_type.is_nullab
le) |
| 334 self.base_type = new_type.base_type | |
| 335 # handle array both in use and in typedef itself: | |
| 336 # typedef Type TypeDef; | |
| 337 # TypeDef[] ... | |
| 338 # and: | |
| 339 # typedef Type[] TypeArray | |
| 340 # TypeArray ... | |
| 341 self.is_array |= new_type.is_array | |
| 342 self.is_sequence |= new_type.is_sequence | |
| 343 return self | 315 return self |
| 344 | 316 |
| 345 | 317 |
| 346 ################################################################################ | 318 ################################################################################ |
| 347 # IdlUnionType | 319 # IdlUnionType |
| 348 ################################################################################ | 320 ################################################################################ |
| 349 | 321 |
| 350 class IdlUnionType(IdlTypeBase): | 322 class IdlUnionType(IdlTypeBase): |
| 351 # http://heycam.github.io/webidl/#idl-union | 323 # http://heycam.github.io/webidl/#idl-union |
| 352 def __init__(self, member_types, is_nullable=False): | 324 def __init__(self, member_types, is_nullable=False): |
| 353 super(IdlUnionType, self).__init__(is_nullable=is_nullable) | 325 super(IdlUnionType, self).__init__(is_nullable=is_nullable) |
| 354 self.member_types = member_types | 326 self.member_types = member_types |
| 355 | 327 |
| 356 @property | 328 @property |
| 357 def is_union_type(self): | 329 def is_union_type(self): |
| 358 return True | 330 return True |
| 359 | 331 |
| 360 @property | 332 @property |
| 361 def name(self): | 333 def inner_name(self): |
| 334 """Return type name (or inner type name if nullable) |
| 335 |
| 336 http://heycam.github.io/webidl/#dfn-type-name |
| 337 """ |
| 362 return 'Or'.join(member_type.name for member_type in self.member_types) | 338 return 'Or'.join(member_type.name for member_type in self.member_types) |
| 363 | 339 |
| 364 def resolve_typedefs(self, typedefs): | 340 def resolve_typedefs(self, typedefs): |
| 365 self.member_types = [ | 341 self.member_types = [ |
| 366 typedefs.get(member_type, member_type) | 342 typedefs.get(member_type, member_type) |
| 367 for member_type in self.member_types] | 343 for member_type in self.member_types] |
| 368 return self | 344 return self |
| 345 |
| 346 |
| 347 ################################################################################ |
| 348 # IdlArrayOrSequenceType, IdlArrayType, IdlSequenceType |
| 349 ################################################################################ |
| 350 |
| 351 class IdlArrayOrSequenceType(IdlTypeBase): |
| 352 """Base class for IdlArrayType and IdlSequenceType.""" |
| 353 |
| 354 def __init__(self, element_type, is_nullable=False): |
| 355 super(IdlArrayOrSequenceType, self).__init__(is_nullable) |
| 356 self.element_type = element_type |
| 357 |
| 358 def resolve_typedefs(self, typedefs): |
| 359 self.element_type = self.element_type.resolve_typedefs(typedefs) |
| 360 return self |
| 361 |
| 362 |
| 363 class IdlArrayType(IdlArrayOrSequenceType): |
| 364 def __init__(self, element_type, is_nullable=False): |
| 365 super(IdlArrayType, self).__init__(element_type, is_nullable) |
| 366 |
| 367 @property |
| 368 def inner_string(self): |
| 369 return '%s[]' % self.element_type |
| 370 |
| 371 @property |
| 372 def inner_name(self): |
| 373 return self.element_type.name + 'Array' |
| 374 |
| 375 |
| 376 class IdlSequenceType(IdlArrayOrSequenceType): |
| 377 def __init__(self, element_type, is_nullable=False): |
| 378 super(IdlSequenceType, self).__init__(element_type, is_nullable) |
| 379 |
| 380 @property |
| 381 def inner_string(self): |
| 382 return 'sequence<%s>' % self.element_type |
| 383 |
| 384 @property |
| 385 def inner_name(self): |
| 386 return self.element_type.name + 'Sequence' |
| OLD | NEW |