From ddc3332958226ab8658bd6c991ff3a51428507c5 Mon Sep 17 00:00:00 2001 From: walon Date: Wed, 30 Apr 2025 16:11:41 +0800 Subject: [PATCH] [change] validate unsupported parameter type(.e.g string) in PInvoke signature when generate MethodBridge file --- Editor/MethodBridge/PInvokeAnalyzer.cs | 28 ++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Editor/MethodBridge/PInvokeAnalyzer.cs b/Editor/MethodBridge/PInvokeAnalyzer.cs index ecca457..c12d5a0 100644 --- a/Editor/MethodBridge/PInvokeAnalyzer.cs +++ b/Editor/MethodBridge/PInvokeAnalyzer.cs @@ -26,6 +26,30 @@ namespace HybridCLR.Editor.MethodBridge } } + private static bool IsSupportedPInvokeTypeSig(TypeSig typeSig) + { + typeSig = typeSig.RemovePinnedAndModifiers(); + if (typeSig.IsByRef) + { + return true; + } + switch (typeSig.ElementType) + { + case ElementType.SZArray: + case ElementType.Array: + //case ElementType.Class: + case ElementType.String: + //case ElementType.Object: + return false; + default: return true; + } + } + + private static bool IsSupportedPInvokeMethodSignature(MethodSig methodSig) + { + return IsSupportedPInvokeTypeSig(methodSig.RetType) && methodSig.Params.All(p => IsSupportedPInvokeTypeSig(p)); + } + public void Run() { foreach (var mod in _rootModules) @@ -36,6 +60,10 @@ namespace HybridCLR.Editor.MethodBridge { if (method.IsPinvokeImpl) { + if (!IsSupportedPInvokeMethodSignature(method.MethodSig)) + { + throw new Exception($"PInvoke method {method.FullName} has unsupported parameter or return type. Please check the method signature."); + } _pinvokeMethodSignatures.Add(new CallNativeMethodSignatureInfo { MethodSig = method.MethodSig }); } }