Skip to content

Change JSClosure.release to deinit #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,22 @@ ObjectRef_Lifetime: do {
let ref2 = identity(ref1).object!
try expectEqual(ref1.prop_2, .number(2))
try expectEqual(ref2.prop_2, .number(2))
identity.release()
} catch {
print(error)
}

func closureScope() -> ObjectIdentifier {
let closure = JSClosure { _ in .undefined }
let result = ObjectIdentifier(closure)
closure.release()
return result
}

Closure_Identifiers: do {
let oid1 = closureScope()
let oid2 = closureScope()
try expectEqual(oid1, oid2)
} catch {
print(error)
}
12 changes: 12 additions & 0 deletions Sources/JavaScriptKit/JSFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class JSClosure: JSFunctionRef {

private var hostFuncRef: JavaScriptHostFuncRef = 0

private var isReleased = false

public init(_ body: @escaping ([JSValue]) -> JSValue) {
super.init(id: 0)
let objectId = ObjectIdentifier(self)
Expand All @@ -83,6 +85,16 @@ public class JSClosure: JSFunctionRef {

public func release() {
Self.sharedFunctions[hostFuncRef] = nil
isReleased = true
}

deinit {
guard isReleased else {
fatalError("""
release() must be called on closures manually before deallocating.
This is caused by the lack of support for the `FinalizationRegistry` API in Safari.
""")
}
}
}

Expand Down