Skip to content

Commit 2f40419

Browse files
committed
Create options for disabling deinit logging on a per-type-basis, and track instantiation contexts.
1 parent cc7336e commit 2f40419

File tree

8 files changed

+41
-24
lines changed

8 files changed

+41
-24
lines changed

ci/LDKSwift/Sources/LDKSwift/batteries/ChannelManagerConstructor.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public class ChannelManagerConstructor: NativeTypeWrapper {
186186
}
187187
}
188188

189-
super.init(conflictAvoidingVariableName: 0)
189+
super.init(conflictAvoidingVariableName: 0, instantiationContext: "ChannelManagerConstructor.swift::\(#function):\(#line)")
190190
// try! self.peerManager.addAnchor(anchor: self)
191191
// try! self.channelManager.addAnchor(anchor: self)
192192

@@ -225,7 +225,7 @@ public class ChannelManagerConstructor: NativeTypeWrapper {
225225
let timestampSeconds = UInt32(NSDate().timeIntervalSince1970)
226226
self.peerManager = PeerManager(messageHandler: messageHandler, currentTime: timestampSeconds, ephemeralRandomData: random_data, logger: params.logger, customMessageHandler: noCustomMessages.asCustomMessageHandler(), nodeSigner: params.nodeSigner)
227227

228-
super.init(conflictAvoidingVariableName: 0)
228+
super.init(conflictAvoidingVariableName: 0, instantiationContext: "ChannelManagerConstructor.swift::\(#function):\(#line)")
229229
// try! self.peerManager.addAnchor(anchor: self)
230230
// try! self.channelManager.addAnchor(anchor: self)
231231
}

src/generation/base_type_generator.mts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
244244
245245
Self.instanceCounter += 1
246246
self.instanceNumber = Self.instanceCounter
247-
super.init(conflictAvoidingVariableName: 0)
247+
super.init(conflictAvoidingVariableName: 0, instantiationContext: "#{swift_class_name}::\\(#function):\\(#line)")
248248
${anchoringCommand}
249249
`;
250250

@@ -875,6 +875,8 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
875875
*/
876876
const hasRecursiveOwnershipFlags = this.isRecursivelyPerpetuallySafelyFreeable(returnType.type);
877877

878+
const instantiationContextInfixTemplate = ', instantiationContext: "#{swift_class_name}::\\(#function):\\(#line)"'
879+
878880
/**
879881
* The returned object cannot live on its own. It needs the container to stick around.
880882
* Should not be used for elided types, however, because that will make even the Swift
@@ -1010,22 +1012,22 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
10101012
dangleSuffix = '.dangle()';
10111013
}
10121014
}
1013-
preparedReturnValue.wrapperSuffix += `${anchorInfix})${dangleSuffix}`;
1015+
preparedReturnValue.wrapperSuffix += `${instantiationContextInfixTemplate}${anchorInfix})${dangleSuffix}`;
10141016
if (returnType.type !== containerType) {
10151017
// it's an elided type, so we pass it through
10161018
preparedReturnValue.wrapperSuffix += '.getValue()';
10171019
}
10181020
} else if (returnType.type instanceof RustTrait) {
10191021
preparedReturnValue.wrapperPrefix += `NativelyImplemented${this.swiftTypeName(returnType.type)}(cType: `;
1020-
preparedReturnValue.wrapperSuffix += `${anchorInfix})${dangleSuffix}`;
1022+
preparedReturnValue.wrapperSuffix += `${instantiationContextInfixTemplate}${anchorInfix})${dangleSuffix}`;
10211023
} else if (returnType.type instanceof RustStruct || returnType.type instanceof RustResult || returnType.type instanceof RustTaggedValueEnum) {
10221024
// basically all the non-elided types
10231025
if (!this.isElidedType(returnType.type) && returnType.type instanceof RustStruct && containerType instanceof RustStruct && containerType.parentType instanceof RustTaggedValueEnum) {
10241026
// one really odd edge case
10251027
preparedReturnValue.wrapperPrefix += 'Bindings.';
10261028
}
10271029
preparedReturnValue.wrapperPrefix += `${this.swiftTypeName(returnType.type)}(cType: `;
1028-
preparedReturnValue.wrapperSuffix += `${anchorInfix})${dangleSuffix}`;
1030+
preparedReturnValue.wrapperSuffix += `${instantiationContextInfixTemplate}${anchorInfix})${dangleSuffix}`;
10291031
} else if (returnType.type instanceof RustPrimitive) {
10301032
// nothing to do here
10311033
return preparedReturnValue;
@@ -1065,20 +1067,20 @@ export abstract class BaseTypeGenerator<Type extends RustType> {
10651067
10661068
internal var cType: ${typeName}?
10671069
1068-
internal init(cType: ${typeName}) {
1070+
internal init(cType: ${typeName}, instantiationContext: String) {
10691071
Self.instanceCounter += 1
10701072
self.instanceNumber = Self.instanceCounter
10711073
self.cType = cType
10721074
${initialCFreeabilityInfix}
1073-
super.init(conflictAvoidingVariableName: 0)
1075+
super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext)
10741076
}
10751077
1076-
internal init(cType: ${typeName}, anchor: NativeTypeWrapper) {
1078+
internal init(cType: ${typeName}, instantiationContext: String, anchor: NativeTypeWrapper) {
10771079
Self.instanceCounter += 1
10781080
self.instanceNumber = Self.instanceCounter
10791081
self.cType = cType
10801082
${initialCFreeabilityInfix}
1081-
super.init(conflictAvoidingVariableName: 0)
1083+
super.init(conflictAvoidingVariableName: 0, instantiationContext: instantiationContext)
10821084
self.dangling = true
10831085
try! self.addAnchor(anchor: anchor)
10841086
}

src/generation/bindings_file_generator.mts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,18 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
9292
9393
private static var globalInstanceCounter: UInt = 0
9494
internal let globalInstanceNumber: UInt
95+
internal let instantiationContext: String
9596
internal var dangling = false
9697
internal private(set) var anchors: Set<NativeTypeWrapper> = []
9798
internal var pointerDebugDescription: String? = nil
9899
99-
init(conflictAvoidingVariableName: UInt) {
100+
@available(*, unavailable, message: "This variable is only available to subclasses.")
101+
public static var enableDeinitLogging = true
102+
103+
init(conflictAvoidingVariableName: UInt, instantiationContext: String) {
100104
Self.globalInstanceCounter += 1
101105
self.globalInstanceNumber = Self.globalInstanceCounter
106+
self.instantiationContext = instantiationContext
102107
}
103108
104109
internal func addAnchor(anchor: NativeTypeWrapper) throws {
@@ -289,7 +294,7 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
289294
public class InstanceCrashSimulator: NativeTraitWrapper {
290295
291296
public init() {
292-
super.init(conflictAvoidingVariableName: 0)
297+
super.init(conflictAvoidingVariableName: 0, instantiationContext: "Bindings.swift::\\(#function):\\(#line)")
293298
}
294299
295300
public func getPointer() -> UnsafeMutableRawPointer {

src/generation/nullable_option_generator.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default class NullableOptionGenerator extends BaseTypeGenerator<RustNulla
5959
6060
${this.inheritedInits(type)}
6161
62-
public init(${valueArgumentName}: ${swiftReturnType}?) {
62+
internal init(${valueArgumentName}: ${swiftReturnType}?) {
6363
Self.instanceCounter += 1
6464
self.instanceNumber = Self.instanceCounter
6565
@@ -70,7 +70,7 @@ export default class NullableOptionGenerator extends BaseTypeGenerator<RustNulla
7070
self.cType = ${noneInitializerMethod!.name}()
7171
}
7272
73-
super.init(conflictAvoidingVariableName: 0)
73+
super.init(conflictAvoidingVariableName: 0, instantiationContext: "${swiftTypeName}.swift::\\(#function):\\(#line)")
7474
}
7575
7676
${generatedMethods}

src/generation/primitive_wrapper_generator.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ export default class PrimitiveWrapperGenerator extends BaseTypeGenerator<RustPri
9898
9999
${this.inheritedInits(type)}
100100
101-
public init(value: ${swiftReturnType}) {
101+
internal init(value: ${swiftReturnType}) {
102102
Self.instanceCounter += 1
103103
self.instanceNumber = Self.instanceCounter
104104
105105
${initializer}
106106
107-
super.init(conflictAvoidingVariableName: 0)
107+
super.init(conflictAvoidingVariableName: 0, instantiationContext: "${swiftTypeName}.swift::\\(#function):\\(#line)")
108108
}
109109
110110
${generatedMethods}

src/generation/trait_generator.mts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export default class TraitGenerator extends BaseTypeGenerator<RustTrait> {
118118
public init(${constructorArguments.join(', ')}) {
119119
Self.instanceCounter += 1
120120
self.instanceNumber = Self.instanceCounter
121-
super.init(conflictAvoidingVariableName: 0)
121+
super.init(conflictAvoidingVariableName: 0, instantiationContext: "${swiftTypeName}.swift::\\(#function):\\(#line)")
122122
123123
let thisArg = Bindings.instanceToPointer(instance: self)
124124
@@ -368,6 +368,8 @@ export default class TraitGenerator extends BaseTypeGenerator<RustTrait> {
368368

369369
let needsUnwrapping = argumentType.isAsteriskPointer && !argumentType.isNonnullablePointer;
370370

371+
const instantiationContextInfixTemplate = ', instantiationContext: "#{swift_class_name}::init()::\\(#function):\\(#line)"'
372+
371373
let memoryManagementInfix = '';
372374
if (!(type instanceof RustTrait) && this.hasFreeMethod(type) && argumentType.isAsteriskPointer) {
373375
// we wanna dangle this value no matter what, because we don't know the longevity
@@ -396,13 +398,13 @@ export default class TraitGenerator extends BaseTypeGenerator<RustTrait> {
396398

397399
if (type instanceof RustVector || type instanceof RustPrimitiveWrapper || type instanceof RustNullableOption) {
398400
preparedArgument.methodCallWrapperPrefix += `${this.swiftTypeName(type)}(cType: `;
399-
preparedArgument.methodCallWrapperSuffix += `)${memoryManagementInfix}.getValue()`;
401+
preparedArgument.methodCallWrapperSuffix += `${instantiationContextInfixTemplate})${memoryManagementInfix}.getValue()`;
400402
} else if (type instanceof RustTrait) {
401403
preparedArgument.methodCallWrapperPrefix += `NativelyImplemented${this.swiftTypeName(type)}(cType: `;
402-
preparedArgument.methodCallWrapperSuffix += `)`;
404+
preparedArgument.methodCallWrapperSuffix += `${instantiationContextInfixTemplate})`;
403405
} else if (type instanceof RustStruct || type instanceof RustResult || type instanceof RustTaggedValueEnum) {
404406
preparedArgument.methodCallWrapperPrefix += `${this.swiftTypeName(type)}(cType: `;
405-
preparedArgument.methodCallWrapperSuffix += `)${memoryManagementInfix}`;
407+
preparedArgument.methodCallWrapperSuffix += `${instantiationContextInfixTemplate})${memoryManagementInfix}`;
406408
} else if (type instanceof RustPrimitive) {
407409
// nothing to do here
408410
} else if (type instanceof RustArray) {

src/generation/vector_generator.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,10 @@ export default class VectorGenerator extends BaseTypeGenerator<RustVector> {
154154
155155
${this.inheritedInits(type)}
156156
157-
public init(array: ${swiftPublicType}) {
157+
internal init(array: ${swiftPublicType}) {
158158
Self.instanceCounter += 1
159159
self.instanceNumber = Self.instanceCounter
160-
super.init(conflictAvoidingVariableName: 0)
160+
super.init(conflictAvoidingVariableName: 0, instantiationContext: "${swiftTypeName}.swift::\\(#function):\\(#line)")
161161
162162
${swiftArrayToRustArrayMapper}
163163

xcode/LDKFramework/DirectlyLinkedBindingsApp/DirectlyLinkedBindingsAppApp.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ struct DirectlyLinkedBindingsAppApp: App {
1313
init() {
1414
print("Directly linked bindings app is initialized")
1515

16-
let compiledVersion = Bindings.getLDKSwiftBindingsSerializationHash()
17-
print("Compiled version: \(compiledVersion)")
16+
func printCompiledVersion() {
17+
let compiledVersion = Bindings.getLDKSwiftBindingsSerializationHash()
18+
print("Compiled version: \(compiledVersion)\n\(#file)::\(#function):\(#line)")
19+
}
20+
printCompiledVersion()
21+
22+
do {
23+
let thing = Bindings.Fallback.initWithSegWitProgram(version: 255, program: [])
24+
}
25+
// let tx = BuiltCommitmentTransaction(transactionArg: [], txidArg: [UInt8](repeating: 0, count: 32))
1826

1927
let nativeNetwork = LDKNetwork_Regtest
2028
let swiftNetwork = Bindings.Network.Bitcoin

0 commit comments

Comments
 (0)