Skip to content

Commit ddc3332

Browse files
committed
[change] validate unsupported parameter type(.e.g string) in PInvoke signature when generate MethodBridge file
1 parent 52f25bd commit ddc3332

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

Editor/MethodBridge/PInvokeAnalyzer.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,30 @@ public PInvokeAnalyzer(AssemblyCache cache, List<string> assemblyNames)
2626
}
2727
}
2828

29+
private static bool IsSupportedPInvokeTypeSig(TypeSig typeSig)
30+
{
31+
typeSig = typeSig.RemovePinnedAndModifiers();
32+
if (typeSig.IsByRef)
33+
{
34+
return true;
35+
}
36+
switch (typeSig.ElementType)
37+
{
38+
case ElementType.SZArray:
39+
case ElementType.Array:
40+
//case ElementType.Class:
41+
case ElementType.String:
42+
//case ElementType.Object:
43+
return false;
44+
default: return true;
45+
}
46+
}
47+
48+
private static bool IsSupportedPInvokeMethodSignature(MethodSig methodSig)
49+
{
50+
return IsSupportedPInvokeTypeSig(methodSig.RetType) && methodSig.Params.All(p => IsSupportedPInvokeTypeSig(p));
51+
}
52+
2953
public void Run()
3054
{
3155
foreach (var mod in _rootModules)
@@ -36,6 +60,10 @@ public void Run()
3660
{
3761
if (method.IsPinvokeImpl)
3862
{
63+
if (!IsSupportedPInvokeMethodSignature(method.MethodSig))
64+
{
65+
throw new Exception($"PInvoke method {method.FullName} has unsupported parameter or return type. Please check the method signature.");
66+
}
3967
_pinvokeMethodSignatures.Add(new CallNativeMethodSignatureInfo { MethodSig = method.MethodSig });
4068
}
4169
}

0 commit comments

Comments
 (0)