Skip to content

Commit 3433f2d

Browse files
jsturtevantsilesmo
andauthored
feat(c#): Multi-return support (#794)
* feat(c#): Multi-return support Signed-off-by: James Sturtevant <[email protected]> * fixes multi return * changes after tuples went in Signed-off-by: James Sturtevant <[email protected]> --------- Signed-off-by: James Sturtevant <[email protected]> Co-authored-by: Timmy Silesmo <[email protected]>
1 parent b4fc767 commit 3433f2d

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

crates/csharp/src/lib.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,13 @@ impl WorldGenerator for CSharp {
385385
BitConverter.TryWriteBytes(span.Slice(offset), value);
386386
}}
387387
388+
public void SetF32(int offset, float value)
389+
{{
390+
Span<byte> span = this;
391+
392+
BitConverter.TryWriteBytes(span.Slice(offset), value);
393+
}}
394+
388395
internal unsafe int AddrOfBuffer()
389396
{{
390397
fixed(byte* ptr = &buffer)
@@ -668,6 +675,12 @@ impl InterfaceGenerator<'_> {
668675
return BitConverter.ToInt32(span);
669676
}}
670677
678+
internal static float GetF32(IntPtr ptr, int offset)
679+
{{
680+
var span = new Span<byte>((void*)ptr, 4);
681+
return BitConverter.ToSingle(span.Slice(offset, 4));
682+
}}
683+
671684
public static string GetUTF8String(IntPtr ptr)
672685
{{
673686
return Encoding.UTF8.GetString((byte*)GetS32(ptr), GetS32(ptr + 4));
@@ -730,6 +743,13 @@ impl InterfaceGenerator<'_> {
730743
BitConverter.TryWriteBytes(span.Slice(offset), value);
731744
}}
732745
746+
public void SetF32(int offset, float value)
747+
{{
748+
Span<byte> span = this;
749+
750+
BitConverter.TryWriteBytes(span.Slice(offset), value);
751+
}}
752+
733753
internal unsafe int AddrOfBuffer()
734754
{{
735755
fixed(byte* ptr = &buffer)
@@ -1481,7 +1501,6 @@ impl Bindgen for FunctionBindgen<'_, '_> {
14811501
}
14821502
.to_owned()
14831503
})),
1484-
14851504
Instruction::I32Load { offset } => {
14861505
if self.gen.in_import {
14871506
results.push(format!("ReturnArea.GetS32(ptr + {offset})"))
@@ -1509,9 +1528,11 @@ impl Bindgen for FunctionBindgen<'_, '_> {
15091528
Instruction::I32Load16S { offset } => {
15101529
results.push(format!("returnArea.GetS16({offset})"))
15111530
}
1512-
Instruction::I64Load { offset } => results.push(format!("returnArea.GetS64({offset})")),
1513-
Instruction::F32Load { offset } => results.push(format!("returnArea.GetF32({offset})")),
1514-
Instruction::F64Load { offset } => results.push(format!("returnArea.GetF64({offset})")),
1531+
Instruction::I64Load { offset } => results.push(format!("ReturnArea.GetS64({offset})")),
1532+
Instruction::F32Load { offset } => {
1533+
results.push(format!("ReturnArea.GetF32(ptr, {offset})"))
1534+
}
1535+
Instruction::F64Load { offset } => results.push(format!("ReturnArea.GetF64({offset})")),
15151536

15161537
Instruction::I32Store { offset } => {
15171538
uwriteln!(self.src, "returnArea.SetS32({}, {});", offset, operands[0])
@@ -1528,7 +1549,9 @@ impl Bindgen for FunctionBindgen<'_, '_> {
15281549
)
15291550
}
15301551
Instruction::I64Store { .. } => todo!("I64Store"),
1531-
Instruction::F32Store { .. } => todo!("F32Store"),
1552+
Instruction::F32Store { offset } => {
1553+
uwriteln!(self.src, "returnArea.SetF32({}, {});", offset, operands[0])
1554+
}
15321555
Instruction::F64Store { .. } => todo!("F64Store"),
15331556

15341557
Instruction::I64FromU64 => results.push(format!("unchecked((long)({}))", operands[0])),
@@ -1541,6 +1564,9 @@ impl Bindgen for FunctionBindgen<'_, '_> {
15411564
Instruction::U32FromI32 => results.push(format!("unchecked((uint)({}))", operands[0])),
15421565
Instruction::U64FromI64 => results.push(format!("unchecked((ulong)({}))", operands[0])),
15431566
Instruction::CharFromI32 => results.push(format!("unchecked((uint)({}))", operands[0])),
1567+
Instruction::Float32FromF32 => {
1568+
results.push(format!("unchecked((float){})", operands[0]))
1569+
}
15441570
Instruction::I64FromS64
15451571
| Instruction::I32FromU16
15461572
| Instruction::I32FromS16
@@ -1551,7 +1577,6 @@ impl Bindgen for FunctionBindgen<'_, '_> {
15511577
| Instruction::F64FromFloat64
15521578
| Instruction::S32FromI32
15531579
| Instruction::S64FromI64
1554-
| Instruction::Float32FromF32
15551580
| Instruction::Float64FromF64 => results.push(operands[0].clone()),
15561581

15571582
Instruction::Bitcasts { .. } => todo!("Bitcasts"),

crates/csharp/tests/codegen.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ macro_rules! codegen_test {
3333
"lift-lower-foreign",
3434
"lists",
3535
"many-arguments",
36-
"multi-return",
3736
"option-result",
3837
"rename-interface",
3938
"resource-alias",

0 commit comments

Comments
 (0)