Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 5caa5df

Browse files
Merge pull request #831 from xamarin/add-handle-to-vtable
Add handle into vtables
2 parents 115c615 + 7cdc011 commit 5caa5df

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

SwiftReflector/NewClassCompiler.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,11 +2053,22 @@ bool ImplementProtocolDeclarationsVTableAndCSProxy (ModuleInventory modInventory
20532053
virtFunctions, virtFunctions [i], vtableEntryIndex, vtableName, vtable, vtableAssignments, use, swiftLibraryPath);
20542054
}
20552055
}
2056+
AddVTHandleIfNeeded (use, vtable, vtableAssignments, vtableName);
20562057
ImplementVTableInitializer (proxyClass, picl, usedPinvokeNames, protocolDecl, vtableAssignments, wrapper, vtableType, vtableName, swiftLibraryPath);
20572058

20582059
return vtable.Fields.Count > 0;
20592060
}
20602061

2062+
void AddVTHandleIfNeeded (CSUsingPackages use, CSStruct vtable, List<CSLine> vtableAssignments, string vtName)
2063+
{
2064+
if (vtable.Fields.Count > 0) {
2065+
use.AddIfNotPresent (typeof (GCHandle));
2066+
var gcType = new CSSimpleType (typeof (GCHandle));
2067+
var decl = new CSFieldDeclaration (gcType, OverrideBuilder.kVtableHandleName, value: null, CSVisibility.Public);
2068+
vtable.Fields.Insert (0, new CSLine (decl));
2069+
}
2070+
}
2071+
20612072
void CollectAllProtocolMethods (List<FunctionDeclaration> functions, ProtocolDeclaration decl)
20622073
{
20632074
foreach (TypeSpec spec in decl.Inheritance.Where (inh => inh.InheritanceKind == InheritanceKind.Protocol).Select (inh => inh.InheritedTypeSpec)) {
@@ -2114,6 +2125,7 @@ int ImplementVtableMethodsSuperMethodsAndVtable (ModuleInventory modInventory, W
21142125
if (vtable.Fields.Count > 0) {
21152126
cl.InnerClasses.Add (vtable);
21162127
}
2128+
AddVTHandleIfNeeded (use, vtable, vtableAssignments, vtableName);
21172129
ImplementVTableInitializer (cl, picl, usedPinvokeNames, classDecl, vtableAssignments, wrapper, vtableType, vtableName, swiftLibraryPath);
21182130
return virtFuncs.Count;
21192131
}

SwiftReflector/OverrideBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace SwiftReflector {
1616
public class OverrideBuilder {
17+
public const string kVtableHandleName = "csVTHandle";
1718
TypeMapper typeMapper;
1819
string vtableTypeName;
1920
string vtableName = null;
@@ -1483,9 +1484,18 @@ SLClass DefineInnerVtableStruct ()
14831484
}
14841485
}
14851486
}
1487+
if (cl.Fields.Count > 0) {
1488+
cl.Fields.Insert (0, VtableHandleDeclaration ());
1489+
}
14861490
return cl;
14871491
}
14881492

1493+
SLLine VtableHandleDeclaration ()
1494+
{
1495+
var fieldLine = SLDeclaration.VarLine (kVtableHandleName, new SLOptionalType (SLSimpleType.OpaquePointer), SLConstant.Nil, Visibility.Public);
1496+
return fieldLine;
1497+
}
1498+
14891499
SLLine ToFieldDeclaration (FunctionDeclaration func, int index, List<string> fieldIdentifiers)
14901500
{
14911501
// this defines the type and member of a vtable entry

0 commit comments

Comments
 (0)