We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 5529520 commit 0c4c45aCopy full SHA for 0c4c45a
1 file changed
Sources/JavaScriptKit/BridgeJSIntrinsics.swift
@@ -420,7 +420,12 @@ extension JSObject: _BridgedSwiftStackType {
420
}
421
422
@_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Int32 {
423
- return _swift_js_retain(Int32(bitPattern: self.id))
+ // withExtendedLifetime is required here to prevent a use-after-free.
424
+ // In a `consuming func`, Swift ARC may release `self` (and thus release
425
+ // the underlying JS reference) as soon as it extracts `self.id`, which
426
+ // happens *before* `_swift_js_retain` is called. `withExtendedLifetime` forces
427
+ // `self` to stay alive until after `_swift_js_retain` returns.
428
+ return withExtendedLifetime(self) { _swift_js_retain(Int32(bitPattern: self.id)) }
429
430
431
@_spi(BridgeJS) public consuming func bridgeJSStackPush() {
0 commit comments