Skip to content

Commit cb3ea6c

Browse files
Merge pull request #423 from swiftwasm/yt/doc-bridgejs
BridgeJS: Standardize lift/lower pattern across Swift and JavaScript
2 parents 3c8726e + 07c1ef7 commit cb3ea6c

File tree

56 files changed

+1993
-1578
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1993
-1578
lines changed

Benchmarks/Sources/Generated/BridgeJS.ImportTS.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func benchmarkHelperNoopWithNumber(_ n: Double) throws(JSException) -> Void {
3030
fatalError("Only available on WebAssembly")
3131
}
3232
#endif
33-
bjs_benchmarkHelperNoopWithNumber(n)
33+
bjs_benchmarkHelperNoopWithNumber(n.bridgeJSLowerParameter())
3434
if let error = _swift_js_take_exception() {
3535
throw error
3636
}
@@ -45,11 +45,7 @@ func benchmarkRunner(_ name: String, _ body: JSObject) throws(JSException) -> Vo
4545
fatalError("Only available on WebAssembly")
4646
}
4747
#endif
48-
var name = name
49-
let nameId = name.withUTF8 { b in
50-
_swift_js_make_js_string(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
51-
}
52-
bjs_benchmarkRunner(nameId, Int32(bitPattern: body.id))
48+
bjs_benchmarkRunner(name.bridgeJSLowerParameter(), body.bridgeJSLowerParameter())
5349
if let error = _swift_js_take_exception() {
5450
throw error
5551
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../../Plugins/BridgeJS/Sources/BridgeJSUtilities

Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.ExportSwift.swift

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,19 @@
1111
public func _bjs_PlayBridgeJS_init() -> UnsafeMutableRawPointer {
1212
#if arch(wasm32)
1313
let ret = PlayBridgeJS()
14-
return Unmanaged.passRetained(ret).toOpaque()
14+
return ret.bridgeJSLowerReturn()
1515
#else
1616
fatalError("Only available on WebAssembly")
1717
#endif
1818
}
1919

2020
@_expose(wasm, "bjs_PlayBridgeJS_update")
2121
@_cdecl("bjs_PlayBridgeJS_update")
22-
public func _bjs_PlayBridgeJS_update(_self: UnsafeMutableRawPointer, swiftSourceBytes: Int32, swiftSourceLen: Int32, dtsSourceBytes: Int32, dtsSourceLen: Int32) -> UnsafeMutableRawPointer {
22+
public func _bjs_PlayBridgeJS_update(_self: UnsafeMutableRawPointer, swiftSourceBytes: Int32, swiftSourceLength: Int32, dtsSourceBytes: Int32, dtsSourceLength: Int32) -> UnsafeMutableRawPointer {
2323
#if arch(wasm32)
2424
do {
25-
let swiftSource = String(unsafeUninitializedCapacity: Int(swiftSourceLen)) { b in
26-
_swift_js_init_memory(swiftSourceBytes, b.baseAddress.unsafelyUnwrapped)
27-
return Int(swiftSourceLen)
28-
}
29-
let dtsSource = String(unsafeUninitializedCapacity: Int(dtsSourceLen)) { b in
30-
_swift_js_init_memory(dtsSourceBytes, b.baseAddress.unsafelyUnwrapped)
31-
return Int(dtsSourceLen)
32-
}
33-
let ret = try Unmanaged<PlayBridgeJS>.fromOpaque(_self).takeUnretainedValue().update(swiftSource: swiftSource, dtsSource: dtsSource)
34-
return Unmanaged.passRetained(ret).toOpaque()
25+
let ret = try PlayBridgeJS.bridgeJSLiftParameter(_self).update(swiftSource: String.bridgeJSLiftParameter(swiftSourceBytes, swiftSourceLength), dtsSource: String.bridgeJSLiftParameter(dtsSourceBytes, dtsSourceLength))
26+
return ret.bridgeJSLowerReturn()
3527
} catch let error {
3628
if let error = error.thrownValue.object {
3729
withExtendedLifetime(error) {
@@ -56,10 +48,16 @@ public func _bjs_PlayBridgeJS_deinit(pointer: UnsafeMutableRawPointer) {
5648
Unmanaged<PlayBridgeJS>.fromOpaque(pointer).release()
5749
}
5850

59-
extension PlayBridgeJS: ConvertibleToJSValue {
51+
extension PlayBridgeJS: ConvertibleToJSValue, _BridgedSwiftHeapObject {
6052
var jsValue: JSValue {
53+
#if arch(wasm32)
6154
@_extern(wasm, module: "PlayBridgeJS", name: "bjs_PlayBridgeJS_wrap")
6255
func _bjs_PlayBridgeJS_wrap(_: UnsafeMutableRawPointer) -> Int32
56+
#else
57+
func _bjs_PlayBridgeJS_wrap(_: UnsafeMutableRawPointer) -> Int32 {
58+
fatalError("Only available on WebAssembly")
59+
}
60+
#endif
6361
return .object(JSObject(id: UInt32(bitPattern: _bjs_PlayBridgeJS_wrap(Unmanaged.passRetained(self).toOpaque()))))
6462
}
6563
}
@@ -68,10 +66,8 @@ extension PlayBridgeJS: ConvertibleToJSValue {
6866
@_cdecl("bjs_PlayBridgeJSOutput_outputJs")
6967
public func _bjs_PlayBridgeJSOutput_outputJs(_self: UnsafeMutableRawPointer) -> Void {
7068
#if arch(wasm32)
71-
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().outputJs()
72-
return ret.withUTF8 { ptr in
73-
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
74-
}
69+
let ret = PlayBridgeJSOutput.bridgeJSLiftParameter(_self).outputJs()
70+
return ret.bridgeJSLowerReturn()
7571
#else
7672
fatalError("Only available on WebAssembly")
7773
#endif
@@ -81,10 +77,8 @@ public func _bjs_PlayBridgeJSOutput_outputJs(_self: UnsafeMutableRawPointer) ->
8177
@_cdecl("bjs_PlayBridgeJSOutput_outputDts")
8278
public func _bjs_PlayBridgeJSOutput_outputDts(_self: UnsafeMutableRawPointer) -> Void {
8379
#if arch(wasm32)
84-
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().outputDts()
85-
return ret.withUTF8 { ptr in
86-
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
87-
}
80+
let ret = PlayBridgeJSOutput.bridgeJSLiftParameter(_self).outputDts()
81+
return ret.bridgeJSLowerReturn()
8882
#else
8983
fatalError("Only available on WebAssembly")
9084
#endif
@@ -94,10 +88,8 @@ public func _bjs_PlayBridgeJSOutput_outputDts(_self: UnsafeMutableRawPointer) ->
9488
@_cdecl("bjs_PlayBridgeJSOutput_importSwiftGlue")
9589
public func _bjs_PlayBridgeJSOutput_importSwiftGlue(_self: UnsafeMutableRawPointer) -> Void {
9690
#if arch(wasm32)
97-
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().importSwiftGlue()
98-
return ret.withUTF8 { ptr in
99-
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
100-
}
91+
let ret = PlayBridgeJSOutput.bridgeJSLiftParameter(_self).importSwiftGlue()
92+
return ret.bridgeJSLowerReturn()
10193
#else
10294
fatalError("Only available on WebAssembly")
10395
#endif
@@ -107,10 +99,8 @@ public func _bjs_PlayBridgeJSOutput_importSwiftGlue(_self: UnsafeMutableRawPoint
10799
@_cdecl("bjs_PlayBridgeJSOutput_exportSwiftGlue")
108100
public func _bjs_PlayBridgeJSOutput_exportSwiftGlue(_self: UnsafeMutableRawPointer) -> Void {
109101
#if arch(wasm32)
110-
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().exportSwiftGlue()
111-
return ret.withUTF8 { ptr in
112-
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
113-
}
102+
let ret = PlayBridgeJSOutput.bridgeJSLiftParameter(_self).exportSwiftGlue()
103+
return ret.bridgeJSLowerReturn()
114104
#else
115105
fatalError("Only available on WebAssembly")
116106
#endif
@@ -122,10 +112,16 @@ public func _bjs_PlayBridgeJSOutput_deinit(pointer: UnsafeMutableRawPointer) {
122112
Unmanaged<PlayBridgeJSOutput>.fromOpaque(pointer).release()
123113
}
124114

125-
extension PlayBridgeJSOutput: ConvertibleToJSValue {
115+
extension PlayBridgeJSOutput: ConvertibleToJSValue, _BridgedSwiftHeapObject {
126116
var jsValue: JSValue {
117+
#if arch(wasm32)
127118
@_extern(wasm, module: "PlayBridgeJS", name: "bjs_PlayBridgeJSOutput_wrap")
128119
func _bjs_PlayBridgeJSOutput_wrap(_: UnsafeMutableRawPointer) -> Int32
120+
#else
121+
func _bjs_PlayBridgeJSOutput_wrap(_: UnsafeMutableRawPointer) -> Int32 {
122+
fatalError("Only available on WebAssembly")
123+
}
124+
#endif
129125
return .object(JSObject(id: UInt32(bitPattern: _bjs_PlayBridgeJSOutput_wrap(Unmanaged.passRetained(self).toOpaque()))))
130126
}
131127
}

Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.ImportTS.swift

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@ func createTS2Skeleton() throws(JSException) -> TS2Skeleton {
1919
if let error = _swift_js_take_exception() {
2020
throw error
2121
}
22-
return TS2Skeleton(takingThis: ret)
22+
return TS2Skeleton.bridgeJSLiftReturn(ret)
2323
}
2424

25-
struct TS2Skeleton {
26-
let this: JSObject
25+
struct TS2Skeleton: _JSBridgedClass {
26+
let jsObject: JSObject
2727

28-
init(this: JSObject) {
29-
self.this = this
30-
}
31-
32-
init(takingThis this: Int32) {
33-
self.this = JSObject(id: UInt32(bitPattern: this))
28+
init(unsafelyWrapping jsObject: JSObject) {
29+
self.jsObject = jsObject
3430
}
3531

3632
func convert(_ ts: String) throws(JSException) -> String {
@@ -42,18 +38,11 @@ struct TS2Skeleton {
4238
fatalError("Only available on WebAssembly")
4339
}
4440
#endif
45-
var ts = ts
46-
let tsId = ts.withUTF8 { b in
47-
_swift_js_make_js_string(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
48-
}
49-
let ret = bjs_TS2Skeleton_convert(Int32(bitPattern: self.this.id), tsId)
41+
let ret = bjs_TS2Skeleton_convert(self.bridgeJSLowerParameter(), ts.bridgeJSLowerParameter())
5042
if let error = _swift_js_take_exception() {
5143
throw error
5244
}
53-
return String(unsafeUninitializedCapacity: Int(ret)) { b in
54-
_swift_js_init_memory_with_result(b.baseAddress.unsafelyUnwrapped, Int32(ret))
55-
return Int(ret)
56-
}
45+
return String.bridgeJSLiftReturn(ret)
5746
}
5847

5948
}

Plugins/BridgeJS/Package.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,22 @@ let package = Package(
2929
name: "BridgeJSCore",
3030
dependencies: [
3131
"BridgeJSSkeleton",
32+
"BridgeJSUtilities",
3233
.product(name: "SwiftParser", package: "swift-syntax"),
3334
.product(name: "SwiftSyntax", package: "swift-syntax"),
3435
.product(name: "SwiftBasicFormat", package: "swift-syntax"),
3536
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
3637
]
3738
),
3839
.target(name: "BridgeJSSkeleton"),
40+
.target(name: "BridgeJSUtilities"),
3941

4042
.target(
4143
name: "BridgeJSLink",
42-
dependencies: ["BridgeJSSkeleton"]
44+
dependencies: [
45+
"BridgeJSSkeleton",
46+
"BridgeJSUtilities",
47+
]
4348
),
4449

4550
.testTarget(

0 commit comments

Comments
 (0)