| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 package serialization | 5 package serialization |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "compress/gzip" | 9 "compress/gzip" |
| 10 "encoding/base64" | 10 "encoding/base64" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 ////////////////////////////////////////////////// | 21 ////////////////////////////////////////////////// |
| 22 | 22 |
| 23 // This variable may be set to false in order to omit emitting line and | 23 // This variable may be set to false in order to omit emitting line and |
| 24 // column numbers. | 24 // column numbers. |
| 25 var emitLineAndColumnNumbers bool = true | 25 var emitLineAndColumnNumbers bool = true |
| 26 | 26 |
| 27 // This variable may be set to false in order to omit emitting serialized | 27 // This variable may be set to false in order to omit emitting serialized |
| 28 // runtime type info. | 28 // runtime type info. |
| 29 var emitSerializedRuntimeTypeInfo bool = true | 29 var emitSerializedRuntimeTypeInfo bool = true |
| 30 | 30 |
| 31 // By default we do not populate the complete type set of each top-level interfa
ce |
| 32 // because doing so is expensive and we are not currently using the the data. |
| 33 var populateCompleteTypeSet bool = false |
| 34 |
| 31 // Serialize serializes the MojomDescriptor into a binary form that is passed to
the | 35 // Serialize serializes the MojomDescriptor into a binary form that is passed to
the |
| 32 // backend of the compiler in order to invoke the code generators. | 36 // backend of the compiler in order to invoke the code generators. |
| 33 // To do this we use Mojo serialization. | 37 // To do this we use Mojo serialization. |
| 34 // If |debug| is true we also return a human-readable representation | 38 // If |debug| is true we also return a human-readable representation |
| 35 // of the serialized mojom_types.FileGraph. | 39 // of the serialized mojom_types.FileGraph. |
| 36 // This function is not thread safe. | 40 // This function is not thread safe. |
| 37 func Serialize(d *mojom.MojomDescriptor, debug bool) (bytes []byte, debugString
string, err error) { | 41 func Serialize(d *mojom.MojomDescriptor, debug bool) (bytes []byte, debugString
string, err error) { |
| 38 » return serialize(d, debug, true, true) | 42 » return serialize(d, debug, true, true, false) |
| 39 } | 43 } |
| 40 | 44 |
| 41 // serialize() is a package-private version of the public method Serialize(). | 45 // serialize() is a package-private version of the public method Serialize(). |
| 42 // It is intended for use in tests because it allows setting of the variables | 46 // It is intended for use in tests because it allows setting of the variables |
| 43 // emitLineAndColumnNumbers and emitSerializedRuntimeTypeInfo. | 47 // emitLineAndColumnNumbers, emitSerializedRuntimeTypeInfo and populateCompleteT
ypeSet. |
| 44 // This function is not thread safe because it accesses the global variables | 48 // This function is not thread safe because it sets and accesses these global |
| 45 // emitLineAndColumnNumbers and emitSerializedRuntimeTypeInfo. | 49 // variables. |
| 46 func serialize(d *mojom.MojomDescriptor, debug, | 50 func serialize(d *mojom.MojomDescriptor, debug, |
| 47 » emitLineAndColumnNumbersParam, emitSerializedRuntimeTypeInfoParam bool)
(bytes []byte, debugString string, err error) { | 51 » emitLineAndColumnNumbersParam, emitSerializedRuntimeTypeInfoParam, |
| 52 » populateCompleteTypeSetParam bool) (bytes []byte, debugString string, er
r error) { |
| 53 |
| 54 » // Save the global state and then set it based on the parameters. |
| 48 saveEmitLineAndColumnNumbers := emitLineAndColumnNumbers | 55 saveEmitLineAndColumnNumbers := emitLineAndColumnNumbers |
| 49 emitLineAndColumnNumbers = emitLineAndColumnNumbersParam | 56 emitLineAndColumnNumbers = emitLineAndColumnNumbersParam |
| 50 saveEmitSerializedRuntimeTypeInfo := emitSerializedRuntimeTypeInfo | 57 saveEmitSerializedRuntimeTypeInfo := emitSerializedRuntimeTypeInfo |
| 51 emitSerializedRuntimeTypeInfo = emitSerializedRuntimeTypeInfoParam | 58 emitSerializedRuntimeTypeInfo = emitSerializedRuntimeTypeInfoParam |
| 59 savePopulateCompleteTypeSet := populateCompleteTypeSet |
| 60 populateCompleteTypeSet = populateCompleteTypeSetParam |
| 52 | 61 |
| 53 fileGraph := translateDescriptor(d) | 62 fileGraph := translateDescriptor(d) |
| 54 if debug { | 63 if debug { |
| 55 debugString = myfmt.Sprintf("%#v", fileGraph) | 64 debugString = myfmt.Sprintf("%#v", fileGraph) |
| 56 } | 65 } |
| 57 encoder := bindings.NewEncoder() | 66 encoder := bindings.NewEncoder() |
| 58 encoder.SetDeterministic(true) | 67 encoder.SetDeterministic(true) |
| 59 fileGraph.Encode(encoder) | 68 fileGraph.Encode(encoder) |
| 60 bytes, _, err = encoder.Data() | 69 bytes, _, err = encoder.Data() |
| 61 | 70 |
| 71 // Restore the original values of the global state. |
| 62 emitLineAndColumnNumbers = saveEmitLineAndColumnNumbers | 72 emitLineAndColumnNumbers = saveEmitLineAndColumnNumbers |
| 63 emitSerializedRuntimeTypeInfo = saveEmitSerializedRuntimeTypeInfo | 73 emitSerializedRuntimeTypeInfo = saveEmitSerializedRuntimeTypeInfo |
| 74 populateCompleteTypeSet = savePopulateCompleteTypeSet |
| 64 return | 75 return |
| 65 } | 76 } |
| 66 | 77 |
| 67 // translateDescriptor translates from a mojom.MojomDescriptor (the pure Go | 78 // translateDescriptor translates from a mojom.MojomDescriptor (the pure Go |
| 68 // representation used by the parser) to a mojom_files.MojomFileGraph (the | 79 // representation used by the parser) to a mojom_files.MojomFileGraph (the |
| 69 // Mojo Go representation used for serialization.) | 80 // Mojo Go representation used for serialization.) |
| 70 func translateDescriptor(d *mojom.MojomDescriptor) *mojom_files.MojomFileGraph { | 81 func translateDescriptor(d *mojom.MojomDescriptor) *mojom_files.MojomFileGraph { |
| 71 fileGraph := mojom_files.MojomFileGraph{} | 82 fileGraph := mojom_files.MojomFileGraph{} |
| 72 | 83 |
| 73 // Add |resolved_types| field. | 84 // Add |resolved_types| field. |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 } | 247 } |
| 237 | 248 |
| 238 // addServiceTypeInfo will add a ServiceTypeInfo to the ServicesByName field of
|typeInfo| corresponding | 249 // addServiceTypeInfo will add a ServiceTypeInfo to the ServicesByName field of
|typeInfo| corresponding |
| 239 // to |intrfc| if |intrfc| is a top-level interface, meaning that it has a non-n
il service name. In that | 250 // to |intrfc| if |intrfc| is a top-level interface, meaning that it has a non-n
il service name. In that |
| 240 // case this method returns true. Otherwise this method will do nothing and retu
rn fals. | 251 // case this method returns true. Otherwise this method will do nothing and retu
rn fals. |
| 241 func addServiceTypeInfo(intrfc *mojom.MojomInterface, typeInfo *mojom_types.Runt
imeTypeInfo) (isTopLevel bool) { | 252 func addServiceTypeInfo(intrfc *mojom.MojomInterface, typeInfo *mojom_types.Runt
imeTypeInfo) (isTopLevel bool) { |
| 242 isTopLevel = intrfc.ServiceName != nil | 253 isTopLevel = intrfc.ServiceName != nil |
| 243 if isTopLevel { | 254 if isTopLevel { |
| 244 serviceTypeInfo := mojom_types.ServiceTypeInfo{} | 255 serviceTypeInfo := mojom_types.ServiceTypeInfo{} |
| 245 serviceTypeInfo.TopLevelInterface = intrfc.TypeKey() | 256 serviceTypeInfo.TopLevelInterface = intrfc.TypeKey() |
| 246 » » serviceTypeInfo.CompleteTypeSet = intrfc.FindReachableTypes() | 257 » » if populateCompleteTypeSet { |
| 258 » » » serviceTypeInfo.CompleteTypeSet = intrfc.FindReachableTy
pes() |
| 259 » » } |
| 247 typeInfo.ServicesByName[*intrfc.ServiceName] = serviceTypeInfo | 260 typeInfo.ServicesByName[*intrfc.ServiceName] = serviceTypeInfo |
| 248 } | 261 } |
| 249 return | 262 return |
| 250 } | 263 } |
| 251 | 264 |
| 252 // translateUserDefinedType translates from a mojom.UserDefinedType (the pure Go | 265 // translateUserDefinedType translates from a mojom.UserDefinedType (the pure Go |
| 253 // representation used by the parser) to a mojom_types.UserDefinedType (the | 266 // representation used by the parser) to a mojom_types.UserDefinedType (the |
| 254 // Mojo Go representation used for serialization.) | 267 // Mojo Go representation used for serialization.) |
| 255 func translateUserDefinedType(t mojom.UserDefinedType) mojom_types.UserDefinedTy
pe { | 268 func translateUserDefinedType(t mojom.UserDefinedType) mojom_types.UserDefinedTy
pe { |
| 256 switch p := t.(type) { | 269 switch p := t.(type) { |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 688 | 701 |
| 689 // stringPointer is a convenience function for creating a pointer to a string wh
ose value | 702 // stringPointer is a convenience function for creating a pointer to a string wh
ose value |
| 690 // is the specified string. It may be used in situations where the compiler will | 703 // is the specified string. It may be used in situations where the compiler will |
| 691 // not allow you to take the address of a string value directly, such as the | 704 // not allow you to take the address of a string value directly, such as the |
| 692 // return value of a function. It is necessary to create pointers to strings bec
ause | 705 // return value of a function. It is necessary to create pointers to strings bec
ause |
| 693 // that is how the Mojom type |string?| (i.e. nullable string) is represented in | 706 // that is how the Mojom type |string?| (i.e. nullable string) is represented in |
| 694 // in the Mojom Go bindings. | 707 // in the Mojom Go bindings. |
| 695 func stringPointer(s string) *string { | 708 func stringPointer(s string) *string { |
| 696 return &s | 709 return &s |
| 697 } | 710 } |
| OLD | NEW |