Skip to content

Commit 2c51958

Browse files
authored
Merge pull request #636 from swiftwasm/yt/fix-opt-jsobject-double-release
BridgeJS: Fix double-release of JSObject when passing a `JSObject?` JS->Swift
2 parents 0d5b431 + 072b0d0 commit 2c51958

File tree

6 files changed

+235
-169
lines changed

6 files changed

+235
-169
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -924,11 +924,6 @@ struct IntrinsicJSFragment: Sendable {
924924
printer.write("\(idVar) = \(JSGlueVariableScope.reservedSwift).memory.retain(\(value));")
925925
}
926926
printer.write("}")
927-
cleanupCode.write("if (\(idVar) !== undefined) {")
928-
cleanupCode.indent {
929-
cleanupCode.write("\(JSGlueVariableScope.reservedSwift).memory.release(\(idVar));")
930-
}
931-
cleanupCode.write("}")
932927
return ["+\(isSomeVar)", "\(isSomeVar) ? \(idVar) : 0"]
933928
default:
934929
return ["+\(isSomeVar)", "\(isSomeVar) ? \(value) : 0"]

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5843,6 +5843,16 @@ public func _bjs_roundTripOptionalTypedPayloadResult(_ resultIsSome: Int32, _ re
58435843
#endif
58445844
}
58455845

5846+
@_expose(wasm, "bjs_takeOptionalJSObject")
5847+
@_cdecl("bjs_takeOptionalJSObject")
5848+
public func _bjs_takeOptionalJSObject(_ valueIsSome: Int32, _ valueValue: Int32) -> Void {
5849+
#if arch(wasm32)
5850+
takeOptionalJSObject(_: Optional<JSObject>.bridgeJSLiftParameter(valueIsSome, valueValue))
5851+
#else
5852+
fatalError("Only available on WebAssembly")
5853+
#endif
5854+
}
5855+
58465856
@_expose(wasm, "bjs_compareAPIResults")
58475857
@_cdecl("bjs_compareAPIResults")
58485858
public func _bjs_compareAPIResults(_ r1IsSome: Int32, _ r1CaseId: Int32, _ r2IsSome: Int32, _ r2CaseId: Int32) -> Void {
@@ -10147,93 +10157,93 @@ func _$jsTranslatePoint(_ point: Point, _ dx: Int, _ dy: Int) throws(JSException
1014710157
}
1014810158

1014910159
#if arch(wasm32)
10150-
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_runJsOptionalSupportTests")
10151-
fileprivate func bjs_runJsOptionalSupportTests() -> Void
10160+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_OptionalSupportImports_jsRoundTripOptionalNumberNull_static")
10161+
fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalNumberNull_static(_ valueIsSome: Int32, _ valueValue: Int32) -> Void
1015210162
#else
10153-
fileprivate func bjs_runJsOptionalSupportTests() -> Void {
10163+
fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalNumberNull_static(_ valueIsSome: Int32, _ valueValue: Int32) -> Void {
1015410164
fatalError("Only available on WebAssembly")
1015510165
}
1015610166
#endif
1015710167

10158-
func _$runJsOptionalSupportTests() throws(JSException) -> Void {
10159-
bjs_runJsOptionalSupportTests()
10160-
if let error = _swift_js_take_exception() {
10161-
throw error
10162-
}
10168+
#if arch(wasm32)
10169+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_OptionalSupportImports_jsRoundTripOptionalNumberUndefined_static")
10170+
fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalNumberUndefined_static(_ valueIsSome: Int32, _ valueValue: Int32) -> Void
10171+
#else
10172+
fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalNumberUndefined_static(_ valueIsSome: Int32, _ valueValue: Int32) -> Void {
10173+
fatalError("Only available on WebAssembly")
1016310174
}
10175+
#endif
1016410176

1016510177
#if arch(wasm32)
10166-
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripOptionalNumberNull")
10167-
fileprivate func bjs_jsRoundTripOptionalNumberNull(_ valueIsSome: Int32, _ valueValue: Int32) -> Void
10178+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static")
10179+
fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static(_ nameIsSome: Int32, _ nameValue: Int32) -> Void
1016810180
#else
10169-
fileprivate func bjs_jsRoundTripOptionalNumberNull(_ valueIsSome: Int32, _ valueValue: Int32) -> Void {
10181+
fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static(_ nameIsSome: Int32, _ nameValue: Int32) -> Void {
1017010182
fatalError("Only available on WebAssembly")
1017110183
}
1017210184
#endif
1017310185

10174-
func _$jsRoundTripOptionalNumberNull(_ value: Optional<Int>) throws(JSException) -> Optional<Int> {
10175-
let (valueIsSome, valueValue) = value.bridgeJSLowerParameter()
10176-
bjs_jsRoundTripOptionalNumberNull(valueIsSome, valueValue)
10177-
if let error = _swift_js_take_exception() {
10178-
throw error
10179-
}
10180-
return Optional<Int>.bridgeJSLiftReturnFromSideChannel()
10186+
#if arch(wasm32)
10187+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static")
10188+
fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static(_ nameIsSome: Int32, _ nameValue: Int32) -> Void
10189+
#else
10190+
fileprivate func bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static(_ nameIsSome: Int32, _ nameValue: Int32) -> Void {
10191+
fatalError("Only available on WebAssembly")
1018110192
}
10193+
#endif
1018210194

1018310195
#if arch(wasm32)
10184-
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripOptionalNumberUndefined")
10185-
fileprivate func bjs_jsRoundTripOptionalNumberUndefined(_ valueIsSome: Int32, _ valueValue: Int32) -> Void
10196+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_OptionalSupportImports_runJsOptionalSupportTests_static")
10197+
fileprivate func bjs_OptionalSupportImports_runJsOptionalSupportTests_static() -> Void
1018610198
#else
10187-
fileprivate func bjs_jsRoundTripOptionalNumberUndefined(_ valueIsSome: Int32, _ valueValue: Int32) -> Void {
10199+
fileprivate func bjs_OptionalSupportImports_runJsOptionalSupportTests_static() -> Void {
1018810200
fatalError("Only available on WebAssembly")
1018910201
}
1019010202
#endif
1019110203

10192-
func _$jsRoundTripOptionalNumberUndefined(_ value: JSUndefinedOr<Int>) throws(JSException) -> JSUndefinedOr<Int> {
10204+
func _$OptionalSupportImports_jsRoundTripOptionalNumberNull(_ value: Optional<Int>) throws(JSException) -> Optional<Int> {
1019310205
let (valueIsSome, valueValue) = value.bridgeJSLowerParameter()
10194-
bjs_jsRoundTripOptionalNumberUndefined(valueIsSome, valueValue)
10206+
bjs_OptionalSupportImports_jsRoundTripOptionalNumberNull_static(valueIsSome, valueValue)
1019510207
if let error = _swift_js_take_exception() {
1019610208
throw error
1019710209
}
10198-
return JSUndefinedOr<Int>.bridgeJSLiftReturnFromSideChannel()
10210+
return Optional<Int>.bridgeJSLiftReturnFromSideChannel()
1019910211
}
1020010212

10201-
#if arch(wasm32)
10202-
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripOptionalStringNull")
10203-
fileprivate func bjs_jsRoundTripOptionalStringNull(_ nameIsSome: Int32, _ nameValue: Int32) -> Void
10204-
#else
10205-
fileprivate func bjs_jsRoundTripOptionalStringNull(_ nameIsSome: Int32, _ nameValue: Int32) -> Void {
10206-
fatalError("Only available on WebAssembly")
10213+
func _$OptionalSupportImports_jsRoundTripOptionalNumberUndefined(_ value: JSUndefinedOr<Int>) throws(JSException) -> JSUndefinedOr<Int> {
10214+
let (valueIsSome, valueValue) = value.bridgeJSLowerParameter()
10215+
bjs_OptionalSupportImports_jsRoundTripOptionalNumberUndefined_static(valueIsSome, valueValue)
10216+
if let error = _swift_js_take_exception() {
10217+
throw error
10218+
}
10219+
return JSUndefinedOr<Int>.bridgeJSLiftReturnFromSideChannel()
1020710220
}
10208-
#endif
1020910221

10210-
func _$jsRoundTripOptionalStringNull(_ name: Optional<String>) throws(JSException) -> Optional<String> {
10222+
func _$OptionalSupportImports_jsRoundTripOptionalStringNull(_ name: Optional<String>) throws(JSException) -> Optional<String> {
1021110223
let (nameIsSome, nameValue) = name.bridgeJSLowerParameter()
10212-
bjs_jsRoundTripOptionalStringNull(nameIsSome, nameValue)
10224+
bjs_OptionalSupportImports_jsRoundTripOptionalStringNull_static(nameIsSome, nameValue)
1021310225
if let error = _swift_js_take_exception() {
1021410226
throw error
1021510227
}
1021610228
return Optional<String>.bridgeJSLiftReturnFromSideChannel()
1021710229
}
1021810230

10219-
#if arch(wasm32)
10220-
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripOptionalStringUndefined")
10221-
fileprivate func bjs_jsRoundTripOptionalStringUndefined(_ nameIsSome: Int32, _ nameValue: Int32) -> Void
10222-
#else
10223-
fileprivate func bjs_jsRoundTripOptionalStringUndefined(_ nameIsSome: Int32, _ nameValue: Int32) -> Void {
10224-
fatalError("Only available on WebAssembly")
10225-
}
10226-
#endif
10227-
10228-
func _$jsRoundTripOptionalStringUndefined(_ name: JSUndefinedOr<String>) throws(JSException) -> JSUndefinedOr<String> {
10231+
func _$OptionalSupportImports_jsRoundTripOptionalStringUndefined(_ name: JSUndefinedOr<String>) throws(JSException) -> JSUndefinedOr<String> {
1022910232
let (nameIsSome, nameValue) = name.bridgeJSLowerParameter()
10230-
bjs_jsRoundTripOptionalStringUndefined(nameIsSome, nameValue)
10233+
bjs_OptionalSupportImports_jsRoundTripOptionalStringUndefined_static(nameIsSome, nameValue)
1023110234
if let error = _swift_js_take_exception() {
1023210235
throw error
1023310236
}
1023410237
return JSUndefinedOr<String>.bridgeJSLiftReturnFromSideChannel()
1023510238
}
1023610239

10240+
func _$OptionalSupportImports_runJsOptionalSupportTests() throws(JSException) -> Void {
10241+
bjs_OptionalSupportImports_runJsOptionalSupportTests_static()
10242+
if let error = _swift_js_take_exception() {
10243+
throw error
10244+
}
10245+
}
10246+
1023710247
#if arch(wasm32)
1023810248
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_SwiftClassSupportImports_jsRoundTripGreeter_static")
1023910249
fileprivate func bjs_SwiftClassSupportImports_jsRoundTripGreeter_static(_ greeter: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer

0 commit comments

Comments
 (0)