Skip to content

Commit ba5c173

Browse files
github-actions[bot]vargazradical
authored
[release/7.0] [mono][wasm] Handle delegates decorated with [UnmanagedFunctionPointe… (#78426)
* [mono][wasm] Handle delegates decorated with [UnmanagedFunctionPointer] in the interp-to-native generator. Fixes #76930. * Update src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs Co-authored-by: Ankit Jain <[email protected]> * Update src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs Co-authored-by: Ankit Jain <[email protected]> * Update src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs Co-authored-by: Ankit Jain <[email protected]> Co-authored-by: Zoltan Varga <[email protected]> Co-authored-by: Ankit Jain <[email protected]>
1 parent 7e6e7ce commit ba5c173

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,22 @@ private void CollectPInvokes(List<PInvoke> pinvokes, List<PInvokeCallback> callb
7979
}
8080
}
8181

82+
if (HasAttribute(type, "System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute"))
83+
{
84+
var method = type.GetMethod("Invoke");
85+
86+
if (method != null)
87+
{
88+
string? signature = SignatureMapper.MethodToSignature(method!);
89+
if (signature == null)
90+
throw new NotSupportedException($"Unsupported parameter type in method '{type.FullName}.{method.Name}'");
91+
92+
93+
Log.LogMessage(MessageImportance.Low, $"Adding pinvoke signature {signature} for method '{type.FullName}.{method.Name}'");
94+
signatures.Add(signature);
95+
}
96+
}
97+
8298
void CollectPInvokesForMethod(MethodInfo method)
8399
{
84100
if ((method.Attributes & MethodAttributes.PinvokeImpl) != 0)
@@ -151,6 +167,29 @@ static bool MethodHasCallbackAttributes(MethodInfo method)
151167
}
152168
}
153169

170+
private static bool HasAttribute(MemberInfo element, params string[] attributeNames)
171+
{
172+
foreach (CustomAttributeData cattr in CustomAttributeData.GetCustomAttributes(element))
173+
{
174+
try
175+
{
176+
for (int i = 0; i < attributeNames.Length; ++i)
177+
{
178+
if (cattr.AttributeType.FullName == attributeNames [i] ||
179+
cattr.AttributeType.Name == attributeNames[i])
180+
{
181+
return true;
182+
}
183+
}
184+
}
185+
catch
186+
{
187+
// Assembly not found, ignore
188+
}
189+
}
190+
return false;
191+
}
192+
154193
private void EmitPInvokeTable(StreamWriter w, Dictionary<string, string> modules, List<PInvoke> pinvokes)
155194
{
156195
w.WriteLine("// GENERATED FILE, DO NOT MODIFY");

0 commit comments

Comments
 (0)