-
Notifications
You must be signed in to change notification settings - Fork 217
C# Fixes wasi http header bug and adds a test for it #1215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -66,7 +66,7 @@ impl<'a, 'b> FunctionBindgen<'a, 'b> { | |||
resource_drops: Vec::new(), | ||||
is_block: false, | ||||
fixed_statments: Vec::new(), | ||||
parameter_type: parameter_type, | ||||
parameter_type, | ||||
} | ||||
} | ||||
|
||||
|
@@ -458,14 +458,14 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
if flags.flags.len() > 32 { | ||||
results.push(format!( | ||||
"unchecked((int)(((long){}) & uint.MaxValue))", | ||||
operands[0].to_string() | ||||
operands[0] | ||||
)); | ||||
results.push(format!( | ||||
"unchecked(((int)((long){} >> 32)))", | ||||
operands[0].to_string() | ||||
operands[0] | ||||
)); | ||||
} else { | ||||
results.push(format!("(int){}", operands[0].to_string())); | ||||
results.push(format!("(int){}", operands[0])); | ||||
} | ||||
} | ||||
|
||||
|
@@ -479,8 +479,8 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
results.push(format!( | ||||
"({})(unchecked((uint)({})) | (ulong)(unchecked((uint)({}))) << 32)", | ||||
qualified_type_name, | ||||
operands[0].to_string(), | ||||
operands[1].to_string() | ||||
operands[0], | ||||
operands[1] | ||||
)); | ||||
} else { | ||||
results.push(format!("({})({})", qualified_type_name, operands[0])) | ||||
|
@@ -502,7 +502,7 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
let mut result = format!("new {} (\n", qualified_type_name); | ||||
|
||||
result.push_str(&operands.join(", ")); | ||||
result.push_str(")"); | ||||
result.push(')'); | ||||
|
||||
results.push(result); | ||||
} | ||||
|
@@ -754,7 +754,7 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
" | ||||
); | ||||
} | ||||
results.push(format!("(nint){ptr}")); | ||||
results.push(format!("(int){ptr}")); | ||||
results.push(format!("({list}).Length")); | ||||
} | ||||
Direction::Export => { | ||||
|
@@ -834,7 +834,7 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
results.push(format!("(int){str_ptr}")); | ||||
} | ||||
|
||||
results.push(format!("{length}")); | ||||
results.push(length); | ||||
if FunctionKind::Freestanding == *self.kind || self.interface_gen.direction == Direction::Export { | ||||
self.interface_gen.require_interop_using("System.Text"); | ||||
self.interface_gen.require_interop_using("System.Runtime.InteropServices"); | ||||
|
@@ -882,20 +882,27 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
); | ||||
let ret_area = self.locals.tmp("retArea"); | ||||
|
||||
let array_size = if align > 1 { | ||||
// Add one additional element in case the starting address is not aligned | ||||
format!("{array_size} * {list}.Count + 1") | ||||
} else { | ||||
format!("{array_size} * {list}.Count") | ||||
}; | ||||
|
||||
match realloc { | ||||
None => { | ||||
self.needs_cleanup = true; | ||||
uwrite!(self.src, | ||||
" | ||||
void* {address}; | ||||
if (({size} * {list}.Count) < 1024) {{ | ||||
var {ret_area} = stackalloc {element_type}[({array_size}*{list}.Count)+1]; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks like this moved the logic to add the additional 1 to the wit-bindgen/crates/csharp/src/function.rs Line 1294 in 6541e44
|
||||
var {ret_area} = stackalloc {element_type}[{array_size}]; | ||||
{address} = (void*)(((int){ret_area}) + ({align} - 1) & -{align}); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this line is still required or in some cases this won't align properly since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor |
||||
}} | ||||
else | ||||
{{ | ||||
var {buffer_size} = {size} * (nuint){list}.Count; | ||||
{address} = NativeMemory.AlignedAlloc({buffer_size}, {align}); | ||||
var {buffer_size} = {size} * {list}.Count; | ||||
{address} = NativeMemory.AlignedAlloc((nuint){buffer_size}, {align}); | ||||
cleanups.Add(() => NativeMemory.AlignedFree({address})); | ||||
}} | ||||
" | ||||
|
@@ -905,8 +912,8 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
//cabi_realloc_post_return will be called to clean up this allocation | ||||
uwrite!(self.src, | ||||
" | ||||
var {buffer_size} = {size} * (nuint){list}.Count; | ||||
void* {address} = NativeMemory.AlignedAlloc({buffer_size}, {align}); | ||||
var {buffer_size} = {size} * {list}.Count; | ||||
void* {address} = NativeMemory.AlignedAlloc((nuint){buffer_size}, {align}); | ||||
" | ||||
); | ||||
} | ||||
|
@@ -1046,7 +1053,7 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
} | ||||
|
||||
Instruction::Return { amt, .. } => { | ||||
if self.fixed_statments.len() > 0 { | ||||
if !self.fixed_statments.is_empty() { | ||||
let fixed: String = self.fixed_statments.iter().map(|f| format!("{} = {}", f.ptr_name, f.item_to_pin)).collect::<Vec<_>>().join(", "); | ||||
self.src.insert_str(0, &format!("fixed (void* {fixed}) | ||||
{{ | ||||
|
@@ -1077,7 +1084,7 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
} | ||||
} | ||||
|
||||
if self.fixed_statments.len() > 0 { | ||||
if !self.fixed_statments.is_empty() { | ||||
uwriteln!(self.src, "}}"); | ||||
} | ||||
} | ||||
|
@@ -1201,7 +1208,7 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
} | ||||
} | ||||
} | ||||
results.push(format!("{handle}")); | ||||
results.push(handle); | ||||
} | ||||
|
||||
Instruction::HandleLift { | ||||
|
@@ -1253,7 +1260,7 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
} | ||||
|
||||
Instruction::Flush { amt } => { | ||||
results.extend(operands.iter().take(*amt).map(|v| v.clone())); | ||||
results.extend(operands.iter().take(*amt).cloned()); | ||||
} | ||||
|
||||
Instruction::AsyncPostCallInterface { .. } | ||||
|
@@ -1288,15 +1295,22 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
// to align the allocation via the stackalloc command, unlike with a fixed array where the pointer will be aligned. | ||||
// We get the final ptr to pass to the wasm runtime by shifting to the | ||||
// correctly aligned pointer (sometimes it can be already aligned). | ||||
let array_size = if self.import_return_pointer_area_align > 1 { | ||||
// Add one additional element in case the starting address is not aligned | ||||
array_size + 1 | ||||
} else { | ||||
array_size | ||||
}; | ||||
|
||||
uwrite!( | ||||
self.src, | ||||
" | ||||
var {ret_area} = stackalloc {element_type}[{array_size}+1]; | ||||
var {ret_area} = stackalloc {element_type}[{array_size}]; | ||||
var {ptr} = ((int){ret_area}) + ({align} - 1) & -{align}; | ||||
", | ||||
align = align.align_wasm32() | ||||
); | ||||
format!("{ptr}") | ||||
ptr | ||||
} | ||||
Direction::Export => { | ||||
// exports need their return area to be live until the post-return call. | ||||
|
@@ -1319,7 +1333,7 @@ impl Bindgen for FunctionBindgen<'_, '_> { | |||
); | ||||
self.interface_gen.csharp_gen.needs_export_return_area = true; | ||||
|
||||
format!("{ptr}") | ||||
ptr | ||||
} | ||||
} | ||||
} | ||||
|
@@ -1395,8 +1409,8 @@ fn perform_cast(op: &String, cast: &Bitcast) -> String { | |||
Bitcast::F64ToI64 => format!("BitConverter.DoubleToInt64Bits({op})"), | ||||
Bitcast::I32ToI64 => format!("(long) ({op})"), | ||||
Bitcast::I64ToI32 => format!("(int) ({op})"), | ||||
Bitcast::I64ToP64 => format!("{op}"), | ||||
Bitcast::P64ToI64 => format!("{op}"), | ||||
Bitcast::I64ToP64 => op.to_string(), | ||||
Bitcast::P64ToI64 => op.to_string(), | ||||
Bitcast::LToI64 | Bitcast::PToP64 => format!("(long) ({op})"), | ||||
Bitcast::I64ToL | Bitcast::P64ToP => format!("(int) ({op})"), | ||||
Bitcast::I32ToP | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it generates
And that uses
TryWriteBytes(Span<Byte>, UInt64)
which is wrong. There is no TryWriteBytes overload fornint
.And then it returns
false
but we ignore it.Let's stop using TryWriteBytes.
Instead it could be just