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

Side by Side Diff: mojo/public/tools/bindings/generators/mojom_python_generator.py

Issue 548343005: mojo: Specialize native type arrays. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 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 unified diff | Download patch
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 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 """Generates Python source files from a mojom.Module.""" 5 """Generates Python source files from a mojom.Module."""
6 6
7 import re 7 import re
8 from itertools import ifilter 8 from itertools import ifilter
9 9
10 import mojom.generate.generator as generator 10 import mojom.generate.generator as generator
(...skipping 19 matching lines...) Expand all
30 mojom.DPPIPE: "_descriptor.TYPE_HANDLE", 30 mojom.DPPIPE: "_descriptor.TYPE_HANDLE",
31 mojom.MSGPIPE: "_descriptor.TYPE_HANDLE", 31 mojom.MSGPIPE: "_descriptor.TYPE_HANDLE",
32 mojom.SHAREDBUFFER: "_descriptor.TYPE_HANDLE", 32 mojom.SHAREDBUFFER: "_descriptor.TYPE_HANDLE",
33 mojom.NULLABLE_HANDLE: "_descriptor.TYPE_NULLABLE_HANDLE", 33 mojom.NULLABLE_HANDLE: "_descriptor.TYPE_NULLABLE_HANDLE",
34 mojom.NULLABLE_DCPIPE: "_descriptor.TYPE_NULLABLE_HANDLE", 34 mojom.NULLABLE_DCPIPE: "_descriptor.TYPE_NULLABLE_HANDLE",
35 mojom.NULLABLE_DPPIPE: "_descriptor.TYPE_NULLABLE_HANDLE", 35 mojom.NULLABLE_DPPIPE: "_descriptor.TYPE_NULLABLE_HANDLE",
36 mojom.NULLABLE_MSGPIPE: "_descriptor.TYPE_NULLABLE_HANDLE", 36 mojom.NULLABLE_MSGPIPE: "_descriptor.TYPE_NULLABLE_HANDLE",
37 mojom.NULLABLE_SHAREDBUFFER: "_descriptor.TYPE_NULLABLE_HANDLE", 37 mojom.NULLABLE_SHAREDBUFFER: "_descriptor.TYPE_NULLABLE_HANDLE",
38 } 38 }
39 39
40 # int64 integers are not handled by array.array
Chris Masone 2014/09/09 14:20:26 Hm. That's a bit of an issue, as many CrOS platfor
qsr 2014/09/09 14:30:20 Hum... Not sure to understand why this is an issue
sdefresne 2014/09/10 08:40:00 Maybe expand the comment to say that int64/uint64
qsr 2014/09/10 09:23:38 Done.
41 _kind_to_typecode = {
42 mojom.INT8: "'b'",
43 mojom.UINT8: "'B'",
44 mojom.INT16: "'h'",
45 mojom.UINT16: "'H'",
46 mojom.INT32: "'i'",
47 mojom.UINT32: "'I'",
48 mojom.FLOAT: "'f'",
49 mojom.DOUBLE: "'d'",
50 }
51
40 52
41 def NameToComponent(name): 53 def NameToComponent(name):
42 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar -> 54 # insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar ->
43 # HTTP_Entry2_FooBar) 55 # HTTP_Entry2_FooBar)
44 name = re.sub('([^_])([A-Z][^A-Z_]+)', r'\1_\2', name) 56 name = re.sub('([^_])([A-Z][^A-Z_]+)', r'\1_\2', name)
45 # insert '_' between non upper and start of upper blocks (e.g., 57 # insert '_' between non upper and start of upper blocks (e.g.,
46 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar) 58 # HTTP_Entry2_FooBar -> HTTP_Entry2_Foo_Bar)
47 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name) 59 name = re.sub('([^A-Z_])([A-Z])', r'\1_\2', name)
48 return [x.lower() for x in name.split('_')] 60 return [x.lower() for x in name.split('_')]
49 61
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 104
93 def GetStructClass(kind): 105 def GetStructClass(kind):
94 name = [] 106 name = []
95 if kind.imported_from: 107 if kind.imported_from:
96 name.append(kind.imported_from['python_module']) 108 name.append(kind.imported_from['python_module'])
97 name.append(GetNameForElement(kind)) 109 name.append(GetNameForElement(kind))
98 return '.'.join(name) 110 return '.'.join(name)
99 111
100 def GetFieldType(kind, field=None): 112 def GetFieldType(kind, field=None):
101 if mojom.IsAnyArrayKind(kind): 113 if mojom.IsAnyArrayKind(kind):
102 arguments = [ GetFieldType(kind.kind) ] 114 if kind.kind in _kind_to_typecode:
115 arguments = [ _kind_to_typecode[kind.kind] ]
116 else:
117 arguments = [ GetFieldType(kind.kind) ]
103 if mojom.IsNullableKind(kind): 118 if mojom.IsNullableKind(kind):
104 arguments.append("nullable=True") 119 arguments.append("nullable=True")
105 if mojom.IsFixedArrayKind(kind): 120 if mojom.IsFixedArrayKind(kind):
106 arguments.append("length=%d" % kind.length) 121 arguments.append("length=%d" % kind.length)
107 return "_descriptor.ArrayType(%s)" % ", ".join(arguments) 122 if kind.kind in _kind_to_typecode:
123 return "_descriptor.NativeArrayType(%s)" % ", ".join(arguments)
124 else:
125 return "_descriptor.PointerArrayType(%s)" % ", ".join(arguments)
108 126
109 if mojom.IsStructKind(kind): 127 if mojom.IsStructKind(kind):
110 arguments = [ GetStructClass(kind) ] 128 arguments = [ GetStructClass(kind) ]
111 if mojom.IsNullableKind(kind): 129 if mojom.IsNullableKind(kind):
112 arguments.append("nullable=True") 130 arguments.append("nullable=True")
113 return "_descriptor.StructType(%s)" % ", ".join(arguments) 131 return "_descriptor.StructType(%s)" % ", ".join(arguments)
114 132
115 if mojom.IsEnumKind(kind): 133 if mojom.IsEnumKind(kind):
116 return GetFieldType(mojom.INT32) 134 return GetFieldType(mojom.INT32)
117 135
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 def GetImports(self): 251 def GetImports(self):
234 for each in self.module.imports: 252 for each in self.module.imports:
235 each['python_module'] = each['module_name'].replace('.mojom', '_mojom') 253 each['python_module'] = each['module_name'].replace('.mojom', '_mojom')
236 return self.module.imports 254 return self.module.imports
237 255
238 def GetJinjaParameters(self): 256 def GetJinjaParameters(self):
239 return { 257 return {
240 'lstrip_blocks': True, 258 'lstrip_blocks': True,
241 'trim_blocks': True, 259 'trim_blocks': True,
242 } 260 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698