diff options
author | tijoytom <tijoytom.com> | 2015-09-03 13:58:14 -0700 |
---|---|---|
committer | tijoytom <tijoytom.com> | 2015-09-18 10:00:14 -0700 |
commit | 1e62862b631fb64c6a9fae0a0084c0ae1d9fbeba (patch) | |
tree | 46c2fd40ae20d229c97cb9a1966cba78c6f04f52 /src/vm/compile.h | |
parent | b2964aff2a5a77b1df87b94005face366921a9b8 (diff) | |
download | coreclr-1e62862b631fb64c6a9fae0a0084c0ae1d9fbeba.tar.gz coreclr-1e62862b631fb64c6a9fae0a0084c0ae1d9fbeba.tar.bz2 coreclr-1e62862b631fb64c6a9fae0a0084c0ae1d9fbeba.zip |
Add support for NativeCallableAttribute
Apply [NativeCallable] attribute to a managed method and then it can be
called from native code.Typical use would be passing a managed method as
callback to native, now it can be done by wrapping the method in a
delegate or directly using Marshal.GetFunctionPointerForDelegate.This's
fine as long as we make sure that delegate is not garbage
collected.[NativeCallable] introduce another way, where you can directly
load the function pointer of a native callable method and use it as
callback.This feature cannot be directly used from C#,but can be very
useful in dynamic code generation scenarios where you want a callback to
be passed to native.
Here's an example of how it can be used.
public static class NativeMethods {
[DllImport("user32.dll")]
public static extern int EnumWindows(IntPtr enumProc, IntPtr lParam);
}
//Method attributed with NativeCallable
[NativeCallable]
public static int CallbackMethod(IntPtr hWnd, IntPtr lParam){ return 1; }
Now you can generate the below IL to load native callable function pointer
( LDFTN) and then pass it a native method.
.locals init ([0] native int ptr)
nop
ldftn int32 CallbackMethod(native int,native int)
stloc.0
ldloc.0
ldsfld native int System.IntPtr::Zero
call bool NativeMethods::EnumWindows(native int,native int)
pop
ret
Encoding native callable methods as ENCODE_METHOD_NATIVECALLABLE_HANDLE
so that we don't have to check for the custom attribute at runtime to
decode the method.Also fixing the remaining code review comments.
Adding runtime check to prevent Native Callable methods from being used as
calli target with an ldftn. Also adding some negative test cases , they
are disabled for now since the tests failfast and msbuild report it as
failure.
Diffstat (limited to 'src/vm/compile.h')
-rw-r--r-- | src/vm/compile.h | 2 |
1 files changed, 2 insertions, 0 deletions
diff --git a/src/vm/compile.h b/src/vm/compile.h index a34bd93460..034f751791 100644 --- a/src/vm/compile.h +++ b/src/vm/compile.h @@ -330,6 +330,8 @@ class CEECompileInfo : public ICorCompileInfo BOOL IsEmptyString(mdString token, CORINFO_MODULE_HANDLE module); + BOOL IsNativeCallableMethod(CORINFO_METHOD_HANDLE handle); + BOOL IsCachingOfInliningHintsEnabled() { return m_fCachingOfInliningHintsEnabled; |