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

Unified Diff: mojo/public/tools/bindings/generators/mojom_python_generator.py

Issue 570563002: mojo: Starting serialization for python bindings. (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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/generators/mojom_python_generator.py
diff --git a/mojo/public/tools/bindings/generators/mojom_python_generator.py b/mojo/public/tools/bindings/generators/mojom_python_generator.py
index 75992a3842cada0491294bb7d13b610e073035d0..e0b3aea3ec0653f80de5fe2db10c6c4f3467263b 100644
--- a/mojo/public/tools/bindings/generators/mojom_python_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_python_generator.py
@@ -40,17 +40,33 @@ _kind_to_type = {
# int64 integers are not handled by array.array. int64/uint64 array are
# supported but storage is not optimized (ie. they are plain python list, not
# array.array)
-_kind_to_typecode = {
- mojom.INT8: "'b'",
- mojom.UINT8: "'B'",
- mojom.INT16: "'h'",
- mojom.UINT16: "'H'",
- mojom.INT32: "'i'",
- mojom.UINT32: "'I'",
- mojom.FLOAT: "'f'",
- mojom.DOUBLE: "'d'",
+_kind_to_typecode_for_array = {
sdefresne 2014/09/15 08:46:54 nit: _kind_to_typecode_for_native_array
qsr 2014/09/15 11:01:53 Done.
+ mojom.INT8: "b",
+ mojom.UINT8: "B",
+ mojom.INT16: "h",
+ mojom.UINT16: "H",
+ mojom.INT32: "i",
+ mojom.UINT32: "I",
+ mojom.FLOAT: "f",
+ mojom.DOUBLE: "d",
}
+_kind_to_typecode = dict(_kind_to_typecode_for_array)
+_kind_to_typecode.update({
+ mojom.INT64: "q",
+ mojom.UINT64: "Q",
+ mojom.HANDLE: "i",
+ mojom.DCPIPE: "i",
+ mojom.DPPIPE: "i",
+ mojom.MSGPIPE: "i",
+ mojom.SHAREDBUFFER: "i",
+ mojom.NULLABLE_HANDLE: "i",
+ mojom.NULLABLE_DCPIPE: "i",
+ mojom.NULLABLE_DPPIPE: "i",
+ mojom.NULLABLE_MSGPIPE: "i",
+ mojom.NULLABLE_SHAREDBUFFER: "i",
+})
+
def NameToComponent(name):
# insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar ->
@@ -113,18 +129,21 @@ def GetStructClass(kind):
def GetFieldType(kind, field=None):
if mojom.IsAnyArrayKind(kind):
- if kind.kind in _kind_to_typecode:
- arguments = [ _kind_to_typecode[kind.kind] ]
- else:
- arguments = [ GetFieldType(kind.kind) ]
+ arguments = []
+ if kind.kind in _kind_to_typecode_for_array:
+ arguments.append('\'%s\'' %_kind_to_typecode_for_array[kind.kind])
sdefresne 2014/09/15 08:46:54 nit: you can use '%r' instead of '\'%s\'' to get a
qsr 2014/09/15 11:01:53 Done.
+ elif kind.kind != mojom.BOOL:
+ arguments.append(GetFieldType(kind.kind))
if mojom.IsNullableKind(kind):
arguments.append("nullable=True")
if mojom.IsFixedArrayKind(kind):
arguments.append("length=%d" % kind.length)
- if kind.kind in _kind_to_typecode:
- return "_descriptor.NativeArrayType(%s)" % ", ".join(arguments)
- else:
- return "_descriptor.PointerArrayType(%s)" % ", ".join(arguments)
+ array_type = 'GenericArrayType'
+ if kind.kind == mojom.BOOL:
+ array_type = 'BooleanArrayType'
+ elif kind.kind in _kind_to_typecode_for_array:
+ array_type = 'NativeArrayType'
+ return "_descriptor.%s(%s)" % (array_type, ", ".join(arguments))
if mojom.IsStructKind(kind):
arguments = [ GetStructClass(kind) ]
@@ -137,19 +156,45 @@ def GetFieldType(kind, field=None):
return _kind_to_type.get(kind, "_descriptor.TYPE_NONE")
+def GetPackedFormatString(struct):
+ res = '='
+ for byte in struct.bytes:
+ if len(byte.packed_fields) > 1:
+ res += 'B'
+ else:
+ for packed_field in byte.packed_fields:
+ kind = packed_field.field.kind
+ if mojom.IsEnumKind(kind):
+ res += 'i'
+ else:
+ res += _kind_to_typecode.get(packed_field.field.kind, 'Q')
+ if byte.is_padding:
+ res += 'x'
+ return res
+
def GetFieldDescriptor(packed_field):
field = packed_field.field
+ class_name = 'SingleFieldGroup'
+ if field.kind == mojom.BOOL:
+ class_name = 'FieldDescriptor'
arguments = [ '\'%s\'' % field.name ]
arguments.append(GetFieldType(field.kind, field))
- arguments.append(str(packed_field.offset))
- if field.kind == mojom.BOOL:
- arguments.append('bit_offset=%d' % packed_field.bit)
+ arguments.append(str(packed_field.field.ordinal))
if field.default:
if mojom.IsStructKind(field.kind):
arguments.append('default_value=True')
else:
arguments.append('default_value=%s' % ExpressionToText(field.default))
- return '_descriptor.FieldDescriptor(%s)' % ', '.join(arguments)
+ return '_descriptor.%s(%s)' % (class_name, ', '.join(arguments))
+
+def GetFieldGroup(byte):
+ if len(byte.packed_fields) > 1:
+ descriptors = map(GetFieldDescriptor, byte.packed_fields)
+ return '_descriptor.BooleanGroup([%s])' % ', '.join(descriptors)
+ for packed_field in byte.packed_fields:
+ return GetFieldDescriptor(packed_field)
sdefresne 2014/09/15 11:14:46 This return in a for loop is strange. Do you only
qsr 2014/09/15 11:42:00 Refactored by asserting the list has a single elem
+ print byte
sdefresne 2014/09/15 08:46:55 Are those two lines debug? Since the last line is
qsr 2014/09/15 11:01:53 The assert False is correct, the print was just fo
sdefresne 2014/09/15 11:14:46 Oh, you mean this line should not be reached? What
qsr 2014/09/15 11:42:00 Removed, asserting the list has a single element i
+ assert False
def ComputeStaticValues(module):
in_progress = set()
@@ -229,12 +274,16 @@ def ComputeStaticValues(module):
return module
+_HEADER_SIZE = 8
+
class Generator(generator.Generator):
python_filters = {
'expression_to_text': ExpressionToText,
- 'field_descriptor': GetFieldDescriptor,
+ 'field_group': GetFieldGroup,
'name': GetNameForElement,
+ 'packed_format_string': GetPackedFormatString,
+ 'struct_size': lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
}
@UseJinja('python_templates/module.py.tmpl', filters=python_filters)

Powered by Google App Engine
This is Rietveld 408576698