@@ -27,7 +27,6 @@ pub(crate) struct FunctionBindgen<'a, 'b> {
27
27
cleanup : Vec < Cleanup > ,
28
28
import_return_pointer_area_size : usize ,
29
29
import_return_pointer_area_align : usize ,
30
- fixed : usize , // Number of `fixed` blocks that need to be closed.
31
30
pub ( crate ) resource_drops : Vec < ( String , String ) > ,
32
31
}
33
32
@@ -61,7 +60,6 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> {
61
60
cleanup : Vec :: new ( ) ,
62
61
import_return_pointer_area_size : 0 ,
63
62
import_return_pointer_area_align : 0 ,
64
- fixed : 0 ,
65
63
resource_drops : Vec :: new ( ) ,
66
64
}
67
65
}
@@ -1016,10 +1014,6 @@ impl Bindgen for FunctionBindgen<'_, '_> {
1016
1014
uwriteln ! ( self . src, "return ({results});" )
1017
1015
}
1018
1016
}
1019
- // Close all the fixed blocks.
1020
- for _ in 0 ..self . fixed {
1021
- uwriteln ! ( self . src, "}}" ) ;
1022
- }
1023
1017
}
1024
1018
}
1025
1019
@@ -1160,8 +1154,6 @@ impl Bindgen for FunctionBindgen<'_, '_> {
1160
1154
fn return_pointer ( & mut self , size : usize , align : usize ) -> String {
1161
1155
let ptr = self . locals . tmp ( "ptr" ) ;
1162
1156
1163
- // Use a stack-based return area for imports, because exports need
1164
- // their return area to be live until the post-return call.
1165
1157
match self . interface_gen . direction {
1166
1158
Direction :: Import => {
1167
1159
self . import_return_pointer_area_size =
@@ -1173,25 +1165,22 @@ impl Bindgen for FunctionBindgen<'_, '_> {
1173
1165
self . import_return_pointer_area_align ,
1174
1166
) ;
1175
1167
let ret_area = self . locals . tmp ( "retArea" ) ;
1176
- let ret_area_byte0 = self . locals . tmp ( "retAreaByte0" ) ;
1168
+ // We can use the stack here to get a return pointer when importing.
1169
+ // We do need to do a slight over-allocation since C# doesn't provide a way
1170
+ // to align the allocation via the stackalloc command, unlike with a fixed array where the pointer will be aligned.
1171
+ // We get the final ptr to pass to the wasm runtime by shifting to the
1172
+ // correctly aligned pointer (sometimes it can be already aligned).
1177
1173
uwrite ! (
1178
1174
self . src,
1179
1175
"
1180
- var {2} = new {0}[{1}];
1181
- fixed ({0}* {3} = &{2}[0])
1182
- {{
1183
- var {ptr} = (nint){3};
1184
- " ,
1185
- element_type,
1186
- array_size,
1187
- ret_area,
1188
- ret_area_byte0
1176
+ var {ret_area} = stackalloc {element_type}[{array_size}+1];
1177
+ var {ptr} = ((int){ret_area}) + ({align} - 1) & -{align};
1178
+ "
1189
1179
) ;
1190
- self . fixed = self . fixed + 1 ;
1191
-
1192
1180
format ! ( "{ptr}" )
1193
1181
}
1194
1182
Direction :: Export => {
1183
+ // exports need their return area to be live until the post-return call.
1195
1184
self . interface_gen . csharp_gen . return_area_size =
1196
1185
self . interface_gen . csharp_gen . return_area_size . max ( size) ;
1197
1186
self . interface_gen . csharp_gen . return_area_align =
0 commit comments