Skip to content

Commit 4f511c6

Browse files
authored
Span & ArraySegment as MemoryView (#76)
#60 - [x] Implement Span codegen - [x] Implement ArraySegment codegen - [x] Add CS generator tests - [x] Add CS jsobject extension generator tests - [x] Add TS Proxy generator tests - [x] Add TS Initializer generator tests - [x] Add TS Snapshot generator tests - [x] Add e2e tests for span return - [x] Add e2e tests for span param - [x] Add e2e tests for arraysegment return - [x] Add e2e tests for arraysegment param - [x] Add e2e tests with initializer - [x] Add e2e tests with snapshots - [x] Implement diagnostics for unsupported type args
1 parent 8980604 commit 4f511c6

16 files changed

Lines changed: 922 additions & 361 deletions

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ Passing an initializer object in another initializer object.
276276

277277
TypeShim enriches the supported types by JSExport by adding _your_ classes to the [types marshalled by .NET](https://learn.microsoft.com/en-us/aspnet/core/client-side/dotnet-interop/?view=aspnetcore-10.0#type-mappings). Repetitive patterns for type transformation are readily supported and tested in TypeShim.
278278

279-
Of course, TypeShim brings all [types marshalled by .NET](https://learn.microsoft.com/en-us/aspnet/core/client-side/dotnet-interop/?view=aspnetcore-10.0#type-mappings) to TypeScript. This work is largely completed with the exception of Span and ArraySegment.
279+
Of course, TypeShim brings all [types marshalled by .NET](https://learn.microsoft.com/en-us/aspnet/core/client-side/dotnet-interop/?view=aspnetcore-10.0#type-mappings) to TypeScript. Makes TypeShim officially offer a superset of the .NET types available in JS.
280280

281281
TypeShim aims to continue to broaden its type support. Suggestions and contributions are welcome.
282282

@@ -312,12 +312,12 @@ TypeShim aims to continue to broaden its type support. Suggestions and contribut
312312
| `JSObject` | `Object` || Requires manual JSObject handling |
313313
| `String` | `String` || |
314314
| `T[]` | `T[]` || * [Only supported .NET types](https://learn.microsoft.com/en-us/aspnet/core/client-side/dotnet-interop/?view=aspnetcore-10.0#type-mappings) |
315-
| `Span<Byte>` | `MemoryView`| 🚧 | |
316-
| `Span<Int32>` | `MemoryView`| 🚧 | |
317-
| `Span<Double>` | `MemoryView`| 🚧 | |
318-
| `ArraySegment<Byte>` | `MemoryView`| 🚧 | |
319-
| `ArraySegment<Int32>`| `MemoryView`| 🚧 | |
320-
| `ArraySegment<Double>`| `MemoryView`| 🚧 | |
315+
| `Span<Byte>` | `MemoryView`| | |
316+
| `Span<Int32>` | `MemoryView`| | |
317+
| `Span<Double>` | `MemoryView`| | |
318+
| `ArraySegment<Byte>` | `MemoryView`| | |
319+
| `ArraySegment<Int32>`| `MemoryView`| | |
320+
| `ArraySegment<Double>`| `MemoryView`| | |
321321
| `Task` | `Promise` || * [Only supported .NET types](https://learn.microsoft.com/en-us/aspnet/core/client-side/dotnet-interop/?view=aspnetcore-10.0#type-mappings) |
322322
| `Action` | `Function` || |
323323
| `Action<T1>` | `Function` || |

src/TypeShim.E2E/TypeShim.E2E.Wasm/ExportedClass.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.InteropServices.JavaScript;
23

34
namespace TypeShim.E2E.Wasm;
45

@@ -12,3 +13,14 @@ public void Dispose()
1213
// no-op for testing purposes
1314
}
1415
}
16+
17+
18+
public partial class ManualExport
19+
{
20+
[JSExport]
21+
[return: JSMarshalAs<JSType.MemoryView>]
22+
public static Span<int> GetSpan()
23+
{
24+
return new Span<int>(new int[] { 1, 2, 3 });
25+
}
26+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System;
2+
3+
namespace TypeShim.E2E.Wasm;
4+
5+
[TSExport]
6+
public class MemoryViewMethodClass
7+
{
8+
public Span<byte> GetByteSpan() => new Span<byte>([1 << 0, 1 << 1, 1 << 2, 1 << 3, 1 << 4]);
9+
public Span<int> GetInt32Span() => new Span<int>([0, 1, 2, 3, 4]);
10+
public Span<double> GetDoubleSpan() => new Span<double>([0.1, 1.1, 2.1, 3.1, 4.1]);
11+
12+
public ArraySegment<byte> GetByteArraySegment() => new ArraySegment<byte>([1 << 0, 1 << 1, 1 << 2, 1 << 3, 1 << 4]);
13+
public ArraySegment<int> GetInt32ArraySegment() => new ArraySegment<int>([0, 1, 2, 3, 4]);
14+
public ArraySegment<double> GetDoubleArraySegment() => new ArraySegment<double>([0.1, 1.1, 2.1, 3.1, 4.1]);
15+
16+
public int SumByteSpan(Span<byte> span)
17+
{
18+
int sum = 0;
19+
foreach (byte b in span)
20+
{
21+
sum += b;
22+
}
23+
return sum;
24+
}
25+
26+
public int SumInt32Span(Span<int> span)
27+
{
28+
int sum = 0;
29+
foreach (int i in span)
30+
{
31+
sum += i;
32+
}
33+
return sum;
34+
}
35+
36+
public double SumDoubleSpan(Span<double> span)
37+
{
38+
double sum = 0;
39+
foreach (double d in span)
40+
{
41+
sum += d;
42+
}
43+
return sum;
44+
}
45+
46+
public int SumByteArraySegment(ArraySegment<byte> segment)
47+
{
48+
int sum = 0;
49+
foreach (byte b in segment)
50+
{
51+
sum += b;
52+
}
53+
return sum;
54+
}
55+
56+
public int SumInt32ArraySegment(ArraySegment<int> segment)
57+
{
58+
int sum = 0;
59+
foreach (int i in segment)
60+
{
61+
sum += i;
62+
}
63+
return sum;
64+
}
65+
66+
public double SumDoubleArraySegment(ArraySegment<double> segment)
67+
{
68+
double sum = 0;
69+
foreach (double d in segment)
70+
{
71+
sum += d;
72+
}
73+
return sum;
74+
}
75+
}
76+
77+
[TSExport]
78+
public class MemoryViewPropertyClass
79+
{
80+
public required ArraySegment<byte> ByteArraySegment { get; set; }
81+
public required ArraySegment<int> Int32ArraySegment { get; set; }
82+
public required ArraySegment<double> DoubleArraySegment { get; set; }
83+
84+
// span properties are not allowed in classes.
85+
}
86+

src/TypeShim.E2E/vitest/package-lock.json

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)