Skip to content

Commit 70b66c2

Browse files
Merge pull request #389 from swiftwasm/yt/fix-bridgejs-test-xcode
BridgeJS: Move intrinsic declarations to JavaScriptKit under SPI
2 parents 4157c74 + 02b5c26 commit 70b66c2

Some content is hidden

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

46 files changed

+328
-616
lines changed

Benchmarks/Sources/Generated/BridgeJS.ExportSwift.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,7 @@
44
// To update this file, just rebuild your project or run
55
// `swift package bridge-js`.
66

7-
@_spi(JSObject_id) import JavaScriptKit
8-
9-
#if arch(wasm32)
10-
@_extern(wasm, module: "bjs", name: "return_string")
11-
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
12-
@_extern(wasm, module: "bjs", name: "init_memory")
13-
private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?)
14-
15-
@_extern(wasm, module: "bjs", name: "swift_js_retain")
16-
private func _swift_js_retain(_ ptr: Int32) -> Int32
17-
@_extern(wasm, module: "bjs", name: "swift_js_throw")
18-
private func _swift_js_throw(_ id: Int32)
19-
#endif
7+
@_spi(BridgeJS) import JavaScriptKit
208

219
@_expose(wasm, "bjs_run")
2210
@_cdecl("bjs_run")

Benchmarks/Sources/Generated/BridgeJS.ImportTS.swift

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,7 @@
44
// To update this file, just rebuild your project or run
55
// `swift package bridge-js`.
66

7-
@_spi(JSObject_id) import JavaScriptKit
8-
9-
#if arch(wasm32)
10-
@_extern(wasm, module: "bjs", name: "make_jsstring")
11-
func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32
12-
#else
13-
func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32 {
14-
fatalError("Only available on WebAssembly")
15-
}
16-
#endif
17-
18-
#if arch(wasm32)
19-
@_extern(wasm, module: "bjs", name: "init_memory_with_result")
20-
func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
21-
#else
22-
func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
23-
fatalError("Only available on WebAssembly")
24-
}
25-
#endif
7+
@_spi(BridgeJS) import JavaScriptKit
268

279
func benchmarkHelperNoop() -> Void {
2810
#if arch(wasm32)
@@ -59,7 +41,7 @@ func benchmarkRunner(_ name: String, _ body: JSObject) -> Void {
5941
#endif
6042
var name = name
6143
let nameId = name.withUTF8 { b in
62-
_make_jsstring(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
44+
_swift_js_make_js_string(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
6345
}
6446
bjs_benchmarkRunner(nameId, Int32(bitPattern: body.id))
6547
}

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

Lines changed: 53 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,65 @@
44
// To update this file, just rebuild your project or run
55
// `swift package bridge-js`.
66

7-
@_spi(JSObject_id) import JavaScriptKit
7+
@_spi(BridgeJS) import JavaScriptKit
88

9-
#if arch(wasm32)
10-
@_extern(wasm, module: "bjs", name: "return_string")
11-
private func _return_string(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
12-
@_extern(wasm, module: "bjs", name: "init_memory")
13-
private func _init_memory(_ sourceId: Int32, _ ptr: UnsafeMutablePointer<UInt8>?)
9+
@_expose(wasm, "bjs_PlayBridgeJS_init")
10+
@_cdecl("bjs_PlayBridgeJS_init")
11+
public func _bjs_PlayBridgeJS_init() -> UnsafeMutableRawPointer {
12+
#if arch(wasm32)
13+
let ret = PlayBridgeJS()
14+
return Unmanaged.passRetained(ret).toOpaque()
15+
#else
16+
fatalError("Only available on WebAssembly")
17+
#endif
18+
}
1419

15-
@_extern(wasm, module: "bjs", name: "swift_js_retain")
16-
private func _swift_js_retain(_ ptr: Int32) -> Int32
17-
@_extern(wasm, module: "bjs", name: "swift_js_throw")
18-
private func _swift_js_throw(_ id: Int32)
19-
#endif
20+
@_expose(wasm, "bjs_PlayBridgeJS_update")
21+
@_cdecl("bjs_PlayBridgeJS_update")
22+
public func _bjs_PlayBridgeJS_update(_self: UnsafeMutableRawPointer, swiftSourceBytes: Int32, swiftSourceLen: Int32, dtsSourceBytes: Int32, dtsSourceLen: Int32) -> UnsafeMutableRawPointer {
23+
#if arch(wasm32)
24+
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()
35+
} catch let error {
36+
if let error = error.thrownValue.object {
37+
withExtendedLifetime(error) {
38+
_swift_js_throw(Int32(bitPattern: $0.id))
39+
}
40+
} else {
41+
let jsError = JSError(message: String(describing: error))
42+
withExtendedLifetime(jsError.jsObject) {
43+
_swift_js_throw(Int32(bitPattern: $0.id))
44+
}
45+
}
46+
return UnsafeMutableRawPointer(bitPattern: -1).unsafelyUnwrapped
47+
}
48+
#else
49+
fatalError("Only available on WebAssembly")
50+
#endif
51+
}
52+
53+
@_expose(wasm, "bjs_PlayBridgeJS_deinit")
54+
@_cdecl("bjs_PlayBridgeJS_deinit")
55+
public func _bjs_PlayBridgeJS_deinit(pointer: UnsafeMutableRawPointer) {
56+
Unmanaged<PlayBridgeJS>.fromOpaque(pointer).release()
57+
}
2058

2159
@_expose(wasm, "bjs_PlayBridgeJSOutput_outputJs")
2260
@_cdecl("bjs_PlayBridgeJSOutput_outputJs")
2361
public func _bjs_PlayBridgeJSOutput_outputJs(_self: UnsafeMutableRawPointer) -> Void {
2462
#if arch(wasm32)
2563
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().outputJs()
2664
return ret.withUTF8 { ptr in
27-
_return_string(ptr.baseAddress, Int32(ptr.count))
65+
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
2866
}
2967
#else
3068
fatalError("Only available on WebAssembly")
@@ -37,7 +75,7 @@ public func _bjs_PlayBridgeJSOutput_outputDts(_self: UnsafeMutableRawPointer) ->
3775
#if arch(wasm32)
3876
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().outputDts()
3977
return ret.withUTF8 { ptr in
40-
_return_string(ptr.baseAddress, Int32(ptr.count))
78+
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
4179
}
4280
#else
4381
fatalError("Only available on WebAssembly")
@@ -50,7 +88,7 @@ public func _bjs_PlayBridgeJSOutput_importSwiftGlue(_self: UnsafeMutableRawPoint
5088
#if arch(wasm32)
5189
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().importSwiftGlue()
5290
return ret.withUTF8 { ptr in
53-
_return_string(ptr.baseAddress, Int32(ptr.count))
91+
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
5492
}
5593
#else
5694
fatalError("Only available on WebAssembly")
@@ -63,7 +101,7 @@ public func _bjs_PlayBridgeJSOutput_exportSwiftGlue(_self: UnsafeMutableRawPoint
63101
#if arch(wasm32)
64102
var ret = Unmanaged<PlayBridgeJSOutput>.fromOpaque(_self).takeUnretainedValue().exportSwiftGlue()
65103
return ret.withUTF8 { ptr in
66-
_return_string(ptr.baseAddress, Int32(ptr.count))
104+
_swift_js_return_string(ptr.baseAddress, Int32(ptr.count))
67105
}
68106
#else
69107
fatalError("Only available on WebAssembly")
@@ -74,54 +112,4 @@ public func _bjs_PlayBridgeJSOutput_exportSwiftGlue(_self: UnsafeMutableRawPoint
74112
@_cdecl("bjs_PlayBridgeJSOutput_deinit")
75113
public func _bjs_PlayBridgeJSOutput_deinit(pointer: UnsafeMutableRawPointer) {
76114
Unmanaged<PlayBridgeJSOutput>.fromOpaque(pointer).release()
77-
}
78-
79-
@_expose(wasm, "bjs_PlayBridgeJS_init")
80-
@_cdecl("bjs_PlayBridgeJS_init")
81-
public func _bjs_PlayBridgeJS_init() -> UnsafeMutableRawPointer {
82-
#if arch(wasm32)
83-
let ret = PlayBridgeJS()
84-
return Unmanaged.passRetained(ret).toOpaque()
85-
#else
86-
fatalError("Only available on WebAssembly")
87-
#endif
88-
}
89-
90-
@_expose(wasm, "bjs_PlayBridgeJS_update")
91-
@_cdecl("bjs_PlayBridgeJS_update")
92-
public func _bjs_PlayBridgeJS_update(_self: UnsafeMutableRawPointer, swiftSourceBytes: Int32, swiftSourceLen: Int32, dtsSourceBytes: Int32, dtsSourceLen: Int32) -> UnsafeMutableRawPointer {
93-
#if arch(wasm32)
94-
do {
95-
let swiftSource = String(unsafeUninitializedCapacity: Int(swiftSourceLen)) { b in
96-
_init_memory(swiftSourceBytes, b.baseAddress.unsafelyUnwrapped)
97-
return Int(swiftSourceLen)
98-
}
99-
let dtsSource = String(unsafeUninitializedCapacity: Int(dtsSourceLen)) { b in
100-
_init_memory(dtsSourceBytes, b.baseAddress.unsafelyUnwrapped)
101-
return Int(dtsSourceLen)
102-
}
103-
let ret = try Unmanaged<PlayBridgeJS>.fromOpaque(_self).takeUnretainedValue().update(swiftSource: swiftSource, dtsSource: dtsSource)
104-
return Unmanaged.passRetained(ret).toOpaque()
105-
} catch let error {
106-
if let error = error.thrownValue.object {
107-
withExtendedLifetime(error) {
108-
_swift_js_throw(Int32(bitPattern: $0.id))
109-
}
110-
} else {
111-
let jsError = JSError(message: String(describing: error))
112-
withExtendedLifetime(jsError.jsObject) {
113-
_swift_js_throw(Int32(bitPattern: $0.id))
114-
}
115-
}
116-
return UnsafeMutableRawPointer(bitPattern: -1).unsafelyUnwrapped
117-
}
118-
#else
119-
fatalError("Only available on WebAssembly")
120-
#endif
121-
}
122-
123-
@_expose(wasm, "bjs_PlayBridgeJS_deinit")
124-
@_cdecl("bjs_PlayBridgeJS_deinit")
125-
public func _bjs_PlayBridgeJS_deinit(pointer: UnsafeMutableRawPointer) {
126-
Unmanaged<PlayBridgeJS>.fromOpaque(pointer).release()
127115
}

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,7 @@
44
// To update this file, just rebuild your project or run
55
// `swift package bridge-js`.
66

7-
@_spi(JSObject_id) import JavaScriptKit
8-
9-
#if arch(wasm32)
10-
@_extern(wasm, module: "bjs", name: "make_jsstring")
11-
func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32
12-
#else
13-
func _make_jsstring(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) -> Int32 {
14-
fatalError("Only available on WebAssembly")
15-
}
16-
#endif
17-
18-
#if arch(wasm32)
19-
@_extern(wasm, module: "bjs", name: "init_memory_with_result")
20-
func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32)
21-
#else
22-
func _init_memory_with_result(_ ptr: UnsafePointer<UInt8>?, _ len: Int32) {
23-
fatalError("Only available on WebAssembly")
24-
}
25-
#endif
7+
@_spi(BridgeJS) import JavaScriptKit
268

279
func createTS2Skeleton() -> TS2Skeleton {
2810
#if arch(wasm32)
@@ -59,11 +41,11 @@ struct TS2Skeleton {
5941
#endif
6042
var ts = ts
6143
let tsId = ts.withUTF8 { b in
62-
_make_jsstring(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
44+
_swift_js_make_js_string(b.baseAddress.unsafelyUnwrapped, Int32(b.count))
6345
}
6446
let ret = bjs_TS2Skeleton_convert(Int32(bitPattern: self.this.id), tsId)
6547
return String(unsafeUninitializedCapacity: Int(ret)) { b in
66-
_init_memory_with_result(b.baseAddress.unsafelyUnwrapped, Int32(ret))
48+
_swift_js_init_memory_with_result(b.baseAddress.unsafelyUnwrapped, Int32(ret))
6749
return Int(ret)
6850
}
6951
}

Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/JavaScript/BridgeJS.ExportSwift.json

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
{
22
"classes" : [
3+
{
4+
"constructor" : {
5+
"abiName" : "bjs_PlayBridgeJS_init",
6+
"effects" : {
7+
"isAsync" : false,
8+
"isThrows" : false
9+
},
10+
"parameters" : [
11+
12+
]
13+
},
14+
"methods" : [
15+
{
16+
"abiName" : "bjs_PlayBridgeJS_update",
17+
"effects" : {
18+
"isAsync" : false,
19+
"isThrows" : true
20+
},
21+
"name" : "update",
22+
"parameters" : [
23+
{
24+
"label" : "swiftSource",
25+
"name" : "swiftSource",
26+
"type" : {
27+
"string" : {
28+
29+
}
30+
}
31+
},
32+
{
33+
"label" : "dtsSource",
34+
"name" : "dtsSource",
35+
"type" : {
36+
"string" : {
37+
38+
}
39+
}
40+
}
41+
],
42+
"returnType" : {
43+
"swiftHeapObject" : {
44+
"_0" : "PlayBridgeJSOutput"
45+
}
46+
}
47+
}
48+
],
49+
"name" : "PlayBridgeJS"
50+
},
351
{
452
"methods" : [
553
{
@@ -68,54 +116,6 @@
68116
}
69117
],
70118
"name" : "PlayBridgeJSOutput"
71-
},
72-
{
73-
"constructor" : {
74-
"abiName" : "bjs_PlayBridgeJS_init",
75-
"effects" : {
76-
"isAsync" : false,
77-
"isThrows" : false
78-
},
79-
"parameters" : [
80-
81-
]
82-
},
83-
"methods" : [
84-
{
85-
"abiName" : "bjs_PlayBridgeJS_update",
86-
"effects" : {
87-
"isAsync" : false,
88-
"isThrows" : true
89-
},
90-
"name" : "update",
91-
"parameters" : [
92-
{
93-
"label" : "swiftSource",
94-
"name" : "swiftSource",
95-
"type" : {
96-
"string" : {
97-
98-
}
99-
}
100-
},
101-
{
102-
"label" : "dtsSource",
103-
"name" : "dtsSource",
104-
"type" : {
105-
"string" : {
106-
107-
}
108-
}
109-
}
110-
],
111-
"returnType" : {
112-
"swiftHeapObject" : {
113-
"_0" : "PlayBridgeJSOutput"
114-
}
115-
}
116-
}
117-
],
118-
"name" : "PlayBridgeJS"
119119
}
120120
],
121121
"functions" : [

Package.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,14 @@ let package = Package(
4040
? [
4141
.unsafeFlags(["-fdeclspec"])
4242
] : nil,
43-
swiftSettings: shouldBuildForEmbedded
44-
? [
45-
.enableExperimentalFeature("Embedded"),
46-
.enableExperimentalFeature("Extern"),
47-
.unsafeFlags(["-Xfrontend", "-emit-empty-object-file"]),
48-
] : nil
43+
swiftSettings: [
44+
.enableExperimentalFeature("Extern")
45+
]
46+
+ (shouldBuildForEmbedded
47+
? [
48+
.enableExperimentalFeature("Embedded"),
49+
.unsafeFlags(["-Xfrontend", "-emit-empty-object-file"]),
50+
] : [])
4951
),
5052
.target(name: "_CJavaScriptKit"),
5153
.testTarget(

0 commit comments

Comments
 (0)