Skip to content

Commit 872da4d

Browse files
fjtrujyclaude
andcommitted
Prevent cross-module inlining of remaining @_extern(wasm) functions
Extends the fix from b3ddd88 (which addressed f32/f64) to all remaining public @_extern(wasm) BridgeJS intrinsics: i32, string, pointer, throw, init_memory, and struct_cleanup. Without @inline(never) wrappers, the Swift compiler can inline these functions across module boundaries, causing the wasm import module attribute to change from "bjs" to "env". This results in a wasm-ld linker error when a downstream module (e.g. via BridgeJS codegen) references the same symbol with a different import module. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a931787 commit 872da4d

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

Sources/JavaScriptKit/BridgeJSIntrinsics.swift

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@ import _CJavaScriptKit
1515

1616
#if arch(wasm32)
1717
@_extern(wasm, module: "bjs", name: "swift_js_throw")
18-
@_spi(BridgeJS) public func _swift_js_throw(_ id: Int32)
18+
private func _swift_js_throw_extern(_ id: Int32)
1919
#else
20+
private func _swift_js_throw_extern(_ id: Int32) {
21+
_onlyAvailableOnWasm()
22+
}
23+
#endif
24+
2025
/// Throws a JavaScript exception from Swift code.
2126
///
2227
/// This function is called by the BridgeJS code generator when a Swift function throws
2328
/// an error. The exception object is retained and stored for later retrieval by the
2429
/// JavaScript-side runtime code.
2530
///
2631
/// - Parameter id: The ID of the JavaScript exception object to throw
27-
@_spi(BridgeJS) public func _swift_js_throw(_ id: Int32) {
28-
_onlyAvailableOnWasm()
32+
@_spi(BridgeJS) @inline(never) public func _swift_js_throw(_ id: Int32) {
33+
_swift_js_throw_extern(id)
2934
}
30-
#endif
3135

3236
/// Retrieves and clears any pending JavaScript exception.
3337
///
@@ -702,31 +706,43 @@ where Self: RawRepresentable, RawValue: _BridgedSwiftTypeLoweredIntoSingleWasmCo
702706

703707
#if arch(wasm32)
704708
@_extern(wasm, module: "bjs", name: "swift_js_init_memory")
705-
@_spi(BridgeJS) public func _swift_js_init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>)
709+
private func _swift_js_init_memory_extern(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>)
706710
#else
707-
@_spi(BridgeJS) public func _swift_js_init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>) {
711+
private func _swift_js_init_memory_extern(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>) {
708712
_onlyAvailableOnWasm()
709713
}
710714
#endif
711715

716+
@_spi(BridgeJS) @inline(never) public func _swift_js_init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>) {
717+
_swift_js_init_memory_extern(sourceId, ptr)
718+
}
719+
712720
#if arch(wasm32)
713721
@_extern(wasm, module: "bjs", name: "swift_js_push_string")
714-
@_spi(BridgeJS) public func _swift_js_push_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
722+
private func _swift_js_push_string_extern(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
715723
#else
716-
@_spi(BridgeJS) public func _swift_js_push_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
724+
private func _swift_js_push_string_extern(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
717725
_onlyAvailableOnWasm()
718726
}
719727
#endif
720728

729+
@_spi(BridgeJS) @inline(never) public func _swift_js_push_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
730+
_swift_js_push_string_extern(ptr, len)
731+
}
732+
721733
#if arch(wasm32)
722734
@_extern(wasm, module: "bjs", name: "swift_js_push_i32")
723-
@_spi(BridgeJS) public func _swift_js_push_i32(_ value: Int32)
735+
private func _swift_js_push_i32_extern(_ value: Int32)
724736
#else
725-
@_spi(BridgeJS) public func _swift_js_push_i32(_ value: Int32) {
737+
private func _swift_js_push_i32_extern(_ value: Int32) {
726738
_onlyAvailableOnWasm()
727739
}
728740
#endif
729741

742+
@_spi(BridgeJS) @inline(never) public func _swift_js_push_i32(_ value: Int32) {
743+
_swift_js_push_i32_extern(value)
744+
}
745+
730746
#if arch(wasm32)
731747
@_extern(wasm, module: "bjs", name: "swift_js_push_f32")
732748
private func _swift_js_push_f32_extern(_ value: Float32)
@@ -755,13 +771,17 @@ private func _swift_js_push_f64_extern(_ value: Float64) {
755771

756772
#if arch(wasm32)
757773
@_extern(wasm, module: "bjs", name: "swift_js_pop_i32")
758-
@_spi(BridgeJS) public func _swift_js_pop_i32() -> Int32
774+
private func _swift_js_pop_i32_extern() -> Int32
759775
#else
760-
@_spi(BridgeJS) public func _swift_js_pop_i32() -> Int32 {
776+
private func _swift_js_pop_i32_extern() -> Int32 {
761777
_onlyAvailableOnWasm()
762778
}
763779
#endif
764780

781+
@_spi(BridgeJS) @inline(never) public func _swift_js_pop_i32() -> Int32 {
782+
_swift_js_pop_i32_extern()
783+
}
784+
765785
#if arch(wasm32)
766786
@_extern(wasm, module: "bjs", name: "swift_js_pop_f32")
767787
private func _swift_js_pop_f32_extern() -> Float32
@@ -792,13 +812,17 @@ private func _swift_js_pop_f64_extern() -> Float64 {
792812

793813
#if arch(wasm32)
794814
@_extern(wasm, module: "bjs", name: "swift_js_struct_cleanup")
795-
@_spi(BridgeJS) public func _swift_js_struct_cleanup(_ cleanupId: Int32)
815+
private func _swift_js_struct_cleanup_extern(_ cleanupId: Int32)
796816
#else
797-
@_spi(BridgeJS) public func _swift_js_struct_cleanup(_ cleanupId: Int32) {
817+
private func _swift_js_struct_cleanup_extern(_ cleanupId: Int32) {
798818
_onlyAvailableOnWasm()
799819
}
800820
#endif
801821

822+
@_spi(BridgeJS) @inline(never) public func _swift_js_struct_cleanup(_ cleanupId: Int32) {
823+
_swift_js_struct_cleanup_extern(cleanupId)
824+
}
825+
802826
// MARK: Wasm externs used by type lowering/lifting
803827

804828
#if arch(wasm32)
@@ -986,13 +1010,17 @@ func _swift_js_return_optional_double(_ isSome: Int32, _ value: Float64) {
9861010

9871011
#if arch(wasm32)
9881012
@_extern(wasm, module: "bjs", name: "swift_js_push_pointer")
989-
@_spi(BridgeJS) public func _swift_js_push_pointer(_ pointer: UnsafeMutableRawPointer)
1013+
private func _swift_js_push_pointer_extern(_ pointer: UnsafeMutableRawPointer)
9901014
#else
991-
@_spi(BridgeJS) public func _swift_js_push_pointer(_ pointer: UnsafeMutableRawPointer) {
1015+
private func _swift_js_push_pointer_extern(_ pointer: UnsafeMutableRawPointer) {
9921016
_onlyAvailableOnWasm()
9931017
}
9941018
#endif
9951019

1020+
@_spi(BridgeJS) @inline(never) public func _swift_js_push_pointer(_ pointer: UnsafeMutableRawPointer) {
1021+
_swift_js_push_pointer_extern(pointer)
1022+
}
1023+
9961024
#if arch(wasm32)
9971025
@_extern(wasm, module: "bjs", name: "swift_js_pop_pointer")
9981026
private func _swift_js_pop_pointer_extern() -> UnsafeMutableRawPointer

0 commit comments

Comments
 (0)