Browse Source

Almost got it working with VTableManaged. One odd issue: commenting out Console.WriteLine in VTable.cs constructor causes

weird Mono assert fail when Marshal.GetDelegateForFunctionPointer is called later.

git-svn-id: https://mono-soc-2010.googlecode.com/svn/trunk/cppinterop@13 a470b8cb-0e6f-1642-1b45-71e107334c4b
pull/1/head
alexander.corrado 15 years ago
parent
commit
162851ea56
  1. 3
      CPPPOC/CPPPOC.csproj
  2. 1
      CPPPOC/Main.cs
  3. 5
      CPPTest/CPPTest.cproj
  4. 30
      Mono.VisualC.Interop/ABI/CppAbi.cs
  5. 1
      Mono.VisualC.Interop/ABI/VTable.cs
  6. 3
      Mono.VisualC.Interop/Mono.VisualC.Interop.csproj

3
CPPPOC/CPPPOC.csproj

@ -35,10 +35,11 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Mac|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Mac|AnyCPU' ">
<DebugType>none</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Mac</OutputPath> <OutputPath>bin\Mac</OutputPath>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />

1
CPPPOC/Main.cs

@ -20,6 +20,7 @@ namespace CPPPOC
CSimpleClass csc2 = new CSimpleClass(2); CSimpleClass csc2 = new CSimpleClass(2);
try { try {
csc1.V0(25, 50);
csc1.M0(); csc1.M0();
Console.WriteLine("Managed code got value: {0}", csc1.value); Console.WriteLine("Managed code got value: {0}", csc1.value);
csc2.M0(); csc2.M0();

5
CPPTest/CPPTest.cproj

@ -19,6 +19,11 @@
<SourceDirectory>.</SourceDirectory> <SourceDirectory>.</SourceDirectory>
<OutputName>CPPTest</OutputName> <OutputName>CPPTest</OutputName>
<CompileTarget>SharedLibrary</CompileTarget> <CompileTarget>SharedLibrary</CompileTarget>
<CustomCommands>
<CustomCommands>
<Command type="Build" command="echo Nothing to do" />
</CustomCommands>
</CustomCommands>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>bin\Release</OutputPath> <OutputPath>bin\Release</OutputPath>

30
Mono.VisualC.Interop/ABI/CppAbi.cs

@ -33,7 +33,7 @@ namespace Mono.VisualC.Interop.ABI {
public virtual Iface ImplementClass<Iface, NLayout> (ModuleBuilder implModule, Type wrapperType, string lib, string className) public virtual Iface ImplementClass<Iface, NLayout> (ModuleBuilder implModule, Type wrapperType, string lib, string className)
where NLayout : struct where NLayout : struct
//where Iface : ICppNativeInterface or ICppNativeInterfaceManaged //where Iface : ICppClassInstantiatable or ICppClassOverridable
{ {
this.implModule = implModule; this.implModule = implModule;
this.library = lib; this.library = lib;
@ -174,7 +174,7 @@ namespace Mono.VisualC.Interop.ABI {
break; break;
case MethodType.NativeDtor: case MethodType.NativeDtor:
EmitDestruct (il, nativeMethod, parameterTypes.Length, nativePtr); EmitDestruct (il, nativeMethod, parameterTypes.Length, cppInstancePtr, nativePtr);
break; break;
default: // regular native method default: // regular native method
@ -347,16 +347,29 @@ namespace Mono.VisualC.Interop.ABI {
new Type[] { typeof (int) }, null)); new Type[] { typeof (int) }, null));
} }
protected virtual void EmitConstruct (ILGenerator il, MethodInfo nativeMethod, int parameterCount, LocalBuilder nativePtr) protected virtual void EmitConstruct (ILGenerator il, MethodInfo nativeMethod, int parameterCount,
LocalBuilder nativePtr)
{ {
EmitCallNative (il, nativeMethod, parameterCount, nativePtr); EmitCallNative (il, nativeMethod, parameterCount, nativePtr);
EmitInitVTable (il, nativePtr); EmitInitVTable (il, nativePtr);
} }
protected virtual void EmitDestruct (ILGenerator il, MethodInfo nativeMethod, int parameterCount, LocalBuilder nativePtr) protected virtual void EmitDestruct (ILGenerator il, MethodInfo nativeMethod, int parameterCount,
LocalBuilder cppInstancePtr, LocalBuilder nativePtr)
{ {
// bail if we weren't alloc'd by managed code
Label bail = il.DefineLabel ();
il.Emit (OpCodes.Ldloca_S, cppInstancePtr);
//il.Emit (OpCodes.Dup);
//il.Emit (OpCodes.Brfalse_S, bail);
il.Emit (OpCodes.Call, typeof (CppInstancePtr).GetProperty ("IsManagedAlloc").GetGetMethod ());
il.Emit (OpCodes.Brfalse_S, bail);
EmitResetVTable (il, nativePtr); EmitResetVTable (il, nativePtr);
EmitCallNative (il, nativeMethod, parameterCount, nativePtr); EmitCallNative (il, nativeMethod, parameterCount, nativePtr);
il.MarkLabel (bail);
} }
/** /**
@ -364,7 +377,8 @@ namespace Mono.VisualC.Interop.ABI {
* GetPInvokeForMethod or the MethodInfo of a vtable method. * GetPInvokeForMethod or the MethodInfo of a vtable method.
* To complete method, emit OpCodes.Ret. * To complete method, emit OpCodes.Ret.
*/ */
protected virtual void EmitCallNative (ILGenerator il, MethodInfo nativeMethod, int parameterCount, LocalBuilder nativePtr) protected virtual void EmitCallNative (ILGenerator il, MethodInfo nativeMethod, int parameterCount,
LocalBuilder nativePtr)
{ {
il.Emit (OpCodes.Ldloc_S, nativePtr); il.Emit (OpCodes.Ldloc_S, nativePtr);
for (int i = 2; i <= parameterCount; i++) for (int i = 2; i <= parameterCount; i++)
@ -403,7 +417,8 @@ namespace Mono.VisualC.Interop.ABI {
* want to call and pass the stackHeight for the call. If no vtable exists, this method * want to call and pass the stackHeight for the call. If no vtable exists, this method
* will emit code to pop the arguments off the stack. * will emit code to pop the arguments off the stack.
*/ */
protected virtual void EmitVTableOp(ILGenerator il, MethodInfo method, int stackHeight, bool throwOnNoVTable) protected virtual void EmitVTableOp(ILGenerator il, MethodInfo method, int stackHeight,
bool throwOnNoVTable)
{ {
// prepare a jump; do not call vtable method if no vtable // prepare a jump; do not call vtable method if no vtable
Label noVirt = il.DefineLabel (); Label noVirt = il.DefineLabel ();
@ -438,7 +453,8 @@ namespace Mono.VisualC.Interop.ABI {
il.MarkLabel (dontPushOrThrow); il.MarkLabel (dontPushOrThrow);
} }
protected virtual void EmitLoadInstancePtr (ILGenerator il, Type firstParamType, out LocalBuilder cppip, out LocalBuilder native) protected virtual void EmitLoadInstancePtr (ILGenerator il, Type firstParamType, out LocalBuilder cppip,
out LocalBuilder native)
{ {
cppip = null; cppip = null;
native = il.DeclareLocal (typeof (IntPtr)); native = il.DeclareLocal (typeof (IntPtr));

1
Mono.VisualC.Interop/ABI/VTable.cs

@ -30,6 +30,7 @@ namespace Mono.VisualC.Interop.ABI {
public VTable (Delegate[] overrides) public VTable (Delegate[] overrides)
{ {
EntryCount = overrides.Length; EntryCount = overrides.Length;
Console.WriteLine ("VTable entry count: {0}", EntryCount);
basePtr = IntPtr.Zero; basePtr = IntPtr.Zero;
vtPtr = IntPtr.Zero; vtPtr = IntPtr.Zero;
} }

3
Mono.VisualC.Interop/Mono.VisualC.Interop.csproj

@ -39,11 +39,12 @@
<AssemblyName>CPPInterop</AssemblyName> <AssemblyName>CPPInterop</AssemblyName>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Mac|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Mac|AnyCPU' ">
<DebugType>none</DebugType> <DebugType>full</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Mac</OutputPath> <OutputPath>bin\Mac</OutputPath>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AssemblyName>Mono.VisualC.Interop</AssemblyName> <AssemblyName>Mono.VisualC.Interop</AssemblyName>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />

Loading…
Cancel
Save