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

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

Issue 317273006: Add serialization/deserialization of structs for mojo java bindings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Follow review Created 6 years, 6 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_java_generator.py
diff --git a/mojo/public/tools/bindings/generators/mojom_java_generator.py b/mojo/public/tools/bindings/generators/mojom_java_generator.py
index cc1569f03cc06d710c9d79197f051db3259e4ad8..c938d22db2b5dd6faeed723d9061b1434cd0416e 100644
--- a/mojo/public/tools/bindings/generators/mojom_java_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_java_generator.py
@@ -17,6 +17,8 @@ from mojom.generate.template_expander import UseJinja
GENERATOR_PREFIX = 'java'
+_HEADER_SIZE = 8
+
_spec_to_java_type = {
'b': 'boolean',
'd': 'double',
@@ -37,6 +39,26 @@ _spec_to_java_type = {
'u8': 'byte',
}
+_spec_to_decode_method = {
+ 'b': 'readBoolean',
+ 'd': 'readDouble',
+ 'f': 'readFloat',
+ 'h:d:c': 'readConsumerHandle',
+ 'h:d:p': 'readProducerHandle',
+ 'h:m': 'readMessagePipeHandle',
+ 'h': 'readUntypedHandle',
+ 'h:s': 'readSharedBufferHandle',
+ 'i16': 'readShort',
+ 'i32': 'readInt',
+ 'i64': 'readLong',
+ 'i8': 'readByte',
+ 's': 'readString',
+ 'u16': 'readShort',
+ 'u32': 'readInt',
+ 'u64': 'readLong',
+ 'u8': 'readByte',
+}
+
_java_primitive_to_boxed_type = {
'boolean': 'Boolean',
'byte': 'Byte',
@@ -99,6 +121,48 @@ def ParseStringAttribute(attribute):
assert isinstance(attribute, basestring)
return attribute
+def IsArray(kind):
+ return isinstance(kind, (mojom.Array, mojom.FixedArray))
+
+@contextfilter
+def DecodeMethod(context, kind, offset, bit):
+ def _DecodeMethodName(kind):
+ if IsArray(kind):
+ return _DecodeMethodName(kind.kind) + 's'
+ if isinstance(kind, mojom.Enum):
+ return _DecodeMethodName(mojom.INT32)
+ if isinstance(kind, mojom.InterfaceRequest):
+ return "readInterfaceRequest"
+ if isinstance(kind, mojom.Interface):
+ return "readServiceInterface"
+ return _spec_to_decode_method[kind.spec]
+ methodName = _DecodeMethodName(kind)
+ additionalParams = ''
+ if (kind == mojom.BOOL):
+ additionalParams = ', %d' % bit
+ if (isinstance(kind, mojom.Interface) or
+ (IsArray(kind) and
+ isinstance(kind.kind, mojom.Interface))):
+ interfaceKind = kind
+ if IsArray(kind):
+ interfaceKind = kind.kind
+ additionalParams = ', %s.BUILDER' % GetJavaType(context, interfaceKind)
rmcilroy 2014/06/25 13:14:40 I think this would be clearer as: if isinstance(
qsr 2014/06/25 14:43:06 Done.
+ return '%s(%s%s)' % (methodName, offset, additionalParams)
+
+@contextfilter
+def EncodeMethod(context, kind, variable, offset, bit):
+ additionalParams = ''
+ if (kind == mojom.BOOL):
+ additionalParams = ', %d' % bit
+ if (isinstance(kind, mojom.Interface) or
+ (IsArray(kind) and
+ isinstance(kind.kind, mojom.Interface))):
+ interfaceKind = kind
+ if IsArray(kind):
+ interfaceKind = kind.kind
+ additionalParams = ', %s.BUILDER' % GetJavaType(context, interfaceKind)
rmcilroy 2014/06/25 13:14:40 ditto
qsr 2014/06/25 14:43:06 Done.
+ return 'encode(%s, %s%s)' % (variable, offset, additionalParams)
+
def GetPackage(module):
if 'JavaPackage' in module.attributes:
return ParseStringAttribute(module.attributes['JavaPackage'])
@@ -135,7 +199,7 @@ def GetJavaType(context, kind, boxed=False):
if isinstance(kind, mojom.InterfaceRequest):
return ("org.chromium.mojo.bindings.InterfaceRequest<%s>" %
GetNameForKind(context, kind.kind))
- if isinstance(kind, (mojom.Array, mojom.FixedArray)):
+ if IsArray(kind):
return "%s[]" % GetJavaType(context, kind.kind)
if isinstance(kind, mojom.Enum):
return "int"
@@ -154,6 +218,12 @@ def DefaultValue(context, field):
ExpressionToText(context, field.default))
@contextfilter
+def NewArray(context, kind, size):
+ if IsArray(kind.kind):
+ return NewArray(context, kind.kind, size) + '[]'
+ return 'new %s[%s]' % (GetJavaType(context, kind.kind), size)
+
+@contextfilter
def ExpressionToText(context, token):
def _TranslateNamedValue(named_value):
entity_name = GetNameForElement(named_value)
@@ -174,6 +244,12 @@ def ExpressionToText(context, token):
return token + 'L'
return token
+def IsPointerArrayKind(kind):
+ if not IsArray(kind):
+ return False
+ sub_kind = kind.kind
+ return generator.IsObjectKind(sub_kind)
+
def GetConstantsMainEntityName(module):
if 'JavaConstantsClassName' in module.attributes:
return ParseStringAttribute(module.attributes['JavaConstantsClassName'])
@@ -187,10 +263,16 @@ class Generator(generator.Generator):
java_filters = {
"interface_response_name": GetInterfaceResponseName,
"default_value": DefaultValue,
+ "decode_method": DecodeMethod,
"expression_to_text": ExpressionToText,
+ "encode_method": EncodeMethod,
"is_handle": IsHandle,
+ "is_pointer_array_kind": IsPointerArrayKind,
+ "is_struct_kind": lambda kind: isinstance(kind, mojom.Struct),
"java_type": GetJavaType,
"name": GetNameForElement,
+ "new_array": NewArray,
+ "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
}
def GetJinjaExports(self):

Powered by Google App Engine
This is Rietveld 408576698