Index: mojo/public/tools/bindings/pylib/mojom/generate/module.py |
diff --git a/mojo/public/tools/bindings/pylib/mojom/generate/module.py b/mojo/public/tools/bindings/pylib/mojom/generate/module.py |
index c55b85e2e3313667dfaafb2bde993cbc12570ea2..9bfdf829572957ef119c11db7a222d1f878df6db 100644 |
--- a/mojo/public/tools/bindings/pylib/mojom/generate/module.py |
+++ b/mojo/public/tools/bindings/pylib/mojom/generate/module.py |
@@ -13,29 +13,49 @@ |
# method.AddParameter('baz', 0, mojom.INT32) |
# |
+ |
class Kind(object): |
def __init__(self, spec=None): |
self.spec = spec |
self.parent_kind = None |
+ |
+class ReferenceKind(Kind): |
+ """ReferenceKind represents pointer types and handle types. |
+ A type is nullable means that NULL (for pointer types) or invalid handle |
+ (for handle types) is a legal value for the type. |
+ """ |
+ |
+ def __init__(self, spec=None, is_nullable=False): |
+ assert spec is None or is_nullable == spec.startswith('?') |
+ Kind.__init__(self, spec) |
+ self.is_nullable = is_nullable |
+ |
+ |
# Initialize the set of primitive types. These can be accessed by clients. |
-BOOL = Kind('b') |
-INT8 = Kind('i8') |
-INT16 = Kind('i16') |
-INT32 = Kind('i32') |
-INT64 = Kind('i64') |
-UINT8 = Kind('u8') |
-UINT16 = Kind('u16') |
-UINT32 = Kind('u32') |
-UINT64 = Kind('u64') |
-FLOAT = Kind('f') |
-DOUBLE = Kind('d') |
-STRING = Kind('s') |
-HANDLE = Kind('h') |
-DCPIPE = Kind('h:d:c') |
-DPPIPE = Kind('h:d:p') |
-MSGPIPE = Kind('h:m') |
-SHAREDBUFFER = Kind('h:s') |
+BOOL = Kind('b') |
+INT8 = Kind('i8') |
+INT16 = Kind('i16') |
+INT32 = Kind('i32') |
+INT64 = Kind('i64') |
+UINT8 = Kind('u8') |
+UINT16 = Kind('u16') |
+UINT32 = Kind('u32') |
+UINT64 = Kind('u64') |
+FLOAT = Kind('f') |
+DOUBLE = Kind('d') |
+STRING = ReferenceKind('s') |
+HANDLE = ReferenceKind('h') |
+DCPIPE = ReferenceKind('h:d:c') |
+DPPIPE = ReferenceKind('h:d:p') |
+MSGPIPE = ReferenceKind('h:m') |
+SHAREDBUFFER = ReferenceKind('h:s') |
+NLBL_STRING = ReferenceKind('?s', True) |
+NLBL_HANDLE = ReferenceKind('?h', True) |
+NLBL_DCPIPE = ReferenceKind('?h:d:c', True) |
+NLBL_DPPIPE = ReferenceKind('?h:d:p', True) |
+NLBL_MSGPIPE = ReferenceKind('?h:m', True) |
+NLBL_SHAREDBUFFER = ReferenceKind('?h:s', True) |
# Collection of all Primitive types |
@@ -56,7 +76,13 @@ PRIMITIVES = ( |
DCPIPE, |
DPPIPE, |
MSGPIPE, |
- SHAREDBUFFER |
+ SHAREDBUFFER, |
+ NLBL_STRING, |
+ NLBL_HANDLE, |
+ NLBL_DCPIPE, |
+ NLBL_DPPIPE, |
+ NLBL_MSGPIPE, |
+ NLBL_SHAREDBUFFER |
) |
@@ -100,7 +126,7 @@ class Field(object): |
self.default = default |
-class Struct(Kind): |
+class Struct(ReferenceKind): |
def __init__(self, name=None, module=None): |
self.name = name |
self.module = module |
@@ -109,7 +135,7 @@ class Struct(Kind): |
spec = 'x:' + name |
else: |
spec = None |
- Kind.__init__(self, spec) |
+ ReferenceKind.__init__(self, spec) |
self.fields = [] |
def AddField(self, name, kind, ordinal=None, default=None): |
@@ -118,30 +144,32 @@ class Struct(Kind): |
return field |
-class Array(Kind): |
+class Array(ReferenceKind): |
def __init__(self, kind=None): |
self.kind = kind |
if kind is not None: |
- Kind.__init__(self, 'a:' + kind.spec) |
+ ReferenceKind.__init__(self, 'a:' + kind.spec) |
else: |
- Kind.__init__(self) |
+ ReferenceKind.__init__(self) |
+ |
-class FixedArray(Kind): |
- def __init__(self, length, kind=None): |
+class FixedArray(ReferenceKind): |
+ def __init__(self, length=-1, kind=None): |
self.kind = kind |
self.length = length |
if kind is not None: |
- Kind.__init__(self, 'a' + length + ':' + kind.spec) |
+ ReferenceKind.__init__(self, 'a%d:%s' % (length, kind.spec)) |
else: |
- Kind.__init__(self) |
+ ReferenceKind.__init__(self) |
+ |
-class InterfaceRequest(Kind): |
+class InterfaceRequest(ReferenceKind): |
def __init__(self, kind=None): |
self.kind = kind |
if kind is not None: |
- Kind.__init__(self, 'r:' + kind.spec) |
+ ReferenceKind.__init__(self, 'r:' + kind.spec) |
else: |
- Kind.__init__(self) |
+ ReferenceKind.__init__(self) |
class Parameter(object): |
@@ -173,7 +201,7 @@ class Method(object): |
return parameter |
-class Interface(Kind): |
+class Interface(ReferenceKind): |
def __init__(self, name=None, client=None, module=None): |
self.module = module |
self.name = name |
@@ -182,7 +210,7 @@ class Interface(Kind): |
spec = 'x:' + name |
else: |
spec = None |
- Kind.__init__(self, spec) |
+ ReferenceKind.__init__(self, spec) |
self.client = client |
self.methods = [] |
@@ -228,3 +256,30 @@ class Module(object): |
struct=Struct(name, module=self) |
self.structs.append(struct) |
return struct |
+ |
+ |
+def MakeNullableKind(ref_kind): |
+ assert isinstance(ref_kind, ReferenceKind) |
+ assert not ref_kind.is_nullable |
+ |
+ if ref_kind == STRING: |
+ return NLBL_STRING |
+ if ref_kind == HANDLE: |
+ return NLBL_HANDLE |
+ if ref_kind == DCPIPE: |
+ return NLBL_DCPIPE |
+ if ref_kind == DPPIPE: |
+ return NLBL_DPPIPE |
+ if ref_kind == MSGPIPE: |
+ return NLBL_MSGPIPE |
+ if ref_kind == SHAREDBUFFER: |
+ return NLBL_SHAREDBUFFER |
+ |
+ nullable_kind = type(ref_kind)() |
+ # Make a shallow copy of the instance's attributes. |
+ nullable_kind.__dict__ = ref_kind.__dict__.copy() |
yzshen1
2014/08/05 05:11:43
Hmm... Thinking more about this solution and it se
yzshen1
2014/08/06 21:39:27
I think I have addressed this issue in the latest
|
+ if ref_kind.spec is not None: |
+ nullable_kind.spec = '?' + ref_kind.spec |
+ nullable_kind.is_nullable = True |
+ |
+ return nullable_kind |