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

1
CPPPOC/Main.cs

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

5
CPPTest/CPPTest.cproj

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

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

@ -33,7 +33,7 @@ namespace Mono.VisualC.Interop.ABI { @@ -33,7 +33,7 @@ namespace Mono.VisualC.Interop.ABI {
public virtual Iface ImplementClass<Iface, NLayout> (ModuleBuilder implModule, Type wrapperType, string lib, string className)
where NLayout : struct
//where Iface : ICppNativeInterface or ICppNativeInterfaceManaged
//where Iface : ICppClassInstantiatable or ICppClassOverridable
{
this.implModule = implModule;
this.library = lib;
@ -174,7 +174,7 @@ namespace Mono.VisualC.Interop.ABI { @@ -174,7 +174,7 @@ namespace Mono.VisualC.Interop.ABI {
break;
case MethodType.NativeDtor:
EmitDestruct (il, nativeMethod, parameterTypes.Length, nativePtr);
EmitDestruct (il, nativeMethod, parameterTypes.Length, cppInstancePtr, nativePtr);
break;
default: // regular native method
@ -347,16 +347,29 @@ namespace Mono.VisualC.Interop.ABI { @@ -347,16 +347,29 @@ namespace Mono.VisualC.Interop.ABI {
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);
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);
EmitCallNative (il, nativeMethod, parameterCount, nativePtr);
il.MarkLabel (bail);
}
/**
@ -364,7 +377,8 @@ namespace Mono.VisualC.Interop.ABI { @@ -364,7 +377,8 @@ namespace Mono.VisualC.Interop.ABI {
* GetPInvokeForMethod or the MethodInfo of a vtable method.
* 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);
for (int i = 2; i <= parameterCount; i++)
@ -403,7 +417,8 @@ namespace Mono.VisualC.Interop.ABI { @@ -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
* 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
Label noVirt = il.DefineLabel ();
@ -438,7 +453,8 @@ namespace Mono.VisualC.Interop.ABI { @@ -438,7 +453,8 @@ namespace Mono.VisualC.Interop.ABI {
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;
native = il.DeclareLocal (typeof (IntPtr));

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

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

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

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

Loading…
Cancel
Save