Skip to content

Commit 68e1435

Browse files
Report where the closure is created when it violates the lifetime contract
This additional information will help developers to find the root cause
1 parent d08a1e4 commit 68e1435

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public class JSClosure: JSFunction, JSClosureProtocol {
6464
private var hostFuncRef: JavaScriptHostFuncRef = 0
6565

6666
#if JAVASCRIPTKIT_WITHOUT_WEAKREFS
67+
private let file: String
68+
private let line: UInt32
6769
private var isReleased: Bool = false
6870
#endif
6971

@@ -77,6 +79,10 @@ public class JSClosure: JSFunction, JSClosureProtocol {
7779
}
7880

7981
public init(_ body: @escaping ([JSValue]) -> JSValue, file: String = #fileID, line: UInt32 = #line) {
82+
#if JAVASCRIPTKIT_WITHOUT_WEAKREFS
83+
self.file = file
84+
self.line = line
85+
#endif
8086
// 1. Fill `id` as zero at first to access `self` to get `ObjectIdentifier`.
8187
super.init(id: 0)
8288

@@ -94,15 +100,15 @@ public class JSClosure: JSFunction, JSClosureProtocol {
94100

95101
#if compiler(>=5.5)
96102
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
97-
public static func async(_ body: @escaping ([JSValue]) async throws -> JSValue) -> JSClosure {
98-
JSClosure(makeAsyncClosure(body))
103+
public static func async(_ body: @escaping ([JSValue]) async throws -> JSValue, file: String = #fileID, line: UInt32 = #line) -> JSClosure {
104+
JSClosure(makeAsyncClosure(body), file: file, line: line)
99105
}
100106
#endif
101107

102108
#if JAVASCRIPTKIT_WITHOUT_WEAKREFS
103109
deinit {
104110
guard isReleased else {
105-
fatalError("release() must be called on JSClosure objects manually before they are deallocated")
111+
fatalError("release() must be called on JSClosure object (\(file):\(line)) manually before they are deallocated")
106112
}
107113
}
108114
#endif

0 commit comments

Comments
 (0)