| OLD | NEW |
| 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 import mojom | 5 import mojom |
| 6 | 6 |
| 7 # mojom_data provides a mechanism to turn mojom Modules to dictionaries and | 7 # mojom_data provides a mechanism to turn mojom Modules to dictionaries and |
| 8 # back again. This can be used to persist a mojom Module created progromatically | 8 # back again. This can be used to persist a mojom Module created progromatically |
| 9 # or to read a dictionary from code or a file. | 9 # or to read a dictionary from code or a file. |
| 10 # Example: | 10 # Example: |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 return kinds[data] | 47 return kinds[data] |
| 48 if data.startswith('a:'): | 48 if data.startswith('a:'): |
| 49 kind = mojom.Array() | 49 kind = mojom.Array() |
| 50 kind.kind = KindFromData(kinds, data[2:]) | 50 kind.kind = KindFromData(kinds, data[2:]) |
| 51 else: | 51 else: |
| 52 kind = mojom.Kind() | 52 kind = mojom.Kind() |
| 53 kind.spec = data | 53 kind.spec = data |
| 54 kinds[data] = kind | 54 kinds[data] = kind |
| 55 return kind | 55 return kind |
| 56 | 56 |
| 57 def ImportFromData(data): | 57 def ImportFromData(module, data): |
| 58 import_module = data['module'] | 58 import_module = data['module'] |
| 59 | 59 |
| 60 import_item = {} | 60 import_item = {} |
| 61 import_item['module_name'] = import_module.name | 61 import_item['module_name'] = import_module.name |
| 62 import_item['namespace'] = import_module.namespace | 62 import_item['namespace'] = import_module.namespace |
| 63 |
| 64 # Copy the struct kinds from our imports into the current module. |
| 65 for kind in import_module.kinds.itervalues(): |
| 66 # TODO(mpcomplete): Handle enums |
| 67 if isinstance(kind, mojom.Struct) and kind.imported_from is None: |
| 68 kind = mojom.Struct.CreateFromImport(kind, import_item) |
| 69 module.kinds[kind.spec] = kind |
| 63 return import_item | 70 return import_item |
| 64 | 71 |
| 65 def StructToData(struct): | 72 def StructToData(struct): |
| 66 return { | 73 return { |
| 67 istr(0, 'name'): struct.name, | 74 istr(0, 'name'): struct.name, |
| 68 istr(1, 'fields'): map(FieldToData, struct.fields) | 75 istr(1, 'fields'): map(FieldToData, struct.fields) |
| 69 } | 76 } |
| 70 | 77 |
| 71 def StructFromData(module, data): | 78 def StructFromData(module, data): |
| 72 struct = mojom.Struct() | 79 struct = mojom.Struct() |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 istr(1, 'namespace'): module.namespace, | 182 istr(1, 'namespace'): module.namespace, |
| 176 istr(2, 'structs'): map(StructToData, module.structs), | 183 istr(2, 'structs'): map(StructToData, module.structs), |
| 177 istr(3, 'interfaces'): map(InterfaceToData, module.interfaces) | 184 istr(3, 'interfaces'): map(InterfaceToData, module.interfaces) |
| 178 } | 185 } |
| 179 | 186 |
| 180 def ModuleFromData(data): | 187 def ModuleFromData(data): |
| 181 module = mojom.Module() | 188 module = mojom.Module() |
| 182 module.kinds = {} | 189 module.kinds = {} |
| 183 for kind in mojom.PRIMITIVES: | 190 for kind in mojom.PRIMITIVES: |
| 184 module.kinds[kind.spec] = kind | 191 module.kinds[kind.spec] = kind |
| 185 # Copy the struct kinds from our imports into the current module. | |
| 186 for import_data in data['imports']: | |
| 187 import_module = import_data['module'] | |
| 188 for kind in import_module.kinds.itervalues(): | |
| 189 # TODO(mpcomplete): Handle enums | |
| 190 if isinstance(kind, mojom.Struct): | |
| 191 kind = mojom.Struct.CreateFromImport( | |
| 192 kind, import_module.namespace) | |
| 193 module.kinds[kind.spec] = kind | |
| 194 | 192 |
| 195 module.name = data['name'] | 193 module.name = data['name'] |
| 196 module.namespace = data['namespace'] | 194 module.namespace = data['namespace'] |
| 195 # Imports must come first, because they add to module.kinds which is used |
| 196 # by by the others. |
| 197 module.imports = map( | 197 module.imports = map( |
| 198 lambda import_data: ImportFromData(import_data), data['imports']) | 198 lambda import_data: ImportFromData(module, import_data), |
| 199 data['imports']) |
| 199 module.structs = map( | 200 module.structs = map( |
| 200 lambda struct: StructFromData(module, struct), data['structs']) | 201 lambda struct: StructFromData(module, struct), data['structs']) |
| 201 module.interfaces = map( | 202 module.interfaces = map( |
| 202 lambda interface: | 203 lambda interface: InterfaceFromData(module, interface), |
| 203 InterfaceFromData(module, interface), data['interfaces']) | 204 data['interfaces']) |
| 204 if data.has_key('enums'): | 205 if data.has_key('enums'): |
| 205 module.enums = map( | 206 module.enums = map( |
| 206 lambda enum: EnumFromData(module.kinds, enum), data['enums']) | 207 lambda enum: EnumFromData(module.kinds, enum), data['enums']) |
| 207 return module | 208 return module |
| 208 | 209 |
| 209 def OrderedModuleFromData(data): | 210 def OrderedModuleFromData(data): |
| 210 module = ModuleFromData(data) | 211 module = ModuleFromData(data) |
| 211 next_interface_ordinal = 0 | 212 next_interface_ordinal = 0 |
| 212 for interface in module.interfaces: | 213 for interface in module.interfaces: |
| 213 next_ordinal = 0 | 214 next_ordinal = 0 |
| 214 for method in interface.methods: | 215 for method in interface.methods: |
| 215 if method.ordinal is None: | 216 if method.ordinal is None: |
| 216 method.ordinal = next_ordinal | 217 method.ordinal = next_ordinal |
| 217 next_ordinal = method.ordinal + 1 | 218 next_ordinal = method.ordinal + 1 |
| 218 return module | 219 return module |
| OLD | NEW |