| 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 # TODO(vtl): "data" is a pretty vague name. Rename it? | 5 # TODO(vtl): "data" is a pretty vague name. Rename it? |
| 6 | 6 |
| 7 import copy | 7 import copy |
| 8 | 8 |
| 9 import module as mojom | 9 import module as mojom |
| 10 | 10 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 kind = mojom.Map(KindFromData(kinds, first_kind, scope), | 142 kind = mojom.Map(KindFromData(kinds, first_kind, scope), |
| 143 KindFromData(kinds, second_kind, scope)) | 143 KindFromData(kinds, second_kind, scope)) |
| 144 else: | 144 else: |
| 145 kind = mojom.Kind(data) | 145 kind = mojom.Kind(data) |
| 146 | 146 |
| 147 kinds[data] = kind | 147 kinds[data] = kind |
| 148 return kind | 148 return kind |
| 149 | 149 |
| 150 def KindFromImport(original_kind, imported_from): | 150 def KindFromImport(original_kind, imported_from): |
| 151 """Used with 'import module' - clones the kind imported from the given | 151 """Used with 'import module' - clones the kind imported from the given |
| 152 module's namespace. Only used with Structs, Interfaces and Enums.""" | 152 module's namespace. Only used with Structs, Unions, Interfaces and Enums.""" |
| 153 kind = copy.copy(original_kind) | 153 kind = copy.copy(original_kind) |
| 154 # |shared_definition| is used to store various properties (see | 154 # |shared_definition| is used to store various properties (see |
| 155 # |AddSharedProperty()| in module.py), including |imported_from|. We don't | 155 # |AddSharedProperty()| in module.py), including |imported_from|. We don't |
| 156 # want the copy to share these with the original, so copy it if necessary. | 156 # want the copy to share these with the original, so copy it if necessary. |
| 157 if hasattr(original_kind, 'shared_definition'): | 157 if hasattr(original_kind, 'shared_definition'): |
| 158 kind.shared_definition = copy.copy(original_kind.shared_definition) | 158 kind.shared_definition = copy.copy(original_kind.shared_definition) |
| 159 kind.imported_from = imported_from | 159 kind.imported_from = imported_from |
| 160 return kind | 160 return kind |
| 161 | 161 |
| 162 def ImportFromData(module, data): | 162 def ImportFromData(module, data): |
| 163 import_module = data['module'] | 163 import_module = data['module'] |
| 164 | 164 |
| 165 import_item = {} | 165 import_item = {} |
| 166 import_item['module_name'] = import_module.name | 166 import_item['module_name'] = import_module.name |
| 167 import_item['namespace'] = import_module.namespace | 167 import_item['namespace'] = import_module.namespace |
| 168 import_item['module'] = import_module | 168 import_item['module'] = import_module |
| 169 | 169 |
| 170 # Copy the struct kinds from our imports into the current module. | 170 # Copy the struct kinds from our imports into the current module. |
| 171 importable_kinds = (mojom.Struct, mojom.Union, mojom.Enum, mojom.Interface) |
| 171 for kind in import_module.kinds.itervalues(): | 172 for kind in import_module.kinds.itervalues(): |
| 172 if (isinstance(kind, (mojom.Struct, mojom.Enum, mojom.Interface)) and | 173 if (isinstance(kind, importable_kinds) and |
| 173 kind.imported_from is None): | 174 kind.imported_from is None): |
| 174 kind = KindFromImport(kind, import_item) | 175 kind = KindFromImport(kind, import_item) |
| 175 module.kinds[kind.spec] = kind | 176 module.kinds[kind.spec] = kind |
| 176 # Ditto for values. | 177 # Ditto for values. |
| 177 for value in import_module.values.itervalues(): | 178 for value in import_module.values.itervalues(): |
| 178 if value.imported_from is None: | 179 if value.imported_from is None: |
| 179 # Values don't have shared definitions (since they're not nullable), so no | 180 # Values don't have shared definitions (since they're not nullable), so no |
| 180 # need to do anything special. | 181 # need to do anything special. |
| 181 value = copy.copy(value) | 182 value = copy.copy(value) |
| 182 value.imported_from = import_item | 183 value.imported_from = import_item |
| (...skipping 14 matching lines...) Expand all Loading... |
| 197 struct.spec = 'x:' + module.namespace + '.' + struct.name | 198 struct.spec = 'x:' + module.namespace + '.' + struct.name |
| 198 module.kinds[struct.spec] = struct | 199 module.kinds[struct.spec] = struct |
| 199 struct.enums = map(lambda enum: | 200 struct.enums = map(lambda enum: |
| 200 EnumFromData(module, enum, struct), data['enums']) | 201 EnumFromData(module, enum, struct), data['enums']) |
| 201 struct.constants = map(lambda constant: | 202 struct.constants = map(lambda constant: |
| 202 ConstantFromData(module, constant, struct), data['constants']) | 203 ConstantFromData(module, constant, struct), data['constants']) |
| 203 # Stash fields data here temporarily. | 204 # Stash fields data here temporarily. |
| 204 struct.fields_data = data['fields'] | 205 struct.fields_data = data['fields'] |
| 205 return struct | 206 return struct |
| 206 | 207 |
| 208 def UnionToData(union): |
| 209 return { |
| 210 istr(0, 'name'): union.name, |
| 211 istr(1, 'fields'): map(FieldToData, union.fields) |
| 212 } |
| 213 |
| 214 def UnionFromData(module, data): |
| 215 union = mojom.Union(module=module) |
| 216 union.name = data['name'] |
| 217 union.spec = 'x:' + module.namespace + '.' + union.name |
| 218 module.kinds[union.spec] = union |
| 219 # Stash fields data here temporarily. |
| 220 union.fields_data = data['fields'] |
| 221 return union |
| 222 |
| 207 def FieldToData(field): | 223 def FieldToData(field): |
| 208 data = { | 224 data = { |
| 209 istr(0, 'name'): field.name, | 225 istr(0, 'name'): field.name, |
| 210 istr(1, 'kind'): KindToData(field.kind) | 226 istr(1, 'kind'): KindToData(field.kind) |
| 211 } | 227 } |
| 212 if field.ordinal != None: | 228 if field.ordinal != None: |
| 213 data[istr(2, 'ordinal')] = field.ordinal | 229 data[istr(2, 'ordinal')] = field.ordinal |
| 214 if field.default != None: | 230 if field.default != None: |
| 215 data[istr(3, 'default')] = field.default | 231 data[istr(3, 'default')] = field.default |
| 216 return data | 232 return data |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 350 |
| 335 value = mojom.ConstantValue(module, parent_kind, constant) | 351 value = mojom.ConstantValue(module, parent_kind, constant) |
| 336 module.values[value.GetSpec()] = value | 352 module.values[value.GetSpec()] = value |
| 337 return constant | 353 return constant |
| 338 | 354 |
| 339 def ModuleToData(module): | 355 def ModuleToData(module): |
| 340 return { | 356 return { |
| 341 istr(0, 'name'): module.name, | 357 istr(0, 'name'): module.name, |
| 342 istr(1, 'namespace'): module.namespace, | 358 istr(1, 'namespace'): module.namespace, |
| 343 istr(2, 'structs'): map(StructToData, module.structs), | 359 istr(2, 'structs'): map(StructToData, module.structs), |
| 344 istr(3, 'interfaces'): map(InterfaceToData, module.interfaces) | 360 istr(3, 'interfaces'): map(InterfaceToData, module.interfaces), |
| 361 istr(4, 'unions'): map(UnionToData, module.unions), |
| 345 } | 362 } |
| 346 | 363 |
| 347 def ModuleFromData(data): | 364 def ModuleFromData(data): |
| 348 module = mojom.Module() | 365 module = mojom.Module() |
| 349 module.kinds = {} | 366 module.kinds = {} |
| 350 for kind in mojom.PRIMITIVES: | 367 for kind in mojom.PRIMITIVES: |
| 351 module.kinds[kind.spec] = kind | 368 module.kinds[kind.spec] = kind |
| 352 | 369 |
| 353 module.values = {} | 370 module.values = {} |
| 354 | 371 |
| 355 module.name = data['name'] | 372 module.name = data['name'] |
| 356 module.namespace = data['namespace'] | 373 module.namespace = data['namespace'] |
| 357 module.attributes = data['attributes'] | 374 module.attributes = data['attributes'] |
| 358 # Imports must come first, because they add to module.kinds which is used | 375 # Imports must come first, because they add to module.kinds which is used |
| 359 # by by the others. | 376 # by by the others. |
| 360 module.imports = map( | 377 module.imports = map( |
| 361 lambda import_data: ImportFromData(module, import_data), | 378 lambda import_data: ImportFromData(module, import_data), |
| 362 data['imports']) | 379 data['imports']) |
| 363 | 380 |
| 364 # First pass collects kinds. | 381 # First pass collects kinds. |
| 365 module.enums = map( | 382 module.enums = map( |
| 366 lambda enum: EnumFromData(module, enum, None), data['enums']) | 383 lambda enum: EnumFromData(module, enum, None), data['enums']) |
| 367 module.structs = map( | 384 module.structs = map( |
| 368 lambda struct: StructFromData(module, struct), data['structs']) | 385 lambda struct: StructFromData(module, struct), data['structs']) |
| 386 module.unions = map( |
| 387 lambda union: UnionFromData(module, struct), data.get('unions', [])) |
| 369 module.interfaces = map( | 388 module.interfaces = map( |
| 370 lambda interface: InterfaceFromData(module, interface), | 389 lambda interface: InterfaceFromData(module, interface), |
| 371 data['interfaces']) | 390 data['interfaces']) |
| 372 module.constants = map( | 391 module.constants = map( |
| 373 lambda constant: ConstantFromData(module, constant, None), | 392 lambda constant: ConstantFromData(module, constant, None), |
| 374 data['constants']) | 393 data['constants']) |
| 375 | 394 |
| 376 # Second pass expands fields and methods. This allows fields and parameters | 395 # Second pass expands fields and methods. This allows fields and parameters |
| 377 # to refer to kinds defined anywhere in the mojom. | 396 # to refer to kinds defined anywhere in the mojom. |
| 378 for struct in module.structs: | 397 for struct in module.structs: |
| 379 struct.fields = map(lambda field: | 398 struct.fields = map(lambda field: |
| 380 FieldFromData(module, field, struct), struct.fields_data) | 399 FieldFromData(module, field, struct), struct.fields_data) |
| 381 del struct.fields_data | 400 del struct.fields_data |
| 401 for union in module.unions: |
| 402 union.fields = map(lambda field: |
| 403 FieldFromData(module, field, union), union.fields_data) |
| 404 del union.fields_data |
| 382 for interface in module.interfaces: | 405 for interface in module.interfaces: |
| 383 interface.methods = map(lambda method: | 406 interface.methods = map(lambda method: |
| 384 MethodFromData(module, method, interface), interface.methods_data) | 407 MethodFromData(module, method, interface), interface.methods_data) |
| 385 del interface.methods_data | 408 del interface.methods_data |
| 386 | 409 |
| 387 return module | 410 return module |
| 388 | 411 |
| 389 def OrderedModuleFromData(data): | 412 def OrderedModuleFromData(data): |
| 390 module = ModuleFromData(data) | 413 module = ModuleFromData(data) |
| 391 for interface in module.interfaces: | 414 for interface in module.interfaces: |
| 392 next_ordinal = 0 | 415 next_ordinal = 0 |
| 393 for method in interface.methods: | 416 for method in interface.methods: |
| 394 if method.ordinal is None: | 417 if method.ordinal is None: |
| 395 method.ordinal = next_ordinal | 418 method.ordinal = next_ordinal |
| 396 next_ordinal = method.ordinal + 1 | 419 next_ordinal = method.ordinal + 1 |
| 397 return module | 420 return module |
| OLD | NEW |