@@ -2638,7 +2638,7 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
26382638
26392639 if matches ! ( self . r#gen. direction, Some ( Direction :: Import ) ) {
26402640 self . needs_deallocate = true ;
2641- self . push_str ( & format ! ( "deallocate ~= {list};\n " ) ) ;
2641+ self . push_str ( & format ! ( "if ({list_src}.length) deallocate ~= {list};\n " ) ) ;
26422642 }
26432643
26442644 self . push_str ( & format ! (
@@ -2671,12 +2671,17 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
26712671 let len = tempname ( "_len" , tmp) ;
26722672
26732673 self . push_str ( & format ! (
2674- "auto {ptr } = cast({elem_name}*)({}) ;
2675- auto {len } = {} ;
2674+ "auto {len } = {} ;
2675+ auto {ptr } = {len} ? cast({elem_name}*)({}) : null ;
26762676 " ,
2677- operands[ 0 ] , operands[ 1 ]
2677+ operands[ 1 ] , operands[ 0 ]
26782678 ) ) ;
26792679
2680+ if matches ! ( self . r#gen. direction, Some ( Direction :: Export ) ) {
2681+ self . needs_deallocate = true ;
2682+ self . push_str ( & format ! ( "if ({len}) deallocate ~= cast(){ptr};\n " ) ) ;
2683+ }
2684+
26802685 results. push ( format ! ( "{list_name}({ptr}[0..{len}])" ) ) ;
26812686 }
26822687 abi:: Instruction :: StringLift => {
@@ -2686,12 +2691,17 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
26862691 let len = tempname ( "_len" , tmp) ;
26872692
26882693 self . push_str ( & format ! (
2689- "auto {ptr } = cast(char*)({}) ;
2690- auto {len } = {} ;
2694+ "auto {len } = {} ;
2695+ auto {ptr } = {len} ? cast(char*)({}) : null ;
26912696 " ,
2692- operands[ 0 ] , operands[ 1 ]
2697+ operands[ 1 ] , operands[ 0 ]
26932698 ) ) ;
26942699
2700+ if matches ! ( self . r#gen. direction, Some ( Direction :: Export ) ) {
2701+ self . needs_deallocate = true ;
2702+ self . push_str ( & format ! ( "if ({len}) deallocate ~= cast(){ptr};\n " ) ) ;
2703+ }
2704+
26952705 results. push ( format ! ( "WitString({ptr}[0..{len}])" ) ) ;
26962706 }
26972707 abi:: Instruction :: ListLift { ty, element, .. } => {
@@ -2719,7 +2729,9 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
27192729
27202730 if matches ! ( self . r#gen. direction, Some ( Direction :: Export ) ) {
27212731 self . needs_deallocate = true ;
2722- self . push_str ( & format ! ( "deallocate ~= cast(void*){list}.ptr;\n " ) ) ;
2732+ self . push_str ( & format ! (
2733+ "if ({list_len}) deallocate ~= cast(void*){list}.ptr;\n "
2734+ ) ) ;
27232735 }
27242736
27252737 self . push_str ( & format ! (
@@ -2732,11 +2744,14 @@ impl<'a, 'b> Bindgen for FunctionBindgen<'a, 'b> {
27322744 self . push_str ( & format ! ( "{block_element} = {};" , block_results[ 0 ] ) ) ;
27332745 self . push_str ( "\n }\n " ) ;
27342746
2735- if ! matches ! ( self . r#gen. direction, Some ( Direction :: Export ) ) {
2747+ if matches ! ( self . r#gen. direction, Some ( Direction :: Import ) ) {
27362748 self . push_str ( & format ! (
27372749 "if ({list_len}) {}.free({list_src});\n " ,
27382750 self . r#gen. r#gen. common_module
27392751 ) ) ;
2752+ } else {
2753+ self . needs_deallocate = true ;
2754+ self . push_str ( & format ! ( "if ({list_len}) deallocate ~= {list_src};\n " ) ) ;
27402755 }
27412756
27422757 let list_name = self . r#gen . type_name ( & Type :: Id ( * ty) , self . r#gen . fqn ) ;
0 commit comments