Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[mono][wasm] Handle delegates decorated with [UnmanagedFunctionPointe…
…r] in the interp-to-native generator.

Fixes #76930.
  • Loading branch information
vargaz authored and github-actions committed Nov 15, 2022
commit 7f7ca7d9ad76977390f2c567ad78f2536a7b9612
34 changes: 34 additions & 0 deletions src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ private void CollectPInvokes(List<PInvoke> pinvokes, List<PInvokeCallback> callb
}
}

if (HasAttribute(type, new string[] {"System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute"})) {
var method = type.GetMethod("Invoke");

if (method != null)
{
string? signature = SignatureMapper.MethodToSignature(method!);
if (signature == null)
{
throw new NotSupportedException($"Unsupported parameter type in method '{type.FullName}.{method.Name}'");
}

Log.LogMessage(MessageImportance.Low, $"Adding pinvoke signature {signature} for method '{type.FullName}.{method.Name}'");
signatures.Add(signature);
}
}

void CollectPInvokesForMethod(MethodInfo method)
{
if ((method.Attributes & MethodAttributes.PinvokeImpl) != 0)
Expand Down Expand Up @@ -151,6 +167,24 @@ static bool MethodHasCallbackAttributes(MethodInfo method)
}
}

private static bool HasAttribute(Type type, string[] attributeNames)
{
foreach (CustomAttributeData cattr in CustomAttributeData.GetCustomAttributes(type))
{
try
{
for (int i = 0; i < attributeNames.Length; ++i)
if (cattr.AttributeType.FullName == attributeNames [i])
return true;
}
catch
{
// Assembly not found, ignore
}
}
return false;
}

private void EmitPInvokeTable(StreamWriter w, Dictionary<string, string> modules, List<PInvoke> pinvokes)
{
w.WriteLine("// GENERATED FILE, DO NOT MODIFY");
Expand Down