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

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom/generate/module.py

Issue 2339413004: Allow Mojo structs as map keys (Closed)
Patch Set: Address sammc's comments and improve Blink tests Created 4 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 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 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 # This module's classes provide an interface to mojo modules. Modules are 5 # This module's classes provide an interface to mojo modules. Modules are
6 # collections of interfaces and structs to be used by mojo ipc clients and 6 # collections of interfaces and structs to be used by mojo ipc clients and
7 # servers. 7 # servers.
8 # 8 #
9 # A simple interface would be created this way: 9 # A simple interface would be created this way:
10 # module = mojom.generate.module.Module('Foo') 10 # module = mojom.generate.module.Module('Foo')
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 if self.attributes else None 287 if self.attributes else None
288 288
289 289
290 class StructField(Field): pass 290 class StructField(Field): pass
291 291
292 292
293 class UnionField(Field): pass 293 class UnionField(Field): pass
294 294
295 295
296 class Struct(ReferenceKind): 296 class Struct(ReferenceKind):
297 """A struct with typed fields.
298
299 Attributes:
300 name: {str} The name of the struct type.
301 native_only: {bool} Does the struct have a body (i.e. any fields) or is it
302 purely a native struct that must be type-mapped.
yzshen1 2016/09/22 23:48:56 Nit: it doesn't have to be typemapped. We have Nat
tibell 2016/09/23 00:07:08 Done.
303 module: {Module} The defining module.
304 imported_from: {dict} Information about where this union was
305 imported from.
306 fields: {List[StructField]} The members of the union.
307 attributes: {dict} Additional information about the struct, such as
308 which Java class name to use to represent it in the generated
yzshen1 2016/09/22 23:48:56 I thought we didn't support this? (But we support
tibell 2016/09/23 00:07:08 Done.
309 bindings.
310 """
311
297 ReferenceKind.AddSharedProperty('name') 312 ReferenceKind.AddSharedProperty('name')
298 ReferenceKind.AddSharedProperty('native_only') 313 ReferenceKind.AddSharedProperty('native_only')
299 ReferenceKind.AddSharedProperty('module') 314 ReferenceKind.AddSharedProperty('module')
300 ReferenceKind.AddSharedProperty('imported_from') 315 ReferenceKind.AddSharedProperty('imported_from')
301 ReferenceKind.AddSharedProperty('fields') 316 ReferenceKind.AddSharedProperty('fields')
302 ReferenceKind.AddSharedProperty('attributes') 317 ReferenceKind.AddSharedProperty('attributes')
303 318
304 def __init__(self, name=None, module=None, attributes=None): 319 def __init__(self, name=None, module=None, attributes=None):
305 if name is not None: 320 if name is not None:
306 spec = 'x:' + name 321 spec = 'x:' + name
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 ReferenceKind.AddSharedProperty('key_kind') 433 ReferenceKind.AddSharedProperty('key_kind')
419 ReferenceKind.AddSharedProperty('value_kind') 434 ReferenceKind.AddSharedProperty('value_kind')
420 435
421 def __init__(self, key_kind=None, value_kind=None): 436 def __init__(self, key_kind=None, value_kind=None):
422 if (key_kind is not None and value_kind is not None): 437 if (key_kind is not None and value_kind is not None):
423 ReferenceKind.__init__(self, 438 ReferenceKind.__init__(self,
424 'm[' + key_kind.spec + '][' + value_kind.spec + 439 'm[' + key_kind.spec + '][' + value_kind.spec +
425 ']') 440 ']')
426 if IsNullableKind(key_kind): 441 if IsNullableKind(key_kind):
427 raise Exception("Nullable kinds cannot be keys in maps.") 442 raise Exception("Nullable kinds cannot be keys in maps.")
428 if IsStructKind(key_kind):
429 # TODO(erg): It would sometimes be nice if we could key on struct
430 # values. However, what happens if the struct has a handle in it? Or
431 # non-copyable data like an array?
432 raise Exception("Structs cannot be keys in maps.")
433 if IsAnyHandleKind(key_kind): 443 if IsAnyHandleKind(key_kind):
434 raise Exception("Handles cannot be keys in maps.") 444 raise Exception("Handles cannot be keys in maps.")
435 if IsAnyInterfaceKind(key_kind): 445 if IsAnyInterfaceKind(key_kind):
436 raise Exception("Interfaces cannot be keys in maps.") 446 raise Exception("Interfaces cannot be keys in maps.")
437 if IsArrayKind(key_kind): 447 if IsArrayKind(key_kind):
438 raise Exception("Arrays cannot be keys in maps.") 448 raise Exception("Arrays cannot be keys in maps.")
439 else: 449 else:
440 ReferenceKind.__init__(self) 450 ReferenceKind.__init__(self)
441 451
442 self.key_kind = key_kind 452 self.key_kind = key_kind
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
833 if _ContainsAssociatedKinds(param.kind, visited_kinds): 843 if _ContainsAssociatedKinds(param.kind, visited_kinds):
834 return True 844 return True
835 return False 845 return False
836 846
837 847
838 def HasSyncMethods(interface): 848 def HasSyncMethods(interface):
839 for method in interface.methods: 849 for method in interface.methods:
840 if method.sync: 850 if method.sync:
841 return True 851 return True
842 return False 852 return False
853
854
855 def ContainsHandles(kind):
856 """Check if the kind contains any handles.
857
858 This check is recursive so it checks all struct fields, containers elements,
859 etc.
860
861 Args:
862 struct: {Kind} The kind to check.
863
864 Returns:
865 {bool}: True if the kind contains handles.
866 """
867 # We remember the types we already checked to avoid infinite recursion when
868 # checking recursive (or mutually recursive) types:
869 checked = set()
870 def Check(kind):
871 if kind.spec in checked:
872 return False
873 checked.add(kind.spec)
874 if IsStructKind(kind):
875 return any(Check(field.kind) for field in kind.fields)
876 elif IsUnionKind(kind):
877 return any(Check(field.kind) for field in kind.fields)
878 elif IsAnyHandleKind(kind):
879 return True
880 elif IsAnyInterfaceKind(kind):
881 # Interfaces are wrappers around handles.
yzshen1 2016/09/22 23:48:56 Strictly speaking, associated interfaces are not h
tibell 2016/09/23 00:07:08 Done.
882 return True
883 elif IsArrayKind(kind):
884 return Check(kind.kind)
885 elif IsMapKind(kind):
886 return Check(kind.key_kind) or Check(kind.value_kind)
887 else:
888 return False
889 return Check(kind)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698