Skip to content

Commit 1cf60d8

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 1cf60d8

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

Sources/JavaScriptKit/BridgeJSIntrinsics.swift

Lines changed: 45 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,44 @@ 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+
{
718+
_swift_js_init_memory_extern(sourceId, ptr)
719+
}
720+
712721
#if arch(wasm32)
713722
@_extern(wasm, module: "bjs", name: "swift_js_push_string")
714-
@_spi(BridgeJS) public func _swift_js_push_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
723+
private func _swift_js_push_string_extern(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
715724
#else
716-
@_spi(BridgeJS) public func _swift_js_push_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
725+
private func _swift_js_push_string_extern(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
717726
_onlyAvailableOnWasm()
718727
}
719728
#endif
720729

730+
@_spi(BridgeJS) @inline(never) public func _swift_js_push_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
731+
_swift_js_push_string_extern(ptr, len)
732+
}
733+
721734
#if arch(wasm32)
722735
@_extern(wasm, module: "bjs", name: "swift_js_push_i32")
723-
@_spi(BridgeJS) public func _swift_js_push_i32(_ value: Int32)
736+
private func _swift_js_push_i32_extern(_ value: Int32)
724737
#else
725-
@_spi(BridgeJS) public func _swift_js_push_i32(_ value: Int32) {
738+
private func _swift_js_push_i32_extern(_ value: Int32) {
726739
_onlyAvailableOnWasm()
727740
}
728741
#endif
729742

743+
@_spi(BridgeJS) @inline(never) public func _swift_js_push_i32(_ value: Int32) {
744+
_swift_js_push_i32_extern(value)
745+
}
746+
730747
#if arch(wasm32)
731748
@_extern(wasm, module: "bjs", name: "swift_js_push_f32")
732749
private func _swift_js_push_f32_extern(_ value: Float32)
@@ -755,13 +772,17 @@ private func _swift_js_push_f64_extern(_ value: Float64) {
755772

756773
#if arch(wasm32)
757774
@_extern(wasm, module: "bjs", name: "swift_js_pop_i32")
758-
@_spi(BridgeJS) public func _swift_js_pop_i32() -> Int32
775+
private func _swift_js_pop_i32_extern() -> Int32
759776
#else
760-
@_spi(BridgeJS) public func _swift_js_pop_i32() -> Int32 {
777+
private func _swift_js_pop_i32_extern() -> Int32 {
761778
_onlyAvailableOnWasm()
762779
}
763780
#endif
764781

782+
@_spi(BridgeJS) @inline(never) public func _swift_js_pop_i32() -> Int32 {
783+
_swift_js_pop_i32_extern()
784+
}
785+
765786
#if arch(wasm32)
766787
@_extern(wasm, module: "bjs", name: "swift_js_pop_f32")
767788
private func _swift_js_pop_f32_extern() -> Float32
@@ -792,13 +813,17 @@ private func _swift_js_pop_f64_extern() -> Float64 {
792813

793814
#if arch(wasm32)
794815
@_extern(wasm, module: "bjs", name: "swift_js_struct_cleanup")
795-
@_spi(BridgeJS) public func _swift_js_struct_cleanup(_ cleanupId: Int32)
816+
private func _swift_js_struct_cleanup_extern(_ cleanupId: Int32)
796817
#else
797-
@_spi(BridgeJS) public func _swift_js_struct_cleanup(_ cleanupId: Int32) {
818+
private func _swift_js_struct_cleanup_extern(_ cleanupId: Int32) {
798819
_onlyAvailableOnWasm()
799820
}
800821
#endif
801822

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

804829
#if arch(wasm32)
@@ -986,13 +1011,17 @@ func _swift_js_return_optional_double(_ isSome: Int32, _ value: Float64) {
9861011

9871012
#if arch(wasm32)
9881013
@_extern(wasm, module: "bjs", name: "swift_js_push_pointer")
989-
@_spi(BridgeJS) public func _swift_js_push_pointer(_ pointer: UnsafeMutableRawPointer)
1014+
private func _swift_js_push_pointer_extern(_ pointer: UnsafeMutableRawPointer)
9901015
#else
991-
@_spi(BridgeJS) public func _swift_js_push_pointer(_ pointer: UnsafeMutableRawPointer) {
1016+
private func _swift_js_push_pointer_extern(_ pointer: UnsafeMutableRawPointer) {
9921017
_onlyAvailableOnWasm()
9931018
}
9941019
#endif
9951020

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

0 commit comments

Comments
 (0)