Skip to content

Commit

Permalink
remove cabi_realloc
Browse files Browse the repository at this point in the history
minor changes to many-arguments test but this requires a change to NativeAOT-LLVM dotnet/runtimelab#2410
  • Loading branch information
yowl committed Oct 4, 2023
1 parent a001351 commit 8b2068d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 61 deletions.
62 changes: 15 additions & 47 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ impl WorldGenerator for CSharp {

uwrite!(
src,
"namespace {namespace} {{
"{CSHARP_IMPORTS}
{CSHARP_IMPORTS}
namespace {namespace} {{
public interface {name}World {{
"
Expand Down Expand Up @@ -406,39 +406,6 @@ impl WorldGenerator for CSharp {
}

src.push_str("\n");

src.push_str(
r#"
internal static class Intrinsics
{
[UnmanagedCallersOnly(EntryPoint = "cabi_realloc")]
internal static IntPtr cabi_realloc(IntPtr ptr, uint old_size, uint align, uint new_size)
{
if (new_size == 0)
{
if(old_size != 0)
{
Marshal.Release(ptr);
}
return new IntPtr((int)align);
}
if (new_size > int.MaxValue)
{
throw new ArgumentException("Cannot allocate more that int.MaxValue", nameof(new_size));
}
if(old_size != 0)
{
return Marshal.ReAllocHGlobal(ptr, (int)new_size);
}
return Marshal.AllocHGlobal((int)new_size);
}
}
"#,
);

src.push_str("}\n");

files.push(&format!("{name}.cs"), indent(&src).as_bytes());
Expand All @@ -450,9 +417,9 @@ impl WorldGenerator for CSharp {

let body = format!(
"// Generated by `wit-bindgen` {version}. DO NOT EDIT!
namespace {namespace}.{name};
{CSHARP_IMPORTS}
{CSHARP_IMPORTS}
namespace {namespace}.{name};
public partial class {stub_class_name} : {interface_name} {{
}}
Expand Down Expand Up @@ -489,14 +456,14 @@ impl WorldGenerator for CSharp {
if body.len() > 0 {
let body = format!(
"// Generated by `wit-bindgen` {version}. DO NOT EDIT!
namespace {namespace}.{name};
{CSHARP_IMPORTS}
{CSHARP_IMPORTS}
namespace {namespace}.{name};
public interface {interface_name} {{
{body}
}}
"
public interface {interface_name} {{
{body}
}}
"
);

files.push(&format!("{name}.cs"), indent(&body).as_bytes());
Expand All @@ -511,10 +478,10 @@ impl WorldGenerator for CSharp {

let body = format!(
"// Generated by `wit-bindgen` {version}. DO NOT EDIT!
namespace {namespace}.{name};
{CSHARP_IMPORTS}
namespace {namespace}.{name};
public static class {interface_name}Interop {{
{body}
}}
Expand Down Expand Up @@ -1339,9 +1306,10 @@ impl Bindgen for FunctionBindgen<'_, '_> {
Instruction::F32Store { .. } => todo!("F32Store"),
Instruction::F64Store { .. } => todo!("F64Store"),

Instruction::I64FromU64 => results.push(format!("(long)({})", operands[0].clone())),

//This is handled in the C interface, so we just pass the value as is.
Instruction::I32FromChar
| Instruction::I64FromU64
| Instruction::I64FromS64
| Instruction::I32FromU32
| Instruction::I32FromS32
Expand Down
31 changes: 18 additions & 13 deletions tests/runtime/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use wasmtime::component::{Component, Instance, Linker};
use wasmtime::{Config, Engine, Store};
use wasmtime_wasi::preview2::{Table, WasiCtx, WasiCtxBuilder, WasiView};
use wit_component::{ComponentEncoder, StringEncoding};
use wit_parser::{Resolve, WorldKey};
use wit_parser::{Resolve, WorldItem};

mod flavorful;
mod lists;
Expand Down Expand Up @@ -626,21 +626,24 @@ fn tests(name: &str, dir_name: &str) -> Result<Vec<PathBuf>> {
for world in &resolve.worlds {

for import in &world.1.imports {
match import.0 {
WorldKey::Name(name) => {
},
WorldKey::Interface(id) => {
let module_name = resolve.name_world_key(import.0);
csproj.push_str("\t<ItemGroup>\n");

for (i, (_name, func)) in resolve.interfaces[*id].functions.iter().enumerate() {
let wasm_import = format!("\t\t<WasmImport Include=\"{}!{}\" />\n", module_name, func.name);
let module_name = resolve.name_world_key(import.0);
csproj.push_str("\t<ItemGroup>\n");

match import.1 {
WorldItem::Function(f) => {
let wasm_import = format!("\t\t<WasmImport Include=\"{}!{}\" />\n", module_name, f.name);
csproj.push_str(&wasm_import);
}
WorldItem::Interface(id) => {
for (_, f) in resolve.interfaces[*id].functions.iter() {
let wasm_import = format!("\t\t<WasmImport Include=\"{}!{}\" />\n", module_name, f.name);
csproj.push_str(&wasm_import);
}

csproj.push_str("\t</ItemGroup>\n\n");
}
};
WorldItem::Type(_) => {}
}

csproj.push_str("\t</ItemGroup>\n\n");
}
}

Expand Down Expand Up @@ -730,6 +733,7 @@ fn tests(name: &str, dir_name: &str) -> Result<Vec<PathBuf>> {
.arg("/p:MSBuildEnableWorkloadResolver=false")
.arg("--self-contained")
.arg("/p:UseAppHost=false")
.arg("/bl")
.arg("-o")
.arg(&out_wasm);
println!("{:?}", cmd);
Expand Down Expand Up @@ -758,6 +762,7 @@ fn tests(name: &str, dir_name: &str) -> Result<Vec<PathBuf>> {
.validate(true)
.adapter("wasi_snapshot_preview1", &wasi_adapter)
.expect("adapter failed to get loaded")
.option(true)
.encode()
.expect(&format!(
"module {:?} can be translated to a component",
Expand Down
2 changes: 1 addition & 1 deletion tests/runtime/many_arguments/wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class ManyArgumentsWorldImpl : ManyArgumentsWorld
{
internal static void ManyArguments(
public static void ManyArguments(
ulong a1,
ulong a2,
ulong a3,
Expand Down

0 comments on commit 8b2068d

Please sign in to comment.