Skip to content

Commit 22ae7e8

Browse files
authored
Support for Delegates (#56)
for [#12](#12) + #57 - [x] implement parsing - [x] implement cs rendering - [x] implement ts rendering - [x] fix analyzer - [x] analyzer rule for max argument list length in delegate - [x] Enable initializers through js > .net delegates - [x] E2E test funky criss cross delegate calls - [x] refactor TSSymbolName renderer to write to renderctx - [x] implement delegates in initializers - [x] reimplement jsobjectextension rendering to only user required types including delegates
1 parent 1c69ae0 commit 22ae7e8

56 files changed

Lines changed: 5742 additions & 1362 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,14 @@ TypeShim aims to continue to broaden its type support. Suggestions and contribut
318318
| `ArraySegment<Int32>`| `MemoryView`| 🚧 | |
319319
| `ArraySegment<Double>`| `MemoryView`| 🚧 | |
320320
| `Task` | `Promise` || * [Only supported .NET types](https://learn.microsoft.com/en-us/aspnet/core/client-side/dotnet-interop/?view=aspnetcore-10.0#type-mappings) |
321-
| `Action` | `Function` | 🚧 | |
322-
| `Action<T1>` | `Function` | 🚧 | |
323-
| `Action<T1, T2>` | `Function` | 🚧 | |
324-
| `Action<T1, T2, T3>` | `Function` | 🚧 | |
325-
| `Func<TResult>` | `Function` | 🚧 | |
326-
| `Func<T1, TResult>` | `Function` | 🚧 | |
327-
| `Func<T1, T2, TResult>` | `Function`| 🚧 | |
328-
| `Func<T1, T2, T3, TResult>` | `Function` | 🚧 | |
321+
| `Action` | `Function` | | |
322+
| `Action<T1>` | `Function` | | |
323+
| `Action<T1, T2>` | `Function` | | |
324+
| `Action<T1, T2, T3>` | `Function` | | |
325+
| `Func<TResult>` | `Function` | | |
326+
| `Func<T1, TResult>` | `Function` | | |
327+
| `Func<T1, T2, TResult>` | `Function`| | |
328+
| `Func<T1, T2, T3, TResult>` | `Function` | | |
329329

330330
*<sub>For `[TSExport]` classes</sub>
331331

TypeShim.E2E/TypeShim.E2E.Wasm/ArrayPropertiesClass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Runtime.InteropServices.JavaScript;
22

3-
namespace TypeShim.Sample;
3+
namespace TypeShim.E2E.Wasm;
44

55
[TSExport]
66
public class ArrayPropertiesClass
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Runtime.InteropServices.JavaScript;
3+
using System.Threading.Tasks;
4+
5+
namespace TypeShim.E2E.Wasm;
6+
7+
[TSExport]
8+
public class DelegatePropertyClass
9+
{
10+
public required Func<ExportedClass, ExportedClass> ExportedClassFuncProperty { get; set; }
11+
}
12+
13+
[TSExport]
14+
public class DelegatesClass
15+
{
16+
public Func<bool, int>? FuncBoolIntProperty { get; set; }
17+
public required Func<char> FuncCharProperty { get; set; }
18+
19+
public void InvokeVoidAction(Action action)
20+
{
21+
action();
22+
}
23+
24+
public void InvokeStringAction(Action<string> action)
25+
{
26+
action("Hello");
27+
}
28+
29+
public void InvokeInt32Action(Action<int> action)
30+
{
31+
action(42);
32+
}
33+
34+
public void InvokeBoolAction(Action<bool> action)
35+
{
36+
action(true);
37+
}
38+
public void InvokeCharAction(Action<char> action)
39+
{
40+
action('Z');
41+
}
42+
43+
public Func<char, char> GetCharCharFunc()
44+
{
45+
return (char c) => (char)(c + 1);
46+
}
47+
48+
public void InvokeBool2Action(Action<bool, bool> action)
49+
{
50+
action(true, false);
51+
}
52+
53+
public void InvokeBool3Action(Action<bool, bool, bool> action)
54+
{
55+
action(true, false, true);
56+
}
57+
58+
public string InvokeStringFunc(Func<string> func)
59+
{
60+
return func();
61+
}
62+
63+
public int InvokeInt32Func(Func<int> func)
64+
{
65+
return func();
66+
}
67+
68+
public bool InvokeBoolFunc(Func<bool> func)
69+
{
70+
return func();
71+
}
72+
73+
public bool InvokeBool2Func(Func<bool, bool> func)
74+
{
75+
return func(true);
76+
}
77+
78+
public void InvokeExportedClassAction(Action<ExportedClass> action)
79+
{
80+
action(new ExportedClass { Id = 100 });
81+
}
82+
83+
public Func<ExportedClass> GetExportedClassFunc()
84+
{
85+
return () => new ExportedClass { Id = 200 };
86+
}
87+
88+
public Func<bool, int, string, ExportedClass> GetBoolIntStringExportFunc()
89+
{
90+
return (bool b, int a, string c) => new ExportedClass { Id = b ? a : c.Length };
91+
}
92+
93+
public Func<bool, int, char, ExportedClass> GetBoolIntCharExportFunc()
94+
{
95+
return (bool b, int a, char c) => new ExportedClass { Id = b ? a : c };
96+
}
97+
98+
public Func<bool, int, ExportedClass, char> GetBoolIntExportCharFunc()
99+
{
100+
return (bool b, int a, ExportedClass c) => b ? (char)a : (char)c.Id;
101+
}
102+
103+
public Func<ExportedClass, ExportedClass> GetExportedClassExportedClassFunc()
104+
{
105+
return (ExportedClass classIn) => classIn;
106+
}
107+
108+
public ExportedClass InvokeExportedClassExportedClassFunc(Func<ExportedClass, ExportedClass> func, Func<ExportedClass> paramFunc)
109+
{
110+
return func(paramFunc());
111+
}
112+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace TypeShim.Sample;
1+
namespace TypeShim.E2E.Wasm;
22

33
[TSExport]
44
public class ExportedClass // for referencing an exported class

TypeShim.E2E/TypeShim.E2E.Wasm/SimplePropertiesTest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices.JavaScript;
33

4-
namespace TypeShim.Sample;
4+
namespace TypeShim.E2E.Wasm;
55

66
[TSExport]
77
public class SimplePropertiesTest
@@ -14,6 +14,7 @@ public class SimplePropertiesTest
1414
public bool BoolProperty { get; set; }
1515
public string StringProperty { get; set; } = string.Empty;
1616
public char CharProperty { get; set; }
17+
public char? CharNullableProperty { get; set; }
1718
public double DoubleProperty { get; set; }
1819
public float FloatProperty { get; set; }
1920
public DateTime DateTimeProperty { get; set; }

TypeShim.E2E/TypeShim.E2E.Wasm/SimpleReturnMethodsClass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Globalization;
33

4-
namespace TypeShim.Sample;
4+
namespace TypeShim.E2E.Wasm;
55

66
[TSExport]
77
public class SimpleReturnMethodsClass

TypeShim.E2E/TypeShim.E2E.Wasm/TaskPropertiesClass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Runtime.InteropServices.JavaScript;
33
using System.Threading.Tasks;
44

5-
namespace TypeShim.Sample;
5+
namespace TypeShim.E2E.Wasm;
66

77
[TSExport]
88
public class TaskPropertiesClass
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Threading.Tasks;
2+
3+
namespace TypeShim.E2E.Wasm;
4+
5+
[TSExport]
6+
public class TaskReturnMethodsClass
7+
{
8+
public Task VoidTaskMethod() => Task.CompletedTask;
9+
public Task<int> Int32TaskMethod() => Task.FromResult(42);
10+
public Task<bool> BoolTaskMethod() => Task.FromResult(true);
11+
public Task<string> StringTaskMethod() => Task.FromResult("Hello, from .NET Task");
12+
13+
public Task<ExportedClass> ExportedClassTaskMethod() => Task.FromResult(new ExportedClass() { Id = 420 });
14+
}

TypeShim.E2E/vitest/src/array-properties.test.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,56 @@ describe('Array Properties Test', () => {
2828
testObject.IntArrayProperty[0] = 42;
2929
expect(testObject.IntArrayProperty).toStrictEqual(original);
3030
});
31+
32+
test('Initialized ExportedClass array property', () => {
33+
expect(testObject.ExportedClassArrayProperty).toBeInstanceOf(Array);
34+
expect(testObject.ExportedClassArrayProperty.length).toBe(1);
35+
const item = testObject.ExportedClassArrayProperty[0];
36+
expect(item).toBeInstanceOf(ExportedClass);
37+
expect(item.Id).toBe(exportedClass.Id);
38+
// TODO: fix identity (https://github.com/ArcadeMode/TypeShim/issues/20)
39+
// expect(item).toBe(exportedClass);
40+
});
41+
42+
test('Initializer JSObject array property', () => {
43+
expect(testObject.JSObjectArrayProperty).toBeInstanceOf(Array);
44+
expect(testObject.JSObjectArrayProperty.length).toBe(3);
45+
expect(testObject.JSObjectArrayProperty[0]).toMatchObject({ a: 1 });
46+
expect(testObject.JSObjectArrayProperty[1]).toMatchObject({ b: 2 });
47+
expect(testObject.JSObjectArrayProperty[2]).toMatchObject({ c: 3 });
48+
});
49+
50+
test('ExportedClass array property set with ExportedClass.Initializer', () => {
51+
const exportedClassInitializer = { Id: 12345 };
52+
testObject.ExportedClassArrayProperty = [exportedClassInitializer];
53+
expect(testObject.ExportedClassArrayProperty).toBeInstanceOf(Array);
54+
expect(testObject.ExportedClassArrayProperty.length).toBe(1);
55+
const item = testObject.ExportedClassArrayProperty[0];
56+
expect(item).toBeInstanceOf(ExportedClass);
57+
expect(item.Id).toBe(12345);
58+
});
59+
60+
test('ExportedClass array property set with ExportedClass.Initializer', () => {
61+
const exportedClassInitializer = { Id: 12345 };
62+
const testObject = new ArrayPropertiesClass({
63+
ByteArrayProperty: [],
64+
IntArrayProperty: [],
65+
StringArrayProperty: [],
66+
DoubleArrayProperty: [],
67+
JSObjectArrayProperty: [],
68+
ObjectArrayProperty: [],
69+
ExportedClassArrayProperty: [exportedClassInitializer],
70+
});
71+
72+
expect(testObject.ExportedClassArrayProperty).toBeInstanceOf(Array);
73+
expect(testObject.ExportedClassArrayProperty.length).toBe(1);
74+
const item = testObject.ExportedClassArrayProperty[0];
75+
expect(item).toBeInstanceOf(ExportedClass);
76+
expect(item.Id).toBe(12345);
77+
});
78+
79+
test('Object from ObjectArrayProperty has reference equality', () => {
80+
const item = testObject.ObjectArrayProperty[0];
81+
expect(item).toBe(exportedClass.instance);
82+
});
3183
});

0 commit comments

Comments
 (0)