@@ -5,41 +5,68 @@ use std::io;
55use std:: collections:: HashSet ;
66use super :: objects:: { Code , ObjectContent , Object , ObjectRef , ObjectStore } ;
77use self :: common:: Object as MarshalObject ;
8+ use self :: common:: Code as MarshalCode ;
89
910macro_rules! translate_vector {
1011 ( $e: expr, $map: ident, $store: ident ) => { $e. into_iter( ) . map( |o| translate_object( o, $map, $store) ) . collect( ) }
1112}
1213
13- fn translate_object ( marshal_object : MarshalObject , translation_map : & Vec < ObjectRef > , store : & mut ObjectStore ) -> ObjectRef {
14+ macro_rules! translate_code_field {
15+ ( $code: ident, $expected: ident, $field: ident, $map: ident, $store: ident, $error: expr ) => {
16+ match translate_object_content( $code. $field, $map, $store) {
17+ ObjectContent :: $expected( v) => v,
18+ _ => panic!( $error) ,
19+ }
20+ } ;
21+ }
22+
23+ // TODO: more fields
24+ fn translate_code ( c : MarshalCode , translation_map : & Vec < ObjectRef > , store : & mut ObjectStore ) -> Code {
25+ let code = translate_code_field ! ( c, Bytes , code, translation_map, store, "Code.code object must be bytes." ) ;
26+ let consts = translate_code_field ! ( c, Tuple , consts, translation_map, store, "Code.consts object must be a tuple." ) ;
27+ let names = translate_code_field ! ( c, Tuple , names, translation_map, store, "Code.names object must be a tuple." ) ;
28+ Code { code : code, consts : consts, names : names }
29+ }
30+
31+ fn translate_object_content ( marshal_object : MarshalObject , translation_map : & Vec < ObjectRef > , store : & mut ObjectStore ) -> ObjectContent {
1432 match marshal_object {
1533 MarshalObject :: Hole => panic ! ( "Remaining hole." ) ,
16- MarshalObject :: None => store . allocate ( ObjectContent :: None ) ,
17- MarshalObject :: False => store . allocate ( ObjectContent :: False ) ,
18- MarshalObject :: True => store . allocate ( ObjectContent :: True ) ,
19- MarshalObject :: Int ( i) => store . allocate ( ObjectContent :: Int ( i) ) ,
20- MarshalObject :: String ( s) => store . allocate ( ObjectContent :: String ( s) ) ,
21- MarshalObject :: Bytes ( v) => store . allocate ( ObjectContent :: Bytes ( v) ) ,
34+ MarshalObject :: None => ObjectContent :: None ,
35+ MarshalObject :: False => ObjectContent :: False ,
36+ MarshalObject :: True => ObjectContent :: True ,
37+ MarshalObject :: Int ( i) => ObjectContent :: Int ( i) ,
38+ MarshalObject :: String ( s) => ObjectContent :: String ( s) ,
39+ MarshalObject :: Bytes ( v) => ObjectContent :: Bytes ( v) ,
2240 MarshalObject :: Tuple ( v) => {
2341 let v = translate_vector ! ( v, translation_map, store) ;
24- store . allocate ( ObjectContent :: Tuple ( v) )
42+ ObjectContent :: Tuple ( v)
2543 } ,
2644 MarshalObject :: List ( v) => {
2745 let v = translate_vector ! ( v, translation_map, store) ;
28- store . allocate ( ObjectContent :: List ( v) )
46+ ObjectContent :: List ( v)
2947 } ,
3048 MarshalObject :: Set ( v) => {
3149 let v = translate_vector ! ( v, translation_map, store) ;
32- store . allocate ( ObjectContent :: Set ( v) )
50+ ObjectContent :: Set ( v)
3351 } ,
3452 MarshalObject :: FrozenSet ( v) => {
3553 let v = translate_vector ! ( v, translation_map, store) ;
36- store . allocate ( ObjectContent :: FrozenSet ( v) )
54+ ObjectContent :: FrozenSet ( v)
3755 } ,
3856 MarshalObject :: Code ( c) => {
39- let code = translate_object ( c. code , translation_map, store) ;
40- store. allocate ( ObjectContent :: Code ( Code { code : code} ) ) // TODO: more fields
57+ ObjectContent :: Code ( translate_code ( * c, translation_map, store) )
4158 } ,
59+ MarshalObject :: Ref ( _) => panic ! ( "For references, call translate_and_allocate_object." )
60+ }
61+ }
62+
63+ fn translate_object ( marshal_object : MarshalObject , translation_map : & Vec < ObjectRef > , store : & mut ObjectStore ) -> ObjectRef {
64+ match marshal_object {
4265 MarshalObject :: Ref ( i) => translation_map. get ( i as usize ) . unwrap ( ) . clone ( ) , // TODO: overflow check
66+ _ => {
67+ let obj_content = translate_object_content ( marshal_object, translation_map, store) ;
68+ store. allocate ( obj_content)
69+ } ,
4370 }
4471}
4572
0 commit comments