| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 3 # for details. All rights reserved. Use of this source code is governed by a | |
| 4 # BSD-style license that can be found in the LICENSE file. | |
| 5 | |
| 6 """This module provides shared functionality for systems to generate | |
| 7 Dart APIs from the IDL database.""" | |
| 8 | |
| 9 import copy | |
| 10 import re | |
| 11 from htmlrenamer import html_interface_renames | |
| 12 | |
| 13 _pure_interfaces = set([ | |
| 14 # TODO(sra): DOMStringMap should be a class implementing Map<String,String>. | |
| 15 'DOMStringMap', | |
| 16 'ElementTimeControl', | |
| 17 'ElementTraversal', | |
| 18 'EventListener', | |
| 19 'MediaQueryListListener', | |
| 20 'NodeSelector', | |
| 21 'SVGExternalResourcesRequired', | |
| 22 'SVGFilterPrimitiveStandardAttributes', | |
| 23 'SVGFitToViewBox', | |
| 24 'SVGLangSpace', | |
| 25 'SVGLocatable', | |
| 26 'SVGStylable', | |
| 27 'SVGTests', | |
| 28 'SVGTransformable', | |
| 29 'SVGURIReference', | |
| 30 'SVGZoomAndPan', | |
| 31 'TimeoutHandler']) | |
| 32 | |
| 33 def IsPureInterface(interface_name): | |
| 34 return interface_name in _pure_interfaces | |
| 35 | |
| 36 | |
| 37 _methods_with_named_formals = set([ | |
| 38 'DataView.getFloat32', | |
| 39 'DataView.getFloat64', | |
| 40 'DataView.getInt16', | |
| 41 'DataView.getInt32', | |
| 42 'DataView.getInt8', | |
| 43 'DataView.getUint16', | |
| 44 'DataView.getUint32', | |
| 45 'DataView.getUint8', | |
| 46 'DataView.setFloat32', | |
| 47 'DataView.setFloat64', | |
| 48 'DataView.setInt16', | |
| 49 'DataView.setInt32', | |
| 50 'DataView.setInt8', | |
| 51 'DataView.setUint16', | |
| 52 'DataView.setUint32', | |
| 53 'DataView.setUint8', | |
| 54 'DirectoryEntry.getDirectory', | |
| 55 'DirectoryEntry.getFile', | |
| 56 ]) | |
| 57 | |
| 58 # | |
| 59 # Renames for attributes that have names that are not legal Dart names. | |
| 60 # | |
| 61 _dart_attribute_renames = { | |
| 62 'default': 'defaultValue', | |
| 63 'final': 'finalValue', | |
| 64 } | |
| 65 | |
| 66 # | |
| 67 # Interface version of the DOM needs to delegate typed array constructors to a | |
| 68 # factory provider. | |
| 69 # | |
| 70 interface_factories = { | |
| 71 'Float32Array': '_TypedArrayFactoryProvider', | |
| 72 'Float64Array': '_TypedArrayFactoryProvider', | |
| 73 'Int8Array': '_TypedArrayFactoryProvider', | |
| 74 'Int16Array': '_TypedArrayFactoryProvider', | |
| 75 'Int32Array': '_TypedArrayFactoryProvider', | |
| 76 'Uint8Array': '_TypedArrayFactoryProvider', | |
| 77 'Uint16Array': '_TypedArrayFactoryProvider', | |
| 78 'Uint32Array': '_TypedArrayFactoryProvider', | |
| 79 'Uint8ClampedArray': '_TypedArrayFactoryProvider', | |
| 80 } | |
| 81 | |
| 82 # | |
| 83 # Custom native specs for the dart2js dom. | |
| 84 # | |
| 85 _dart2js_dom_custom_native_specs = { | |
| 86 # Decorate the singleton Console object, if present (workers do not have a | |
| 87 # console). | |
| 88 'Console': "=(typeof console == 'undefined' ? {} : console)", | |
| 89 | |
| 90 # DOMWindow aliased with global scope. | |
| 91 'DOMWindow': '@*DOMWindow', | |
| 92 } | |
| 93 | |
| 94 def IsRegisteredType(type_name): | |
| 95 return type_name in _idl_type_registry | |
| 96 | |
| 97 def MakeNativeSpec(javascript_binding_name): | |
| 98 if javascript_binding_name in _dart2js_dom_custom_native_specs: | |
| 99 return _dart2js_dom_custom_native_specs[javascript_binding_name] | |
| 100 else: | |
| 101 # Make the class 'hidden' so it is dynamically patched at runtime. This | |
| 102 # is useful for browser compat. | |
| 103 return '*' + javascript_binding_name | |
| 104 | |
| 105 | |
| 106 def MatchSourceFilter(thing): | |
| 107 return 'WebKit' in thing.annotations or 'Dart' in thing.annotations | |
| 108 | |
| 109 | |
| 110 class ParamInfo(object): | |
| 111 """Holder for various information about a parameter of a Dart operation. | |
| 112 | |
| 113 Attributes: | |
| 114 name: Name of parameter. | |
| 115 type_id: Original type id. None for merged types. | |
| 116 is_optional: Parameter optionality. | |
| 117 """ | |
| 118 def __init__(self, name, type_id, is_optional): | |
| 119 self.name = name | |
| 120 self.type_id = type_id | |
| 121 self.is_optional = is_optional | |
| 122 | |
| 123 def Copy(self): | |
| 124 return ParamInfo(self.name, self.type_id, self.is_optional) | |
| 125 | |
| 126 def __repr__(self): | |
| 127 content = 'name = %s, type_id = %s, is_optional = %s' % ( | |
| 128 self.name, self.type_id, self.is_optional) | |
| 129 return '<ParamInfo(%s)>' % content | |
| 130 | |
| 131 | |
| 132 # Given a list of overloaded arguments, render dart arguments. | |
| 133 def _BuildArguments(args, interface, constructor=False): | |
| 134 def IsOptional(argument): | |
| 135 if 'Callback' in argument.ext_attrs: | |
| 136 # Callbacks with 'Optional=XXX' are treated as optional arguments. | |
| 137 return 'Optional' in argument.ext_attrs | |
| 138 if constructor: | |
| 139 # FIXME: Constructors with 'Optional=XXX' shouldn't be treated as | |
| 140 # optional arguments. | |
| 141 return 'Optional' in argument.ext_attrs | |
| 142 return False | |
| 143 | |
| 144 # Given a list of overloaded arguments, choose a suitable name. | |
| 145 def OverloadedName(args): | |
| 146 return '_OR_'.join(sorted(set(arg.id for arg in args))) | |
| 147 | |
| 148 def DartType(idl_type_name): | |
| 149 if idl_type_name in _idl_type_registry: | |
| 150 return _idl_type_registry[idl_type_name].dart_type or idl_type_name | |
| 151 return idl_type_name | |
| 152 | |
| 153 # Given a list of overloaded arguments, choose a suitable type. | |
| 154 def OverloadedType(args): | |
| 155 type_ids = sorted(set(arg.type.id for arg in args)) | |
| 156 if len(set(DartType(arg.type.id) for arg in args)) == 1: | |
| 157 return type_ids[0] | |
| 158 else: | |
| 159 return None | |
| 160 | |
| 161 result = [] | |
| 162 | |
| 163 is_optional = False | |
| 164 for arg_tuple in map(lambda *x: x, *args): | |
| 165 is_optional = is_optional or any(arg is None or IsOptional(arg) for arg in a
rg_tuple) | |
| 166 | |
| 167 filtered = filter(None, arg_tuple) | |
| 168 type_id = OverloadedType(filtered) | |
| 169 name = OverloadedName(filtered) | |
| 170 result.append(ParamInfo(name, type_id, is_optional)) | |
| 171 | |
| 172 return result | |
| 173 | |
| 174 def IsOptional(argument): | |
| 175 return ('Optional' in argument.ext_attrs and | |
| 176 argument.ext_attrs['Optional'] == None) | |
| 177 | |
| 178 def AnalyzeOperation(interface, operations): | |
| 179 """Makes operation calling convention decision for a set of overloads. | |
| 180 | |
| 181 Returns: An OperationInfo object. | |
| 182 """ | |
| 183 | |
| 184 # split operations with optional args into multiple operations | |
| 185 split_operations = [] | |
| 186 for operation in operations: | |
| 187 for i in range(0, len(operation.arguments)): | |
| 188 if IsOptional(operation.arguments[i]): | |
| 189 new_operation = copy.deepcopy(operation) | |
| 190 new_operation.arguments = new_operation.arguments[:i] | |
| 191 split_operations.append(new_operation) | |
| 192 split_operations.append(operation) | |
| 193 | |
| 194 # Zip together arguments from each overload by position, then convert | |
| 195 # to a dart argument. | |
| 196 info = OperationInfo() | |
| 197 info.operations = operations | |
| 198 info.overloads = split_operations | |
| 199 info.declared_name = operations[0].id | |
| 200 info.name = operations[0].ext_attrs.get('DartName', info.declared_name) | |
| 201 info.constructor_name = None | |
| 202 info.js_name = info.declared_name | |
| 203 info.type_name = operations[0].type.id # TODO: widen. | |
| 204 info.param_infos = _BuildArguments([op.arguments for op in split_operations],
interface) | |
| 205 full_name = '%s.%s' % (interface.id, info.declared_name) | |
| 206 info.requires_named_arguments = full_name in _methods_with_named_formals | |
| 207 return info | |
| 208 | |
| 209 | |
| 210 def AnalyzeConstructor(interface): | |
| 211 """Returns an OperationInfo object for the constructor. | |
| 212 | |
| 213 Returns None if the interface has no Constructor. | |
| 214 """ | |
| 215 if 'Constructor' in interface.ext_attrs: | |
| 216 name = None | |
| 217 func_value = interface.ext_attrs.get('Constructor') | |
| 218 if not func_value: | |
| 219 args = [] | |
| 220 idl_args = [] | |
| 221 elif 'NamedConstructor' in interface.ext_attrs: | |
| 222 func_value = interface.ext_attrs.get('NamedConstructor') | |
| 223 name = func_value.id | |
| 224 else: | |
| 225 return None | |
| 226 | |
| 227 if func_value: | |
| 228 idl_args = func_value.arguments | |
| 229 args =_BuildArguments([idl_args], interface, True) | |
| 230 | |
| 231 info = OperationInfo() | |
| 232 info.overloads = None | |
| 233 info.idl_args = idl_args | |
| 234 info.declared_name = name | |
| 235 info.name = name | |
| 236 info.constructor_name = None | |
| 237 info.js_name = name | |
| 238 info.type_name = interface.id | |
| 239 info.param_infos = args | |
| 240 info.requires_named_arguments = False | |
| 241 info.pure_dart_constructor = False | |
| 242 return info | |
| 243 | |
| 244 def IsDartListType(type): | |
| 245 return type == 'List' or type.startswith('sequence<') | |
| 246 | |
| 247 def IsDartCollectionType(type): | |
| 248 return IsDartListType(type) | |
| 249 | |
| 250 def FindMatchingAttribute(interface, attr1): | |
| 251 matches = [attr2 for attr2 in interface.attributes | |
| 252 if attr1.id == attr2.id] | |
| 253 if matches: | |
| 254 assert len(matches) == 1 | |
| 255 return matches[0] | |
| 256 return None | |
| 257 | |
| 258 | |
| 259 def DartDomNameOfAttribute(attr): | |
| 260 """Returns the Dart name for an IDLAttribute. | |
| 261 | |
| 262 attr.id is the 'native' or JavaScript name. | |
| 263 | |
| 264 To ensure uniformity, work with the true IDL name until as late a possible, | |
| 265 e.g. translate to the Dart name when generating Dart code. | |
| 266 """ | |
| 267 name = attr.id | |
| 268 name = _dart_attribute_renames.get(name, name) | |
| 269 name = attr.ext_attrs.get('DartName', None) or name | |
| 270 return name | |
| 271 | |
| 272 | |
| 273 def TypeOrNothing(dart_type, comment=None): | |
| 274 """Returns string for declaring something with |dart_type| in a context | |
| 275 where a type may be omitted. | |
| 276 The string is empty or has a trailing space. | |
| 277 """ | |
| 278 if dart_type == 'dynamic': | |
| 279 if comment: | |
| 280 return '/*%s*/ ' % comment # Just a comment foo(/*T*/ x) | |
| 281 else: | |
| 282 return '' # foo(x) looks nicer than foo(var|dynamic x) | |
| 283 else: | |
| 284 return dart_type + ' ' | |
| 285 | |
| 286 | |
| 287 def TypeOrVar(dart_type, comment=None): | |
| 288 """Returns string for declaring something with |dart_type| in a context | |
| 289 where if a type is omitted, 'var' must be used instead.""" | |
| 290 if dart_type == 'dynamic': | |
| 291 if comment: | |
| 292 return 'var /*%s*/' % comment # e.g. var /*T*/ x; | |
| 293 else: | |
| 294 return 'var' # e.g. var x; | |
| 295 else: | |
| 296 return dart_type | |
| 297 | |
| 298 | |
| 299 class OperationInfo(object): | |
| 300 """Holder for various derived information from a set of overloaded operations. | |
| 301 | |
| 302 Attributes: | |
| 303 overloads: A list of IDL operation overloads with the same name. | |
| 304 name: A string, the simple name of the operation. | |
| 305 constructor_name: A string, the name of the constructor iff the constructor | |
| 306 is named, e.g. 'fromList' in Int8Array.fromList(list). | |
| 307 type_name: A string, the name of the return type of the operation. | |
| 308 param_infos: A list of ParamInfo. | |
| 309 factory_parameters: A list of parameters used for custom designed Factory | |
| 310 calls. | |
| 311 """ | |
| 312 | |
| 313 def __init__(self): | |
| 314 self.factory_parameters = None | |
| 315 | |
| 316 def ParametersDeclaration(self, rename_type, force_optional=False): | |
| 317 def FormatParam(param): | |
| 318 dart_type = rename_type(param.type_id) if param.type_id else 'dynamic' | |
| 319 return '%s%s' % (TypeOrNothing(dart_type, param.type_id), param.name) | |
| 320 | |
| 321 required = [] | |
| 322 optional = [] | |
| 323 for param_info in self.param_infos: | |
| 324 if param_info.is_optional: | |
| 325 optional.append(param_info) | |
| 326 else: | |
| 327 if optional: | |
| 328 raise Exception('Optional parameters cannot precede required ones: ' | |
| 329 + str(params)) | |
| 330 required.append(param_info) | |
| 331 argtexts = map(FormatParam, required) | |
| 332 if optional: | |
| 333 needs_named = self.requires_named_arguments and not force_optional | |
| 334 left_bracket, right_bracket = '{}' if needs_named else '[]' | |
| 335 argtexts.append( | |
| 336 left_bracket + | |
| 337 ', '.join(map(FormatParam, optional)) + | |
| 338 right_bracket) | |
| 339 return ', '.join(argtexts) | |
| 340 | |
| 341 def ParametersAsArgumentList(self, parameter_count = None): | |
| 342 """Returns a string of the parameter names suitable for passing the | |
| 343 parameters as arguments. | |
| 344 """ | |
| 345 if parameter_count is None: | |
| 346 parameter_count = len(self.param_infos) | |
| 347 return ', '.join(map( | |
| 348 lambda param_info: param_info.name, | |
| 349 self.param_infos[:parameter_count])) | |
| 350 | |
| 351 def IsStatic(self): | |
| 352 is_static = self.overloads[0].is_static | |
| 353 assert any([is_static == o.is_static for o in self.overloads]) | |
| 354 return is_static | |
| 355 | |
| 356 def _ConstructorFullName(self, rename_type): | |
| 357 if self.constructor_name: | |
| 358 return rename_type(self.type_name) + '.' + self.constructor_name | |
| 359 else: | |
| 360 return rename_type(self.type_name) | |
| 361 | |
| 362 def ConstantOutputOrder(a, b): | |
| 363 """Canonical output ordering for constants.""" | |
| 364 return cmp(a.id, b.id) | |
| 365 | |
| 366 | |
| 367 def _FormatNameList(names): | |
| 368 """Returns JavaScript array literal expression with one name per line.""" | |
| 369 #names = sorted(names) | |
| 370 if len(names) <= 1: | |
| 371 expression_string = str(names) # e.g. ['length'] | |
| 372 else: | |
| 373 expression_string = ',\n '.join(str(names).split(',')) | |
| 374 expression_string = expression_string.replace('[', '[\n ') | |
| 375 return expression_string | |
| 376 | |
| 377 | |
| 378 def IndentText(text, indent): | |
| 379 """Format lines of text with indent.""" | |
| 380 def FormatLine(line): | |
| 381 if line.strip(): | |
| 382 return '%s%s\n' % (indent, line) | |
| 383 else: | |
| 384 return '\n' | |
| 385 return ''.join(FormatLine(line) for line in text.split('\n')) | |
| 386 | |
| 387 # Given a sorted sequence of type identifiers, return an appropriate type | |
| 388 # name | |
| 389 def TypeName(type_ids, interface): | |
| 390 # Dynamically type this field for now. | |
| 391 return 'dynamic' | |
| 392 | |
| 393 # ------------------------------------------------------------------------------ | |
| 394 | |
| 395 class Conversion(object): | |
| 396 """Represents a way of converting between types.""" | |
| 397 def __init__(self, name, input_type, output_type): | |
| 398 # input_type is the type of the API input (and the argument type of the | |
| 399 # conversion function) | |
| 400 # output_type is the type of the API output (and the result type of the | |
| 401 # conversion function) | |
| 402 self.function_name = name | |
| 403 self.input_type = input_type | |
| 404 self.output_type = output_type | |
| 405 | |
| 406 # "TYPE DIRECTION INTERFACE.MEMBER" -> conversion | |
| 407 # Specific member of interface | |
| 408 # "TYPE DIRECTION INTERFACE.*" -> conversion | |
| 409 # All members of interface getting (setting) with type. | |
| 410 # "TYPE DIRECTION" -> conversion | |
| 411 # All getters (setters) of type. | |
| 412 # | |
| 413 # where DIRECTION is 'get' for getters and operation return values, 'set' for | |
| 414 # setters and operation arguments. INTERFACE and MEMBER are the idl names. | |
| 415 # | |
| 416 | |
| 417 _serialize_SSV = Conversion('convertDartToNative_SerializedScriptValue', | |
| 418 'dynamic', 'dynamic') | |
| 419 | |
| 420 dart2js_conversions = { | |
| 421 # Wrap non-local Windows. We need to check EventTarget (the base type) | |
| 422 # as well. Note, there are no functions that take a non-local Window | |
| 423 # as a parameter / setter. | |
| 424 'DOMWindow get': | |
| 425 Conversion('_convertNativeToDart_Window', 'dynamic', 'WindowBase'), | |
| 426 'EventTarget get': | |
| 427 Conversion('_convertNativeToDart_EventTarget', 'dynamic', | |
| 428 'EventTarget'), | |
| 429 'EventTarget set': | |
| 430 Conversion('_convertDartToNative_EventTarget', 'EventTarget', | |
| 431 'dynamic'), | |
| 432 | |
| 433 'IDBKey get': | |
| 434 Conversion('_convertNativeToDart_IDBKey', 'dynamic', 'dynamic'), | |
| 435 'IDBKey set': | |
| 436 Conversion('_convertDartToNative_IDBKey', 'dynamic', 'dynamic'), | |
| 437 | |
| 438 'ImageData get': | |
| 439 Conversion('_convertNativeToDart_ImageData', 'dynamic', 'ImageData'), | |
| 440 'ImageData set': | |
| 441 Conversion('_convertDartToNative_ImageData', 'ImageData', 'dynamic'), | |
| 442 | |
| 443 'Dictionary get': | |
| 444 Conversion('convertNativeToDart_Dictionary', 'dynamic', 'Map'), | |
| 445 'Dictionary set': | |
| 446 Conversion('convertDartToNative_Dictionary', 'Map', 'dynamic'), | |
| 447 | |
| 448 'DOMString[] set': | |
| 449 Conversion('convertDartToNative_StringArray', 'List<String>', 'List'), | |
| 450 | |
| 451 'any set IDBObjectStore.add': _serialize_SSV, | |
| 452 'any set IDBObjectStore.put': _serialize_SSV, | |
| 453 'any set IDBCursor.update': _serialize_SSV, | |
| 454 | |
| 455 # postMessage | |
| 456 'any set DedicatedWorkerContext.postMessage': _serialize_SSV, | |
| 457 'any set MessagePort.postMessage': _serialize_SSV, | |
| 458 'SerializedScriptValue set DOMWindow.postMessage': _serialize_SSV, | |
| 459 'SerializedScriptValue set Worker.postMessage': _serialize_SSV, | |
| 460 | |
| 461 # receiving message via MessageEvent | |
| 462 '* get MessageEvent.data': | |
| 463 Conversion('convertNativeToDart_SerializedScriptValue', | |
| 464 'dynamic', 'dynamic'), | |
| 465 | |
| 466 '* get History.state': | |
| 467 Conversion('_convertNativeToDart_SerializedScriptValue', | |
| 468 'dynamic', 'dynamic'), | |
| 469 | |
| 470 '* get PopStateEvent.state': | |
| 471 Conversion('convertNativeToDart_SerializedScriptValue', | |
| 472 'dynamic', 'dynamic'), | |
| 473 | |
| 474 # IDBAny is problematic. Some uses are just a union of other IDB types, | |
| 475 # which need no conversion.. Others include data values which require | |
| 476 # serialized script value processing. | |
| 477 'IDBAny get IDBCursorWithValue.value': | |
| 478 Conversion('_convertNativeToDart_IDBAny', 'dynamic', 'dynamic'), | |
| 479 | |
| 480 # This is problematic. The result property of IDBRequest is used for | |
| 481 # all requests. Read requests like IDBDataStore.getObject need | |
| 482 # conversion, but other requests like opening a database return | |
| 483 # something that does not need conversion. | |
| 484 'IDBAny get IDBRequest.result': | |
| 485 Conversion('_convertNativeToDart_IDBAny', 'dynamic', 'dynamic'), | |
| 486 | |
| 487 # "source: On getting, returns the IDBObjectStore or IDBIndex that the | |
| 488 # cursor is iterating. ...". So we should not try to convert it. | |
| 489 'IDBAny get IDBCursor.source': None, | |
| 490 | |
| 491 # Should be either a DOMString, an Array of DOMStrings or null. | |
| 492 'IDBAny get IDBObjectStore.keyPath': None, | |
| 493 } | |
| 494 | |
| 495 def FindConversion(idl_type, direction, interface, member): | |
| 496 table = dart2js_conversions | |
| 497 return (table.get('%s %s %s.%s' % (idl_type, direction, interface, member)) or | |
| 498 table.get('* %s %s.%s' % (direction, interface, member)) or | |
| 499 table.get('%s %s %s.*' % (idl_type, direction, interface)) or | |
| 500 table.get('%s %s' % (idl_type, direction))) | |
| 501 return None | |
| 502 | |
| 503 # ------------------------------------------------------------------------------ | |
| 504 | |
| 505 # Annotations to be placed on native members. The table is indexed by the IDL | |
| 506 # interface and member name, and by IDL return or field type name. Both are | |
| 507 # used to assemble the annotations: | |
| 508 # | |
| 509 # INTERFACE.MEMBER: annotations for member. | |
| 510 # +TYPE: add annotations only if there are member annotations. | |
| 511 # -TYPE: add annotations only if there are no member annotations. | |
| 512 # TYPE: add regardless of member annotations. | |
| 513 | |
| 514 dart2js_annotations = { | |
| 515 | |
| 516 'CanvasRenderingContext2D.createImageData': | |
| 517 "@Creates('ImageData|=Object')", | |
| 518 | |
| 519 'CanvasRenderingContext2D.getImageData': | |
| 520 "@Creates('ImageData|=Object')", | |
| 521 | |
| 522 'CanvasRenderingContext2D.webkitGetImageDataHD': | |
| 523 "@Creates('ImageData|=Object')", | |
| 524 | |
| 525 'CanvasRenderingContext2D.fillStyle': | |
| 526 "@Creates('String|CanvasGradient|CanvasPattern') " | |
| 527 "@Returns('String|CanvasGradient|CanvasPattern')", | |
| 528 | |
| 529 'CanvasRenderingContext2D.strokeStyle': | |
| 530 "@Creates('String|CanvasGradient|CanvasPattern') " | |
| 531 "@Returns('String|CanvasGradient|CanvasPattern')", | |
| 532 | |
| 533 # Methods returning Window can return a local window, or a cross-frame | |
| 534 # window (=Object) that needs wrapping. | |
| 535 'DOMWindow': | |
| 536 "@Creates('Window|=Object') @Returns('Window|=Object')", | |
| 537 | |
| 538 'DOMWindow.openDatabase': "@Creates('Database') @Creates('DatabaseSync')", | |
| 539 | |
| 540 # Cross-frame windows are EventTargets. | |
| 541 '-EventTarget': | |
| 542 "@Creates('EventTarget|=Object') @Returns('EventTarget|=Object')", | |
| 543 | |
| 544 # To be in callback with the browser-created Event, we had to have called | |
| 545 # addEventListener on the target, so we avoid | |
| 546 'Event.currentTarget': | |
| 547 "@Creates('Null') @Returns('EventTarget|=Object')", | |
| 548 | |
| 549 # Only nodes in the DOM bubble and have target !== currentTarget. | |
| 550 'Event.target': | |
| 551 "@Creates('Node') @Returns('EventTarget|=Object')", | |
| 552 | |
| 553 'MouseEvent.relatedTarget': | |
| 554 "@Creates('Node') @Returns('EventTarget|=Object')", | |
| 555 | |
| 556 # Touch targets are Elements in a Document, or the Document. | |
| 557 'Touch.target': | |
| 558 "@Creates('Element|Document') @Returns('Element|Document')", | |
| 559 | |
| 560 | |
| 561 'FileReader.result': "@Creates('String|ArrayBuffer|Null')", | |
| 562 | |
| 563 # Rather than have the result of an IDBRequest as a union over all possible | |
| 564 # results, we mark the result as instantiating any classes, and mark | |
| 565 # each operation with the classes that it could cause to be asynchronously | |
| 566 # instantiated. | |
| 567 'IDBRequest.result': "@Creates('Null')", | |
| 568 | |
| 569 # The source is usually a participant in the operation that generated the | |
| 570 # IDBRequest. | |
| 571 'IDBRequest.source': "@Creates('Null')", | |
| 572 | |
| 573 'IDBFactory.open': "@Creates('Database')", | |
| 574 | |
| 575 'IDBObjectStore.put': "@_annotation_Creates_IDBKey", | |
| 576 'IDBObjectStore.add': "@_annotation_Creates_IDBKey", | |
| 577 'IDBObjectStore.get': "@annotation_Creates_SerializedScriptValue", | |
| 578 'IDBObjectStore.openCursor': "@Creates('Cursor')", | |
| 579 | |
| 580 'IDBIndex.get': "@annotation_Creates_SerializedScriptValue", | |
| 581 'IDBIndex.getKey': | |
| 582 "@annotation_Creates_SerializedScriptValue " | |
| 583 # The source is the object store behind the index. | |
| 584 "@Creates('ObjectStore')", | |
| 585 'IDBIndex.openCursor': "@Creates('Cursor')", | |
| 586 'IDBIndex.openKeyCursor': "@Creates('Cursor')", | |
| 587 | |
| 588 'IDBCursorWithValue.value': | |
| 589 '@annotation_Creates_SerializedScriptValue ' | |
| 590 '@annotation_Returns_SerializedScriptValue', | |
| 591 | |
| 592 'IDBCursor.key': "@_annotation_Creates_IDBKey @_annotation_Returns_IDBKey", | |
| 593 | |
| 594 '+IDBRequest': "@Returns('Request') @Creates('Request')", | |
| 595 | |
| 596 '+IDBOpenDBRequest': "@Returns('Request') @Creates('Request')", | |
| 597 '+IDBVersionChangeRequest': "@Returns('Request') @Creates('Request')", | |
| 598 | |
| 599 | |
| 600 'MessageEvent.ports': "@Creates('=List')", | |
| 601 | |
| 602 'MessageEvent.data': | |
| 603 "@annotation_Creates_SerializedScriptValue " | |
| 604 "@annotation_Returns_SerializedScriptValue", | |
| 605 'PopStateEvent.state': | |
| 606 "@annotation_Creates_SerializedScriptValue " | |
| 607 "@annotation_Returns_SerializedScriptValue", | |
| 608 'SerializedScriptValue': | |
| 609 "@annotation_Creates_SerializedScriptValue " | |
| 610 "@annotation_Returns_SerializedScriptValue", | |
| 611 | |
| 612 'SQLResultSetRowList.item': "@Creates('=Object')", | |
| 613 | |
| 614 'XMLHttpRequest.response': | |
| 615 "@Creates('ArrayBuffer|Blob|Document|=Object|=List|String|num')", | |
| 616 } | |
| 617 | |
| 618 _indexed_db_annotations = [ | |
| 619 "@SupportedBrowser(SupportedBrowser.CHROME, '23.0')", | |
| 620 "@SupportedBrowser(SupportedBrowser.FIREFOX, '15.0')", | |
| 621 "@SupportedBrowser(SupportedBrowser.IE, '10.0')", | |
| 622 "@Experimental()", | |
| 623 ] | |
| 624 | |
| 625 # Annotations to be placed on generated members. | |
| 626 # The table is indexed as: | |
| 627 # INTERFACE: annotations to be added to the interface declaration | |
| 628 # INTERFACE.MEMBER: annotation to be added to the member declaration | |
| 629 dart_annotations = { | |
| 630 'DOMWindow.indexedDB': _indexed_db_annotations, | |
| 631 'IDBFactory': _indexed_db_annotations, | |
| 632 'IDBDatabase': _indexed_db_annotations, | |
| 633 'WorkerContext.indexedDB': _indexed_db_annotations, | |
| 634 } | |
| 635 | |
| 636 def FindCommonAnnotations(interface_name, member_name=None): | |
| 637 """ Finds annotations common between dart2js and dartium. | |
| 638 """ | |
| 639 if member_name: | |
| 640 return dart_annotations.get('%s.%s' % (interface_name, member_name)) | |
| 641 else: | |
| 642 return dart_annotations.get(interface_name) | |
| 643 | |
| 644 def FindDart2JSAnnotations(idl_type, interface_name, member_name): | |
| 645 """ Finds all annotations for Dart2JS members- including annotations for | |
| 646 both dart2js and dartium. | |
| 647 """ | |
| 648 annotations = FindCommonAnnotations(interface_name, member_name) | |
| 649 if annotations: | |
| 650 annotations = ' '.join(annotations) | |
| 651 | |
| 652 ann2 = _FindDart2JSSpecificAnnotations(idl_type, interface_name, member_name) | |
| 653 if ann2: | |
| 654 if annotations: | |
| 655 annotations = annotations + ' ' + ann2 | |
| 656 else: | |
| 657 annotations = ann2 | |
| 658 return annotations | |
| 659 | |
| 660 def _FindDart2JSSpecificAnnotations(idl_type, interface_name, member_name): | |
| 661 """ Finds dart2js-specific annotations. This does not include ones shared with | |
| 662 dartium. | |
| 663 """ | |
| 664 ann1 = dart2js_annotations.get("%s.%s" % (interface_name, member_name)) | |
| 665 if ann1: | |
| 666 ann2 = dart2js_annotations.get('+' + idl_type) | |
| 667 if ann2: | |
| 668 return ann2 + ' ' + ann1 | |
| 669 ann2 = dart2js_annotations.get(idl_type) | |
| 670 if ann2: | |
| 671 return ann2 + ' ' + ann1 | |
| 672 return ann1 | |
| 673 | |
| 674 ann2 = dart2js_annotations.get('-' + idl_type) | |
| 675 if ann2: | |
| 676 return ann2 | |
| 677 ann2 = dart2js_annotations.get(idl_type) | |
| 678 return ann2 | |
| 679 | |
| 680 # ------------------------------------------------------------------------------ | |
| 681 | |
| 682 class IDLTypeInfo(object): | |
| 683 def __init__(self, idl_type, data): | |
| 684 self._idl_type = idl_type | |
| 685 self._data = data | |
| 686 | |
| 687 def idl_type(self): | |
| 688 return self._idl_type | |
| 689 | |
| 690 def dart_type(self): | |
| 691 return self._data.dart_type or self._idl_type | |
| 692 | |
| 693 def narrow_dart_type(self): | |
| 694 return self.dart_type() | |
| 695 | |
| 696 def interface_name(self): | |
| 697 raise NotImplementedError() | |
| 698 | |
| 699 def implementation_name(self): | |
| 700 raise NotImplementedError() | |
| 701 | |
| 702 def has_generated_interface(self): | |
| 703 raise NotImplementedError() | |
| 704 | |
| 705 def list_item_type(self): | |
| 706 raise NotImplementedError() | |
| 707 | |
| 708 def is_typed_array(self): | |
| 709 raise NotImplementedError() | |
| 710 | |
| 711 def merged_interface(self): | |
| 712 return None | |
| 713 | |
| 714 def merged_into(self): | |
| 715 return None | |
| 716 | |
| 717 def native_type(self): | |
| 718 return self._data.native_type or self._idl_type | |
| 719 | |
| 720 def bindings_class(self): | |
| 721 return 'Dart%s' % self.idl_type() | |
| 722 | |
| 723 def vector_to_dart_template_parameter(self): | |
| 724 return self.bindings_class() | |
| 725 | |
| 726 def requires_v8_scope(self): | |
| 727 return self._data.requires_v8_scope | |
| 728 | |
| 729 def to_native_info(self, idl_node, interface_name): | |
| 730 cls = self.bindings_class() | |
| 731 | |
| 732 if 'Callback' in idl_node.ext_attrs: | |
| 733 return '%s', 'RefPtr<%s>' % self.native_type(), cls, 'create' | |
| 734 | |
| 735 if self.custom_to_native(): | |
| 736 type = 'RefPtr<%s>' % self.native_type() | |
| 737 argument_expression_template = '%s.get()' | |
| 738 else: | |
| 739 type = '%s*' % self.native_type() | |
| 740 if isinstance(self, SVGTearOffIDLTypeInfo) and not interface_name.endswith
('List'): | |
| 741 argument_expression_template = '%s->propertyReference()' | |
| 742 else: | |
| 743 argument_expression_template = '%s' | |
| 744 return argument_expression_template, type, cls, 'toNative' | |
| 745 | |
| 746 def pass_native_by_ref(self): return False | |
| 747 | |
| 748 def custom_to_native(self): | |
| 749 return self._data.custom_to_native | |
| 750 | |
| 751 def parameter_type(self): | |
| 752 return '%s*' % self.native_type() | |
| 753 | |
| 754 def webcore_includes(self): | |
| 755 WTF_INCLUDES = [ | |
| 756 'ArrayBuffer', | |
| 757 'ArrayBufferView', | |
| 758 'Float32Array', | |
| 759 'Float64Array', | |
| 760 'Int8Array', | |
| 761 'Int16Array', | |
| 762 'Int32Array', | |
| 763 'Uint8Array', | |
| 764 'Uint16Array', | |
| 765 'Uint32Array', | |
| 766 'Uint8ClampedArray', | |
| 767 ] | |
| 768 | |
| 769 if self._idl_type in WTF_INCLUDES: | |
| 770 return ['<wtf/%s.h>' % self.native_type()] | |
| 771 | |
| 772 if not self._idl_type.startswith('SVG'): | |
| 773 return ['"%s.h"' % self.native_type()] | |
| 774 | |
| 775 if self._idl_type in ['SVGNumber', 'SVGPoint']: | |
| 776 return ['"SVGPropertyTearOff.h"'] | |
| 777 if self._idl_type.startswith('SVGPathSeg'): | |
| 778 include = self._idl_type.replace('Abs', '').replace('Rel', '') | |
| 779 else: | |
| 780 include = self._idl_type | |
| 781 return ['"%s.h"' % include] + _svg_supplemental_includes | |
| 782 | |
| 783 def receiver(self): | |
| 784 return 'receiver->' | |
| 785 | |
| 786 def conversion_includes(self): | |
| 787 includes = [self._idl_type] + (self._data.conversion_includes or []) | |
| 788 return ['"Dart%s.h"' % include for include in includes] | |
| 789 | |
| 790 def to_dart_conversion(self, value, interface_name=None, attributes=None): | |
| 791 return 'Dart%s::toDart(%s)' % (self._idl_type, value) | |
| 792 | |
| 793 def custom_to_dart(self): | |
| 794 return self._data.custom_to_dart | |
| 795 | |
| 796 | |
| 797 class InterfaceIDLTypeInfo(IDLTypeInfo): | |
| 798 def __init__(self, idl_type, data, dart_interface_name, type_registry): | |
| 799 super(InterfaceIDLTypeInfo, self).__init__(idl_type, data) | |
| 800 self._dart_interface_name = dart_interface_name | |
| 801 self._type_registry = type_registry | |
| 802 | |
| 803 def dart_type(self): | |
| 804 if self.list_item_type() and not self.has_generated_interface(): | |
| 805 return 'List<%s>' % self._type_registry.TypeInfo(self._data.item_type).dar
t_type() | |
| 806 return self._data.dart_type or self._dart_interface_name | |
| 807 | |
| 808 def narrow_dart_type(self): | |
| 809 if self.list_item_type(): | |
| 810 return self.implementation_name() | |
| 811 # TODO(podivilov): only primitive and collection types should override | |
| 812 # dart_type. | |
| 813 if self._data.dart_type != None: | |
| 814 return self.dart_type() | |
| 815 if IsPureInterface(self.idl_type()): | |
| 816 return self.idl_type() | |
| 817 return self.interface_name() | |
| 818 | |
| 819 def interface_name(self): | |
| 820 return self._dart_interface_name | |
| 821 | |
| 822 def implementation_name(self): | |
| 823 implementation_name = self._dart_interface_name | |
| 824 if self.merged_into(): | |
| 825 implementation_name = '_%s_Merged' % implementation_name | |
| 826 | |
| 827 if not self.has_generated_interface(): | |
| 828 implementation_name = '_%s' % implementation_name | |
| 829 | |
| 830 return implementation_name | |
| 831 | |
| 832 def has_generated_interface(self): | |
| 833 return not self._data.suppress_interface | |
| 834 | |
| 835 def list_item_type(self): | |
| 836 return self._data.item_type | |
| 837 | |
| 838 def is_typed_array(self): | |
| 839 return self._data.is_typed_array | |
| 840 | |
| 841 def merged_interface(self): | |
| 842 # All constants, attributes, and operations of merged interface should be | |
| 843 # added to this interface. Merged idl interface does not have corresponding | |
| 844 # Dart generated interface, and all references to merged idl interface | |
| 845 # (e.g. parameter types, return types, parent interfaces) should be replaced | |
| 846 # with this interface. There are two important restrictions: | |
| 847 # 1) Merged and target interfaces shouldn't have common members, otherwise | |
| 848 # there would be duplicated declarations in generated Dart code. | |
| 849 # 2) Merged interface should be direct child of target interface, so the | |
| 850 # children of merged interface are not affected by the merge. | |
| 851 # As a consequence, target interface implementation and its direct children | |
| 852 # interface implementations should implement merged attribute accessors and | |
| 853 # operations. For example, SVGElement and Element implementation classes | |
| 854 # should implement HTMLElement.insertAdjacentElement(), | |
| 855 # HTMLElement.innerHTML, etc. | |
| 856 return self._data.merged_interface | |
| 857 | |
| 858 def merged_into(self): | |
| 859 return self._data.merged_into | |
| 860 | |
| 861 | |
| 862 class CallbackIDLTypeInfo(IDLTypeInfo): | |
| 863 def __init__(self, idl_type, data): | |
| 864 super(CallbackIDLTypeInfo, self).__init__(idl_type, data) | |
| 865 | |
| 866 | |
| 867 class SequenceIDLTypeInfo(IDLTypeInfo): | |
| 868 def __init__(self, idl_type, data, item_info): | |
| 869 super(SequenceIDLTypeInfo, self).__init__(idl_type, data) | |
| 870 self._item_info = item_info | |
| 871 | |
| 872 def dart_type(self): | |
| 873 return 'List<%s>' % self._item_info.dart_type() | |
| 874 | |
| 875 def interface_name(self): | |
| 876 return self.dart_type() | |
| 877 | |
| 878 def implementation_name(self): | |
| 879 return self.dart_type() | |
| 880 | |
| 881 def vector_to_dart_template_parameter(self): | |
| 882 raise Exception('sequences of sequences are not supported yet') | |
| 883 | |
| 884 def to_native_info(self, idl_node, interface_name): | |
| 885 item_native_type = self._item_info.vector_to_dart_template_parameter() | |
| 886 return '%s', 'Vector<%s>' % item_native_type, 'DartUtilities', 'toNativeVect
or<%s>' % item_native_type | |
| 887 | |
| 888 def pass_native_by_ref(self): return True | |
| 889 | |
| 890 def to_dart_conversion(self, value, interface_name=None, attributes=None): | |
| 891 return 'DartDOMWrapper::vectorToDart<%s>(%s)' % (self._item_info.vector_to_d
art_template_parameter(), value) | |
| 892 | |
| 893 def conversion_includes(self): | |
| 894 return self._item_info.conversion_includes() | |
| 895 | |
| 896 | |
| 897 class DOMStringArrayTypeInfo(SequenceIDLTypeInfo): | |
| 898 def __init__(self, data, item_info): | |
| 899 super(DOMStringArrayTypeInfo, self).__init__('DOMString[]', data, item_info) | |
| 900 | |
| 901 def to_native_info(self, idl_node, interface_name): | |
| 902 return '%s', 'RefPtr<DOMStringList>', 'DartDOMStringList', 'toNative' | |
| 903 | |
| 904 def pass_native_by_ref(self): return False | |
| 905 | |
| 906 | |
| 907 class PrimitiveIDLTypeInfo(IDLTypeInfo): | |
| 908 def __init__(self, idl_type, data): | |
| 909 super(PrimitiveIDLTypeInfo, self).__init__(idl_type, data) | |
| 910 | |
| 911 def vector_to_dart_template_parameter(self): | |
| 912 # Ugly hack. Usually IDLs floats are treated as C++ doubles, however | |
| 913 # sequence<float> should map to Vector<float> | |
| 914 if self.idl_type() == 'float': return 'float' | |
| 915 return self.native_type() | |
| 916 | |
| 917 def to_native_info(self, idl_node, interface_name): | |
| 918 type = self.native_type() | |
| 919 if type == 'SerializedScriptValue': | |
| 920 type = 'RefPtr<%s>' % type | |
| 921 if type == 'String': | |
| 922 type = 'DartStringAdapter' | |
| 923 return '%s', type, 'DartUtilities', 'dartTo%s' % self._capitalized_native_ty
pe() | |
| 924 | |
| 925 def parameter_type(self): | |
| 926 if self.native_type() == 'String': | |
| 927 return 'const String&' | |
| 928 return self.native_type() | |
| 929 | |
| 930 def conversion_includes(self): | |
| 931 return [] | |
| 932 | |
| 933 def to_dart_conversion(self, value, interface_name=None, attributes=None): | |
| 934 function_name = self._capitalized_native_type() | |
| 935 function_name = function_name[0].lower() + function_name[1:] | |
| 936 function_name = 'DartUtilities::%sToDart' % function_name | |
| 937 if attributes and 'TreatReturnedNullStringAs' in attributes: | |
| 938 function_name += 'WithNullCheck' | |
| 939 return '%s(%s)' % (function_name, value) | |
| 940 | |
| 941 def webcore_getter_name(self): | |
| 942 return self._data.webcore_getter_name | |
| 943 | |
| 944 def webcore_setter_name(self): | |
| 945 return self._data.webcore_setter_name | |
| 946 | |
| 947 def _capitalized_native_type(self): | |
| 948 return re.sub(r'(^| )([a-z])', lambda x: x.group(2).upper(), self.native_typ
e()) | |
| 949 | |
| 950 | |
| 951 class SVGTearOffIDLTypeInfo(InterfaceIDLTypeInfo): | |
| 952 def __init__(self, idl_type, data, interface_name, type_registry): | |
| 953 super(SVGTearOffIDLTypeInfo, self).__init__( | |
| 954 idl_type, data, interface_name, type_registry) | |
| 955 | |
| 956 def native_type(self): | |
| 957 if self._data.native_type: | |
| 958 return self._data.native_type | |
| 959 tear_off_type = 'SVGPropertyTearOff' | |
| 960 if self._idl_type.endswith('List'): | |
| 961 tear_off_type = 'SVGListPropertyTearOff' | |
| 962 return '%s<%s>' % (tear_off_type, self._idl_type) | |
| 963 | |
| 964 def receiver(self): | |
| 965 if self._idl_type.endswith('List'): | |
| 966 return 'receiver->' | |
| 967 return 'receiver->propertyReference().' | |
| 968 | |
| 969 def to_dart_conversion(self, value, interface_name, attributes): | |
| 970 svg_primitive_types = ['SVGAngle', 'SVGLength', 'SVGMatrix', | |
| 971 'SVGNumber', 'SVGPoint', 'SVGRect', 'SVGTransform'] | |
| 972 conversion_cast = '%s::create(%s)' | |
| 973 if interface_name.startswith('SVGAnimated'): | |
| 974 conversion_cast = 'static_cast<%s*>(%s)' | |
| 975 elif self.idl_type() == 'SVGStringList': | |
| 976 conversion_cast = '%s::create(receiver, %s)' | |
| 977 elif interface_name.endswith('List'): | |
| 978 conversion_cast = 'static_cast<%s*>(%s.get())' | |
| 979 elif self.idl_type() in svg_primitive_types: | |
| 980 conversion_cast = '%s::create(%s)' | |
| 981 else: | |
| 982 conversion_cast = 'static_cast<%s*>(%s)' | |
| 983 conversion_cast = conversion_cast % (self.native_type(), value) | |
| 984 return 'Dart%s::toDart(%s)' % (self._idl_type, conversion_cast) | |
| 985 | |
| 986 def argument_expression(self, name, interface_name): | |
| 987 return name if interface_name.endswith('List') else '%s->propertyReference()
' % name | |
| 988 | |
| 989 | |
| 990 class TypeData(object): | |
| 991 def __init__(self, clazz, dart_type=None, native_type=None, | |
| 992 merged_interface=None, merged_into=None, | |
| 993 custom_to_dart=None, custom_to_native=None, | |
| 994 conversion_includes=None, | |
| 995 webcore_getter_name='getAttribute', | |
| 996 webcore_setter_name='setAttribute', | |
| 997 requires_v8_scope=False, | |
| 998 item_type=None, suppress_interface=False, is_typed_array=False): | |
| 999 self.clazz = clazz | |
| 1000 self.dart_type = dart_type | |
| 1001 self.native_type = native_type | |
| 1002 self.merged_interface = merged_interface | |
| 1003 self.merged_into = merged_into | |
| 1004 self.custom_to_dart = custom_to_dart | |
| 1005 self.custom_to_native = custom_to_native | |
| 1006 self.conversion_includes = conversion_includes | |
| 1007 self.webcore_getter_name = webcore_getter_name | |
| 1008 self.webcore_setter_name = webcore_setter_name | |
| 1009 self.requires_v8_scope = requires_v8_scope | |
| 1010 self.item_type = item_type | |
| 1011 self.suppress_interface = suppress_interface | |
| 1012 self.is_typed_array = is_typed_array | |
| 1013 | |
| 1014 | |
| 1015 def TypedArrayTypeData(item_type): | |
| 1016 return TypeData(clazz='Interface', item_type=item_type, is_typed_array=True) | |
| 1017 | |
| 1018 | |
| 1019 _idl_type_registry = { | |
| 1020 'boolean': TypeData(clazz='Primitive', dart_type='bool', native_type='bool', | |
| 1021 webcore_getter_name='hasAttribute', | |
| 1022 webcore_setter_name='setBooleanAttribute'), | |
| 1023 'byte': TypeData(clazz='Primitive', dart_type='int', native_type='int'), | |
| 1024 'octet': TypeData(clazz='Primitive', dart_type='int', native_type='int'), | |
| 1025 'short': TypeData(clazz='Primitive', dart_type='int', native_type='int'), | |
| 1026 'unsigned short': TypeData(clazz='Primitive', dart_type='int', | |
| 1027 native_type='int'), | |
| 1028 'int': TypeData(clazz='Primitive', dart_type='int'), | |
| 1029 'unsigned int': TypeData(clazz='Primitive', dart_type='int', | |
| 1030 native_type='unsigned'), | |
| 1031 'long': TypeData(clazz='Primitive', dart_type='int', native_type='int', | |
| 1032 webcore_getter_name='getIntegralAttribute', | |
| 1033 webcore_setter_name='setIntegralAttribute'), | |
| 1034 'unsigned long': TypeData(clazz='Primitive', dart_type='int', | |
| 1035 native_type='unsigned', | |
| 1036 webcore_getter_name='getUnsignedIntegralAttribute'
, | |
| 1037 webcore_setter_name='setUnsignedIntegralAttribute'
), | |
| 1038 'long long': TypeData(clazz='Primitive', dart_type='int'), | |
| 1039 'unsigned long long': TypeData(clazz='Primitive', dart_type='int'), | |
| 1040 'float': TypeData(clazz='Primitive', dart_type='num', native_type='double'), | |
| 1041 'double': TypeData(clazz='Primitive', dart_type='num'), | |
| 1042 | |
| 1043 'any': TypeData(clazz='Primitive', dart_type='Object', native_type='ScriptVa
lue', requires_v8_scope=True), | |
| 1044 'Array': TypeData(clazz='Primitive', dart_type='List'), | |
| 1045 'custom': TypeData(clazz='Primitive', dart_type='dynamic'), | |
| 1046 'Date': TypeData(clazz='Primitive', dart_type='Date', native_type='double'), | |
| 1047 'DOMObject': TypeData(clazz='Primitive', dart_type='Object', native_type='Sc
riptValue'), | |
| 1048 'DOMString': TypeData(clazz='Primitive', dart_type='String', native_type='St
ring'), | |
| 1049 # TODO(vsm): This won't actually work until we convert the Map to | |
| 1050 # a native JS Map for JS DOM. | |
| 1051 'Dictionary': TypeData(clazz='Primitive', dart_type='Map', requires_v8_scope
=True), | |
| 1052 # TODO(sra): Flags is really a dictionary: {create:bool, exclusive:bool} | |
| 1053 # http://dev.w3.org/2009/dap/file-system/file-dir-sys.html#the-flags-interfa
ce | |
| 1054 'Flags': TypeData(clazz='Primitive', dart_type='Object'), | |
| 1055 'DOMTimeStamp': TypeData(clazz='Primitive', dart_type='int', native_type='un
signed long long'), | |
| 1056 'object': TypeData(clazz='Primitive', dart_type='Object', native_type='Scrip
tValue'), | |
| 1057 'ObjectArray': TypeData(clazz='Primitive', dart_type='List'), | |
| 1058 'PositionOptions': TypeData(clazz='Primitive', dart_type='Object'), | |
| 1059 # TODO(sra): Come up with some meaningful name so that where this appears in | |
| 1060 # the documentation, the user is made aware that only a limited subset of | |
| 1061 # serializable types are actually permitted. | |
| 1062 'SerializedScriptValue': TypeData(clazz='Primitive', dart_type='dynamic'), | |
| 1063 # TODO(sra): Flags is really a dictionary: {create:bool, exclusive:bool} | |
| 1064 # http://dev.w3.org/2009/dap/file-system/file-dir-sys.html#the-flags-interfa
ce | |
| 1065 'WebKitFlags': TypeData(clazz='Primitive', dart_type='Object'), | |
| 1066 | |
| 1067 'sequence': TypeData(clazz='Primitive', dart_type='List'), | |
| 1068 'void': TypeData(clazz='Primitive', dart_type='void'), | |
| 1069 | |
| 1070 'CSSRule': TypeData(clazz='Interface', conversion_includes=['CSSImportRule']
), | |
| 1071 'DOMException': TypeData(clazz='Interface', native_type='DOMCoreException'), | |
| 1072 'DOMStringMap': TypeData(clazz='Interface', dart_type='Map<String, String>')
, | |
| 1073 'DOMWindow': TypeData(clazz='Interface', custom_to_dart=True), | |
| 1074 'Element': TypeData(clazz='Interface', merged_interface='HTMLElement', | |
| 1075 custom_to_dart=True), | |
| 1076 'EventListener': TypeData(clazz='Interface', custom_to_native=True), | |
| 1077 'EventTarget': TypeData(clazz='Interface', custom_to_native=True), | |
| 1078 'HTMLElement': TypeData(clazz='Interface', merged_into='Element', | |
| 1079 custom_to_dart=True), | |
| 1080 'IDBAny': TypeData(clazz='Interface', dart_type='dynamic', custom_to_native=
True), | |
| 1081 'IDBKey': TypeData(clazz='Interface', dart_type='dynamic', custom_to_native=
True), | |
| 1082 'MutationRecordArray': TypeData(clazz='Interface', # C++ pass by pointer. | |
| 1083 native_type='MutationRecordArray', dart_type='List<MutationRecord>'), | |
| 1084 'StyleSheet': TypeData(clazz='Interface', conversion_includes=['CSSStyleShee
t']), | |
| 1085 'SVGElement': TypeData(clazz='Interface', custom_to_dart=True), | |
| 1086 | |
| 1087 'ClientRectList': TypeData(clazz='Interface', | |
| 1088 item_type='ClientRect', suppress_interface=True), | |
| 1089 'CSSRuleList': TypeData(clazz='Interface', | |
| 1090 item_type='CSSRule', suppress_interface=True), | |
| 1091 'CSSValueList': TypeData(clazz='Interface', | |
| 1092 item_type='CSSValue', suppress_interface=True), | |
| 1093 'DOMMimeTypeArray': TypeData(clazz='Interface', item_type='DOMMimeType'), | |
| 1094 'DOMPluginArray': TypeData(clazz='Interface', item_type='DOMPlugin'), | |
| 1095 'DOMStringList': TypeData(clazz='Interface', item_type='DOMString', | |
| 1096 dart_type='List<String>', custom_to_native=True), | |
| 1097 'EntryArray': TypeData(clazz='Interface', item_type='Entry', | |
| 1098 suppress_interface=True), | |
| 1099 'EntryArraySync': TypeData(clazz='Interface', item_type='EntrySync', | |
| 1100 suppress_interface=True), | |
| 1101 'FileList': TypeData(clazz='Interface', item_type='File', | |
| 1102 dart_type='List<File>'), | |
| 1103 'GamepadList': TypeData(clazz='Interface', item_type='Gamepad', | |
| 1104 suppress_interface=True), | |
| 1105 'HTMLAllCollection': TypeData(clazz='Interface', item_type='Node'), | |
| 1106 'HTMLCollection': TypeData(clazz='Interface', item_type='Node'), | |
| 1107 'MediaStreamList': TypeData(clazz='Interface', | |
| 1108 item_type='MediaStream', suppress_interface=True), | |
| 1109 'NamedNodeMap': TypeData(clazz='Interface', item_type='Node'), | |
| 1110 'NodeList': TypeData(clazz='Interface', item_type='Node', | |
| 1111 suppress_interface=False, dart_type='List<Node>'), | |
| 1112 'SVGElementInstanceList': TypeData(clazz='Interface', | |
| 1113 item_type='SVGElementInstance', suppress_interface=True), | |
| 1114 'SourceBufferList': TypeData(clazz='Interface', item_type='SourceBuffer'), | |
| 1115 'SpeechGrammarList': TypeData(clazz='Interface', item_type='SpeechGrammar'), | |
| 1116 'SpeechInputResultList': TypeData(clazz='Interface', | |
| 1117 item_type='SpeechInputResult', suppress_interface=True), | |
| 1118 'SpeechRecognitionResultList': TypeData(clazz='Interface', | |
| 1119 item_type='SpeechRecognitionResult', suppress_interface=True), | |
| 1120 'SQLResultSetRowList': TypeData(clazz='Interface', item_type='Dictionary'), | |
| 1121 'StyleSheetList': TypeData(clazz='Interface', | |
| 1122 item_type='StyleSheet', suppress_interface=True), | |
| 1123 'TextTrackCueList': TypeData(clazz='Interface', item_type='TextTrackCue'), | |
| 1124 'TextTrackList': TypeData(clazz='Interface', item_type='TextTrack'), | |
| 1125 'TouchList': TypeData(clazz='Interface', item_type='Touch'), | |
| 1126 'WebKitAnimationList': TypeData(clazz='Interface', | |
| 1127 item_type='WebKitAnimation', suppress_interface=True), | |
| 1128 | |
| 1129 'Float32Array': TypedArrayTypeData('double'), | |
| 1130 'Float64Array': TypedArrayTypeData('double'), | |
| 1131 'Int8Array': TypedArrayTypeData('int'), | |
| 1132 'Int16Array': TypedArrayTypeData('int'), | |
| 1133 'Int32Array': TypedArrayTypeData('int'), | |
| 1134 'Uint8Array': TypedArrayTypeData('int'), | |
| 1135 'Uint16Array': TypedArrayTypeData('int'), | |
| 1136 'Uint32Array': TypedArrayTypeData('int'), | |
| 1137 | |
| 1138 'SVGAngle': TypeData(clazz='SVGTearOff'), | |
| 1139 'SVGLength': TypeData(clazz='SVGTearOff'), | |
| 1140 'SVGLengthList': TypeData(clazz='SVGTearOff', item_type='SVGLength'), | |
| 1141 'SVGMatrix': TypeData(clazz='SVGTearOff'), | |
| 1142 'SVGNumber': TypeData(clazz='SVGTearOff', native_type='SVGPropertyTearOff<fl
oat>'), | |
| 1143 'SVGNumberList': TypeData(clazz='SVGTearOff', item_type='SVGNumber'), | |
| 1144 'SVGPathSegList': TypeData(clazz='SVGTearOff', item_type='SVGPathSeg', | |
| 1145 native_type='SVGPathSegListPropertyTearOff'), | |
| 1146 'SVGPoint': TypeData(clazz='SVGTearOff', native_type='SVGPropertyTearOff<Flo
atPoint>'), | |
| 1147 'SVGPointList': TypeData(clazz='SVGTearOff'), | |
| 1148 'SVGPreserveAspectRatio': TypeData(clazz='SVGTearOff'), | |
| 1149 'SVGRect': TypeData(clazz='SVGTearOff', native_type='SVGPropertyTearOff<Floa
tRect>'), | |
| 1150 'SVGStringList': TypeData(clazz='SVGTearOff', item_type='DOMString', | |
| 1151 native_type='SVGStaticListPropertyTearOff<SVGStringList>'), | |
| 1152 'SVGTransform': TypeData(clazz='SVGTearOff'), | |
| 1153 'SVGTransformList': TypeData(clazz='SVGTearOff', item_type='SVGTransform', | |
| 1154 native_type='SVGTransformListPropertyTearOff'), | |
| 1155 } | |
| 1156 | |
| 1157 _svg_supplemental_includes = [ | |
| 1158 '"SVGAnimatedPropertyTearOff.h"', | |
| 1159 '"SVGAnimatedListPropertyTearOff.h"', | |
| 1160 '"SVGStaticListPropertyTearOff.h"', | |
| 1161 '"SVGAnimatedListPropertyTearOff.h"', | |
| 1162 '"SVGTransformListPropertyTearOff.h"', | |
| 1163 '"SVGPathSegListPropertyTearOff.h"', | |
| 1164 ] | |
| 1165 | |
| 1166 class TypeRegistry(object): | |
| 1167 def __init__(self, database, renamer=None): | |
| 1168 self._database = database | |
| 1169 self._renamer = renamer | |
| 1170 self._cache = {} | |
| 1171 | |
| 1172 def TypeInfo(self, type_name): | |
| 1173 if not type_name in self._cache: | |
| 1174 self._cache[type_name] = self._TypeInfo(type_name) | |
| 1175 return self._cache[type_name] | |
| 1176 | |
| 1177 def DartType(self, type_name): | |
| 1178 return self.TypeInfo(type_name).dart_type() | |
| 1179 | |
| 1180 def _TypeInfo(self, type_name): | |
| 1181 match = re.match(r'(?:sequence<(\w+)>|(\w+)\[\])$', type_name) | |
| 1182 if match: | |
| 1183 if type_name == 'DOMString[]': | |
| 1184 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt
ring')) | |
| 1185 item_info = self.TypeInfo(match.group(1) or match.group(2)) | |
| 1186 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) | |
| 1187 | |
| 1188 if not type_name in _idl_type_registry: | |
| 1189 interface = self._database.GetInterface(type_name) | |
| 1190 if 'Callback' in interface.ext_attrs: | |
| 1191 return CallbackIDLTypeInfo(type_name, TypeData('Callback', | |
| 1192 self._renamer.DartifyTypeName(type_name))) | |
| 1193 return InterfaceIDLTypeInfo( | |
| 1194 type_name, | |
| 1195 TypeData('Interface'), | |
| 1196 self._renamer.RenameInterface(interface), | |
| 1197 self) | |
| 1198 | |
| 1199 type_data = _idl_type_registry.get(type_name) | |
| 1200 | |
| 1201 if type_data.clazz == 'Interface': | |
| 1202 if self._database.HasInterface(type_name): | |
| 1203 dart_interface_name = self._renamer.RenameInterface( | |
| 1204 self._database.GetInterface(type_name)) | |
| 1205 else: | |
| 1206 dart_interface_name = self._renamer.DartifyTypeName(type_name) | |
| 1207 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name, | |
| 1208 self) | |
| 1209 | |
| 1210 if type_data.clazz == 'SVGTearOff': | |
| 1211 dart_interface_name = self._renamer.RenameInterface( | |
| 1212 self._database.GetInterface(type_name)) | |
| 1213 return SVGTearOffIDLTypeInfo( | |
| 1214 type_name, type_data, dart_interface_name, self) | |
| 1215 | |
| 1216 class_name = '%sIDLTypeInfo' % type_data.clazz | |
| 1217 return globals()[class_name](type_name, type_data) | |
| OLD | NEW |