OLD | NEW |
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 generates Dart APIs from the IDL database.""" | 6 """This module generates Dart APIs from the IDL database.""" |
7 | 7 |
8 import emitter | 8 import emitter |
9 import idlnode | 9 import idlnode |
10 import logging | 10 import logging |
(...skipping 707 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 if optional: | 718 if optional: |
719 raise Exception('Optional arguments cannot precede required ones: ' | 719 raise Exception('Optional arguments cannot precede required ones: ' |
720 + str(args)) | 720 + str(args)) |
721 required.append((name, type, None)) | 721 required.append((name, type, None)) |
722 argtexts = map(FormatArg, required) | 722 argtexts = map(FormatArg, required) |
723 if optional: | 723 if optional: |
724 argtexts.append('[' + ', '.join(map(FormatArg, optional)) + ']') | 724 argtexts.append('[' + ', '.join(map(FormatArg, optional)) + ']') |
725 return ', '.join(argtexts) | 725 return ', '.join(argtexts) |
726 | 726 |
727 | 727 |
| 728 def MaybeListElementTypeName(type_name): |
| 729 """Returns the List element type T from string of form "List<T>", or None.""" |
| 730 match = re.match(r'List<(\w*)>$', type_name) |
| 731 if match: |
| 732 return match.group(1) |
| 733 return None |
| 734 |
728 def MaybeListElementType(interface): | 735 def MaybeListElementType(interface): |
729 """Returns the List element type T, or None in interface does not implement | 736 """Returns the List element type T, or None in interface does not implement |
730 List<T>. | 737 List<T>. |
731 """ | 738 """ |
732 for parent in interface.parents: | 739 for parent in interface.parents: |
733 match = re.match(r'List<(\w*)>$', parent.type.id) | 740 element_type = MaybeListElementTypeName(parent.type.id) |
734 if match: | 741 if element_type: |
735 return match.group(1) | 742 return element_type |
736 return None | 743 return None |
737 | 744 |
738 def MaybeTypedArrayElementType(interface): | 745 def MaybeTypedArrayElementType(interface): |
739 """Returns the typed array element type, or None in interface is not a | 746 """Returns the typed array element type, or None in interface is not a |
740 TypedArray. | 747 TypedArray. |
741 """ | 748 """ |
742 # Typed arrays implement ArrayBufferView and List<T>. | 749 # Typed arrays implement ArrayBufferView and List<T>. |
743 for parent in interface.parents: | 750 for parent in interface.parents: |
744 if parent.type.id == 'ArrayBufferView': | 751 if parent.type.id == 'ArrayBufferView': |
745 return MaybeListElementType(interface) | 752 return MaybeListElementType(interface) |
(...skipping 1104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1850 native_spec = _frog_dom_custom_native_specs[interface_name] | 1857 native_spec = _frog_dom_custom_native_specs[interface_name] |
1851 else: | 1858 else: |
1852 # Make the class 'hidden' so it is dynamically patched at runtime. This | 1859 # Make the class 'hidden' so it is dynamically patched at runtime. This |
1853 # is useful not only for browser compat, but to allow code that links | 1860 # is useful not only for browser compat, but to allow code that links |
1854 # against dart:dom to load in a worker isolate. | 1861 # against dart:dom to load in a worker isolate. |
1855 native_spec = '*' + interface_name | 1862 native_spec = '*' + interface_name |
1856 | 1863 |
1857 if base: | 1864 if base: |
1858 extends = ' extends ' + base | 1865 extends = ' extends ' + base |
1859 elif native_spec[0] == '=': | 1866 elif native_spec[0] == '=': |
| 1867 # The implementation is a singleton with no prototype. |
1860 extends = '' | 1868 extends = '' |
1861 else: | 1869 else: |
1862 extends = ' extends DOMTypeJs' | 1870 extends = ' extends DOMTypeJs' |
1863 | 1871 |
1864 # TODO: Include all implemented interfaces, including other Lists. | 1872 # TODO: Include all implemented interfaces, including other Lists. |
1865 implements = [interface_name] | 1873 implements = [interface_name] |
1866 element_type = MaybeTypedArrayElementType(self._interface) | 1874 element_type = MaybeTypedArrayElementType(self._interface) |
1867 if element_type: | 1875 if element_type: |
1868 implements.append('List<' + element_type + '>') | 1876 implements.append('List<' + element_type + '>') |
1869 | 1877 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1969 # In the non-root case we have to choose between: | 1977 # In the non-root case we have to choose between: |
1970 # | 1978 # |
1971 # class YImpl extends XImpl { add List<T> methods; } | 1979 # class YImpl extends XImpl { add List<T> methods; } |
1972 # | 1980 # |
1973 # and | 1981 # and |
1974 # | 1982 # |
1975 # class YImpl extends ListBase<T> { copies of transitive XImpl methods; } | 1983 # class YImpl extends ListBase<T> { copies of transitive XImpl methods; } |
1976 # | 1984 # |
1977 self._members_emitter.Emit( | 1985 self._members_emitter.Emit( |
1978 '\n' | 1986 '\n' |
1979 ' $TYPE operator[](int index) native;\n', | 1987 ' $TYPE operator[](int index) native "return this[index];";\n', |
1980 TYPE=self._NarrowOutputType(element_type)) | 1988 TYPE=self._NarrowOutputType(element_type)) |
1981 | 1989 |
1982 if 'HasCustomIndexSetter' in self._interface.ext_attrs: | 1990 if 'HasCustomIndexSetter' in self._interface.ext_attrs: |
1983 self._members_emitter.Emit( | 1991 self._members_emitter.Emit( |
1984 '\n' | 1992 '\n' |
1985 ' void operator[]=(int index, $TYPE value) native;\n', | 1993 ' void operator[]=(int index, $TYPE value) native "this[index] = valu
e";\n', |
1986 TYPE=self._NarrowInputType(element_type)) | 1994 TYPE=self._NarrowInputType(element_type)) |
1987 else: | 1995 else: |
1988 self._members_emitter.Emit( | 1996 self._members_emitter.Emit( |
1989 '\n' | 1997 '\n' |
1990 ' void operator[]=(int index, $TYPE value) {\n' | 1998 ' void operator[]=(int index, $TYPE value) {\n' |
1991 ' throw new UnsupportedOperationException("Cannot assign element of
immutable List.");\n' | 1999 ' throw new UnsupportedOperationException("Cannot assign element of
immutable List.");\n' |
1992 ' }\n', | 2000 ' }\n', |
1993 TYPE=self._NarrowInputType(element_type)) | 2001 TYPE=self._NarrowInputType(element_type)) |
1994 | 2002 |
| 2003 # TODO(sra): Use separate mixins for mutable implementations of List<T>. |
| 2004 # TODO(sra): Use separate mixins for typed array implementations of List<T>. |
| 2005 template_file = 'immutable_list_mixin.darttemplate' |
| 2006 template = self._system._templates.Load(template_file) |
| 2007 self._members_emitter.Emit(template, E=element_type) |
| 2008 |
1995 | 2009 |
1996 def AddTypedArrayConstructors(self, element_type): | 2010 def AddTypedArrayConstructors(self, element_type): |
1997 self._members_emitter.Emit( | 2011 self._members_emitter.Emit( |
1998 '\n' | 2012 '\n' |
1999 ' factory $CTOR(int length) => _construct(length);\n' | 2013 ' factory $CTOR(int length) => _construct(length);\n' |
2000 '\n' | 2014 '\n' |
2001 ' factory $CTOR.fromList(List<$TYPE> list) => _construct(list);\n' | 2015 ' factory $CTOR.fromList(List<$TYPE> list) => _construct(list);\n' |
2002 '\n' | 2016 '\n' |
2003 ' factory $CTOR.fromBuffer(ArrayBuffer buffer) => _construct(buffer);\n
' | 2017 ' factory $CTOR.fromBuffer(ArrayBuffer buffer) => _construct(buffer);\n
' |
2004 '\n' | 2018 '\n' |
2005 ' static _construct(arg) native \'return new $CTOR(arg);\';\n', | 2019 ' static _construct(arg) native \'return new $CTOR(arg);\';\n', |
2006 CTOR=self._interface.id, | 2020 CTOR=self._interface.id, |
2007 TYPE=element_type) | 2021 TYPE=element_type) |
2008 | 2022 |
2009 | 2023 |
2010 def AddOperation(self, info): | 2024 def AddOperation(self, info): |
2011 """ | 2025 """ |
2012 Arguments: | 2026 Arguments: |
2013 info: An OperationInfo object. | 2027 info: An OperationInfo object. |
2014 """ | 2028 """ |
2015 # TODO(vsm): Handle overloads. | 2029 # TODO(vsm): Handle overloads. |
2016 self._members_emitter.Emit( | 2030 self._members_emitter.Emit( |
2017 '\n' | 2031 '\n' |
2018 ' $TYPE $NAME($PARAMS) native;\n', | 2032 ' $TYPE $NAME($PARAMS) native;\n', |
2019 TYPE=self._NarrowOutputType(info.type_name), | 2033 TYPE=self._NarrowOutputType(info.type_name), |
2020 NAME=info.name, | 2034 NAME=info.name, |
2021 PARAMS=info.ParametersImplementationDeclaration( | 2035 PARAMS=info.ParametersImplementationDeclaration( |
2022 lambda type_name: self._NarrowInputType(type_name))) | 2036 lambda type_name: self._NarrowInputType(type_name))) |
OLD | NEW |