We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent aa9aa77 commit b010ac8Copy full SHA for b010ac8
1 file changed
Sources/JavaScriptKit/BridgeJSIntrinsics.swift
@@ -384,7 +384,12 @@ extension JSObject: _BridgedSwiftStackType {
384
}
385
386
@_spi(BridgeJS) public consuming func bridgeJSLowerReturn() -> Int32 {
387
- return _swift_js_retain(Int32(bitPattern: self.id))
+ // withExtendedLifetime is required here to prevent a use-after-free.
388
+ // In a `consuming func`, Swift ARC may release `self` (and thus release
389
+ // the underlying JS reference) as soon as it extracts `self.id`, which
390
+ // happens *before* `_swift_js_retain` is called. `withExtendedLifetime` forces
391
+ // `self` to stay alive until after `_swift_js_retain` returns.
392
+ return withExtendedLifetime(self) { _swift_js_retain(Int32(bitPattern: self.id)) }
393
394
395
@_spi(BridgeJS) public consuming func bridgeJSStackPush() {
0 commit comments