]> asedeno.scripts.mit.edu Git - cl-protobufs.git/blob - serialize.lisp
87d852318b8900bc276f746239211b0e05c40497
[cl-protobufs.git] / serialize.lisp
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;;                                                                  ;;;
3 ;;; Confidential and proprietary information of ITA Software, Inc.   ;;;
4 ;;;                                                                  ;;;
5 ;;; Copyright (c) 2012 ITA Software, Inc.  All rights reserved.      ;;;
6 ;;;                                                                  ;;;
7 ;;; Original author: Scott McKay                                     ;;;
8 ;;;                                                                  ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10
11 (in-package "PROTO-IMPL")
12
13
14 ;;; Protobuf serialization from Lisp objects
15
16 ;;; Serialization
17
18 ;; Serialize the object using the given protobuf "schema"
19 (defun serialize-object-to-stream (object protobuf &key (stream *standard-output*) visited)
20   "Serializes the object 'object' as a protobuf object defined in the schema 'protobuf'
21    onto the stream 'stream' using the wire format.
22    'visited' is a hash table used to cache object sizes. If it is supplied, it will be
23    cleared before it is used; otherwise, a fresh table will be created.
24    The return value is the buffer containing the serialized object. If the stream is
25    nil, the buffer is not actually written to anywhere."
26   (let* ((visited (let ((v (or visited (make-hash-table))))
27                     (clrhash v)
28                     v))
29          (size    (object-size object protobuf :visited visited))
30          (buffer  (make-array size :element-type '(unsigned-byte 8))))
31     (serialize-object object protobuf buffer 0 :visited visited)
32     (when stream
33       (write-sequence buffer stream))
34     buffer))
35
36 ;; Allow clients to add their own methods
37 ;; This is how we address the problem of cycles, e.g. -- if you have an object
38 ;; that may contain cycles, serialize the cyclic object using a "handle"
39 (defgeneric serialize-object (object protobuf buffer index &key visited)
40   (:documentation
41    "Serializes the object 'object' as a protobuf object defined in the schema 'protobuf'
42     into the byte array given by 'buffer' starting at the fixnum index 'index' using
43     the wire format.
44     'visited' is a hash table used to cache object sizes.
45     The return value is the buffer containing the serialized object."))
46
47 ;; 'visited' is used to cache object sizes
48 ;; If it's passed in explicitly, it is assumed to already have the sizes within it
49 ;; The default method uses meta-data from the protobuf "schema"
50 (defmethod serialize-object ((object standard-object) protobuf buffer index &key visited)
51   (declare (type (simple-array (unsigned-byte 8)) buffer)
52            (type fixnum index))
53   (check-type protobuf (or protobuf protobuf-message))
54   (let* ((class   (class-of object))
55          (message (find-message-for-class protobuf class))
56          (visited (or visited (make-hash-table))))
57     (assert message ()
58             "There is no Protobuf message for the class ~S" class)
59     (macrolet ((read-slot (object slot reader)
60                  ;; Don't do a boundp check, we assume the object is fully populated
61                  ;; Unpopulated slots should be "nullable" and should contain nil
62                  `(if ,reader
63                     (funcall ,reader ,object)
64                     (slot-value ,object ,slot))))
65       (labels ((do-field (object trace field)
66                  ;; We don't do cycle detection here
67                  ;; If the client needs it, he can define his own 'serialize-object'
68                  ;; method to clean things up first
69                  (let* ((cl     (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
70                         (slot   (proto-value field))
71                         (reader (proto-reader field))
72                         msg)
73                    (when (or slot reader)
74                      (cond ((eq (proto-required field) :repeated)
75                             (cond ((and (proto-packed field) (packed-type-p cl))
76                                    (let ((tag (make-tag cl (proto-index field))))
77                                      (setq index (serialize-packed (read-slot object slot reader)
78                                                                    cl tag buffer index))))
79                                   ((keywordp cl)
80                                    (let ((tag (make-tag cl (proto-index field))))
81                                      (map () #'(lambda (v)
82                                                  (setq index (serialize-prim v cl tag buffer index)))
83                                              (read-slot object slot reader))))
84                                   ((typep (setq msg (and cl (loop for p in trace
85                                                                   thereis (or (find-message-for-class p cl)
86                                                                               (find-enum-for-type p cl)))))
87                                           'protobuf-message)
88                                    (dolist (v (if slot (read-slot object slot reader) (list object)))
89                                      ;; To serialize an embedded message, first say that it's
90                                      ;; a string, then encode its size, then serialize its fields
91                                      (let ((tag (make-tag $wire-type-string (proto-index field)))
92                                            (len (object-size v protobuf :visited visited)))
93                                        (setq index (encode-uint32 tag buffer index))
94                                        (setq index (encode-uint32 len buffer index)))
95                                      (map () (curry #'do-field v (cons msg trace))
96                                              (proto-fields msg))))
97                                   ((typep msg 'protobuf-enum)
98                                    (let ((tag (make-tag $wire-type-varint (proto-index field))))
99                                      (map () #'(lambda (v)
100                                                  (setq index (serialize-enum v (proto-values msg) tag buffer index)))
101                                              (read-slot object slot reader))))))
102                            (t
103                             (cond ((keywordp cl)
104                                    (let ((v (read-slot object slot reader)))
105                                      (when (or v (eq cl :bool))
106                                        (let ((tag (make-tag cl (proto-index field))))
107                                          (setq index (serialize-prim v cl tag buffer index))))))
108                                   ((typep (setq msg (and cl (loop for p in trace
109                                                                   thereis (or (find-message-for-class p cl)
110                                                                               (find-enum-for-type p cl)))))
111                                           'protobuf-message)
112                                    (let ((v (if slot (read-slot object slot reader) object)))
113                                      (let ((tag (make-tag $wire-type-string (proto-index field)))
114                                            (len (object-size v protobuf :visited visited)))
115                                        (setq index (encode-uint32 tag buffer index))
116                                        (setq index (encode-uint32 len buffer index)))
117                                      (when v
118                                        (map () (curry #'do-field v (cons msg trace))
119                                                (proto-fields msg)))))
120                                   ((typep msg 'protobuf-enum)
121                                    (let ((v (read-slot object slot reader)))
122                                      (when v
123                                        (let ((tag (make-tag $wire-type-varint (proto-index field))))
124                                          (setq index (serialize-enum v (proto-values msg) tag buffer index)))))))))))))
125         (declare (dynamic-extent #'do-field))
126         (map () (curry #'do-field object (list message protobuf)) (proto-fields message)))))
127   (values buffer index))
128
129
130 ;;; Deserialization
131
132 (defun deserialize-object-from-stream (class protobuf &key (stream *standard-input*))
133   "Deserializes an object of the give class 'class' as a protobuf object defined
134    in the schema 'protobuf' from the stream 'stream' using the wire format.
135    The return value is the object."
136   (let* ((size    (file-length stream))
137          (buffer  (make-array size :element-type '(unsigned-byte 8))))
138     (read-sequence buffer stream)
139     (deserialize-object class protobuf buffer 0)))
140
141 ;; Allow clients to add their own methods
142 ;; This is you might preserve object identity, e.g.
143 (defgeneric deserialize-object (class protobuf buffer &optional index length)
144   (:documentation
145    "Deserializes an object of the given class 'class' as a protobuf object defined
146     in the schema 'protobuf' from the byte array given by 'buffer' starting at
147     the fixnum index 'index' up to the length of the buffer, given by 'length'.
148     The return value is the object."))
149
150 ;; The default method uses meta-data from the protobuf "schema"
151 ;; Note that 'class' is the Lisp name of the Protobufs message (class)
152 ;; It is not the name of any overriding class ('proto-class-override')
153 (defmethod deserialize-object ((class symbol) protobuf buffer &optional (index 0) length)
154   (declare (type (simple-array (unsigned-byte 8)) buffer)
155            (type fixnum index))
156   (check-type protobuf (or protobuf protobuf-message))
157   (let ((length (or length (length buffer))))
158     (declare (type fixnum length))
159     (labels ((deserialize (class trace &optional (end length))
160                (declare (type fixnum end))
161                (let* ((message (loop for p in trace
162                                      thereis (or (find-message-for-class p class)
163                                                  (find-enum-for-type p class))))
164                       (object  (make-instance (or (proto-class-override message) class))))
165                  (assert (eql (proto-class message) class) ()
166                          "The class in message ~S does not match the Lisp class ~S"
167                          (proto-class message) class)
168                  (assert message ()
169                          "There is no Protobuf message for the class ~S" class)
170                  (loop
171                    (when (>= index end)
172                      (return-from deserialize (values object index)))
173                    (multiple-value-bind (val idx)
174                        (decode-uint32 buffer index)
175                      (setq index idx)
176                      (let* ((type  (ilogand val #x7))
177                             (fld   (ilogand (iash val -3) #x1FFFFFFF))
178                             (field (find fld (proto-fields message) :key #'proto-index))
179                             (cl    (and field (if (eq (proto-class field) 'boolean) :bool (proto-class field))))
180                             ;; It's OK for this to be null
181                             ;; That means we're parsing some version of a message
182                             ;; that has the field, but our current message does not
183                             ;; We still have to deserialize everything, though
184                             (slot  (and field (proto-value field)))
185                             msg)
186                        (if (null field)
187                          ;; If there's no field descriptor for this index,
188                          ;; just skip the next element in the buffer
189                          (setq index (skip-element buffer index type))
190                          ;;--- Check for mismatched types, running past end of buffer, etc
191                          (cond ((and field (eq (proto-required field) :repeated))
192                                 (cond ((and (proto-packed field) (packed-type-p cl))
193                                        (multiple-value-bind (values idx)
194                                            (deserialize-packed cl buffer index)
195                                          (setq index idx)
196                                          (when slot
197                                            (setf (slot-value object slot) values))))
198                                       ((keywordp cl)
199                                        (multiple-value-bind (val idx)
200                                            (deserialize-prim cl buffer index)
201                                          (setq index idx)
202                                          (when slot
203                                            (setf (slot-value object slot)
204                                                  (nconc (slot-value object slot) (list val))))))
205                                       ((typep (setq msg (and cl (or (find-message-for-class protobuf cl)
206                                                                     (find-enum-for-type protobuf cl))))
207                                               'protobuf-message)
208                                        (multiple-value-bind (len idx)
209                                            (decode-uint32 buffer index)
210                                          (setq index idx)
211                                          (let ((obj (deserialize cl (cons msg trace) (+ index len))))
212                                            (when slot
213                                              (setf (slot-value object slot)
214                                                    (nconc (slot-value object slot) (list obj)))))))
215                                       ((typep msg 'protobuf-enum)
216                                        (multiple-value-bind (val idx)
217                                            (deserialize-enum (proto-values msg) buffer index)
218                                          (setq index idx)
219                                          (when slot
220                                            (setf (slot-value object slot)
221                                                  (nconc (slot-value object slot) (list val))))))))
222                                (t
223                                 (cond ((keywordp cl)
224                                        (multiple-value-bind (val idx)
225                                            (deserialize-prim cl buffer index)
226                                          (setq index idx)
227                                          (when slot
228                                            (setf (slot-value object slot) val))))
229                                       ((typep (setq msg (and cl (or (find-message-for-class protobuf cl)
230                                                                     (find-enum-for-type protobuf cl))))
231                                               'protobuf-message)
232                                        (multiple-value-bind (len idx)
233                                            (decode-uint32 buffer index)
234                                          (setq index idx)
235                                          (let ((obj (deserialize cl (cons msg trace) (+ index len))))
236                                            (when slot
237                                              (setf (slot-value object slot) obj)))))
238                                       ((typep msg 'protobuf-enum)
239                                        (multiple-value-bind (val idx)
240                                            (deserialize-enum (proto-values msg) buffer index)
241                                          (setq index idx)
242                                          (when slot
243                                            (setf (slot-value object slot) val))))))))))))))
244       (declare (dynamic-extent #'deserialize))
245       (deserialize class (list protobuf)))))
246
247
248 ;;; Object sizes
249
250 ;; Allow clients to add their own methods
251 ;; This is how we address the problem of cycles, e.g. -- if you have an object
252 ;; that may contain cycles, return the size of the "handle" to the object
253 (defgeneric object-size (object protobuf &key visited)
254   (:documentation
255    "Computes the size in bytes of the object 'object' defined in the schema 'protobuf'.
256     'visited' is a hash table used to cache object sizes.
257     The return value is the size of the object in bytes."))
258
259 ;; 'visited' is used to cache object sizes
260 ;; The default method uses meta-data from the protobuf "schema"
261 (defmethod object-size ((object standard-object) protobuf &key visited)
262   (check-type protobuf (or protobuf protobuf-message))
263   (let ((size (and visited (gethash object visited))))
264     (when size
265       (return-from object-size size)))
266   (let* ((class   (class-of object))
267          (message (find-message-for-class protobuf class))
268          (size    0))
269     (declare (type fixnum size))
270     (assert message ()
271             "There is no Protobuf message for the class ~S" class)
272     (macrolet ((read-slot (object slot reader)
273                  ;; Don't do a boundp check, we assume the object is fully populated
274                  ;; Unpopulated slots should be "nullable" and should contain nil
275                  `(if ,reader
276                     (funcall ,reader ,object)
277                     (slot-value ,object ,slot))))
278       (labels ((do-field (object trace field)
279                  ;; We don't do cycle detection here
280                  ;; If the client needs it, he can define his own 'object-size'
281                  ;; method to clean things up first
282                  (let* ((cl     (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
283                         (slot   (proto-value field))
284                         (reader (proto-reader field))
285                         msg)
286                    (when (or slot reader)
287                      (cond ((eq (proto-required field) :repeated)
288                             (cond ((and (proto-packed field) (packed-type-p cl))
289                                    (let ((tag (make-tag cl (proto-index field))))
290                                      (iincf size (packed-size (read-slot object slot reader) cl tag))))
291                                   ((keywordp cl)
292                                    (let ((tag (make-tag cl (proto-index field))))
293                                      (map () #'(lambda (v)
294                                                  (iincf size (prim-size v cl tag)))
295                                              (read-slot object slot reader))))
296                                   ((typep (setq msg (and cl (loop for p in trace
297                                                                   thereis (or (find-message-for-class p cl)
298                                                                               (find-enum-for-type p cl)))))
299                                           'protobuf-message)
300                                    (dolist (v (if slot (read-slot object slot reader) (list object)))
301                                      (let ((tag (make-tag $wire-type-string (proto-index field)))
302                                            (len (object-size v protobuf :visited visited)))
303                                        (iincf size (length32 tag))
304                                        (iincf size (length32 len)))
305                                      (map () (curry #'do-field v (cons msg trace))
306                                              (proto-fields msg))))
307                                   ((typep msg 'protobuf-enum)
308                                    (let ((tag (make-tag $wire-type-varint (proto-index field))))
309                                      (map () #'(lambda (v)
310                                                  (iincf size (enum-size v (proto-values msg) tag)))
311                                              (read-slot object slot reader))))))
312                            (t
313                             (cond ((keywordp cl)
314                                    (let ((v (read-slot object slot reader)))
315                                      (when (or v (eq cl :bool))
316                                        (let ((tag (make-tag cl (proto-index field))))
317                                          (iincf size (prim-size v cl tag))))))
318                                   ((typep (setq msg (and cl (loop for p in trace
319                                                                   thereis (or (find-message-for-class p cl)
320                                                                               (find-enum-for-type p cl)))))
321                                           'protobuf-message)
322                                    (let ((v (if slot (read-slot object slot reader) object)))
323                                      (when v
324                                        (let ((tag (make-tag $wire-type-string (proto-index field)))
325                                              (len (object-size v protobuf :visited visited)))
326                                          (iincf size (length32 tag))
327                                          (iincf size (length32 len)))
328                                        (map () (curry #'do-field v (cons msg trace))
329                                                (proto-fields msg)))))
330                                   ((typep msg 'protobuf-enum)
331                                    (let ((v (read-slot object slot reader)))
332                                      (when v
333                                        (let ((tag (make-tag $wire-type-varint (proto-index field))))
334                                          (iincf size (enum-size (read-slot object slot reader) (proto-values msg) tag)))))))))))))
335         (declare (dynamic-extent #'do-field))
336         (map () (curry #'do-field object (list message protobuf)) (proto-fields message))
337         (when visited
338           (setf (gethash object visited) size))   ;cache the size
339         size))))
340
341 \f
342 ;;; Compile-time generation of serializers
343
344 (defun generate-serializer (protobuf message)
345   "Generate a 'serialize-object' method for the given message."
346   (with-gensyms (vobj vproto vbuf vidx vval)
347     (with-collectors ((serializers collect-serializer))
348       (dolist (field (proto-fields message))
349         (let* ((class  (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
350                (msg    (and class (not (keywordp class))
351                             (or (or (find-message-for-class message class)
352                                     (find-enum-for-type message class))
353                                 (or (find-message-for-class protobuf class)
354                                     (find-enum-for-type protobuf class)))))
355                (reader (cond ((proto-reader field)
356                               `(,(proto-reader field) ,vobj))
357                              ((proto-value field)
358                               `(slot-value ,vobj ',(proto-value field)))))
359                (index  (proto-index field)))
360           (when reader
361             (cond ((eq (proto-required field) :repeated)
362                    (cond ((and (proto-packed field) (packed-type-p class))
363                           (collect-serializer
364                            (let ((tag (make-tag class index)))
365                              `(setq ,vidx (serialize-packed ,reader ,class ,tag ,vbuf ,vidx)))))
366                          ((keywordp class)
367                           (collect-serializer
368                            (let ((tag (make-tag class index)))
369                              `(dolist (,vval ,reader)
370                                 (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))
371                          ((typep msg 'protobuf-message)
372                           (collect-serializer
373                            (let ((tag (make-tag $wire-type-string index)))
374                              `(dolist (,vval ,reader)
375                                 (let ((len (or (and visited (gethash ,vval visited))
376                                                (object-size ,vval ,vproto :visited visited))))
377                                   (setq ,vidx (encode-uint32 ,tag ,vbuf ,vidx))
378                                   (setq ,vidx (encode-uint32 len ,vbuf ,vidx))
379                                   (serialize-object ,vval ,vproto ,vbuf ,vidx :visited visited)
380                                   (iincf ,vidx len))))))
381                          ((typep msg 'protobuf-enum)
382                           (collect-serializer
383                            (let ((tag (make-tag $wire-type-varint index)))
384                              `(dolist (,vval ,reader)
385                                 (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx))))))))
386                   (t
387                    (cond ((keywordp class)
388                           (collect-serializer
389                            (let ((tag (make-tag class index)))
390                              (if (eq class :bool)
391                                `(let ((,vval ,reader))
392                                   (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx)))
393                                `(let ((,vval ,reader))
394                                   (when ,vval
395                                     (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))))
396                          ((typep msg 'protobuf-message)
397                           (collect-serializer
398                            (let ((tag (make-tag $wire-type-string index)))
399                              `(let ((,vval ,reader))
400                                 (when ,vval
401                                   (let ((len (or (and visited (gethash ,vval visited))
402                                                  (object-size ,vval ,vproto :visited visited))))
403                                     (setq ,vidx (encode-uint32 ,tag ,vbuf ,vidx))
404                                     (setq ,vidx (encode-uint32 len ,vbuf ,vidx))
405                                     (serialize-object ,vval ,vproto ,vbuf ,vidx :visited visited)
406                                     (iincf ,vidx len)))))))
407                          ((typep msg 'protobuf-enum)
408                           (collect-serializer
409                            (let ((tag (make-tag $wire-type-varint index)))
410                              `(let ((,vval ,reader))
411                                 (when ,vval
412                                   (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx)))))))))))))
413       `(defmethod serialize-object ((,vobj ,(proto-class message)) ,vproto ,vbuf ,vidx &key visited) 
414          (declare (ignorable visited)
415                   (type (simple-array (unsigned-byte 8)) ,vbuf)
416                   (type fixnum ,vidx))
417          (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
418           ,@serializers
419            (values ,vbuf ,vidx))))))
420
421 (defun generate-deserializer (protobuf message)
422   "Generate a 'deserialize-object' method for the given message."
423   (with-gensyms (vclass vproto vbuf vidx vlen vobj vval)
424     (with-collectors ((deserializers collect-deserializer))
425       (dolist (field (proto-fields message))
426         (let* ((class  (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
427                (msg    (and class (not (keywordp class))
428                             (or (or (find-message-for-class message class)
429                                     (find-enum-for-type message class))
430                                 (or (find-message-for-class protobuf class)
431                                     (find-enum-for-type protobuf class)))))
432                (slot   (proto-value field))
433                (index  (proto-index field)))
434           (cond ((eq (proto-required field) :repeated)
435                  (cond ((and (proto-packed field) (packed-type-p class))
436                         (collect-deserializer
437                          `((,(make-tag class index))
438                            (multiple-value-bind (,vval idx)
439                                (deserialize-packed ,class ,vbuf ,vidx)
440                              (setq ,vidx idx)
441                              ,(when slot
442                                 `(setf (slot-value ,vobj ',slot) ,vval))))))
443                        ((keywordp class)
444                         (collect-deserializer
445                          `((,(make-tag class index))
446                            (multiple-value-bind (,vval idx)
447                                (deserialize-prim ,class ,vbuf ,vidx)
448                              (setq ,vidx idx)
449                              ,(when slot
450                                 `(setf (slot-value ,vobj ',slot)
451                                        (nconc (slot-value ,vobj ',slot) (list ,vval))))))))
452                        ((typep msg 'protobuf-message)
453                         (collect-deserializer
454                          `((,(make-tag $wire-type-string index))
455                            (multiple-value-bind (len idx)
456                                (decode-uint32 ,vbuf ,vidx)
457                              (setq ,vidx idx)
458                              (multiple-value-bind (,vval idx)
459                                  (deserialize-object ',class ,vproto ,vbuf ,vidx (i+ ,vidx len))
460                                (setq ,vidx idx)
461                                ,(when slot
462                                   `(setf (slot-value ,vobj ',slot)
463                                          (nconc (slot-value ,vobj ',slot) (list ,vval)))))))))
464                        ((typep msg 'protobuf-enum)
465                         (collect-deserializer
466                          `((,(make-tag $wire-type-varint index))
467                            (multiple-value-bind (,vval idx)
468                                (deserialize-enum '(,@(proto-values msg)) ,vbuf ,vidx)
469                              (setq ,vidx idx)
470                              ,(when slot
471                                 `(setf (slot-value ,vobj ',slot)
472                                        (nconc (slot-value ,vobj ',slot) (list ,vval))))))))))
473                 (t
474                  (cond ((keywordp class)
475                         (collect-deserializer
476                          `((,(make-tag class index))
477                            (multiple-value-bind (,vval idx)
478                                (deserialize-prim ,class ,vbuf ,vidx)
479                              (setq ,vidx idx)
480                              ,(when slot
481                                 `(setf (slot-value ,vobj ',slot) ,vval))))))
482                        ((typep msg 'protobuf-message)
483                         (collect-deserializer
484                          `((,(make-tag $wire-type-string index))
485                            (multiple-value-bind (len idx)
486                                (decode-uint32 ,vbuf ,vidx)
487                              (setq ,vidx idx)
488                              (multiple-value-bind (,vval idx)
489                                  (deserialize-object ',class ,vproto ,vbuf ,vidx (i+ ,vidx len))
490                                (setq ,vidx idx)
491                                ,(when slot
492                                   `(setf (slot-value ,vobj ',slot) ,vval)))))))
493                        ((typep msg 'protobuf-enum)
494                         (collect-deserializer
495                          `((,(make-tag $wire-type-varint index))
496                            (multiple-value-bind (,vval idx)
497                                (deserialize-enum '(,@(proto-values msg)) ,vbuf ,vidx)
498                              (setq ,vidx idx)
499                              ,(when slot
500                                 `(setf (slot-value ,vobj ',slot) ,vval)))))))))))
501     `(defmethod deserialize-object ((,vclass (eql ',(proto-class message))) ,vproto ,vbuf
502                                     &optional (,vidx 0) ,vlen)
503        (declare (type (simple-array (unsigned-byte 8)) ,vbuf)
504                 (type fixnum ,vidx))
505        (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
506          (let ((,vlen (or ,vlen (length ,vbuf))))
507            (declare (type fixnum ,vlen))
508            (let ((,vobj (make-instance ',(or (proto-class-override message) (proto-class message)))))
509              (loop
510                (when (>= ,vidx ,vlen)
511                  (return-from deserialize-object (values ,vobj ,vidx)))
512                (multiple-value-bind (tag idx)
513                    (decode-uint32 ,vbuf ,vidx)
514                  (setq ,vidx idx)
515                  (case tag
516                    ,@deserializers
517                    (otherwise
518                     (setq ,vidx (skip-element ,vbuf ,vidx (ilogand tag #x7))))))))))))))
519
520 (defun generate-object-size (protobuf message)
521   "Generate an 'object-size' method for the given message."
522   (with-gensyms (vobj vproto vsize vval)
523     (with-collectors ((sizers collect-sizer))
524       (dolist (field (proto-fields message))
525         (let* ((class  (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
526                (msg    (and class (not (keywordp class))
527                             (or (or (find-message-for-class message class)
528                                     (find-enum-for-type message class))
529                                 (or (find-message-for-class protobuf class)
530                                     (find-enum-for-type protobuf class)))))
531                (reader (cond ((proto-reader field)
532                               `(,(proto-reader field) ,vobj))
533                              ((proto-value field)
534                               `(slot-value ,vobj ',(proto-value field)))))
535                (index  (proto-index field)))
536           (when reader
537             (cond ((eq (proto-required field) :repeated)
538                    (cond ((and (proto-packed field) (packed-type-p class))
539                           (collect-sizer
540                            (let ((tag (make-tag class index)))
541                              `(iincf ,vsize (packed-size ,reader ,class ,tag)))))
542                          ((keywordp class)
543                           (collect-sizer
544                            (let ((tag (make-tag class index)))
545                              `(dolist (,vval ,reader)
546                                 (iincf ,vsize (prim-size ,vval ,class ,tag))))))
547                          ((typep msg 'protobuf-message)
548                           (collect-sizer
549                            (let ((tag (make-tag $wire-type-string index)))
550                              `(dolist (,vval ,reader)
551                                 (let ((len (or (and visited (gethash ,vval visited))
552                                                (object-size ,vval ,vproto :visited visited))))
553                                   (iincf ,vsize (length32 ,tag))
554                                   (iincf ,vsize (length32 len))
555                                   (iincf ,vsize len))))))
556                          ((typep msg 'protobuf-enum)
557                           (let ((tag (make-tag $wire-type-varint index)))
558                             (collect-sizer
559                              `(dolist (,vval ,reader)
560                                 (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag))))))))
561                   (t
562                    (cond ((keywordp class)
563                           (let ((tag (make-tag class index)))
564                             (collect-sizer
565                              (if (eq class :bool)
566                                `(let ((,vval ,reader))
567                                   (iincf ,vsize (prim-size ,vval ,class ,tag)))
568                                `(let ((,vval ,reader))
569                                   (when ,vval
570                                     (iincf ,vsize (prim-size ,vval ,class ,tag))))))))
571                          ((typep msg 'protobuf-message)
572                           (collect-sizer
573                            (let ((tag (make-tag $wire-type-string index)))
574                              `(let ((,vval ,reader))
575                                 (when ,vval
576                                   (let ((len (or (and visited (gethash ,vval visited))
577                                                  (object-size ,vval ,vproto :visited visited))))
578                                     (iincf ,vsize (length32 ,tag))
579                                     (iincf ,vsize (length32 len))
580                                     (iincf ,vsize len)))))))
581                          ((typep msg 'protobuf-enum)
582                           (let ((tag (make-tag $wire-type-varint index)))
583                             (collect-sizer
584                              `(let ((,vval ,reader))
585                                 (when ,vval
586                                   (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag)))))))))))))
587       `(defmethod object-size ((,vobj ,(proto-class message)) ,vproto &key visited)
588          (declare (ignorable visited))
589          (locally (declare (optimize (speed 3) (safety 0) (debug 0)))
590            (let ((,vsize (and visited (gethash ,vobj visited))))
591              (when ,vsize
592                (return-from object-size ,vsize)))
593            (let ((,vsize 0))
594              (declare (type fixnum ,vsize))
595              ,@sizers
596              (when visited
597                (setf (gethash ,vobj visited) ,vsize))
598              ,vsize))))))