Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 305ca67

Browse filesBrowse files
BridgeJS: Gate @_extern/@expose usage behind arch(wasm32)
1 parent c090528 commit 305ca67
Copy full SHA for 305ca67

20 files changed

+530-51Lines changed: 530 additions & 51 deletions
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Plugins/BridgeJS/Sources/BridgeJSTool/ExportSwift.swift‎

Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Sources/BridgeJSTool/ExportSwift.swift
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ class ExportSwift {
272272
273273
@_spi(JSObject_id) import JavaScriptKit
274274
275+
#if arch(wasm32)
275276
@_extern(wasm, module: "bjs", name: "return_string")
276277
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
277278
@_extern(wasm, module: "bjs", name: "init_memory")
@@ -281,6 +282,7 @@ class ExportSwift {
281282
private func _swift_js_retain(_ ptr: Int32) -> Int32
282283
@_extern(wasm, module: "bjs", name: "swift_js_throw")
283284
private func _swift_js_throw(_ id: Int32)
285+
#endif
284286
"""
285287

286288
func renderSwiftGlue() -> String? {
@@ -512,7 +514,11 @@ class ExportSwift {
512514
@_expose(wasm, "\(raw: abiName)")
513515
@_cdecl("\(raw: abiName)")
514516
public func _\(raw: abiName)(\(raw: parameterSignature())) -> \(raw: returnSignature()) {
517+
#if arch(wasm32)
515518
\(body)
519+
#else
520+
fatalError("Only available on WebAssembly")
521+
#endif
516522
}
517523
"""
518524
}
Collapse file

‎Plugins/BridgeJS/Sources/BridgeJSTool/ImportTS.swift‎

Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Sources/BridgeJSTool/ImportTS.swift
+47-22Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -241,29 +241,42 @@ struct ImportTS {
241241
}
242242

243243
func renderImportDecl() -> DeclSyntax {
244-
return DeclSyntax(
245-
FunctionDeclSyntax(
246-
attributes: AttributeListSyntax(itemsBuilder: {
247-
"@_extern(wasm, module: \"\(raw: moduleName)\", name: \"\(raw: abiName)\")"
248-
}).with(\.trailingTrivia, .newline),
249-
name: .identifier(abiName),
250-
signature: FunctionSignatureSyntax(
251-
parameterClause: FunctionParameterClauseSyntax(parametersBuilder: {
252-
for param in abiParameterSignatures {
253-
FunctionParameterSyntax(
254-
firstName: .wildcardToken(),
255-
secondName: .identifier(param.name),
256-
type: IdentifierTypeSyntax(name: .identifier(param.type.swiftType))
257-
)
258-
}
259-
}),
260-
returnClause: ReturnClauseSyntax(
261-
arrow: .arrowToken(),
262-
type: IdentifierTypeSyntax(name: .identifier(abiReturnType.map { $0.swiftType } ?? "Void"))
263-
)
244+
let baseDecl = FunctionDeclSyntax(
245+
funcKeyword: .keyword(.func).with(\.trailingTrivia, .space),
246+
name: .identifier(abiName),
247+
signature: FunctionSignatureSyntax(
248+
parameterClause: FunctionParameterClauseSyntax(parametersBuilder: {
249+
for param in abiParameterSignatures {
250+
FunctionParameterSyntax(
251+
firstName: .wildcardToken().with(\.trailingTrivia, .space),
252+
secondName: .identifier(param.name),
253+
type: IdentifierTypeSyntax(name: .identifier(param.type.swiftType))
254+
)
255+
}
256+
}),
257+
returnClause: ReturnClauseSyntax(
258+
arrow: .arrowToken(),
259+
type: IdentifierTypeSyntax(name: .identifier(abiReturnType.map { $0.swiftType } ?? "Void"))
264260
)
265261
)
266262
)
263+
var externDecl = baseDecl
264+
externDecl.attributes = AttributeListSyntax(itemsBuilder: {
265+
"@_extern(wasm, module: \"\(raw: moduleName)\", name: \"\(raw: abiName)\")"
266+
}).with(\.trailingTrivia, .newline)
267+
var stubDecl = baseDecl
268+
stubDecl.body = CodeBlockSyntax {
269+
"""
270+
fatalError("Only available on WebAssembly")
271+
"""
272+
}
273+
return """
274+
#if arch(wasm32)
275+
\(externDecl)
276+
#else
277+
\(stubDecl)
278+
#endif
279+
"""
267280
}
268281

269282
func renderThunkDecl(name: String, parameters: [Parameter], returnType: BridgeType) -> DeclSyntax {
@@ -328,11 +341,23 @@ struct ImportTS {
328341
329342
@_spi(JSObject_id) import JavaScriptKit
330343
344+
#if arch(wasm32)
331345
@_extern(wasm, module: "bjs", name: "make_jsstring")
332-
private func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32
346+
func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32
347+
#else
348+
func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32 {
349+
fatalError("Only available on WebAssembly")
350+
}
351+
#endif
333352
353+
#if arch(wasm32)
334354
@_extern(wasm, module: "bjs", name: "init_memory_with_result")
335-
private func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
355+
func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
356+
#else
357+
func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
358+
fatalError("Only available on WebAssembly")
359+
}
360+
#endif
336361
"""
337362

338363
func renderSwiftThunk(
Collapse file

‎Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveParameters.swift‎

Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveParameters.swift
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
@_spi(JSObject_id) import JavaScriptKit
88

9+
#if arch(wasm32)
910
@_extern(wasm, module: "bjs", name: "return_string")
1011
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
1112
@_extern(wasm, module: "bjs", name: "init_memory")
@@ -15,9 +16,14 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?
1516
private func _swift_js_retain(_ ptr: Int32) -> Int32
1617
@_extern(wasm, module: "bjs", name: "swift_js_throw")
1718
private func _swift_js_throw(_ id: Int32)
19+
#endif
1820
1921
@_expose(wasm, "bjs_check")
2022
@_cdecl("bjs_check")
2123
public func _bjs_check(a: Int32, b: Float32, c: Float64, d: Int32) -> Void {
24+
#if arch(wasm32)
2225
check(a: Int(a), b: b, c: c, d: d == 1)
26+
#else
27+
fatalError("Only available on WebAssembly")
28+
#endif
2329
}
Collapse file

‎Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveReturn.swift‎

Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/PrimitiveReturn.swift
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
@_spi(JSObject_id) import JavaScriptKit
88

9+
#if arch(wasm32)
910
@_extern(wasm, module: "bjs", name: "return_string")
1011
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
1112
@_extern(wasm, module: "bjs", name: "init_memory")
@@ -15,31 +16,48 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?
1516
private func _swift_js_retain(_ ptr: Int32) -> Int32
1617
@_extern(wasm, module: "bjs", name: "swift_js_throw")
1718
private func _swift_js_throw(_ id: Int32)
19+
#endif
1820
1921
@_expose(wasm, "bjs_checkInt")
2022
@_cdecl("bjs_checkInt")
2123
public func _bjs_checkInt() -> Int32 {
24+
#if arch(wasm32)
2225
let ret = checkInt()
2326
return Int32(ret)
27+
#else
28+
fatalError("Only available on WebAssembly")
29+
#endif
2430
}
2531

2632
@_expose(wasm, "bjs_checkFloat")
2733
@_cdecl("bjs_checkFloat")
2834
public func _bjs_checkFloat() -> Float32 {
35+
#if arch(wasm32)
2936
let ret = checkFloat()
3037
return Float32(ret)
38+
#else
39+
fatalError("Only available on WebAssembly")
40+
#endif
3141
}
3242

3343
@_expose(wasm, "bjs_checkDouble")
3444
@_cdecl("bjs_checkDouble")
3545
public func _bjs_checkDouble() -> Float64 {
46+
#if arch(wasm32)
3647
let ret = checkDouble()
3748
return Float64(ret)
49+
#else
50+
fatalError("Only available on WebAssembly")
51+
#endif
3852
}
3953

4054
@_expose(wasm, "bjs_checkBool")
4155
@_cdecl("bjs_checkBool")
4256
public func _bjs_checkBool() -> Int32 {
57+
#if arch(wasm32)
4358
let ret = checkBool()
4459
return Int32(ret ? 1 : 0)
60+
#else
61+
fatalError("Only available on WebAssembly")
62+
#endif
4563
}
Collapse file

‎Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringParameter.swift‎

Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringParameter.swift
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
@_spi(JSObject_id) import JavaScriptKit
88

9+
#if arch(wasm32)
910
@_extern(wasm, module: "bjs", name: "return_string")
1011
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
1112
@_extern(wasm, module: "bjs", name: "init_memory")
@@ -15,13 +16,18 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?
1516
private func _swift_js_retain(_ ptr: Int32) -> Int32
1617
@_extern(wasm, module: "bjs", name: "swift_js_throw")
1718
private func _swift_js_throw(_ id: Int32)
19+
#endif
1820
1921
@_expose(wasm, "bjs_checkString")
2022
@_cdecl("bjs_checkString")
2123
public func _bjs_checkString(aBytes: Int32, aLen: Int32) -> Void {
24+
#if arch(wasm32)
2225
let a = String(unsafeUninitializedCapacity: Int(aLen)) { b in
2326
_init_memory(aBytes, b.baseAddress.unsafelyUnwrapped)
2427
return Int(aLen)
2528
}
2629
checkString(a: a)
30+
#else
31+
fatalError("Only available on WebAssembly")
32+
#endif
2733
}
Collapse file

‎Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringReturn.swift‎

Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/StringReturn.swift
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
@_spi(JSObject_id) import JavaScriptKit
88

9+
#if arch(wasm32)
910
@_extern(wasm, module: "bjs", name: "return_string")
1011
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
1112
@_extern(wasm, module: "bjs", name: "init_memory")
@@ -15,12 +16,17 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?
1516
private func _swift_js_retain(_ ptr: Int32) -> Int32
1617
@_extern(wasm, module: "bjs", name: "swift_js_throw")
1718
private func _swift_js_throw(_ id: Int32)
19+
#endif
1820
1921
@_expose(wasm, "bjs_checkString")
2022
@_cdecl("bjs_checkString")
2123
public func _bjs_checkString() -> Void {
24+
#if arch(wasm32)
2225
var ret = checkString()
2326
return ret.withUTF8 { ptr in
2427
_return_string(ptr.baseAddress, Int32(ptr.count))
2528
}
29+
#else
30+
fatalError("Only available on WebAssembly")
31+
#endif
2632
}
Collapse file

‎Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/SwiftClass.swift‎

Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/SwiftClass.swift
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
@_spi(JSObject_id) import JavaScriptKit
88

9+
#if arch(wasm32)
910
@_extern(wasm, module: "bjs", name: "return_string")
1011
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
1112
@_extern(wasm, module: "bjs", name: "init_memory")
@@ -15,41 +16,58 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?
1516
private func _swift_js_retain(_ ptr: Int32) -> Int32
1617
@_extern(wasm, module: "bjs", name: "swift_js_throw")
1718
private func _swift_js_throw(_ id: Int32)
19+
#endif
1820
1921
@_expose(wasm, "bjs_takeGreeter")
2022
@_cdecl("bjs_takeGreeter")
2123
public func _bjs_takeGreeter(greeter: UnsafeMutableRawPointer) -> Void {
24+
#if arch(wasm32)
2225
takeGreeter(greeter: Unmanaged<Greeter>.fromOpaque(greeter).takeUnretainedValue())
26+
#else
27+
fatalError("Only available on WebAssembly")
28+
#endif
2329
}
2430

2531
@_expose(wasm, "bjs_Greeter_init")
2632
@_cdecl("bjs_Greeter_init")
2733
public func _bjs_Greeter_init(nameBytes: Int32, nameLen: Int32) -> UnsafeMutableRawPointer {
34+
#if arch(wasm32)
2835
let name = String(unsafeUninitializedCapacity: Int(nameLen)) { b in
2936
_init_memory(nameBytes, b.baseAddress.unsafelyUnwrapped)
3037
return Int(nameLen)
3138
}
3239
let ret = Greeter(name: name)
3340
return Unmanaged.passRetained(ret).toOpaque()
41+
#else
42+
fatalError("Only available on WebAssembly")
43+
#endif
3444
}
3545

3646
@_expose(wasm, "bjs_Greeter_greet")
3747
@_cdecl("bjs_Greeter_greet")
3848
public func _bjs_Greeter_greet(_self: UnsafeMutableRawPointer) -> Void {
49+
#if arch(wasm32)
3950
var ret = Unmanaged<Greeter>.fromOpaque(_self).takeUnretainedValue().greet()
4051
return ret.withUTF8 { ptr in
4152
_return_string(ptr.baseAddress, Int32(ptr.count))
4253
}
54+
#else
55+
fatalError("Only available on WebAssembly")
56+
#endif
4357
}
4458

4559
@_expose(wasm, "bjs_Greeter_changeName")
4660
@_cdecl("bjs_Greeter_changeName")
4761
public func _bjs_Greeter_changeName(_self: UnsafeMutableRawPointer, nameBytes: Int32, nameLen: Int32) -> Void {
62+
#if arch(wasm32)
4863
let name = String(unsafeUninitializedCapacity: Int(nameLen)) { b in
4964
_init_memory(nameBytes, b.baseAddress.unsafelyUnwrapped)
5065
return Int(nameLen)
5166
}
5267
Unmanaged<Greeter>.fromOpaque(_self).takeUnretainedValue().changeName(name: name)
68+
#else
69+
fatalError("Only available on WebAssembly")
70+
#endif
5371
}
5472

5573
@_expose(wasm, "bjs_Greeter_deinit")
Collapse file

‎Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Throws.swift‎

Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/Throws.swift
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
@_spi(JSObject_id) import JavaScriptKit
88

9+
#if arch(wasm32)
910
@_extern(wasm, module: "bjs", name: "return_string")
1011
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
1112
@_extern(wasm, module: "bjs", name: "init_memory")
@@ -15,10 +16,12 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?
1516
private func _swift_js_retain(_ ptr: Int32) -> Int32
1617
@_extern(wasm, module: "bjs", name: "swift_js_throw")
1718
private func _swift_js_throw(_ id: Int32)
19+
#endif
1820
1921
@_expose(wasm, "bjs_throwsSomething")
2022
@_cdecl("bjs_throwsSomething")
2123
public func _bjs_throwsSomething() -> Void {
24+
#if arch(wasm32)
2225
do {
2326
try throwsSomething()
2427
} catch let error {
@@ -34,4 +37,7 @@ public func _bjs_throwsSomething() -> Void {
3437
}
3538
return
3639
}
40+
#else
41+
fatalError("Only available on WebAssembly")
42+
#endif
3743
}
Collapse file

‎Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/VoidParameterVoidReturn.swift‎

Copy file name to clipboardExpand all lines: Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/ExportSwiftTests/VoidParameterVoidReturn.swift
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
@_spi(JSObject_id) import JavaScriptKit
88

9+
#if arch(wasm32)
910
@_extern(wasm, module: "bjs", name: "return_string")
1011
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
1112
@_extern(wasm, module: "bjs", name: "init_memory")
@@ -15,9 +16,14 @@ private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?
1516
private func _swift_js_retain(_ ptr: Int32) -> Int32
1617
@_extern(wasm, module: "bjs", name: "swift_js_throw")
1718
private func _swift_js_throw(_ id: Int32)
19+
#endif
1820
1921
@_expose(wasm, "bjs_check")
2022
@_cdecl("bjs_check")
2123
public func _bjs_check() -> Void {
24+
#if arch(wasm32)
2225
check()
26+
#else
27+
fatalError("Only available on WebAssembly")
28+
#endif
2329
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.