Chromium Code Reviews| Index: mojo/public/python/mojo_bindings/serialization.py |
| diff --git a/mojo/public/python/mojo_bindings/serialization.py b/mojo/public/python/mojo_bindings/serialization.py |
| index 32f60f066dbad9a320b08fb40b81d59532f60433..5aff488e1468d92f9d1e8c534493fcb86d00cd38 100644 |
| --- a/mojo/public/python/mojo_bindings/serialization.py |
| +++ b/mojo/public/python/mojo_bindings/serialization.py |
| @@ -218,3 +218,69 @@ def _GetStruct(groups): |
| if alignment_needed: |
| codes.append('x' * alignment_needed) |
| return struct.Struct(''.join(codes)) |
| + |
| + |
| +class UnionSerializer(object): |
| + """ |
| + Helper class to serialize/deserialize a union. |
| + """ |
| + def __init__(self, fields): |
| + self._fields = {field.index: field for field in fields} |
| + |
| + def SerializeInline(self, union, handle_offset): |
| + data = bytearray() |
| + field = self._fields[union.tag] |
| + |
| + (entry, handles) = field.field_type.Serialize( |
| + union.data, 8, data, handle_offset) |
| + |
| + if hasattr(field.field_type, 'union_type'): |
|
qsr
2015/07/16 09:35:26
I really don't like this.
Each time I see a CL fo
azani
2015/07/16 21:57:38
I can certainly see your point, but that would als
qsr
2015/07/17 10:53:58
Well, first of all this is a 8 bytes overhead at w
|
| + nested_union = bytearray(16) |
| + HEADER_STRUCT.pack_into(nested_union, 0, entry[0], entry[1]) |
| + struct.pack_into('<Q', nested_union, 8, entry[2]) |
|
qsr
2015/07/16 09:35:25
You should have a constant for this struct -> you
azani
2015/07/16 21:57:39
Done.
|
| + |
| + data = nested_union + data |
| + entry = 8 |
| + |
| + return (16, union.tag, entry, data), handles |
| + |
| + def Serialize(self, union, handle_offset): |
| + (size, tag, entry, extra_data), handles = self.SerializeInline( |
| + union, handle_offset) |
| + data = bytearray(16) |
| + data.extend(extra_data) |
| + |
| + field = self._fields[union.tag] |
| + |
| + HEADER_STRUCT.pack_into(data, 0, size, tag) |
| + typecode = field.GetTypeCode() |
| + if hasattr(field.field_type, 'union_type'): |
| + typecode = 'Q' |
| + |
| + struct.pack_into('<%s' % typecode, data, 8, entry) |
| + return data, handles |
| + |
| + def Deserialize(self, context, union_class): |
| + if len(context.data) < HEADER_STRUCT.size: |
| + raise DeserializationException( |
| + 'Available data too short to contain header.') |
| + (size, tag) = HEADER_STRUCT.unpack_from(context.data) |
| + if size == 0: |
| + return None |
| + |
| + if size != 16: |
| + raise DeserializationException('Invalid union size %s' % size) |
| + |
| + field = self._fields[tag] |
| + if hasattr(field.field_type, 'union_type'): |
| + ptr = struct.unpack_from('<Q', context.data, 8)[0] |
| + value = field.field_type.Deserialize(ptr, context.GetSubContext(ptr+8)) |
| + else: |
| + raw_value = struct.unpack_from( |
| + field.GetTypeCode(), context.data, 8)[0] |
| + value = field.field_type.Deserialize(raw_value, context.GetSubContext(8)) |
| + |
| + union = union_class.__new__(union_class) |
| + union._tag = tag |
| + union._data = value |
| + return union |