Skip to content

Commit fe8a32b

Browse files
committed
Hack in proper marshal/unmarshal logic for int64 and uint64.
1 parent b6e8f4d commit fe8a32b

4 files changed

Lines changed: 63 additions & 5 deletions

File tree

Libraries/JSIL.Unsafe.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1689,7 +1689,34 @@ JSIL.$EmitMemcpyIntrinsic = function (body, destToken, sourceToken, destOffsetTo
16891689
};
16901690

16911691
JSIL.$MakeInt64MarshalFunctionSource = function (typeObject, marshal, isConstructor, closure, body) {
1692-
// FIXME
1692+
var targetVar = isConstructor ? "this" : "struct";
1693+
var triplet;
1694+
1695+
if (marshal) {
1696+
triplet = function (targetField, offset, count) {
1697+
var f = "struct." + targetField;
1698+
1699+
body.push("bytes[(offset + " + offset + ") | 0] = " + f + " & 0xFF;");
1700+
if (count > 1)
1701+
body.push("bytes[(offset + " + (offset + 1) + ") | 0] = (" + f + " >> 8) & 0xFF;");
1702+
if (count > 2)
1703+
body.push("bytes[(offset + " + (offset + 2) + ") | 0] = (" + f + " >> 16) & 0xFF;");
1704+
};
1705+
} else {
1706+
triplet = function (targetField, offset, count) {
1707+
body.push(targetVar + "." + targetField + " = (");
1708+
body.push(" bytes[(offset + " + offset + ") | 0] ");
1709+
if (count > 1)
1710+
body.push(" | (bytes[(offset + " + (offset + 1) + ") | 0] << 8)");
1711+
if (count > 2)
1712+
body.push(" | (bytes[(offset + " + (offset + 2) + ") | 0] << 16)");
1713+
body.push(");");
1714+
};
1715+
}
1716+
1717+
triplet("a", 0, 3);
1718+
triplet("b", 3, 3);
1719+
triplet("c", 6, 2);
16931720
};
16941721

16951722
JSIL.$MakeStructMarshalFunctionSource = function (typeObject, marshal, isConstructor, closure, body) {

Tests/Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@
449449
<None Include="UnsafeTestCases\UnionContainingEnum.cs" />
450450
<None Include="UnsafeTestCases\SDLTouchStructLayout.cs" />
451451
<None Include="UnsafeTestCases\ReadInt64InStruct.cs" />
452+
<None Include="UnsafeTestCases\WriteUInt64InStruct.cs" />
452453
<Compile Include="XMLTests.cs" />
453454
<Compile Include="DependencyTests.cs" />
454455
<None Include="TestCases\GenericParameterNameShadowing.cs" />

Tests/UnsafeTestCases/ReadInt64InStruct.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ public override string ToString () {
1313
}
1414

1515
public static unsafe void Main (string[] args) {
16-
var bytes = new byte[] {
16+
var bytes = new byte[20] {
1717
0x02, 0x00, 0x00, 0x00,
18-
0x02, 0x00, 0x00, 0x00,
19-
0x00, 0x00, 0x60, 0x40,
20-
0x00, 0x00, 0x60, 0x40,
18+
19+
// Padding fill
20+
0xFF, 0xFF, 0xFF, 0xFF,
21+
22+
0x02, 0x08, 0x00, 0x02,
23+
0x12, 0x00, 0x60, 0x40,
24+
2125
0x00, 0x00, 0x60, 0x40
2226
};
2327

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
public static class Program {
5+
public struct StructWithUInt64 {
6+
public uint A;
7+
public UInt64 B;
8+
public uint C;
9+
}
10+
11+
public static unsafe void Main (string[] args) {
12+
var bytes = new byte[20];
13+
14+
fixed (byte* pBytes = bytes) {
15+
var pStruct = (StructWithUInt64*)pBytes;
16+
17+
*pStruct = new StructWithUInt64 {
18+
A = 0xAABBCCDD,
19+
B = 0x0011223344556677UL,
20+
C = 0xDDCCBBAA
21+
};
22+
}
23+
24+
Util.PrintBytes(bytes);
25+
}
26+
}

0 commit comments

Comments
 (0)