Skip to content

Commit

Permalink
Add in the new bitcases
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
  • Loading branch information
jsturtevant committed Mar 2, 2024
1 parent d4d7200 commit 6be6ebc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
70 changes: 36 additions & 34 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ mod component_type_object;
use anyhow::Result;
use heck::{ToLowerCamelCase, ToShoutySnakeCase, ToUpperCamelCase};
use std::{
collections::{HashMap, HashSet},
fmt::Write,
iter, mem,
ops::Deref,
any::Any, collections::{HashMap, HashSet}, fmt::Write, iter, mem, ops::Deref
};
use wit_bindgen_core::{
abi::{self, AbiVariant, Bindgen, Bitcast, Instruction, LiftLower, WasmType},
Expand Down Expand Up @@ -1752,30 +1749,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
| Instruction::S64FromI64 => results.push(operands[0].clone()),

Instruction::Bitcasts { casts } => {
results.extend(casts.iter().zip(operands).map(|(cast, op)| match cast {
Bitcast::I32ToF32 => format!("BitConverter.Int32BitsToSingle({op})"),
Bitcast::I64ToF32 => format!("BitConverter.Int32BitsToSingle((int){op})"),
Bitcast::F32ToI32 => format!("BitConverter.SingleToInt32Bits({op})"),
Bitcast::F32ToI64 => format!("BitConverter.SingleToInt32Bits({op})"),
Bitcast::I64ToF64 => format!("BitConverter.Int64BitsToDouble({op})"),
Bitcast::F64ToI64 => format!("BitConverter.DoubleToInt64Bits({op})"),
Bitcast::I32ToI64 => format!("(long) ({op})"),
Bitcast::I64ToI32 => format!("(int) ({op})"),
Bitcast::None => op.to_owned(),
Bitcast::P64ToI64 => todo!(),
Bitcast::I64ToP64 => todo!(),
Bitcast::P64ToP => todo!(),
Bitcast::PToP64 => todo!(),
Bitcast::I32ToP => todo!(),
Bitcast::PToI32 => todo!(),
Bitcast::PToL => todo!(),
Bitcast::LToP => todo!(),
Bitcast::I32ToL => todo!(),
Bitcast::LToI32 => todo!(),
Bitcast::I64ToL => todo!(),
Bitcast::LToI64 => todo!(),
Bitcast::Sequence(_) => todo!(),
}))
results.extend(casts.iter().zip(operands).map(|(cast, op)| perform_cast(op, cast)))
}

Instruction::I32FromBool => {
Expand Down Expand Up @@ -2022,12 +1996,12 @@ impl Bindgen for FunctionBindgen<'_, '_> {
let op = &operands[0];
results.push(format!("({}){}", t, op));

uwriteln!(
self.src,
"Debug.Assert(Enum.IsDefined(typeof({}), {}));",
t,
op
);
// uwriteln!(
// self.src,
// "Debug.Assert(Enum.IsDefined(typeof({}), {}));",
// t,
// op
// );
}

Instruction::ListCanonLower { element, realloc } => {
Expand Down Expand Up @@ -2386,6 +2360,34 @@ impl Bindgen for FunctionBindgen<'_, '_> {
}
}

fn perform_cast(op: &String, cast: &Bitcast) -> String {
match cast {
Bitcast::I32ToF32 => format!("BitConverter.Int32BitsToSingle({op})"),
Bitcast::I64ToF32 => format!("BitConverter.Int32BitsToSingle((int){op})"),
Bitcast::F32ToI32 => format!("BitConverter.SingleToInt32Bits({op})"),
Bitcast::F32ToI64 => format!("BitConverter.SingleToInt32Bits({op})"),
Bitcast::I64ToF64 => format!("BitConverter.Int64BitsToDouble({op})"),
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::LToI64 | Bitcast::PToP64 => format!("(long) ({op})"),
Bitcast::I64ToL | Bitcast::P64ToP => format!("(int) ({op})"),
Bitcast::I32ToP
| Bitcast::PToI32
| Bitcast::I32ToL
| Bitcast::LToI32
| Bitcast::LToP
| Bitcast::PToL
| Bitcast::None => op.to_owned(),
Bitcast::Sequence(sequence) => {
let [first, second] = &**sequence;
perform_cast(&perform_cast(op, first), second)
}
}
}

fn int_type(int: Int) -> &'static str {
match int {
Int::U8 => "byte",
Expand Down
1 change: 1 addition & 0 deletions crates/csharp/tests/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ macro_rules! codegen_test {
"unused-import",
"use-across-interfaces",
"worlds-with-types",
"variants-unioning-types",
"go_params",
]
.contains(&$name)
Expand Down
3 changes: 3 additions & 0 deletions tests/runtime/variants/wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ public class VariantsWorldImpl : IVariantsWorld
{
public static void TestImports()
{
Console.WriteLine("test options");
Debug.Assert(TestInterop.RoundtripOption(new Option<float>(1.0f)).Value == 1);
Debug.Assert(TestInterop.RoundtripOption(Option<float>.None).HasValue == false);
Debug.Assert(TestInterop.RoundtripOption(new Option<float>(2.0f)).Value == 2);

Console.WriteLine("test results");
Debug.Assert(TestInterop.RoundtripResult(Result<uint, float>.ok(2)).AsOk == 2.0);
Debug.Assert(TestInterop.RoundtripResult(Result<uint, float>.ok(4)).AsOk == 4.0);
Debug.Assert(TestInterop.RoundtripResult(Result<uint, float>.err(5.3f)).AsErr == 5);

Console.WriteLine("test enums");
Debug.Assert(TestInterop.RoundtripEnum(ITest.E1.A) == ITest.E1.A);
Debug.Assert(TestInterop.RoundtripEnum(ITest.E1.B) == ITest.E1.B);

Expand Down

0 comments on commit 6be6ebc

Please sign in to comment.