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

Unified Diff: client/dom/scripts/dartgenerator.py

Issue 9342008: start of code to generate dart:html using dart:dom scripts (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: merged Created 8 years, 10 months 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 side-by-side diff with in-line comments
Download patch
Index: client/dom/scripts/dartgenerator.py
diff --git a/client/dom/scripts/dartgenerator.py b/client/dom/scripts/dartgenerator.py
index c2408f40caa405a1afe7ef7b197dd21ddc004349..fd1bc936bfbd731909f9b9a3d4b57a6f3a9fe796 100755
--- a/client/dom/scripts/dartgenerator.py
+++ b/client/dom/scripts/dartgenerator.py
@@ -376,11 +376,17 @@ class DartGenerator(object):
self._systems = []
+ # TODO(jmesserly): only create these if needed
interface_system = InterfacesSystem(
TemplateLoader(self._template_dir, ['dom/interface', 'dom', '']),
self._database, self._emitters, self._output_dir)
self._systems.append(interface_system)
+ html_interface_system = HtmlInterfacesSystem(
+ TemplateLoader(self._template_dir, ['html/interface', 'html', '']),
+ self._database, self._emitters, self._output_dir)
+ self._systems.append(html_interface_system)
+
if 'native' in systems:
native_system = NativeImplementationSystem(
TemplateLoader(self._template_dir, ['dom/native', 'dom', '']),
@@ -407,6 +413,21 @@ class DartGenerator(object):
frog_system._interface_system = interface_system
self._systems.append(frog_system)
+ if 'htmlfrog' in systems:
+ html_system = HtmlFrogSystem(
+ TemplateLoader(self._template_dir, ['html/frog', 'html', '']),
+ self._database, self._emitters, self._output_dir)
+
+ html_system._interface_system = html_interface_system
+ self._systems.append(html_system)
+
+ if 'htmldartium' in systems:
+ html_system = HtmlDartiumSystem(
+ TemplateLoader(self._template_dir, ['html/dartium', 'html', '']),
+ self._database, self._emitters, self._output_dir)
+
+ html_system._interface_system = html_interface_system
+ self._systems.append(html_system)
# Render all interfaces into Dart and save them in files.
for interface in database.GetInterfaces():
@@ -473,11 +494,17 @@ class DartGenerator(object):
generator.AddConstant(const)
attributes = [attr for attr in interface.attributes
- if not self._OmitAttribute(interface, attr)]
+ if not self._IsEventAttribute(interface, attr)]
for (getter, setter) in _PairUpAttributes(attributes):
for generator in generators:
generator.AddAttribute(getter, setter)
+ events = _PairUpAttributes([attr for attr in interface.attributes
+ if self._IsEventAttribute(interface, attr)])
+ if events:
+ for generator in generators:
+ generator.AddEvents(events)
sra1 2012/02/07 05:06:16 AddEventAttributes would be a better name.
Jennifer Messerly 2012/02/07 21:05:14 Done.
+
# The implementation should define an indexer if the interface directly
# extends List.
element_type = MaybeListElementType(interface)
@@ -532,7 +559,7 @@ class DartGenerator(object):
generator.FinishInterface()
return
- def _OmitAttribute(self, interface, attr):
+ def _IsEventAttribute(self, interface, attr):
# Remove EventListener attributes like 'onclick' when addEventListener
# is available.
if attr.type.id == 'EventListener':
@@ -968,6 +995,57 @@ class InterfacesSystem(System):
# ------------------------------------------------------------------------------
+class HtmlInterfacesSystem(System):
+
+ def __init__(self, templates, database, emitters, output_dir):
+ super(HtmlInterfacesSystem, self).__init__(
+ templates, database, emitters, output_dir)
+ self._dart_interface_file_paths = []
+
+
+ def InterfaceGenerator(self,
+ interface,
+ common_prefix,
+ super_interface_name,
+ source_filter):
+ """."""
+ interface_name = interface.id
+ dart_interface_file_path = self._FilePathForDartInterface(interface_name)
+
+ self._dart_interface_file_paths.append(dart_interface_file_path)
+
+ dart_interface_code = self._emitters.FileEmitter(dart_interface_file_path)
+
+ template_file = 'interface_%s.darttemplate' % interface_name
+ template = self._templates.TryLoad(template_file)
+ if not template:
+ template = self._templates.Load('interface.darttemplate')
+
+ return HtmlDartInterfaceGenerator(
+ interface, dart_interface_code,
+ template,
+ common_prefix, super_interface_name,
+ source_filter)
+
+ def ProcessCallback(self, interface, info):
+ """Generates a typedef for the callback interface."""
+ interface_name = interface.id
+ file_path = self._FilePathForDartInterface(interface_name)
+ self._ProcessCallback(interface, info, file_path)
+
+ def GenerateLibraries(self, lib_dir):
+ pass
+
+
+ def _FilePathForDartInterface(self, interface_name):
+ """Returns the file path of the Dart interface definition."""
+ # TODO(jmesserly): is this the right path
+ return os.path.join(self._output_dir, 'html', 'interface',
+ '%s.dart' % interface_name)
+
+
+# ------------------------------------------------------------------------------
+
class WrappingImplementationSystem(System):
def __init__(self, templates, database, emitters, output_dir):
@@ -1124,6 +1202,57 @@ class FrogSystem(System):
return os.path.join(self._output_dir, 'src', 'frog',
'%s.dart' % interface_name)
+
+# ------------------------------------------------------------------------------
+
+class HtmlFrogSystem(System):
+
+ def __init__(self, templates, database, emitters, output_dir):
+ super(HtmlFrogSystem, self).__init__(
+ templates, database, emitters, output_dir)
+ self._dart_frog_file_paths = []
+
+ def InterfaceGenerator(self,
+ interface,
+ common_prefix,
+ super_interface_name,
+ source_filter):
+ """."""
+ dart_frog_file_path = self._FilePathForFrogImpl(interface.id)
+ self._dart_frog_file_paths.append(dart_frog_file_path)
+
+ template_file = 'impl_%s.darttemplate' % interface.id
+ template = self._templates.TryLoad(template_file)
+ if not template:
+ template = self._templates.Load('frog_impl.darttemplate')
+
+ dart_code = self._emitters.FileEmitter(dart_frog_file_path)
+ # dart_code.Emit(self._templates.Load('frog_impl.darttemplate'))
+ return HtmlFrogInterfaceGenerator(self, interface, template,
+ super_interface_name, dart_code)
+
+ #def ProcessCallback(self, interface, info):
+ # """Generates a typedef for the callback interface."""
+ # file_path = self._FilePathForFrogImpl(interface.id)
+ # self._ProcessCallback(interface, info, file_path)
sra1 2012/02/07 05:06:16 remove commented out code
Jennifer Messerly 2012/02/07 21:05:14 This was also commented out in FrogSystem, so I pr
+
+ def GenerateLibraries(self, lib_dir):
+ self._GenerateLibFile(
+ 'html_frog.darttemplate',
+ os.path.join(lib_dir, 'html_frog.dart'),
+ (self._interface_system._dart_interface_file_paths +
+ self._interface_system._dart_callback_file_paths +
+ self._dart_frog_file_paths))
+
+ def Finish(self):
+ pass
+
+ def _FilePathForFrogImpl(self, interface_name):
+ """Returns the file path of the Frog implementation."""
+ # TODO(jmesserly): is this the right path
+ return os.path.join(self._output_dir, 'html', 'frog',
+ '%s.dart' % interface_name)
+
# ------------------------------------------------------------------------------
class DartInterfaceGenerator(object):
@@ -1265,7 +1394,8 @@ class DartInterfaceGenerator(object):
def AddSecondaryOperation(self, interface, attr):
pass
-
+ def AddEvents(self, event_attrs):
+ pass
# Given a sorted sequence of type identifiers, return an appropriate type
# name
@@ -1273,6 +1403,30 @@ def TypeName(typeIds, interface):
# Dynamically type this field for now.
return 'var'
+# ------------------------------------------------------------------------------
+
+# TODO(jmesserly): inheritance is probably not the right way to factor this long
+# term, but it makes merging better for now.
+class HtmlDartInterfaceGenerator(DartInterfaceGenerator):
+ """Generates Dart Interface definition for one DOM IDL interface."""
+
+ def __init__(self, interface, emitter, template,
+ common_prefix, super_interface, source_filter):
+ super(HtmlDartInterfaceGenerator, self).__init__(interface,
+ emitter, template, common_prefix, super_interface, source_filter)
+
+ def AddEvents(self, event_attrs):
+ events_interface = self._interface.id + 'Events'
+ self._members_emitter.Emit('\n $TYPE get on();\n',
+ TYPE=events_interface)
+ events_members = self._emitter.Emit(
+ '\ninterface $INTERFACE {\n$!MEMBERS}\n',
+ INTERFACE=events_interface)
+
+ for getter, setter in event_attrs:
+ event = getter or setter
+ events_members.Emit('\n EventListenerList get $NAME();\n', NAME=event.id)
+
# ------------------------------------------------------------------------------
@@ -1435,6 +1589,9 @@ class WrappingInterfaceGenerator(object):
self._SecondaryContext(interface)
self.AddOperation(info)
+ def AddEvents(self, event_attrs):
+ pass
+
def _SecondaryContext(self, interface):
if interface is not self._current_secondary_parent:
self._current_secondary_parent = interface
@@ -1995,6 +2152,9 @@ class FrogInterfaceGenerator(object):
self._SecondaryContext(interface)
self.AddOperation(info)
+ def AddEvents(self, event_attrs):
+ pass
+
def _SecondaryContext(self, interface):
if interface is not self._current_secondary_parent:
self._current_secondary_parent = interface
@@ -2075,6 +2235,39 @@ class FrogInterfaceGenerator(object):
# ------------------------------------------------------------------------------
+# TODO(jmesserly): inheritance is probably not the right way to factor this long
+# term, but it makes merging better for now.
+class HtmlFrogInterfaceGenerator(FrogInterfaceGenerator):
+ """Generates a Frog class for the dart:html library from a DOM IDL
+ interface.
+ """
+
+ def __init__(self, system, interface, template, super_interface, dart_code):
+ super(HtmlFrogInterfaceGenerator, self).__init__(
+ system, interface, template, super_interface, dart_code)
+
+ def AddEvents(self, event_attrs):
+ events_class = self._interface.id + 'EventsImpl'
+ events_interface = self._interface.id + 'Events'
+ self._members_emitter.Emit('\n $TYPE get on() =>\n new $TYPE(this);\n',
sra1 2012/02/07 05:06:16 I find this kind of string easier to read if you a
Jennifer Messerly 2012/02/07 21:05:14 Done.
+ TYPE=events_class)
+
+ events_members = self._dart_code.Emit(
+ '\nclass $CLASSNAME extends EventsImplementation '
+ 'implements $INTERFACE {\n'
+ ' $CLASSNAME(_ptr) : super._wrap(_ptr);\n$!MEMBERS}\n',
+ CLASSNAME=events_class,
+ INTERFACE=events_interface)
+
+ for getter, setter in event_attrs:
+ event = getter or setter
+ events_members.Emit(
+ "\n EventListenerList get $NAME() => _get('$NAME');\n",
+ NAME=event.id)
+
+
+# ------------------------------------------------------------------------------
+
class NativeImplementationSystem(System):
def __init__(self, templates, database, emitters, auxiliary_dir, output_dir):

Powered by Google App Engine
This is Rietveld 408576698