Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: sdk/lib/html/scripts/systemhtml.py

Issue 11364134: Merge libv1. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Reupload due to error Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/html/scripts/htmlrenamer.py ('k') | sdk/lib/html/scripts/systemnative.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 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 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. 4 # BSD-style license that can be found in the LICENSE file.
5 5
6 """This module provides shared functionality for the system to generate 6 """This module provides shared functionality for the system to generate
7 Dart:html APIs from the IDL database.""" 7 Dart:html APIs from the IDL database."""
8 8
9 import emitter 9 import emitter
10 import os 10 import os
11 from generator import * 11 from generator import *
12 from htmldartgenerator import *
12 13
13 _js_custom_members = set([ 14 _js_custom_members = set([
14 'AudioBufferSourceNode.start', 15 'AudioBufferSourceNode.start',
15 'AudioBufferSourceNode.stop', 16 'AudioBufferSourceNode.stop',
16 'AudioContext.createGain', 17 'AudioContext.createGain',
17 'AudioContext.createScriptProcessor', 18 'AudioContext.createScriptProcessor',
18 'CSSStyleDeclaration.setProperty', 19 'CSSStyleDeclaration.setProperty',
19 'Element.insertAdjacentElement', 20 'Element.insertAdjacentElement',
20 'Element.insertAdjacentHTML', 21 'Element.insertAdjacentHTML',
21 'Element.insertAdjacentText', 22 'Element.insertAdjacentText',
22 'Element.remove', 23 'Element.remove',
23 'ElementEvents.mouseWheel', 24 'ElementEvents.mouseWheel',
24 'IDBDatabase.transaction', 25 'IDBDatabase.transaction',
25 'MouseEvent.offsetX', 26 'MouseEvent.offsetX',
26 'MouseEvent.offsetY', 27 'MouseEvent.offsetY',
27 'SelectElement.options', 28 'SelectElement.options',
28 'SelectElement.selectedOptions', 29 'SelectElement.selectedOptions',
29 'TableElement.createTBody', 30 'TableElement.createTBody',
30 'LocalWindow.cancelAnimationFrame', 31 'LocalWindow.cancelAnimationFrame',
31 'LocalWindow.document', 32 'LocalWindow.document',
32 'LocalWindow.indexedDB', 33 'LocalWindow.indexedDB',
33 'LocalWindow.location', 34 'LocalWindow.location',
34 'LocalWindow.open', 35 'LocalWindow.open',
35 'LocalWindow.requestAnimationFrame', 36 'LocalWindow.requestAnimationFrame',
36 'LocalWindow.webkitCancelAnimationFrame', 37 'LocalWindow.webkitCancelAnimationFrame',
37 'LocalWindow.webkitRequestAnimationFrame', 38 'LocalWindow.webkitRequestAnimationFrame',
39 'Url.createObjectURL',
40 'Url.revokeObjectURL',
38 'WheelEvent.wheelDeltaX', 41 'WheelEvent.wheelDeltaX',
39 'WheelEvent.wheelDeltaY', 42 'WheelEvent.wheelDeltaY',
40 ]) 43 ])
41 44
42 45
46 # Classes that offer only static methods, and therefore we should suppress
47 # constructor creation.
48 _static_classes = set(['Url'])
49
43 # Types that are accessible cross-frame in a limited fashion. 50 # Types that are accessible cross-frame in a limited fashion.
44 # In these cases, the base type (e.g., Window) provides restricted access 51 # In these cases, the base type (e.g., Window) provides restricted access
45 # while the subtype (e.g., LocalWindow) provides full access to the 52 # while the subtype (e.g., LocalWindow) provides full access to the
46 # corresponding objects if there are from the same frame. 53 # corresponding objects if there are from the same frame.
47 _secure_base_types = { 54 _secure_base_types = {
48 'LocalWindow': 'Window', 55 'LocalWindow': 'Window',
49 'LocalLocation': 'Location', 56 'LocalLocation': 'Location',
50 'LocalHistory': 'History', 57 'LocalHistory': 'History',
51 } 58 }
52 59
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 return infos 182 return infos
176 183
177 def EmitHtmlElementFactoryConstructors(emitter, infos, typename, class_name, 184 def EmitHtmlElementFactoryConstructors(emitter, infos, typename, class_name,
178 rename_type): 185 rename_type):
179 for info in infos: 186 for info in infos:
180 constructor_info = info.ConstructorInfo(typename) 187 constructor_info = info.ConstructorInfo(typename)
181 188
182 inits = emitter.Emit( 189 inits = emitter.Emit(
183 '\n' 190 '\n'
184 ' static $RETURN_TYPE $CONSTRUCTOR($PARAMS) {\n' 191 ' static $RETURN_TYPE $CONSTRUCTOR($PARAMS) {\n'
185 ' $CLASS _e = _document.$dom_createElement("$TAG");\n' 192 ' $CLASS _e = document.$dom_createElement("$TAG");\n'
186 '$!INITS' 193 '$!INITS'
187 ' return _e;\n' 194 ' return _e;\n'
188 ' }\n', 195 ' }\n',
189 RETURN_TYPE=rename_type(constructor_info.type_name), 196 RETURN_TYPE=rename_type(constructor_info.type_name),
190 CONSTRUCTOR=constructor_info.ConstructorFactoryName(rename_type), 197 CONSTRUCTOR=constructor_info.ConstructorFactoryName(rename_type),
191 CLASS=class_name, 198 CLASS=class_name,
192 TAG=info.tag, 199 TAG=info.tag,
193 PARAMS=constructor_info.ParametersDeclaration( 200 PARAMS=constructor_info.ParametersDeclaration(
194 rename_type, force_optional=True)) 201 rename_type, force_optional=True))
195 for param in constructor_info.param_infos: 202 for param in constructor_info.param_infos:
(...skipping 30 matching lines...) Expand all
226 code = self._library_emitter.FileEmitter(self._interface.id) 233 code = self._library_emitter.FileEmitter(self._interface.id)
227 code.Emit(self._template_loader.Load('callback.darttemplate')) 234 code.Emit(self._template_loader.Load('callback.darttemplate'))
228 code.Emit('typedef void $NAME($PARAMS);\n', 235 code.Emit('typedef void $NAME($PARAMS);\n',
229 NAME=self._interface.id, 236 NAME=self._interface.id,
230 PARAMS=info.ParametersDeclaration(self._DartType)) 237 PARAMS=info.ParametersDeclaration(self._DartType))
231 self._backend.GenerateCallback(info) 238 self._backend.GenerateCallback(info)
232 239
233 def GenerateInterface(self): 240 def GenerateInterface(self):
234 interface_name = self._interface_type_info.interface_name() 241 interface_name = self._interface_type_info.interface_name()
235 242
236 if (self._interface_type_info.has_generated_interface() and 243 # TODO: this is just tossing the interface, need to skip it completely.
237 not self._interface_type_info.merged_into()): 244 interface_emitter = emitter.Emitter()
238 interface_emitter = self._library_emitter.FileEmitter(interface_name)
239 else:
240 interface_emitter = emitter.Emitter()
241 245
242 template_file = 'interface_%s.darttemplate' % interface_name 246 template_file = 'interface_%s.darttemplate' % interface_name
243 interface_template = (self._template_loader.TryLoad(template_file) or 247 interface_template = (self._template_loader.TryLoad(template_file) or
244 self._template_loader.Load('interface.darttemplate')) 248 self._template_loader.Load('interface.darttemplate'))
245 249
246 implements = [] 250 implements = []
247 for parent in self._interface.parents: 251 for parent in self._interface.parents:
248 parent_type_info = self._type_registry.TypeInfo(parent.type.id) 252 parent_type_info = self._type_registry.TypeInfo(parent.type.id)
249 implements.append(parent_type_info.interface_name()) 253 implements.append(parent_type_info.interface_name())
250 254
251 if self._interface_type_info.list_item_type(): 255 if self._interface_type_info.list_item_type():
252 item_type_info = self._type_registry.TypeInfo( 256 item_type_info = self._type_registry.TypeInfo(
253 self._interface_type_info.list_item_type()) 257 self._interface_type_info.list_item_type())
254 implements.append('List<%s>' % item_type_info.dart_type()) 258 implements.append('List<%s>' % item_type_info.dart_type())
255 259
256 if interface_name in _secure_base_types: 260 if interface_name in _secure_base_types:
257 implements.append(_secure_base_types[interface_name]) 261 implements.append(_secure_base_types[interface_name])
258 262
259 comment = ' extends' 263 comment = ' extends'
260 implements_str = '' 264 implements_str = ''
261 if implements: 265 if implements:
262 implements_str += ' implements ' + ', '.join(implements) 266 implements_str += ' implements ' + ', '.join(implements)
263 comment = ',' 267 comment = ','
264 268
265 factory_provider = None 269 factory_provider = None
266 if interface_name in interface_factories: 270 if interface_name in interface_factories:
267 factory_provider = interface_factories[interface_name] 271 factory_provider = interface_factories[interface_name]
268 272
269 constructors = [] 273 constructors = []
270 constructor_info = AnalyzeConstructor(self._interface) 274 if interface_name in _static_classes:
275 constructor_info = None
276 else:
277 constructor_info = AnalyzeConstructor(self._interface)
271 if constructor_info: 278 if constructor_info:
272 constructors.append(constructor_info) 279 constructors.append(constructor_info)
273 factory_provider = '_' + interface_name + 'FactoryProvider' 280 factory_provider = '_' + interface_name + 'FactoryProvider'
274 factory_provider_emitter = self._library_emitter.FileEmitter( 281 factory_provider_emitter = self._library_emitter.FileEmitter(
275 '_%sFactoryProvider' % interface_name) 282 '_%sFactoryProvider' % interface_name)
276 self._backend.EmitFactoryProvider( 283 self._backend.EmitFactoryProvider(
277 constructor_info, factory_provider, factory_provider_emitter) 284 constructor_info, factory_provider, factory_provider_emitter)
278 285
279 infos = HtmlElementConstructorInfos(interface_name) 286 infos = HtmlElementConstructorInfos(interface_name)
280 if infos: 287 if infos:
(...skipping 11 matching lines...) Expand all
292 if factory_provider: 299 if factory_provider:
293 assert factory_provider == info.factory_provider_name 300 assert factory_provider == info.factory_provider_name
294 else: 301 else:
295 factory_provider = info.factory_provider_name 302 factory_provider = info.factory_provider_name
296 303
297 # TODO(vsm): Add appropriate package / namespace syntax. 304 # TODO(vsm): Add appropriate package / namespace syntax.
298 (self._type_comment_emitter, 305 (self._type_comment_emitter,
299 self._members_emitter, 306 self._members_emitter,
300 self._top_level_emitter) = interface_emitter.Emit( 307 self._top_level_emitter) = interface_emitter.Emit(
301 interface_template + '$!TOP_LEVEL', 308 interface_template + '$!TOP_LEVEL',
302 ID=interface_name, 309 ID='_I%s' % interface_name,
303 EXTENDS=implements_str) 310 EXTENDS=implements_str)
304 311
305 self._type_comment_emitter.Emit("/// @domName $DOMNAME",
306 DOMNAME=self._interface.doc_js_name)
307
308 implementation_emitter = self._ImplementationEmitter() 312 implementation_emitter = self._ImplementationEmitter()
309 313
310 base_class = self._backend.RootClassName() 314 base_type_info = None
311 if self._interface.parents: 315 if self._interface.parents:
312 supertype = self._interface.parents[0].type.id 316 supertype = self._interface.parents[0].type.id
313 if not IsDartCollectionType(supertype) and not IsPureInterface(supertype): 317 if not IsDartCollectionType(supertype) and not IsPureInterface(supertype):
314 type_info = self._type_registry.TypeInfo(supertype) 318 base_type_info = self._type_registry.TypeInfo(supertype)
315 if type_info.merged_into() and self._backend.ImplementsMergedMembers(): 319 if base_type_info.merged_into() \
316 type_info = self._type_registry.TypeInfo(type_info.merged_into()) 320 and self._backend.ImplementsMergedMembers():
317 base_class = type_info.implementation_name() 321 base_type_info = self._type_registry.TypeInfo(
322 base_type_info.merged_into())
318 323
319 implemented_interfaces = [interface_name] +\ 324 if base_type_info:
320 self._backend.AdditionalImplementedInterfaces() 325 base_class = base_type_info.implementation_name()
326 else:
327 base_class = self._backend.RootClassName()
328
329 implements = self._backend.AdditionalImplementedInterfaces()
330 for parent in self._interface.parents:
331 parent_type_info = self._type_registry.TypeInfo(parent.type.id)
332 if parent_type_info != base_type_info:
333 implements.append(parent_type_info.interface_name())
334
335 if interface_name in _secure_base_types:
336 implements.append(_secure_base_types[interface_name])
337
338 implements_str = ''
339 if implements:
340 implements_str = ' implements ' + ', '.join(set(implements))
341
321 self._implementation_members_emitter = implementation_emitter.Emit( 342 self._implementation_members_emitter = implementation_emitter.Emit(
322 self._backend.ImplementationTemplate(), 343 self._backend.ImplementationTemplate(),
323 CLASSNAME=self._interface_type_info.implementation_name(), 344 CLASSNAME=self._interface_type_info.implementation_name(),
324 EXTENDS=' extends %s' % base_class if base_class else '', 345 EXTENDS=' extends %s' % base_class if base_class else '',
325 IMPLEMENTS=' implements ' + ', '.join(implemented_interfaces), 346 IMPLEMENTS=implements_str,
347 DOMNAME=self._interface.doc_js_name,
326 NATIVESPEC=self._backend.NativeSpec()) 348 NATIVESPEC=self._backend.NativeSpec())
327 self._backend.StartInterface(self._implementation_members_emitter) 349 self._backend.StartInterface(self._implementation_members_emitter)
328 350
329 for constructor_info in constructors: 351 for constructor_info in constructors:
330 constructor_info.GenerateFactoryInvocation( 352 constructor_info.GenerateFactoryInvocation(
331 self._DartType, self._members_emitter, factory_provider) 353 self._DartType, self._members_emitter, factory_provider)
332 354
333 element_type = None 355 self._backend.AddConstructors(constructors, factory_provider,
356 self._interface_type_info.implementation_name(),
357 base_class)
358
359 typed_array_type = None
334 for interface in self._database.Hierarchy(self._interface): 360 for interface in self._database.Hierarchy(self._interface):
335 type_info = self._type_registry.TypeInfo(interface.id) 361 type_info = self._type_registry.TypeInfo(interface.id)
336 if type_info.is_typed_array(): 362 if type_info.is_typed_array():
337 element_type = type_info.list_item_type() 363 typed_array_type = type_info.list_item_type()
338 break 364 break
339 if element_type: 365 if typed_array_type:
340 self._members_emitter.Emit( 366 self._members_emitter.Emit(
341 '\n' 367 '\n'
342 ' factory $CTOR(int length) =>\n' 368 ' factory $CTOR(int length) =>\n'
343 ' $FACTORY.create$(CTOR)(length);\n' 369 ' $FACTORY.create$(CTOR)(length);\n'
344 '\n' 370 '\n'
345 ' factory $CTOR.fromList(List<$TYPE> list) =>\n' 371 ' factory $CTOR.fromList(List<$TYPE> list) =>\n'
346 ' $FACTORY.create$(CTOR)_fromList(list);\n' 372 ' $FACTORY.create$(CTOR)_fromList(list);\n'
347 '\n' 373 '\n'
348 ' factory $CTOR.fromBuffer(ArrayBuffer buffer, [int byteOffset, int l ength]) => \n' 374 ' factory $CTOR.fromBuffer(ArrayBuffer buffer, [int byteOffset, int l ength]) => \n'
349 ' $FACTORY.create$(CTOR)_fromBuffer(buffer, byteOffset, length);\n' , 375 ' $FACTORY.create$(CTOR)_fromBuffer(buffer, byteOffset, length);\n' ,
350 CTOR=self._interface.id, 376 CTOR=self._interface.id,
351 TYPE=self._DartType(element_type), 377 TYPE=self._DartType(typed_array_type),
352 FACTORY=factory_provider) 378 FACTORY=factory_provider)
353 379
354 events_interface = self._event_generator.ProcessInterface( 380 events_interface = self._event_generator.ProcessInterface(
355 self._interface, interface_name, 381 self._interface, interface_name,
356 self._backend.CustomJSMembers(), 382 self._backend.CustomJSMembers(),
357 interface_emitter, implementation_emitter) 383 interface_emitter, implementation_emitter)
358 if events_interface: 384 if events_interface:
359 self._EmitEventGetter(events_interface, '_%sImpl' % events_interface) 385 self._EmitEventGetter(events_interface, events_interface)
360 386
361 old_backend = self._backend 387 old_backend = self._backend
362 if not self._backend.ImplementsMergedMembers(): 388 if not self._backend.ImplementsMergedMembers():
363 self._backend = HtmlGeneratorDummyBackend() 389 self._backend = HtmlGeneratorDummyBackend()
364 merged_interface = self._interface_type_info.merged_interface() 390 merged_interface = self._interface_type_info.merged_interface()
365 if merged_interface: 391 if merged_interface:
366 self.AddMembers(self._database.GetInterface(merged_interface)) 392 self.AddMembers(self._database.GetInterface(merged_interface))
367 self._backend = old_backend 393 self._backend = old_backend
368 394
369 self.AddMembers(self._interface) 395 self.AddMembers(self._interface)
396 if merged_interface and not self._backend.ImplementsMergedMembers():
397 self.AddMembers(self._database.GetInterface(merged_interface), True)
398
370 self.AddSecondaryMembers(self._interface) 399 self.AddSecondaryMembers(self._interface)
371 self._backend.FinishInterface() 400 self._backend.FinishInterface()
372 401
373 def AddMembers(self, interface): 402 def AddMembers(self, interface, declare_only=False):
374 for const in sorted(interface.constants, ConstantOutputOrder): 403 for const in sorted(interface.constants, ConstantOutputOrder):
375 self.AddConstant(const) 404 self.AddConstant(const)
376 405
377 for attr in sorted(interface.attributes, ConstantOutputOrder): 406 for attr in sorted(interface.attributes, ConstantOutputOrder):
378 if attr.type.id != 'EventListener': 407 if attr.type.id != 'EventListener':
379 self.AddAttribute(attr) 408 self.AddAttribute(attr, False, declare_only)
380 409
381 # The implementation should define an indexer if the interface directly 410 # The implementation should define an indexer if the interface directly
382 # extends List. 411 # extends List.
383 element_type = None 412 element_type = None
384 requires_indexer = False 413 requires_indexer = False
385 if self._interface_type_info.list_item_type(): 414 if self._interface_type_info.list_item_type():
386 self.AddIndexer(self._interface_type_info.list_item_type()) 415 self.AddIndexer(self._interface_type_info.list_item_type())
387 else: 416 else:
388 for parent in self._database.Hierarchy(self._interface): 417 for parent in self._database.Hierarchy(self._interface):
389 if parent == self._interface: 418 if parent == self._interface:
390 continue 419 continue
391 parent_type_info = self._type_registry.TypeInfo(parent.id) 420 parent_type_info = self._type_registry.TypeInfo(parent.id)
392 if parent_type_info.list_item_type(): 421 if parent_type_info.list_item_type():
393 self.AmendIndexer(parent_type_info.list_item_type()) 422 self.AmendIndexer(parent_type_info.list_item_type())
394 break 423 break
395 424
396 # Group overloaded operations by id 425 # Group overloaded operations by id
397 operationsById = {} 426 operationsById = {}
398 for operation in interface.operations: 427 for operation in interface.operations:
399 if operation.id not in operationsById: 428 if operation.id not in operationsById:
400 operationsById[operation.id] = [] 429 operationsById[operation.id] = []
401 operationsById[operation.id].append(operation) 430 operationsById[operation.id].append(operation)
402 431
403 # Generate operations 432 # Generate operations
404 for id in sorted(operationsById.keys()): 433 for id in sorted(operationsById.keys()):
405 operations = operationsById[id] 434 operations = operationsById[id]
406 info = AnalyzeOperation(interface, operations) 435 info = AnalyzeOperation(interface, operations)
407 self.AddOperation(info) 436 self.AddOperation(info, False, declare_only)
408 437
409 def AddSecondaryMembers(self, interface): 438 def AddSecondaryMembers(self, interface):
410 # With multiple inheritance, attributes and operations of non-first 439 # With multiple inheritance, attributes and operations of non-first
411 # interfaces need to be added. Sometimes the attribute or operation is 440 # interfaces need to be added. Sometimes the attribute or operation is
412 # defined in the current interface as well as a parent. In that case we 441 # defined in the current interface as well as a parent. In that case we
413 # avoid making a duplicate definition and pray that the signatures match. 442 # avoid making a duplicate definition and pray that the signatures match.
414 secondary_parents = self._TransitiveSecondaryParents(interface) 443 secondary_parents = self._TransitiveSecondaryParents(interface)
415 for parent_interface in sorted(secondary_parents): 444 for parent_interface in sorted(secondary_parents):
416 if isinstance(parent_interface, str): # IsDartCollectionType(parent_inter face) 445 if isinstance(parent_interface, str): # IsDartCollectionType(parent_inter face)
417 continue 446 continue
(...skipping 14 matching lines...) Expand all
432 operations = operationsById[id] 461 operations = operationsById[id]
433 info = AnalyzeOperation(interface, operations) 462 info = AnalyzeOperation(interface, operations)
434 self.AddSecondaryOperation(parent_interface, info) 463 self.AddSecondaryOperation(parent_interface, info)
435 464
436 def AddIndexer(self, element_type): 465 def AddIndexer(self, element_type):
437 self._backend.AddIndexer(element_type) 466 self._backend.AddIndexer(element_type)
438 467
439 def AmendIndexer(self, element_type): 468 def AmendIndexer(self, element_type):
440 self._backend.AmendIndexer(element_type) 469 self._backend.AmendIndexer(element_type)
441 470
442 def AddAttribute(self, attribute, is_secondary=False): 471 def AddAttribute(self, attribute, is_secondary=False, declare_only=False):
443 dom_name = DartDomNameOfAttribute(attribute) 472 dom_name = DartDomNameOfAttribute(attribute)
444 html_name = self._renamer.RenameMember( 473 html_name = self._renamer.RenameMember(
445 self._interface.id, attribute, dom_name, 'get:') 474 self._interface.id, attribute, dom_name, 'get:')
446 if not html_name or self._IsPrivate(html_name): 475 if not html_name or self._IsPrivate(html_name):
447 return 476 return
448 477
449 478
450 html_setter_name = self._renamer.RenameMember( 479 html_setter_name = self._renamer.RenameMember(
451 self._interface.id, attribute, dom_name, 'set:') 480 self._interface.id, attribute, dom_name, 'set:')
452 read_only = (attribute.is_read_only or 'Replaceable' in attribute.ext_attrs 481 read_only = (attribute.is_read_only or 'Replaceable' in attribute.ext_attrs
453 or not html_setter_name) 482 or not html_setter_name)
454 483
455 # We don't yet handle inconsistent renames of the getter and setter yet. 484 # We don't yet handle inconsistent renames of the getter and setter yet.
456 assert(not html_setter_name or html_name == html_setter_name) 485 assert(not html_setter_name or html_name == html_setter_name)
457 486
458 if not is_secondary: 487 if not is_secondary:
459 self._members_emitter.Emit('\n /** @domName $DOMINTERFACE.$DOMNAME */', 488 self._members_emitter.Emit('\n /** @domName $DOMINTERFACE.$DOMNAME */',
460 DOMINTERFACE=attribute.doc_js_interface_name, 489 DOMINTERFACE=attribute.doc_js_interface_name,
461 DOMNAME=dom_name) 490 DOMNAME=dom_name)
462 if read_only: 491 if read_only:
463 template = '\n $TYPE get $NAME;\n' 492 template = '\n $TYPE get $NAME;\n'
464 else: 493 else:
465 template = '\n $TYPE $NAME;\n' 494 template = '\n $TYPE $NAME;\n'
466 495
467 self._members_emitter.Emit(template, 496 self._members_emitter.Emit(template,
468 NAME=html_name, 497 NAME=html_name,
469 TYPE=SecureOutputType(self, attribute.type.id)) 498 TYPE=SecureOutputType(self, attribute.type.id))
470 499
471 self._backend.AddAttribute(attribute, html_name, read_only) 500 if declare_only:
501 self._backend.DeclareAttribute(attribute,
502 SecureOutputType(self, attribute.type.id), html_name, read_only)
503 else:
504 self._backend.AddAttribute(attribute, html_name, read_only)
472 505
473 def AddSecondaryAttribute(self, interface, attribute): 506 def AddSecondaryAttribute(self, interface, attribute):
474 self._backend.SecondaryContext(interface) 507 self._backend.SecondaryContext(interface)
475 self.AddAttribute(attribute, True) 508 self.AddAttribute(attribute, True)
476 509
477 def AddOperation(self, info, skip_declaration=False): 510 def AddOperation(self, info, skip_declaration=False, declare_only=False):
478 """ 511 """
479 Arguments: 512 Arguments:
480 operations - contains the overloads, one or more operations with the same 513 operations - contains the overloads, one or more operations with the same
481 name. 514 name.
482 """ 515 """
483 # FIXME: When we pass in operations[0] below, we're assuming all 516 # FIXME: When we pass in operations[0] below, we're assuming all
484 # overloaded operations have the same security attributes. This 517 # overloaded operations have the same security attributes. This
485 # is currently true, but we should consider filtering earlier or 518 # is currently true, but we should consider filtering earlier or
486 # merging the relevant data into info itself. 519 # merging the relevant data into info itself.
487 html_name = self._renamer.RenameMember(self._interface.id, 520 html_name = self._renamer.RenameMember(self._interface.id,
(...skipping 17 matching lines...) Expand all
505 ' static final $NAME = $IMPL_CLASS_NAME.$NAME;\n', 538 ' static final $NAME = $IMPL_CLASS_NAME.$NAME;\n',
506 IMPL_CLASS_NAME=self._interface_type_info.implementation_name(), 539 IMPL_CLASS_NAME=self._interface_type_info.implementation_name(),
507 NAME=html_name) 540 NAME=html_name)
508 else: 541 else:
509 self._members_emitter.Emit( 542 self._members_emitter.Emit(
510 '\n' 543 '\n'
511 ' $TYPE $NAME($PARAMS);\n', 544 ' $TYPE $NAME($PARAMS);\n',
512 TYPE=SecureOutputType(self, info.type_name), 545 TYPE=SecureOutputType(self, info.type_name),
513 NAME=html_name, 546 NAME=html_name,
514 PARAMS=info.ParametersDeclaration(self._DartType)) 547 PARAMS=info.ParametersDeclaration(self._DartType))
515 self._backend.AddOperation(info, html_name) 548 if declare_only:
549 self._backend.DeclareOperation(info,
550 SecureOutputType(self, info.type_name), html_name)
551 else:
552 self._backend.AddOperation(info, html_name)
516 553
517 def AddSecondaryOperation(self, interface, info): 554 def AddSecondaryOperation(self, interface, info):
518 self._backend.SecondaryContext(interface) 555 self._backend.SecondaryContext(interface)
519 self.AddOperation(info, True) 556 self.AddOperation(info)
520 557
521 def AddConstant(self, constant): 558 def AddConstant(self, constant):
522 type = TypeOrNothing(self._DartType(constant.type.id), constant.type.id) 559 type = TypeOrNothing(self._DartType(constant.type.id), constant.type.id)
523 self._members_emitter.Emit('\n static const $TYPE$NAME = $VALUE;\n', 560 self._members_emitter.Emit('\n static const $TYPE$NAME = $VALUE;\n',
524 NAME=constant.id, 561 NAME=constant.id,
525 TYPE=type, 562 TYPE=type,
526 VALUE=constant.value) 563 VALUE=constant.value)
527 564
565 self._backend.AddConstant(constant)
566
528 def _ImplementationEmitter(self): 567 def _ImplementationEmitter(self):
529 if IsPureInterface(self._interface.id):
530 return emitter.Emitter()
531 basename = self._interface_type_info.implementation_name() 568 basename = self._interface_type_info.implementation_name()
532 if (self._interface_type_info.merged_into() and 569 if (self._interface_type_info.merged_into() and
533 self._backend.ImplementsMergedMembers()): 570 self._backend.ImplementsMergedMembers()):
534 # Merged members are implemented in target interface implementation. 571 # Merged members are implemented in target interface implementation.
535 return emitter.Emitter() 572 return emitter.Emitter()
536 return self._library_emitter.FileEmitter(basename.lstrip('_')) 573 return self._library_emitter.FileEmitter(basename)
537 574
538 def _EmitEventGetter(self, events_interface, events_class): 575 def _EmitEventGetter(self, events_interface, events_class):
539 self._members_emitter.Emit( 576 self._members_emitter.Emit(
540 '\n /**' 577 '\n /**'
541 '\n * @domName EventTarget.addEventListener, ' 578 '\n * @domName EventTarget.addEventListener, '
542 'EventTarget.removeEventListener, EventTarget.dispatchEvent' 579 'EventTarget.removeEventListener, EventTarget.dispatchEvent'
543 '\n */' 580 '\n */'
544 '\n $TYPE get on;\n', 581 '\n $TYPE get on;\n',
545 TYPE=events_interface) 582 TYPE=events_interface)
546 583
547 self._implementation_members_emitter.Emit( 584 self._implementation_members_emitter.Emit(
585 '\n /**'
586 '\n * @domName EventTarget.addEventListener, '
587 'EventTarget.removeEventListener, EventTarget.dispatchEvent'
588 '\n */'
548 '\n $TYPE get on =>\n new $TYPE(this);\n', 589 '\n $TYPE get on =>\n new $TYPE(this);\n',
549 TYPE=events_class) 590 TYPE=events_class)
550 591
551 def _TransitiveSecondaryParents(self, interface): 592 def _TransitiveSecondaryParents(self, interface):
552 """Returns a list of all non-primary parents. 593 """Returns a list of all non-primary parents.
553 594
554 The list contains the interface objects for interfaces defined in the 595 The list contains the interface objects for interfaces defined in the
555 database, and the name for undefined interfaces. 596 database, and the name for undefined interfaces.
556 """ 597 """
557 def walk(parents): 598 def walk(parents):
(...skipping 30 matching lines...) Expand all
588 class HtmlGeneratorDummyBackend(object): 629 class HtmlGeneratorDummyBackend(object):
589 def AddAttribute(self, attribute, html_name, read_only): 630 def AddAttribute(self, attribute, html_name, read_only):
590 pass 631 pass
591 632
592 def AddOperation(self, info, html_name): 633 def AddOperation(self, info, html_name):
593 pass 634 pass
594 635
595 636
596 # ------------------------------------------------------------------------------ 637 # ------------------------------------------------------------------------------
597 638
598 class Dart2JSBackend(object): 639 class Dart2JSBackend(HtmlDartGenerator):
599 """Generates a dart2js class for the dart:html library from a DOM IDL 640 """Generates a dart2js class for the dart:html library from a DOM IDL
600 interface. 641 interface.
601 """ 642 """
602 643
603 def __init__(self, interface, options): 644 def __init__(self, interface, options):
604 self._interface = interface 645 super(Dart2JSBackend, self).__init__(interface, options)
646
605 self._database = options.database 647 self._database = options.database
606 self._template_loader = options.templates 648 self._template_loader = options.templates
607 self._type_registry = options.type_registry 649 self._type_registry = options.type_registry
650 self._renamer = options.renamer
608 self._interface_type_info = self._type_registry.TypeInfo(self._interface.id) 651 self._interface_type_info = self._type_registry.TypeInfo(self._interface.id)
609 self._current_secondary_parent = None 652 self._current_secondary_parent = None
610 653
611 def ImplementsMergedMembers(self): 654 def ImplementsMergedMembers(self):
612 return True 655 return True
613 656
614 def GenerateCallback(self, info): 657 def GenerateCallback(self, info):
615 pass 658 pass
616 659
617 def RootClassName(self): 660 def RootClassName(self):
618 return None 661 return None
619 662
620 def AdditionalImplementedInterfaces(self): 663 def AdditionalImplementedInterfaces(self):
621 # TODO: Include all implemented interfaces, including other Lists. 664 implements = super(Dart2JSBackend, self).AdditionalImplementedInterfaces()
622 implements = []
623 if self._interface_type_info.is_typed_array():
624 element_type = self._interface_type_info.list_item_type()
625 implements.append('List<%s>' % self._DartType(element_type))
626 if self._interface_type_info.list_item_type(): 665 if self._interface_type_info.list_item_type():
627 implements.append('JavaScriptIndexingBehavior') 666 implements.append('JavaScriptIndexingBehavior')
628 return implements 667 return implements
629 668
630 def NativeSpec(self): 669 def NativeSpec(self):
631 native_spec = MakeNativeSpec(self._interface.javascript_binding_name) 670 native_spec = MakeNativeSpec(self._interface.javascript_binding_name)
632 return ' native "%s"' % native_spec 671 return ' native "%s"' % native_spec
633 672
634 def ImplementationTemplate(self): 673 def ImplementationTemplate(self):
674 if IsPureInterface(self._interface.id):
675 return self._template_loader.Load('pure_interface.darttemplate')
676
635 template_file = ('impl_%s.darttemplate' % 677 template_file = ('impl_%s.darttemplate' %
636 self._interface_type_info.interface_name()) 678 self._interface_type_info.interface_name())
637 return (self._template_loader.TryLoad(template_file) or 679 return (self._template_loader.TryLoad(template_file) or
638 self._template_loader.Load('dart2js_impl.darttemplate')) 680 self._template_loader.Load('dart2js_impl.darttemplate'))
639 681
640 def StartInterface(self, emitter): 682 def StartInterface(self, emitter):
641 self._members_emitter = emitter 683 self._members_emitter = emitter
642 684
643 def FinishInterface(self): 685 def FinishInterface(self):
644 pass 686 pass
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 has_contains = any(op.id == 'contains' for op in self._interface.operation s) 759 has_contains = any(op.id == 'contains' for op in self._interface.operation s)
718 template = self._template_loader.Load( 760 template = self._template_loader.Load(
719 template_file, 761 template_file,
720 {'DEFINE_CONTAINS': not has_contains}) 762 {'DEFINE_CONTAINS': not has_contains})
721 self._members_emitter.Emit(template, E=self._DartType(element_type)) 763 self._members_emitter.Emit(template, E=self._DartType(element_type))
722 764
723 def AddAttribute(self, attribute, html_name, read_only): 765 def AddAttribute(self, attribute, html_name, read_only):
724 if self._HasCustomImplementation(attribute.id): 766 if self._HasCustomImplementation(attribute.id):
725 return 767 return
726 768
769 if IsPureInterface(self._interface.id):
770 self._AddInterfaceAttribute(attribute)
771 return
772
727 if attribute.id != html_name: 773 if attribute.id != html_name:
728 self._AddAttributeUsingProperties(attribute, html_name, read_only) 774 self._AddAttributeUsingProperties(attribute, html_name, read_only)
729 return 775 return
730 776
731 # If the attribute is shadowing, we can't generate a shadowing 777 # If the attribute is shadowing, we can't generate a shadowing
732 # field (Issue 1633). 778 # field (Issue 1633).
733 # TODO(sra): _FindShadowedAttribute does not take into account the html 779 # TODO(sra): _FindShadowedAttribute does not take into account the html
734 # renaming. we should be looking for another attribute that has the same 780 # renaming. we should be looking for another attribute that has the same
735 # html_name. Two attributes with the same IDL name might not match if one 781 # html_name. Two attributes with the same IDL name might not match if one
736 # is renamed. 782 # is renamed.
(...skipping 18 matching lines...) Expand all
755 801
756 # If the type has a conversion we need a getter or setter to contain the 802 # If the type has a conversion we need a getter or setter to contain the
757 # conversion code. 803 # conversion code.
758 if (self._OutputConversion(attribute.type.id, attribute.id) or 804 if (self._OutputConversion(attribute.type.id, attribute.id) or
759 self._InputConversion(attribute.type.id, attribute.id)): 805 self._InputConversion(attribute.type.id, attribute.id)):
760 self._AddAttributeUsingProperties(attribute, html_name, read_only) 806 self._AddAttributeUsingProperties(attribute, html_name, read_only)
761 return 807 return
762 808
763 output_type = self._NarrowOutputType(attribute.type.id) 809 output_type = self._NarrowOutputType(attribute.type.id)
764 input_type = self._NarrowInputType(attribute.type.id) 810 input_type = self._NarrowInputType(attribute.type.id)
811 self.EmitAttributeDocumentation(attribute)
765 if not read_only: 812 if not read_only:
766 self._members_emitter.Emit( 813 self._members_emitter.Emit(
767 '\n $TYPE $NAME;' 814 '\n $TYPE $NAME;'
768 '\n', 815 '\n',
769 NAME=DartDomNameOfAttribute(attribute), 816 NAME=DartDomNameOfAttribute(attribute),
770 TYPE=output_type) 817 TYPE=output_type)
771 else: 818 else:
772 self._members_emitter.Emit( 819 self._members_emitter.Emit(
773 '\n final $TYPE $NAME;' 820 '\n final $TYPE $NAME;'
774 '\n', 821 '\n',
775 NAME=DartDomNameOfAttribute(attribute), 822 NAME=DartDomNameOfAttribute(attribute),
776 TYPE=output_type) 823 TYPE=output_type)
777 824
778 def _AddAttributeUsingProperties(self, attribute, html_name, read_only): 825 def _AddAttributeUsingProperties(self, attribute, html_name, read_only):
779 self._AddRenamingGetter(attribute, html_name) 826 self._AddRenamingGetter(attribute, html_name)
780 if not read_only: 827 if not read_only:
781 self._AddRenamingSetter(attribute, html_name) 828 self._AddRenamingSetter(attribute, html_name)
782 829
830 def _AddInterfaceAttribute(self, attribute):
831 self._members_emitter.Emit(
832 '\n $TYPE $NAME;'
833 '\n',
834 NAME=DartDomNameOfAttribute(attribute),
835 TYPE=self._NarrowOutputType(attribute.type.id))
836
783 def _AddRenamingGetter(self, attr, html_name): 837 def _AddRenamingGetter(self, attr, html_name):
838 self.EmitAttributeDocumentation(attr)
839
784 conversion = self._OutputConversion(attr.type.id, attr.id) 840 conversion = self._OutputConversion(attr.type.id, attr.id)
785 if conversion: 841 if conversion:
786 return self._AddConvertingGetter(attr, html_name, conversion) 842 return self._AddConvertingGetter(attr, html_name, conversion)
787 return_type = self._NarrowOutputType(attr.type.id) 843 return_type = self._NarrowOutputType(attr.type.id)
788 self._members_emitter.Emit( 844 self._members_emitter.Emit(
789 # TODO(sra): Use metadata to provide native name. 845 # TODO(sra): Use metadata to provide native name.
790 '\n $TYPE get $HTML_NAME => JS("$TYPE", "#.$NAME", this);' 846 '\n $TYPE get $HTML_NAME => JS("$TYPE", "#.$NAME", this);'
791 '\n', 847 '\n',
792 HTML_NAME=html_name, 848 HTML_NAME=html_name,
793 NAME=attr.id, 849 NAME=attr.id,
794 TYPE=return_type) 850 TYPE=return_type)
795 851
796 def _AddRenamingSetter(self, attr, html_name): 852 def _AddRenamingSetter(self, attr, html_name):
853 self.EmitAttributeDocumentation(attr)
854
797 conversion = self._InputConversion(attr.type.id, attr.id) 855 conversion = self._InputConversion(attr.type.id, attr.id)
798 if conversion: 856 if conversion:
799 return self._AddConvertingSetter(attr, html_name, conversion) 857 return self._AddConvertingSetter(attr, html_name, conversion)
800 self._members_emitter.Emit( 858 self._members_emitter.Emit(
801 # TODO(sra): Use metadata to provide native name. 859 # TODO(sra): Use metadata to provide native name.
802 '\n void set $HTML_NAME($TYPE value) {' 860 '\n void set $HTML_NAME($TYPE value) {'
803 '\n JS("void", "#.$NAME = #", this, value);' 861 '\n JS("void", "#.$NAME = #", this, value);'
804 '\n }' 862 '\n }'
805 '\n', 863 '\n',
806 HTML_NAME=html_name, 864 HTML_NAME=html_name,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
840 pass 898 pass
841 899
842 def AddOperation(self, info, html_name): 900 def AddOperation(self, info, html_name):
843 """ 901 """
844 Arguments: 902 Arguments:
845 info: An OperationInfo object. 903 info: An OperationInfo object.
846 """ 904 """
847 if self._HasCustomImplementation(info.name): 905 if self._HasCustomImplementation(info.name):
848 return 906 return
849 907
850 # Any conversions needed? 908 self.EmitOperationDocumentation(info)
851 if any(self._OperationRequiresConversions(op) for op in info.overloads): 909
910 if IsPureInterface(self._interface.id):
911 self._AddInterfaceOperation(info, html_name)
912 elif any(self._OperationRequiresConversions(op) for op in info.overloads):
913 # Any conversions needed?
852 self._AddOperationWithConversions(info, html_name) 914 self._AddOperationWithConversions(info, html_name)
853 else: 915 else:
854 self._AddDirectNativeOperation(info, html_name) 916 self._AddDirectNativeOperation(info, html_name)
855 917
856 def _AddDirectNativeOperation(self, info, html_name): 918 def _AddDirectNativeOperation(self, info, html_name):
857 # Do we need a native body? 919 # Do we need a native body?
858 if html_name != info.declared_name: 920 if html_name != info.declared_name:
859 return_type = self._NarrowOutputType(info.type_name) 921 return_type = self._NarrowOutputType(info.type_name)
860 922
861 operation_emitter = self._members_emitter.Emit('$!SCOPE', 923 operation_emitter = self._members_emitter.Emit('$!SCOPE',
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
1010 else: 1072 else:
1011 operation = operations[0] 1073 operation = operations[0]
1012 argument_count = len(operation.arguments) 1074 argument_count = len(operation.arguments)
1013 for position, argument in list(enumerate(operation.arguments))[::-1]: 1075 for position, argument in list(enumerate(operation.arguments))[::-1]:
1014 if self._IsOptional(operation, argument): 1076 if self._IsOptional(operation, argument):
1015 check = '?%s' % parameter_names[position] 1077 check = '?%s' % parameter_names[position]
1016 GenerateCall(operation, position + 1, [check]) 1078 GenerateCall(operation, position + 1, [check])
1017 argument_count = position 1079 argument_count = position
1018 GenerateCall(operation, argument_count, []) 1080 GenerateCall(operation, argument_count, [])
1019 1081
1082 def _AddInterfaceOperation(self, info, html_name):
1083 self._members_emitter.Emit(
1084 '\n'
1085 ' $TYPE $NAME($PARAMS);\n',
1086 TYPE=self._NarrowOutputType(info.type_name),
1087 NAME=info.name,
1088 PARAMS=info.ParametersDeclaration(self._NarrowInputType))
1089
1090 def AddConstant(self, constant):
1091 type = TypeOrNothing(self._DartType(constant.type.id), constant.type.id)
1092 self._members_emitter.Emit('\n static const $TYPE$NAME = $VALUE;\n',
1093 NAME=constant.id,
1094 TYPE=type,
1095 VALUE=constant.value)
1020 1096
1021 def _IsOptional(self, operation, argument): 1097 def _IsOptional(self, operation, argument):
1022 return IsOptional(argument) 1098 return IsOptional(argument)
1023 1099
1024 1100
1025 def _OperationRequiresConversions(self, operation): 1101 def _OperationRequiresConversions(self, operation):
1026 return (self._OperationRequiresOutputConversion(operation) or 1102 return (self._OperationRequiresOutputConversion(operation) or
1027 self._OperationRequiresInputConversions(operation)) 1103 self._OperationRequiresInputConversions(operation))
1028 1104
1029 def _OperationRequiresOutputConversion(self, operation): 1105 def _OperationRequiresOutputConversion(self, operation):
(...skipping 17 matching lines...) Expand all
1047 def CustomJSMembers(self): 1123 def CustomJSMembers(self):
1048 return _js_custom_members 1124 return _js_custom_members
1049 1125
1050 def _NarrowToImplementationType(self, type_name): 1126 def _NarrowToImplementationType(self, type_name):
1051 return self._type_registry.TypeInfo(type_name).narrow_dart_type() 1127 return self._type_registry.TypeInfo(type_name).narrow_dart_type()
1052 1128
1053 def _NarrowInputType(self, type_name): 1129 def _NarrowInputType(self, type_name):
1054 return self._NarrowToImplementationType(type_name) 1130 return self._NarrowToImplementationType(type_name)
1055 1131
1056 def _NarrowOutputType(self, type_name): 1132 def _NarrowOutputType(self, type_name):
1057 secure_name = SecureOutputType(self, type_name, True) 1133 return SecureOutputType(self, type_name)
1058 return self._NarrowToImplementationType(secure_name)
1059 1134
1060 def _FindShadowedAttribute(self, attr): 1135 def _FindShadowedAttribute(self, attr):
1061 """Returns (attribute, superinterface) or (None, None).""" 1136 """Returns (attribute, superinterface) or (None, None)."""
1062 def FindInParent(interface): 1137 def FindInParent(interface):
1063 """Returns matching attribute in parent, or None.""" 1138 """Returns matching attribute in parent, or None."""
1064 if interface.parents: 1139 if interface.parents:
1065 parent = interface.parents[0] 1140 parent = interface.parents[0]
1066 if IsDartCollectionType(parent.type.id): 1141 if IsDartCollectionType(parent.type.id):
1067 return (None, None) 1142 return (None, None)
1068 if IsPureInterface(parent.type.id): 1143 if IsPureInterface(parent.type.id):
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 1196
1122 library_emitter = self._multiemitter.FileEmitter(library_file_path) 1197 library_emitter = self._multiemitter.FileEmitter(library_file_path)
1123 library_file_dir = os.path.dirname(library_file_path) 1198 library_file_dir = os.path.dirname(library_file_path)
1124 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir) 1199 auxiliary_dir = os.path.relpath(auxiliary_dir, library_file_dir)
1125 imports_emitter = library_emitter.Emit( 1200 imports_emitter = library_emitter.Emit(
1126 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir)) 1201 self._template, AUXILIARY_DIR=massage_path(auxiliary_dir))
1127 for path in sorted(self._path_to_emitter.keys()): 1202 for path in sorted(self._path_to_emitter.keys()):
1128 relpath = os.path.relpath(path, library_file_dir) 1203 relpath = os.path.relpath(path, library_file_dir)
1129 imports_emitter.Emit( 1204 imports_emitter.Emit(
1130 "part '$PATH';\n", PATH=massage_path(relpath)) 1205 "part '$PATH';\n", PATH=massage_path(relpath))
OLDNEW
« no previous file with comments | « sdk/lib/html/scripts/htmlrenamer.py ('k') | sdk/lib/html/scripts/systemnative.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698