Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mono][wasm] Handle delegates decorated with [UnmanagedFunctionPointe… #77709

Merged
merged 4 commits into from
Nov 2, 2022
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
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");
vargaz marked this conversation as resolved.
Show resolved Hide resolved

if (method != null)
{
string? signature = SignatureMapper.MethodToSignature(method!);
if (signature == null)
{
throw new NotSupportedException($"Unsupported parameter type in method '{type.FullName}.{method.Name}'");
}
vargaz marked this conversation as resolved.
Show resolved Hide resolved

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;
}
vargaz marked this conversation as resolved.
Show resolved Hide resolved

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