| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | ! Copyright (C) 2006 Adam Langley and Chris Double. | 
					
						
							|  |  |  | ! Adam Langley was the original author of this work. | 
					
						
							|  |  |  | !
 | 
					
						
							|  |  |  | ! Chris Double modified it to fix bugs and get it working | 
					
						
							|  |  |  | ! correctly under the latest versions of Factor. | 
					
						
							|  |  |  | !
 | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | !
 | 
					
						
							|  |  |  | USING: namespaces sequences kernel math io math.functions | 
					
						
							| 
									
										
										
										
											2008-03-29 04:34:48 -04:00
										 |  |  | io.binary strings classes words sbufs classes.tuple arrays | 
					
						
							| 
									
										
										
										
											2008-07-02 03:03:30 -04:00
										 |  |  | vectors byte-arrays quotations hashtables assocs help.syntax | 
					
						
							|  |  |  | help.markup splitting io.streams.byte-array io.encodings.string | 
					
						
							|  |  |  | io.encodings.utf8 io.encodings.binary combinators accessors | 
					
						
							|  |  |  | locals prettyprint compiler.units sequences.private | 
					
						
							|  |  |  | classes.tuple.private ;
 | 
					
						
							| 
									
										
										
										
											2008-03-17 04:28:07 -04:00
										 |  |  | IN: serialize | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-15 03:35:53 -05:00
										 |  |  | GENERIC: (serialize) ( obj -- )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 23:49:17 -05:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  | ! Variable holding a assoc of objects already serialized | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | SYMBOL: serialized | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  | TUPLE: id obj ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <id> id | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-11 22:59:09 -04:00
										 |  |  | M: id hashcode* obj>> hashcode* ;
 | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  | M: id equal? over id? [ [ obj>> ] bi@ eq? ] [ 2drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : add-object ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     #! Add an object to the sequence of already serialized | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  |     #! objects. | 
					
						
							|  |  |  |     serialized get [ assoc-size swap <id> ] keep set-at ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : object-id ( obj -- id )
 | 
					
						
							|  |  |  |     #! Return the id of an already serialized object  | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  |     <id> serialized get at ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  | ! Numbers are serialized as follows: | 
					
						
							|  |  |  | ! 0 => B{ 0 } | 
					
						
							|  |  |  | ! 1<=x<=126 => B{ x | 0x80 } | 
					
						
							|  |  |  | ! x>127 => B{ length(x) x[0] x[1] ... } | 
					
						
							|  |  |  | ! x>2^1024 => B{ 0xff length(x) x[0] x[1] ... } | 
					
						
							|  |  |  | ! The last case is needed because a very large number would | 
					
						
							|  |  |  | ! otherwise be confused with a small number. | 
					
						
							|  |  |  | : serialize-cell ( n -- )
 | 
					
						
							|  |  |  |     dup zero? [ drop 0 write1 ] [ | 
					
						
							|  |  |  |         dup HEX: 7e <= [ | 
					
						
							|  |  |  |             HEX: 80 bitor write1
 | 
					
						
							|  |  |  |         ] [ | 
					
						
							|  |  |  |             dup log2 8 /i 1+  | 
					
						
							|  |  |  |             dup HEX: 7f >= [ | 
					
						
							|  |  |  |                 HEX: ff write1
 | 
					
						
							|  |  |  |                 dup serialize-cell | 
					
						
							|  |  |  |             ] [ | 
					
						
							|  |  |  |                 dup write1
 | 
					
						
							|  |  |  |             ] if
 | 
					
						
							|  |  |  |             >be write
 | 
					
						
							|  |  |  |         ] if
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  | : deserialize-cell ( -- n )
 | 
					
						
							|  |  |  |     read1 { | 
					
						
							|  |  |  |         { [ dup HEX: ff = ] [ drop deserialize-cell read be> ] } | 
					
						
							|  |  |  |         { [ dup HEX: 80 >= ] [ HEX: 80 bitxor ] } | 
					
						
							| 
									
										
										
										
											2008-04-11 13:57:43 -04:00
										 |  |  |         [ read be> ] | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     } cond ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : serialize-shared ( obj quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:17:37 -05:00
										 |  |  |     [ | 
					
						
							|  |  |  |         dup object-id | 
					
						
							|  |  |  |         [ CHAR: o write1 serialize-cell drop ] | 
					
						
							|  |  |  |     ] dip if* ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: f (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     drop CHAR: n write1 ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: integer (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     dup zero? [ | 
					
						
							|  |  |  |         drop CHAR: z write1
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |         dup 0 < [ neg CHAR: m ] [ CHAR: p ] if write1
 | 
					
						
							|  |  |  |         serialize-cell | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: float (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     CHAR: F write1
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     double>bits serialize-cell ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: complex (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     CHAR: c write1
 | 
					
						
							| 
									
										
										
										
											2008-03-31 03:30:06 -04:00
										 |  |  |     [ real-part (serialize) ] | 
					
						
							|  |  |  |     [ imaginary-part (serialize) ] bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: ratio (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     CHAR: r write1
 | 
					
						
							| 
									
										
										
										
											2008-03-31 03:30:06 -04:00
										 |  |  |     [ numerator (serialize) ] | 
					
						
							|  |  |  |     [ denominator (serialize) ] bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  | : serialize-seq ( obj code -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         write1
 | 
					
						
							|  |  |  |         [ add-object ] | 
					
						
							|  |  |  |         [ length serialize-cell ] | 
					
						
							|  |  |  |         [ [ (serialize) ] each ] tri
 | 
					
						
							|  |  |  |     ] curry serialize-shared ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: tuple (serialize) ( obj -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |         CHAR: T write1
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |         [ class (serialize) ] | 
					
						
							|  |  |  |         [ add-object ] | 
					
						
							| 
									
										
										
										
											2008-04-26 03:01:43 -04:00
										 |  |  |         [ tuple>array rest (serialize) ] | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |         tri
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] serialize-shared ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: array (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     CHAR: a serialize-seq ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  | M: quotation (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-03-31 03:30:06 -04:00
										 |  |  |         CHAR: q write1
 | 
					
						
							|  |  |  |         [ >array (serialize) ] [ add-object ] bi
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     ] serialize-shared ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  | M: hashtable (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |         CHAR: h write1
 | 
					
						
							|  |  |  |         [ add-object ] [ >alist (serialize) ] bi
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] serialize-shared ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  | M: byte-array (serialize) ( obj -- )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         CHAR: A write1
 | 
					
						
							|  |  |  |         [ add-object ] | 
					
						
							|  |  |  |         [ length serialize-cell ] | 
					
						
							|  |  |  |         [ write ] tri
 | 
					
						
							|  |  |  |     ] serialize-shared ;
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  | M: string (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |         CHAR: s write1
 | 
					
						
							|  |  |  |         [ add-object ] | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             utf8 encode | 
					
						
							|  |  |  |             [ length serialize-cell ] | 
					
						
							|  |  |  |             [ write ] bi
 | 
					
						
							|  |  |  |         ] bi
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] serialize-shared ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  | : serialize-true ( word -- )
 | 
					
						
							|  |  |  |     drop CHAR: t write1 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : serialize-gensym ( word -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |         CHAR: G write1
 | 
					
						
							|  |  |  |         [ add-object ] | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  |         [ def>> (serialize) ] | 
					
						
							|  |  |  |         [ props>> (serialize) ] | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |         tri
 | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  |     ] serialize-shared ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  | : serialize-word ( word -- )
 | 
					
						
							|  |  |  |     CHAR: w write1
 | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  |     [ name>> (serialize) ] | 
					
						
							|  |  |  |     [ vocabulary>> (serialize) ] | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     bi ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: word (serialize) ( obj -- )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         { [ dup t eq? ] [ serialize-true ] } | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  |         { [ dup vocabulary>> not ] [ serialize-gensym ] } | 
					
						
							| 
									
										
										
										
											2008-04-11 13:57:43 -04:00
										 |  |  |         [ serialize-word ] | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     } cond ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | M: wrapper (serialize) ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     CHAR: W write1
 | 
					
						
							| 
									
										
										
										
											2008-06-30 06:13:50 -04:00
										 |  |  |     wrapped>> (serialize) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | DEFER: (deserialize) ( -- obj )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  | SYMBOL: deserialized | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : intern-object ( obj -- )
 | 
					
						
							|  |  |  |     deserialized get push ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : deserialize-false ( -- f )
 | 
					
						
							|  |  |  |     f ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  | : deserialize-true ( -- f )
 | 
					
						
							|  |  |  |     t ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : deserialize-positive-integer ( -- number )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |     deserialize-cell ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : deserialize-negative-integer ( -- number )
 | 
					
						
							|  |  |  |     deserialize-positive-integer neg ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : deserialize-zero ( -- number )
 | 
					
						
							|  |  |  |     0 ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : deserialize-float ( -- float )
 | 
					
						
							|  |  |  |     deserialize-cell bits>double ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : deserialize-ratio ( -- ratio )
 | 
					
						
							|  |  |  |     (deserialize) (deserialize) / ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : deserialize-complex ( -- complex )
 | 
					
						
							|  |  |  |     (deserialize) (deserialize) rect> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  | : (deserialize-string) ( -- string )
 | 
					
						
							|  |  |  |     deserialize-cell read utf8 decode ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  | : deserialize-string ( -- string )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  |     (deserialize-string) dup intern-object ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : deserialize-word ( -- word )
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     (deserialize) (deserialize) 2dup lookup | 
					
						
							|  |  |  |     dup [ 2nip ] [ | 
					
						
							| 
									
										
										
										
											2008-04-26 06:49:59 -04:00
										 |  |  |         drop
 | 
					
						
							| 
									
										
										
										
											2009-01-23 19:20:47 -05:00
										 |  |  |         2array unparse "Unknown word: " prepend throw
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : deserialize-gensym ( -- word )
 | 
					
						
							| 
									
										
										
										
											2008-03-31 03:30:06 -04:00
										 |  |  |     gensym { | 
					
						
							|  |  |  |         [ intern-object ] | 
					
						
							|  |  |  |         [ (deserialize) define ] | 
					
						
							| 
									
										
										
										
											2008-06-30 06:13:50 -04:00
										 |  |  |         [ (deserialize) >>props drop ] | 
					
						
							| 
									
										
										
										
											2008-03-31 03:30:06 -04:00
										 |  |  |         [ ] | 
					
						
							|  |  |  |     } cleave ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : deserialize-wrapper ( -- wrapper )
 | 
					
						
							|  |  |  |     (deserialize) <wrapper> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  | :: (deserialize-seq) ( exemplar quot -- seq )
 | 
					
						
							| 
									
										
										
										
											2008-04-14 07:04:01 -04:00
										 |  |  |     deserialize-cell exemplar new-sequence
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     [ intern-object ] | 
					
						
							|  |  |  |     [ dup [ drop quot call ] change-each ] bi ; inline
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : deserialize-array ( -- array )
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     { } [ (deserialize) ] (deserialize-seq) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : deserialize-quotation ( -- array )
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     (deserialize) >quotation dup intern-object ;
 | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | : deserialize-byte-array ( -- byte-array )
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     B{ } [ read1 ] (deserialize-seq) ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : deserialize-hashtable ( -- hashtable )
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     H{ } clone
 | 
					
						
							|  |  |  |     [ intern-object ] | 
					
						
							|  |  |  |     [ (deserialize) update ] | 
					
						
							|  |  |  |     [ ] tri ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : copy-seq-to-tuple ( seq tuple -- )
 | 
					
						
							| 
									
										
										
										
											2008-12-17 20:17:37 -05:00
										 |  |  |     [ dup length ] dip [ set-array-nth ] curry 2each ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : deserialize-tuple ( -- array )
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     #! Ugly because we have to intern the tuple before reading | 
					
						
							|  |  |  |     #! slots | 
					
						
							| 
									
										
										
										
											2008-04-13 16:06:27 -04:00
										 |  |  |     (deserialize) new
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     [ intern-object ] | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ (deserialize) ] | 
					
						
							|  |  |  |         [ [ copy-seq-to-tuple ] keep ] bi*
 | 
					
						
							|  |  |  |     ] bi ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : deserialize-unknown ( -- object )
 | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  |     deserialize-cell deserialized get nth ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : deserialize* ( -- object ? )
 | 
					
						
							|  |  |  |     read1 [ | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |         { | 
					
						
							|  |  |  |             { CHAR: A [ deserialize-byte-array ] } | 
					
						
							|  |  |  |             { CHAR: F [ deserialize-float ] } | 
					
						
							|  |  |  |             { CHAR: T [ deserialize-tuple ] } | 
					
						
							|  |  |  |             { CHAR: W [ deserialize-wrapper ] } | 
					
						
							|  |  |  |             { CHAR: a [ deserialize-array ] } | 
					
						
							|  |  |  |             { CHAR: c [ deserialize-complex ] } | 
					
						
							|  |  |  |             { CHAR: h [ deserialize-hashtable ] } | 
					
						
							|  |  |  |             { CHAR: m [ deserialize-negative-integer ] } | 
					
						
							|  |  |  |             { CHAR: n [ deserialize-false ] } | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |             { CHAR: t [ deserialize-true ] } | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |             { CHAR: o [ deserialize-unknown ] } | 
					
						
							|  |  |  |             { CHAR: p [ deserialize-positive-integer ] } | 
					
						
							|  |  |  |             { CHAR: q [ deserialize-quotation ] } | 
					
						
							|  |  |  |             { CHAR: r [ deserialize-ratio ] } | 
					
						
							|  |  |  |             { CHAR: s [ deserialize-string ] } | 
					
						
							|  |  |  |             { CHAR: w [ deserialize-word ] } | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |             { CHAR: G [ deserialize-word ] } | 
					
						
							| 
									
										
										
										
											2008-03-08 03:51:26 -05:00
										 |  |  |             { CHAR: z [ deserialize-zero ] } | 
					
						
							|  |  |  |         } case t
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  |     ] [ | 
					
						
							|  |  |  |         f f
 | 
					
						
							|  |  |  |     ] if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (deserialize) ( -- obj )
 | 
					
						
							|  |  |  |     deserialize* [ "End of stream" throw ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-14 23:49:17 -05:00
										 |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-08 04:50:03 -05:00
										 |  |  | : deserialize ( -- obj )
 | 
					
						
							| 
									
										
										
										
											2008-03-18 02:26:30 -04:00
										 |  |  |     V{ } clone deserialized | 
					
						
							|  |  |  |     [ (deserialize) ] with-variable ;
 | 
					
						
							| 
									
										
										
										
											2007-09-20 18:09:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : serialize ( obj -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-17 00:41:26 -04:00
										 |  |  |     H{ } clone serialized [ (serialize) ] with-variable ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : bytes>object ( bytes -- obj )
 | 
					
						
							|  |  |  |     binary [ deserialize ] with-byte-reader ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : object>bytes ( obj -- bytes )
 | 
					
						
							|  |  |  |     binary [ serialize ] with-byte-writer ;
 |