@@ -99,8 +99,12 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
9999 internal var pointerDebugDescription: String? = nil
100100
101101 init(conflictAvoidingVariableName: UInt, instantiationContext: String) {
102- Self.globalInstanceCounter += 1
103- self.globalInstanceNumber = Self.globalInstanceCounter
102+ var instanceIndex: UInt! = nil
103+ Bindings.instanceIndexQueue.sync {
104+ Self.globalInstanceCounter += 1
105+ instanceIndex = Self.globalInstanceCounter
106+ }
107+ self.globalInstanceNumber = instanceIndex
104108 self.instantiationContext = instantiationContext
105109 }
106110
@@ -156,6 +160,10 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
156160
157161 public class Bindings {
158162
163+ fileprivate static let instanceIndexQueue = DispatchQueue(label: "org.lightningdevkit.Bindings.instanceIndexQueue")
164+ static var nativelyExposedInstances = [UInt: NativeTraitWrapper]()
165+ static var nativelyExposedInstanceReferenceCounter = [UInt: Int]()
166+
159167 internal static var suspendFreedom = false
160168
161169 internal static var minimumPrintSeverity: PrintSeverity = .WARNING
@@ -179,13 +187,13 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
179187 // if #available(iOS 14.0, *) {
180188 // #if canImport(os)
181189 // if severity == Self.PrintSeverity.DEBUG {
182- // logger.debug("\\ (string)")
190+ // logger.debug("\(string)")
183191 // }else if severity == Self.PrintSeverity.WARNING {
184- // logger.warning("\\ (string)")
192+ // logger.warning("\(string)")
185193 // }else if severity == Self.PrintSeverity.ERROR {
186- // logger.error("\\ (string)")
194+ // logger.error("\(string)")
187195 // }else {
188- // logger.log("\\ (string)")
196+ // logger.log("\(string)")
189197 // }
190198 // #else
191199 // Swift.print(string)
@@ -201,58 +209,50 @@ export default class BindingsFileGenerator extends BaseTypeGenerator<GlobalBindi
201209 Self.minimumPrintSeverity = severity
202210 }
203211
204- static var nativelyExposedInstances = [UInt: NativeTraitWrapper]()
205- static var nativelyExposedInstanceReferenceCounter = [UInt: Int]()
206-
207212 public class func cacheInstance(instance: NativeTraitWrapper, countIdempotently: Bool = false) {
208213 let key = instance.globalInstanceNumber
209- let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) + 1
210- if (!countIdempotently || referenceCount == 1){
211- // if we count non-idempotently, always update the counter
212- // otherwise, only update the counter the first time
213- Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
214- }
215- if referenceCount == 1 {
216- print("Caching global instance \\(key). Cached instance count: \\(nativelyExposedInstanceReferenceCounter.count)")
217- Self.nativelyExposedInstances[key] = instance
218- }
214+
215+ Bindings.instanceIndexQueue.sync {
216+ let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) + 1
217+ if (!countIdempotently || referenceCount == 1){
218+ // if we count non-idempotently, always update the counter
219+ // otherwise, only update the counter the first time
220+ Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
221+ }
222+ if referenceCount == 1 {
223+ print("Caching global instance \(key). Cached instance count: \(nativelyExposedInstanceReferenceCounter.count)")
224+ Self.nativelyExposedInstances[key] = instance
225+ }
226+ }
219227 }
220228
221229 public class func instanceToPointer(instance: NativeTraitWrapper) -> UnsafeMutableRawPointer {
222230 let key = instance.globalInstanceNumber
223231 let pointer = UnsafeMutableRawPointer(bitPattern: key)!
224- print("Caching instance \\ (key) -> \ \(pointer)", severity: .DEBUG)
232+ print("Caching instance \(key) -> \(pointer)", severity: .DEBUG)
225233 // don't automatically cache the trait instance
226- Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
234+ Bindings.instanceIndexQueue.sync {
235+ Self.nativelyExposedInstances[instance.globalInstanceNumber] = instance
236+ }
227237 return pointer
228238 }
229239
230240 public class func pointerToInstance<T: NativeTraitWrapper>(pointer: UnsafeRawPointer, sourceMarker: String?) -> T{
231241 let key = UInt(bitPattern: pointer)
232- print("Looking up instance \\(pointer) -> \\(key)", severity: .DEBUG)
233- let referenceCount = Self.nativelyExposedInstanceReferenceCounter[key] ?? 0
234- if referenceCount < 1 {
235- print("Bad lookup: non-positive reference count for instance \\(key): \\(referenceCount)!", severity: .ERROR)
236- }
237- let value = Self.nativelyExposedInstances[key] as! T
242+ print("Looking up instance \(pointer) -> \(key)", severity: .DEBUG)
243+
244+ var rawValue: NativeTraitWrapper! = nil
245+ Bindings.instanceIndexQueue.sync {
246+ let referenceCount = Self.nativelyExposedInstanceReferenceCounter[key] ?? 0
247+ if referenceCount < 1 {
248+ print("Bad lookup: non-positive reference count for instance \(key): \(referenceCount)!", severity: .ERROR)
249+ }
250+ rawValue = Self.nativelyExposedInstances[key]
251+ }
252+ let value = rawValue as! T
238253 return value
239254 }
240255
241- public class func removeInstancePointer(instance: NativeTraitWrapper) -> Bool {
242- let key = instance.globalInstanceNumber
243- let referenceCount = (Self.nativelyExposedInstanceReferenceCounter[key] ?? 0) - 1
244- Self.nativelyExposedInstanceReferenceCounter[key] = referenceCount
245- if referenceCount == 0 {
246- print("Uncaching global instance \\(key)")
247- // TODO: fix counting
248- // Self.nativelyExposedInstances.removeValue(forKey: key)
249- // instance.pointerDebugDescription = nil
250- } else if referenceCount < 0 {
251- print("Bad uncache: negative reference count (\\(referenceCount)) for instance \\(key)!", severity: .ERROR)
252- }
253- return true
254- }
255-
256256 /*
257257 public class func clearInstancePointers() {
258258 for (_, currentInstance) in Self.nativelyExposedInstances {
0 commit comments