Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Benchmarks/Sources/Generated/BridgeJS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2429,20 +2429,20 @@ func _$benchmarkHelperNoopWithNumber(_ n: Double) throws(JSException) -> Void {

#if arch(wasm32)
@_extern(wasm, module: "Benchmarks", name: "bjs_benchmarkRunner")
fileprivate func bjs_benchmarkRunner_extern(_ nameBytes: Int32, _ nameLength: Int32, _ body: Int32) -> Void
fileprivate func bjs_benchmarkRunner_extern(_ nameWord0: Int32, _ nameWord1: Int32, _ nameWord2: Int32, _ body: Int32) -> Void
#else
fileprivate func bjs_benchmarkRunner_extern(_ nameBytes: Int32, _ nameLength: Int32, _ body: Int32) -> Void {
fileprivate func bjs_benchmarkRunner_extern(_ nameWord0: Int32, _ nameWord1: Int32, _ nameWord2: Int32, _ body: Int32) -> Void {
fatalError("Only available on WebAssembly")
}
#endif
@inline(never) fileprivate func bjs_benchmarkRunner(_ nameBytes: Int32, _ nameLength: Int32, _ body: Int32) -> Void {
return bjs_benchmarkRunner_extern(nameBytes, nameLength, body)
@inline(never) fileprivate func bjs_benchmarkRunner(_ nameWord0: Int32, _ nameWord1: Int32, _ nameWord2: Int32, _ body: Int32) -> Void {
return bjs_benchmarkRunner_extern(nameWord0, nameWord1, nameWord2, body)
}

func _$benchmarkRunner(_ name: String, _ body: JSObject) throws(JSException) -> Void {
name.bridgeJSWithLoweredParameter { (nameBytes, nameLength) in
name.bridgeJSWithLoweredParameter { (nameWord0, nameWord1, nameWord2) in
let bodyValue = body.bridgeJSLowerParameter()
bjs_benchmarkRunner(nameBytes, nameLength, bodyValue)
bjs_benchmarkRunner(nameWord0, nameWord1, nameWord2, bodyValue)
}
if let error = _swift_js_take_exception() {
throw error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,20 @@ func _$createTS2Swift() throws(JSException) -> TS2Swift {

#if arch(wasm32)
@_extern(wasm, module: "PlayBridgeJS", name: "bjs_TS2Swift_convert")
fileprivate func bjs_TS2Swift_convert_extern(_ self: Int32, _ tsBytes: Int32, _ tsLength: Int32) -> Int32
fileprivate func bjs_TS2Swift_convert_extern(_ self: Int32, _ tsWord0: Int32, _ tsWord1: Int32, _ tsWord2: Int32) -> Int32
#else
fileprivate func bjs_TS2Swift_convert_extern(_ self: Int32, _ tsBytes: Int32, _ tsLength: Int32) -> Int32 {
fileprivate func bjs_TS2Swift_convert_extern(_ self: Int32, _ tsWord0: Int32, _ tsWord1: Int32, _ tsWord2: Int32) -> Int32 {
fatalError("Only available on WebAssembly")
}
#endif
@inline(never) fileprivate func bjs_TS2Swift_convert(_ self: Int32, _ tsBytes: Int32, _ tsLength: Int32) -> Int32 {
return bjs_TS2Swift_convert_extern(self, tsBytes, tsLength)
@inline(never) fileprivate func bjs_TS2Swift_convert(_ self: Int32, _ tsWord0: Int32, _ tsWord1: Int32, _ tsWord2: Int32) -> Int32 {
return bjs_TS2Swift_convert_extern(self, tsWord0, tsWord1, tsWord2)
}

func _$TS2Swift_convert(_ self: JSObject, _ ts: String) throws(JSException) -> String {
let ret0 = ts.bridgeJSWithLoweredParameter { (tsBytes, tsLength) in
let ret0 = ts.bridgeJSWithLoweredParameter { (tsWord0, tsWord1, tsWord2) in
let selfValue = self.bridgeJSLowerParameter()
let ret = bjs_TS2Swift_convert(selfValue, tsBytes, tsLength)
let ret = bjs_TS2Swift_convert(selfValue, tsWord0, tsWord1, tsWord2)
return ret
}
let ret = ret0
Expand Down
5 changes: 3 additions & 2 deletions Plugins/BridgeJS/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ The ABI will not be stable, and not meant to be interposed by other tools.
Parameter passing follows Wasm calling conventions, with custom handling for complex types:

- **Primitives**: Passed directly as Wasm arguments (`i32`, `i64`, `f32`, `f64`)
- **Strings**: UTF-8 bytes stored in `swift.memory`, ID + length passed as Wasm arguments
- **Strings (JS → Swift)**: UTF-8 bytes stored in `swift.memory`, ID + length passed as Wasm arguments
- **Strings (Swift → JS)**: the wasm32 12-byte `String` layout passed as three `i32` words. JS fast-decodes small strings, caches large immortals by a 53-bit `ptr + len` pack when `len < 2**21`, and otherwise reads UTF-8 at `word1 + 20` (`nativeBias`). This layout is a Swift stdlib implementation detail and can change with the toolchain.
- **Swift Classes**: Raw Swift heap pointer passed as `i32`
- **JSObjects**: Object stored in `swift.memory.heap`, object ID passed as `i32`
- **Structs/Arrays**: Fields/elements pushed to type-specific stacks, Swift pops in reverse order
Expand All @@ -150,7 +151,7 @@ Parameter passing follows Wasm calling conventions, with custom handling for com
Return values use direct Wasm returns for primitives, and imported intrinsic functions for complex types:

- **Primitives**: Returned directly via Wasm return value
- **Strings**: Swift writes UTF-8 bytes to shared memory, JS decodes
- **Strings**: Swift passes the 12-byte `String` layout; JS decodes (small / immortal-cached / UTF-8)
- **Swift Classes**: Pointer returned directly, JS wraps in `SwiftHeapObject` with `FinalizationRegistry`
- **Structs/Arrays**: Swift pushes fields/elements to type-specific stacks, JS reconstructs

Expand Down
4 changes: 1 addition & 3 deletions Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,7 @@ public class ExportSwift {
/// public func _bjs_Greeter_greet(pointer: UnsafeMutableRawPointer) -> Void {
/// let _self = Unmanaged<Greeter>.fromOpaque(pointer).takeUnretainedValue()
/// var ret = _self.greet()
/// return ret.withUTF8 { ptr in
/// _swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
/// }
/// return ret.bridgeJSLowerReturn()
/// }
/// @_expose(wasm, "bjs_Greeter_deinit")
/// @_cdecl("bjs_Greeter_deinit")
Expand Down
2 changes: 1 addition & 1 deletion Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ extension BridgeType {
static let float = LoweringParameterInfo(loweredParameters: [("value", .f32)])
static let double = LoweringParameterInfo(loweredParameters: [("value", .f64)])
static let string = LoweringParameterInfo(
loweredParameters: [("bytes", .i32), ("length", .i32)],
loweredParameters: [("word0", .i32), ("word1", .i32), ("word2", .i32)],
useBorrowing: true
)
static let jsObject = LoweringParameterInfo(loweredParameters: [("value", .i32)])
Expand Down
Loading
Loading