// Serialize serializes the MojomDescriptor into a binary form that is passed to the // backend of the compiler in order to invoke the code generators. // To do this we use Mojo serialization. // If |debug| is true we also return a human-readable representation // of the serialized mojom_types.FileGraph. func Serialize(d *mojom.MojomDescriptor, debug bool) (bytes []byte, debugString string, err error) { fileGraph := translateDescriptor(d) if debug { debugString = myfmt.Sprintf("%#v", fileGraph) } encoder := bindings.NewEncoder() fileGraph.Encode(encoder) bytes, _, err = encoder.Data() return }
// Serializes the MojomDescriptor into a binary form that is passed to the // backend of the compiler in order to invoke the code generators. // To do this we use Mojo serialization. func (d *MojomDescriptor) Serialize() (bytes []byte, err error) { fileGraph := translateDescriptor(d) encoder := bindings.NewEncoder() fileGraph.Encode(encoder) bytes, _, err = encoder.Data() return }
func BenchmarkMojoEncoding(b *testing.B) { for i := 0; i < b.N; i++ { enc := bindings.NewEncoder() customer.Encode(enc) enc.Data() } }
func encodeMessageFromVom(header bindings.MessageHeader, argptrs []interface{}, t *vdl.Type) (*bindings.Message, error) { // Convert argptrs into their true form: []*vom.RawBytes inargs := make([]*vom.RawBytes, len(argptrs)) for i := range argptrs { inargs[i] = *argptrs[i].(**vom.RawBytes) } encoder := bindings.NewEncoder() if err := header.Encode(encoder); err != nil { return nil, err } if bytes, handles, err := encoder.Data(); err != nil { return nil, err } else { target := transcoder.ToMojomTarget() if err := util.JoinRawBytesAsStruct(target, t, inargs); err != nil { return nil, err } moreBytes := target.Bytes() // Append the encoded "payload" to the end of the slice. bytes = append(bytes, moreBytes...) return &bindings.Message{ Header: header, Bytes: bytes, Handles: handles, Payload: moreBytes, }, nil } }
func mojomBytesCustomer() []byte { enc := bindings.NewEncoder() err := customer.Encode(enc) if err != nil { panic(err) } data, _, err := enc.Data() if err != nil { panic(err) } return data }
func encode(t *testing.T, value Encodable) ([]byte, []system.UntypedHandle, error) { encoder := bindings.NewEncoder() err := value.Encode(encoder) if err != nil { return nil, nil, err } bytes, handles, err := encoder.Data() if err != nil { return nil, nil, err } return bytes, handles, nil }
func mojoEncode(mojoValue interface{}) ([]byte, error) { payload, ok := mojoValue.(encodable) if !ok { return nil, fmt.Errorf("type %T lacks an Encode() method", mojoValue) } enc := bindings.NewEncoder() err := payload.Encode(enc) if err != nil { return nil, fmt.Errorf("error in Encode: %v", err) } data, _, err := enc.Data() if err != nil { return nil, fmt.Errorf("error in Data()", err) } return data, nil }
func (s *messageReceiver) Accept(message *bindings.Message) (err error) { if _, ok := s.header.ifaceSig.Methods[message.Header.Type]; !ok { return fmt.Errorf("Method had index %d, but interface only has %d methods", message.Header.Type, len(s.header.ifaceSig.Methods)) } methodSig := s.header.ifaceSig.Methods[message.Header.Type] methodName := *methodSig.DeclData.ShortName // Should we perform validation of flags like generated methods? // Does this handle 0-arg methods? messageBytes := message.Payload if methodSig.ResponseParams == nil { // This may be complicated to support because of a race on the server side. // When we send the no-response message through mojo we don't know when it has been received // and we can close the pipe (one is opened per incoming message). // Note that because the client expects no response, it will not receive the following // error. return fmt.Errorf("this error not received") } response, err := s.call(s.header.v23Name, methodName, messageBytes, methodSig.Parameters, methodSig.ResponseParams) if err != nil { return err } // TODO(alexfandrianto): This assumes that bindings.Encoder has the method // WriteRawBytes. We will need to add this to Mojo ourselves. // func (e *Encoder) WriteRawBytes(data []byte) { // first := e.end // e.claimData(align(len(data), defaultAlignment)) // copy(e.buf[first:], data) // } // // See: https://codereview.chromium.org/1416433002/ responseHeader := bindings.MessageHeader{ Type: message.Header.Type, Flags: bindings.MessageIsResponseFlag, RequestId: message.Header.RequestId, } // responseMessage, err := bindings.EncodeMessage(responseHeader, byteCopyingPayload(response)) // if err != nil { // return err // } // return s.connector.WriteMessage(responseMessage) // TODO(alexfandrianto): Replace this block with the above. encoder := bindings.NewEncoder() if err := responseHeader.Encode(encoder); err != nil { return err } if bytes, handles, err := encoder.Data(); err != nil { return err } else { // response is our payload; append to the end of our slice. bytes = append(bytes, response...) // This is analogous to bindings.newMessage responseMessage := &bindings.Message{ Header: responseHeader, Bytes: bytes, Handles: handles, Payload: response, } return s.connector.WriteMessage(responseMessage) } }