@@ -20,6 +20,7 @@ open class NativeTypeWrapper: Hashable {
20
20
internal let globalInstanceNumber : UInt
21
21
internal var dangling = false
22
22
internal private( set) var anchors : Set < NativeTypeWrapper > = [ ]
23
+ internal var pointerDebugDescription : String ? = nil
23
24
24
25
init ( conflictAvoidingVariableName: UInt ) {
25
26
Self . globalInstanceCounter += 1
@@ -182,25 +183,42 @@ public class Bindings{
182
183
}
183
184
/* STATIC_METHODS_END */
184
185
185
- static var nativelyExposedInstances = [ String: AnyObject ] ( )
186
+ static var nativelyExposedInstances = [ String: NativeTypeWrapper ] ( )
186
187
187
- public class func instanceToPointer( instance: AnyObject ) -> UnsafeMutableRawPointer {
188
+ public class func instanceToPointer( instance: NativeTypeWrapper ) -> UnsafeMutableRawPointer {
188
189
let pointer = Unmanaged . passUnretained ( instance) . toOpaque ( )
190
+ instance. pointerDebugDescription = pointer. debugDescription
189
191
Self . nativelyExposedInstances [ pointer. debugDescription] = instance
190
192
return pointer
191
193
}
192
194
193
- public class func pointerToInstance< T: AnyObject > ( pointer: UnsafeRawPointer , sourceMarker: String ? ) -> T {
195
+ public class func pointerToInstance< T: NativeTypeWrapper > ( pointer: UnsafeRawPointer , sourceMarker: String ? ) -> T {
194
196
195
197
let callStack = Thread . callStackSymbols
196
198
let caller = sourceMarker ?? callStack [ 1 ]
197
- print ( " Retrieving instance from pointer for caller: \( caller) " )
199
+ // print("Retrieving instance from pointer for caller: \(caller)")
198
200
// let value = Unmanaged<T>.fromOpaque(pointer).takeUnretainedValue()
199
201
let value = Self . nativelyExposedInstances [ pointer. debugDescription] as! T
200
- print ( " Instance retrieved for caller: \( caller) " )
202
+ // print("Instance retrieved for caller: \(caller)")
201
203
return value
202
204
}
203
205
206
+ public class func removeInstancePointer( instance: NativeTypeWrapper ) -> Bool {
207
+ guard let debugDescription = instance. pointerDebugDescription else {
208
+ return false
209
+ }
210
+ Self . nativelyExposedInstances. removeValue ( forKey: debugDescription)
211
+ instance. pointerDebugDescription = nil
212
+ return true
213
+ }
214
+
215
+ public class func clearInstancePointers( ) {
216
+ for (_, currentInstance) in Self . nativelyExposedInstances {
217
+ currentInstance. pointerDebugDescription = nil
218
+ }
219
+ Self . nativelyExposedInstances. removeAll ( )
220
+ }
221
+
204
222
/* SWIFT_TO_RUST_START */
205
223
public class func new_LDKTransactionWrapper( array: [ UInt8 ] ) -> LDKTransactionWrapper {
206
224
/* DIMENSION_REDUCTION_PREP */
@@ -332,11 +350,11 @@ public class Bindings{
332
350
333
351
}
334
352
335
- public class InstanceCrashSimulator {
353
+ public class InstanceCrashSimulator : NativeTypeWrapper {
336
354
337
355
public init ( ) {
338
-
339
- }
356
+ super . init ( conflictAvoidingVariableName : 0 )
357
+ }
340
358
341
359
public func getPointer( ) -> UnsafeMutableRawPointer {
342
360
let pointer = Bindings . instanceToPointer ( instance: self )
0 commit comments