Browse Source

Merge pull request #180 from ddobrev/master

Added tests for v-tables checking calls to virtuals from within C++ and passing parameters.
pull/181/head
João Matos 12 years ago
parent
commit
ad57f3a8c1
  1. 12
      src/Generator/AST/VTables.cs
  2. 3
      tests/VTables/VTables.Tests.cs
  3. 9
      tests/VTables/VTables.cpp
  4. 2
      tests/VTables/VTables.h

12
src/Generator/AST/VTables.cs

@ -19,7 +19,7 @@ namespace CppSharp.AST
throw new NotSupportedException(); throw new NotSupportedException();
} }
public static List<VTableComponent> GatherVTableMethodEntries(VTableLayout layout) private static List<VTableComponent> GatherVTableMethodEntries(VTableLayout layout)
{ {
var entries = new List<VTableComponent>(); var entries = new List<VTableComponent>();
if (layout == null) if (layout == null)
@ -50,11 +50,6 @@ namespace CppSharp.AST
return GatherVTableMethodEntries(@class.Layout.Layout); return GatherVTableMethodEntries(@class.Layout.Layout);
} }
public static int GetVTableComponentIndex(VTableLayout layout, VTableComponent entry)
{
return layout.Components.IndexOf(entry);
}
public static int GetVTableComponentIndex(Class @class, VTableComponent entry) public static int GetVTableComponentIndex(Class @class, VTableComponent entry)
{ {
switch (@class.Layout.ABI) switch (@class.Layout.ABI)
@ -62,13 +57,14 @@ namespace CppSharp.AST
case CppAbi.Microsoft: case CppAbi.Microsoft:
foreach (var vfptr in @class.Layout.VFTables) foreach (var vfptr in @class.Layout.VFTables)
{ {
var index = GetVTableComponentIndex(vfptr.Layout, entry); var index = vfptr.Layout.Components.IndexOf(entry);
if (index >= 0) if (index >= 0)
return index; return index;
} }
break; break;
case CppAbi.Itanium: case CppAbi.Itanium:
return GetVTableComponentIndex(@class.Layout.Layout, entry); // ignore offset to top and RTTI
return @class.Layout.Layout.Components.IndexOf(entry) - 2;
} }
throw new NotSupportedException(); throw new NotSupportedException();

3
tests/VTables/VTables.Tests.cs

@ -19,8 +19,9 @@ public class VTablesTests : GeneratorTestFixture
{ {
var foo = new Foo(); var foo = new Foo();
Assert.That(foo.vfoo(), Is.EqualTo(5)); Assert.That(foo.vfoo(), Is.EqualTo(5));
Assert.That(foo.Vbar(), Is.EqualTo(3)); Assert.That(foo.Vbar(), Is.EqualTo(5));
Assert.That(foo.CallFoo(), Is.EqualTo(7)); Assert.That(foo.CallFoo(), Is.EqualTo(7));
Assert.That(foo.CallVirtualWithParameter(6514), Is.EqualTo(6514 + 1));
var foo2 = new FooDerived(); var foo2 = new FooDerived();
Assert.That(foo2.CallFoo(), Is.EqualTo(12)); Assert.That(foo2.CallFoo(), Is.EqualTo(12));

9
tests/VTables/VTables.cpp

@ -11,7 +11,7 @@ int Foo::vfoo()
int Foo::vbar() int Foo::vbar()
{ {
return 3; return vfoo();
} }
int Foo::append() int Foo::append()
@ -24,7 +24,12 @@ int Foo::append(int a)
return ++a; return ++a;
} }
int Foo::callVirtualWithParameter(int a)
{
return append(a);
}
int FooCallFoo(Foo* foo) int FooCallFoo(Foo* foo)
{ {
return foo->vfoo() + 2; return foo->vfoo() + 2;
} }

2
tests/VTables/VTables.h

@ -18,7 +18,7 @@ public:
virtual int append(); virtual int append();
virtual int append(int a); virtual int append(int a);
void callVirtualWithParameter(int a); int callVirtualWithParameter(int a);
}; };
DLL_API int FooCallFoo(Foo* foo); DLL_API int FooCallFoo(Foo* foo);

Loading…
Cancel
Save