Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5e79c1f

Browse files
authored
Change JSClosure.release to deinit (swiftwasm#33)
* Change `JSClosure.release` to `deinit` * Rename test case in PrimaryTests * Add newline to `PrimaryTests/main.swift` * Add `isRelease` property to `JSClosure`
1 parent de61e95 commit 5e79c1f

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

+16
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,22 @@ ObjectRef_Lifetime: do {
331331
let ref2 = identity(ref1).object!
332332
try expectEqual(ref1.prop_2, .number(2))
333333
try expectEqual(ref2.prop_2, .number(2))
334+
identity.release()
335+
} catch {
336+
print(error)
337+
}
338+
339+
func closureScope() -> ObjectIdentifier {
340+
let closure = JSClosure { _ in .undefined }
341+
let result = ObjectIdentifier(closure)
342+
closure.release()
343+
return result
344+
}
345+
346+
Closure_Identifiers: do {
347+
let oid1 = closureScope()
348+
let oid2 = closureScope()
349+
try expectEqual(oid1, oid2)
334350
} catch {
335351
print(error)
336352
}

Sources/JavaScriptKit/JSFunction.swift

+12
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public class JSClosure: JSFunctionRef {
6767

6868
private var hostFuncRef: JavaScriptHostFuncRef = 0
6969

70+
private var isReleased = false
71+
7072
public init(_ body: @escaping ([JSValue]) -> JSValue) {
7173
super.init(id: 0)
7274
let objectId = ObjectIdentifier(self)
@@ -82,6 +84,16 @@ public class JSClosure: JSFunctionRef {
8284

8385
public func release() {
8486
Self.sharedFunctions[hostFuncRef] = nil
87+
isReleased = true
88+
}
89+
90+
deinit {
91+
guard isReleased else {
92+
fatalError("""
93+
release() must be called on closures manually before deallocating.
94+
This is caused by the lack of support for the `FinalizationRegistry` API in Safari.
95+
""")
96+
}
8597
}
8698
}
8799

0 commit comments

Comments
 (0)