Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix illegal delegate usage#1328

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
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
4 changes: 4 additions & 0 deletionssrc/runtime/interop.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -491,6 +491,9 @@ internal static Type GetPrototype(string name)
return pmap[name] as Type;
}


internal static Dictionary<IntPtr, Delegate> allocatedThunks = new Dictionary<IntPtr, Delegate>();

internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null)
{
Type dt;
Expand All@@ -505,6 +508,7 @@ internal static ThunkInfo GetThunk(MethodInfo method, string funcType = null)
}
Delegate d = Delegate.CreateDelegate(dt, method);
var info = new ThunkInfo(d);
allocatedThunks[info.Address] = d;
return info;
}

Expand Down
7 changes: 4 additions & 3 deletionssrc/runtime/managedtype.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -178,7 +178,7 @@ internal static int PyVisit(IntPtr ob, IntPtr visit, IntPtr arg)
{
return 0;
}
var visitFunc =(Interop.ObjObjFunc)Marshal.GetDelegateForFunctionPointer(visit, typeof(Interop.ObjObjFunc));
var visitFunc =NativeCall.GetDelegate<Interop.ObjObjFunc>(visit);
return visitFunc(ob, arg);
}

Expand All@@ -196,7 +196,7 @@ internal void CallTypeClear()
{
return;
}
var clearFunc =(Interop.InquiryFunc)Marshal.GetDelegateForFunctionPointer(clearPtr, typeof(Interop.InquiryFunc));
var clearFunc =NativeCall.GetDelegate<Interop.InquiryFunc>(clearPtr);
clearFunc(pyHandle);
}

Expand All@@ -214,7 +214,8 @@ internal void CallTypeTraverse(Interop.ObjObjFunc visitproc, IntPtr arg)
{
return;
}
var traverseFunc = (Interop.ObjObjArgFunc)Marshal.GetDelegateForFunctionPointer(traversePtr, typeof(Interop.ObjObjArgFunc));
var traverseFunc = NativeCall.GetDelegate<Interop.ObjObjArgFunc>(traversePtr);

var visiPtr = Marshal.GetFunctionPointerForDelegate(visitproc);
traverseFunc(pyHandle, visiPtr, arg);
}
Expand Down
11 changes: 8 additions & 3 deletionssrc/runtime/nativecall.cs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -40,10 +40,15 @@ public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
return d(a1, a2, a3);
}

private static T GetDelegate<T>(IntPtr fp) where T: Delegate
internal static T GetDelegate<T>(IntPtr fp) where T: Delegate
{
// Use Marshal.GetDelegateForFunctionPointer<> directly after upgrade the framework
return (T)Marshal.GetDelegateForFunctionPointer(fp, typeof(T));
Delegate d = null;
if (!Interop.allocatedThunks.TryGetValue(fp, out d))
Comment on lines +45 to +46
Copy link
Member

@lostmsulostmsuDec 18, 2020
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

NIT: you don't need to assign an initial value tod. You could also doTryGetValue(fp, out var d).

{
// We don't cache this delegate because this is a pure delegate ot unmanaged.
d = Marshal.GetDelegateForFunctionPointer<T>(fp);
}
return (T)d;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

You'd save a couple instructions with:

Delegate d; // no initif (trygetvalue(out d)) {  return (T)d;} else {  return Marshal.GetDelegate...(); // no cast}

}
}
}

[8]ページ先頭

©2009-2025 Movatter.jp