Skip to content
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

c#: Remove Copy of data during import call for base types #1122

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions crates/csharp/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,21 +581,24 @@ impl Bindgen for FunctionBindgen<'_, '_> {
}

Instruction::ListCanonLower { element, realloc } => {
let list = &operands[0];
let (_size, ty) = list_element_info(element);

let list: &String = &operands[0];
match self.interface_gen.direction {
Direction::Import => {
let buffer: String = self.locals.tmp("buffer");
let ptr: String = self.locals.tmp("listPtr");
let handle: String = self.locals.tmp("gcHandle");
// Despite the name GCHandle.Alloc here this does not actually allocate memory on the heap.
// It pins the array with the garbage collector so that it can be passed to unmanaged code.
// It is required to free the pin after use which is done in the Cleanup section.
uwrite!(
self.src,
"
void* {buffer} = stackalloc {ty}[({list}).Length];
{list}.AsSpan<{ty}>().CopyTo(new Span<{ty}>({buffer}, {list}.Length));
var {handle} = GCHandle.Alloc({list}, GCHandleType.Pinned);
var {ptr} = {handle}.AddrOfPinnedObject();
"
);
results.push(format!("(int){buffer}"));
results.push(format!("{ptr}"));
results.push(format!("({list}).Length"));
self.cleanup.push(Cleanup { address: handle });
}
Direction::Export => {
let address = self.locals.tmp("address");
Expand Down Expand Up @@ -973,7 +976,6 @@ impl Bindgen for FunctionBindgen<'_, '_> {
uwriteln!(self.src, "return ({results});")
}
}

// Close all the fixed blocks.
for _ in 0..self.fixed {
uwriteln!(self.src, "}}");
Expand Down Expand Up @@ -1063,7 +1065,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {

match direction {
Direction::Import => {
let import_name = self.interface_gen.type_name_with_qualifier(&Type::Id(id), true);
let import_name = self.interface_gen.type_name_with_qualifier(&Type::Id(id), true);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some how these two lines had tabs, while rest of the file was spaces


if let FunctionKind::Constructor(_) = self.kind {
resource = "this".to_owned();
Expand All @@ -1082,13 +1084,13 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Direction::Export => {
self.interface_gen.csharp_gen.needs_rep_table = true;

let export_name = self.interface_gen.csharp_gen.all_resources[&id].export_impl_name();
let export_name = self.interface_gen.csharp_gen.all_resources[&id].export_impl_name();
if is_own {
uwriteln!(
self.src,
"var {resource} = ({export_name}) {export_name}.repTable.Get\
({export_name}.WasmInterop.wasmImportResourceRep({op}));
{resource}.Handle = {op};"
({export_name}.WasmInterop.wasmImportResourceRep({op}));
{resource}.Handle = {op};"
);
} else {
uwriteln!(self.src, "var {resource} = ({export_name}) {export_name}.repTable.Get({op});");
Expand Down
Loading