| OLD | NEW | 
|---|
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be | 
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. | 
| 4 """ | 4 """ | 
| 5 Generator language component for compiler.py that adds Dart language support. | 5 Generator language component for compiler.py that adds Dart language support. | 
| 6 """ | 6 """ | 
| 7 | 7 | 
| 8 from code import Code | 8 from code import Code | 
| 9 from model import * | 9 from model import * | 
| 10 from schema_util import * | 10 from schema_util import * | 
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 217         .Sblock("for (var o in JS('List', '#.%s', this._jsObject)) {" % | 217         .Sblock("for (var o in JS('List', '#.%s', this._jsObject)) {" % | 
| 218                 prop.name) | 218                 prop.name) | 
| 219         .Append('__proxy_%s.add(new %s._proxy(o));' % (prop.name, | 219         .Append('__proxy_%s.add(new %s._proxy(o));' % (prop.name, | 
| 220              self._GetDartType(prop.type_.item_type))) | 220              self._GetDartType(prop.type_.item_type))) | 
| 221         .Eblock('}') | 221         .Eblock('}') | 
| 222         .Append('return __proxy_%s;' % prop.name) | 222         .Append('return __proxy_%s;' % prop.name) | 
| 223         .Eblock('}') | 223         .Eblock('}') | 
| 224       ) | 224       ) | 
| 225     elif self._IsObjectType(prop.type_): | 225     elif self._IsObjectType(prop.type_): | 
| 226       # TODO(sashab): Think of a way to serialize generic Dart objects. | 226       # TODO(sashab): Think of a way to serialize generic Dart objects. | 
| 227       c.Append("%s get %s => JS('%s', '#.%s', this._jsObject);" % | 227       if type_name in self._types: | 
| 228           (type_name, prop.name, type_name, prop.name)) | 228         c.Append("%s get %s => new %s._proxy(JS('%s', '#.%s', " | 
|  | 229                  "this._jsObject));" % | 
|  | 230             (type_name, prop.name, type_name, type_name, prop.name)) | 
|  | 231       else: | 
|  | 232         c.Append("%s get %s => JS('%s', '#.%s', this._jsObject);" % | 
|  | 233             (type_name, prop.name, type_name, prop.name)) | 
| 229     else: | 234     else: | 
| 230       raise Exception( | 235       raise Exception( | 
| 231           "Could not generate wrapper for %s.%s: unserializable type %s" % | 236           "Could not generate wrapper for %s.%s: unserializable type %s" % | 
| 232           (type_.name, prop.name, type_name) | 237           (type_.name, prop.name, type_name) | 
| 233       ) | 238       ) | 
| 234     return c | 239     return c | 
| 235 | 240 | 
| 236   def _GenerateSetter(self, type_, prop): | 241   def _GenerateSetter(self, type_, prop): | 
| 237     """Given a Type and Property, returns the Code object for the setter for | 242     """Given a Type and Property, returns the Code object for the setter for | 
| 238     that property. | 243     that property. | 
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 366     is this._jsObject. | 371     is this._jsObject. | 
| 367 | 372 | 
| 368     e.g. | 373     e.g. | 
| 369         JS('void', '#.resizeTo(#, #)', this._jsObject, width, height) | 374         JS('void', '#.resizeTo(#, #)', this._jsObject, width, height) | 
| 370         JS('void', '#.setBounds(#)', this._jsObject, convertArgument(bounds)) | 375         JS('void', '#.setBounds(#)', this._jsObject, convertArgument(bounds)) | 
| 371     """ | 376     """ | 
| 372     n_params = len(function.params) | 377     n_params = len(function.params) | 
| 373     if function.callback: | 378     if function.callback: | 
| 374       n_params += 1 | 379       n_params += 1 | 
| 375 | 380 | 
| 376     params = ["'%s'" % self._GetDartType(function.returns), | 381     return_type_str = self._GetDartType(function.returns) | 
| 377               "'#.%s(%s)'" % (function.name, ', '.join(['#'] * n_params)), | 382     params = [] | 
| 378               call_target] | 383 | 
|  | 384     # If this object is serializable, don't convert the type from JS - pass the | 
|  | 385     # JS object straight into the proxy. | 
|  | 386     if self._IsSerializableObjectType(function.returns): | 
|  | 387       params.append("''") | 
|  | 388     else: | 
|  | 389       params.append("'%s'" % return_type_str) | 
|  | 390 | 
|  | 391     params.append("'#.%s(%s)'" % (function.name, ', '.join(['#'] * n_params))) | 
|  | 392     params.append(call_target) | 
| 379 | 393 | 
| 380     for param in function.params: | 394     for param in function.params: | 
| 381       if not self._IsBaseType(param.type_): | 395       if not self._IsBaseType(param.type_): | 
| 382         params.append('convertArgument(%s)' % param.name) | 396         params.append('convertArgument(%s)' % param.name) | 
| 383       else: | 397       else: | 
| 384         params.append(param.name) | 398         params.append(param.name) | 
| 385     if function.callback: | 399     if function.callback: | 
| 386       # If this isn't a base type, we need a proxied callback. | 400       # If this isn't a base type, we need a proxied callback. | 
| 387       callback_name = function.callback.name | 401       callback_name = function.callback.name | 
| 388       if self._NeedsProxiedCallback(function): | 402       if self._NeedsProxiedCallback(function): | 
| 389         callback_name = "__proxy_callback" | 403         callback_name = "__proxy_callback" | 
| 390       params.append('convertDartClosureToJS(%s, %s)' % (callback_name, | 404       params.append('convertDartClosureToJS(%s, %s)' % (callback_name, | 
| 391                     len(function.callback.params))) | 405                     len(function.callback.params))) | 
| 392 | 406 | 
| 393     return 'JS(%s)' % ', '.join(params) | 407     # If the object is serializable, call the proxy constructor for this type. | 
|  | 408     proxy_call = 'JS(%s)' % ', '.join(params) | 
|  | 409     if self._IsSerializableObjectType(function.returns): | 
|  | 410       proxy_call = 'new %s._proxy(%s)' % (return_type_str, proxy_call) | 
|  | 411 | 
|  | 412     return proxy_call | 
| 394 | 413 | 
| 395   def _GenerateEvent(self, event): | 414   def _GenerateEvent(self, event): | 
| 396     """Given a Function object, returns the Code with the .dart for this event, | 415     """Given a Function object, returns the Code with the .dart for this event, | 
| 397     represented by the function. | 416     represented by the function. | 
| 398 | 417 | 
| 399     All events extend the Event base type. | 418     All events extend the Event base type. | 
| 400     """ | 419     """ | 
| 401     c = Code() | 420     c = Code() | 
| 402 | 421 | 
| 403     # Add documentation for this event. | 422     # Add documentation for this event. | 
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 602       return None | 621       return None | 
| 603 | 622 | 
| 604     if document_with is not None: | 623     if document_with is not None: | 
| 605       c.Concat(self._GenerateDocumentation(document_with)) | 624       c.Concat(self._GenerateDocumentation(document_with)) | 
| 606     for line in contents.strip('\n').split('\n'): | 625     for line in contents.strip('\n').split('\n'): | 
| 607       c.Append(line) | 626       c.Append(line) | 
| 608     return c | 627     return c | 
| 609 | 628 | 
| 610   def _AddPrefix(self, name): | 629   def _AddPrefix(self, name): | 
| 611     """Given the name of a type, prefixes the namespace (as camelcase) and | 630     """Given the name of a type, prefixes the namespace (as camelcase) and | 
| 612     return the new name. | 631     returns the new name. | 
| 613     """ | 632     """ | 
| 614     # TODO(sashab): Split the dart library into multiple files, avoiding the | 633     # TODO(sashab): Split the dart library into multiple files, avoiding the | 
| 615     # need for this prefixing. | 634     # need for this prefixing. | 
| 616     return ('%s%s' % ( | 635     return ('%s%s' % ( | 
| 617         ''.join(s.capitalize() for s in self._namespace.name.split('.')), | 636         ''.join(s.capitalize() for s in self._namespace.name.split('.')), | 
| 618         name)) | 637         name)) | 
| 619 | 638 | 
| 620   def _IsFunction(self, type_): | 639   def _IsFunction(self, type_): | 
| 621     """Given a model.Type, returns whether this type is a function. | 640     """Given a model.Type, returns whether this type is a function. | 
| 622     """ | 641     """ | 
| 623     return type_.property_type == PropertyType.FUNCTION | 642     return type_.property_type == PropertyType.FUNCTION | 
| 624 | 643 | 
| 625   def _IsSerializableObjectType(self, type_): | 644   def _IsSerializableObjectType(self, type_): | 
| 626     """Given a model.Type, returns whether this type is a serializable object. | 645     """Given a model.Type, returns whether this type is a serializable object. | 
| 627     Serializable objects are custom types defined in this namespace. | 646     Serializable objects are custom types defined in this namespace. | 
| 628 | 647 | 
| 629     If this object is a reference to something not in this namespace, assumes | 648     If this object is a reference to something not in this namespace, assumes | 
| 630     its a serializable object. | 649     its a serializable object. | 
| 631     """ | 650     """ | 
|  | 651     if type_ is None: | 
|  | 652       return False | 
| 632     if type_.property_type is PropertyType.CHOICES: | 653     if type_.property_type is PropertyType.CHOICES: | 
| 633       return all(self._IsSerializableObjectType(c) for c in type_.choices) | 654       return all(self._IsSerializableObjectType(c) for c in type_.choices) | 
| 634     if type_.property_type is PropertyType.REF: | 655     if type_.property_type is PropertyType.REF: | 
| 635       if type_.ref_type in self._types: | 656       if type_.ref_type in self._types: | 
| 636         return self._IsObjectType(self._types[type_.ref_type]) | 657         return self._IsObjectType(self._types[type_.ref_type]) | 
| 637       return True | 658       return True | 
| 638     if (type_.property_type == PropertyType.OBJECT | 659     if (type_.property_type == PropertyType.OBJECT | 
| 639         and type_.instance_of in self._types): | 660         and type_.instance_of in self._types): | 
| 640       return self._IsObjectType(self._types[type_.instance_of]) | 661       return self._IsObjectType(self._types[type_.instance_of]) | 
| 641     return False | 662     return False | 
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 730       return 'Object' | 751       return 'Object' | 
| 731     elif prop_type is PropertyType.FUNCTION: | 752     elif prop_type is PropertyType.FUNCTION: | 
| 732       return 'Function' | 753       return 'Function' | 
| 733     elif prop_type is PropertyType.ARRAY: | 754     elif prop_type is PropertyType.ARRAY: | 
| 734       return 'List<%s>' % self._GetDartType(type_.item_type) | 755       return 'List<%s>' % self._GetDartType(type_.item_type) | 
| 735     elif prop_type is PropertyType.BINARY: | 756     elif prop_type is PropertyType.BINARY: | 
| 736       return 'String' | 757       return 'String' | 
| 737     else: | 758     else: | 
| 738       raise NotImplementedError(prop_type) | 759       raise NotImplementedError(prop_type) | 
| 739 | 760 | 
| OLD | NEW | 
|---|