Skip to content

Commit 2e71e20

Browse files
BridgeJS: Stablize the order of exported classs in the generated files
1 parent 8df0593 commit 2e71e20

File tree

4 files changed

+109
-104
lines changed

4 files changed

+109
-104
lines changed

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

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,56 @@
66

77
@_spi(BridgeJS) import JavaScriptKit
88

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+
}
19+
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+
}
58+
959
@_expose(wasm, "bjs_PlayBridgeJSOutput_outputJs")
1060
@_cdecl("bjs_PlayBridgeJSOutput_outputJs")
1161
public func _bjs_PlayBridgeJSOutput_outputJs(_self: UnsafeMutableRawPointer) -> Void {
@@ -62,54 +112,4 @@ public func _bjs_PlayBridgeJSOutput_exportSwiftGlue(_self: UnsafeMutableRawPoint
62112
@_cdecl("bjs_PlayBridgeJSOutput_deinit")
63113
public func _bjs_PlayBridgeJSOutput_deinit(pointer: UnsafeMutableRawPointer) {
64114
Unmanaged<PlayBridgeJSOutput>.fromOpaque(pointer).release()
65-
}
66-
67-
@_expose(wasm, "bjs_PlayBridgeJS_init")
68-
@_cdecl("bjs_PlayBridgeJS_init")
69-
public func _bjs_PlayBridgeJS_init() -> UnsafeMutableRawPointer {
70-
#if arch(wasm32)
71-
let ret = PlayBridgeJS()
72-
return Unmanaged.passRetained(ret).toOpaque()
73-
#else
74-
fatalError("Only available on WebAssembly")
75-
#endif
76-
}
77-
78-
@_expose(wasm, "bjs_PlayBridgeJS_update")
79-
@_cdecl("bjs_PlayBridgeJS_update")
80-
public func _bjs_PlayBridgeJS_update(_self: UnsafeMutableRawPointer, swiftSourceBytes: Int32, swiftSourceLen: Int32, dtsSourceBytes: Int32, dtsSourceLen: Int32) -> UnsafeMutableRawPointer {
81-
#if arch(wasm32)
82-
do {
83-
let swiftSource = String(unsafeUninitializedCapacity: Int(swiftSourceLen)) { b in
84-
_swift_js_init_memory(swiftSourceBytes, b.baseAddress.unsafelyUnwrapped)
85-
return Int(swiftSourceLen)
86-
}
87-
let dtsSource = String(unsafeUninitializedCapacity: Int(dtsSourceLen)) { b in
88-
_swift_js_init_memory(dtsSourceBytes, b.baseAddress.unsafelyUnwrapped)
89-
return Int(dtsSourceLen)
90-
}
91-
let ret = try Unmanaged<PlayBridgeJS>.fromOpaque(_self).takeUnretainedValue().update(swiftSource: swiftSource, dtsSource: dtsSource)
92-
return Unmanaged.passRetained(ret).toOpaque()
93-
} catch let error {
94-
if let error = error.thrownValue.object {
95-
withExtendedLifetime(error) {
96-
_swift_js_throw(Int32(bitPattern: $0.id))
97-
}
98-
} else {
99-
let jsError = JSError(message: String(describing: error))
100-
withExtendedLifetime(jsError.jsObject) {
101-
_swift_js_throw(Int32(bitPattern: $0.id))
102-
}
103-
}
104-
return UnsafeMutableRawPointer(bitPattern: -1).unsafelyUnwrapped
105-
}
106-
#else
107-
fatalError("Only available on WebAssembly")
108-
#endif
109-
}
110-
111-
@_expose(wasm, "bjs_PlayBridgeJS_deinit")
112-
@_cdecl("bjs_PlayBridgeJS_deinit")
113-
public func _bjs_PlayBridgeJS_deinit(pointer: UnsafeMutableRawPointer) {
114-
Unmanaged<PlayBridgeJS>.fromOpaque(pointer).release()
115115
}

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" : [

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ class ExportSwift {
5656

5757
fileprivate final class APICollector: SyntaxAnyVisitor {
5858
var exportedFunctions: [ExportedFunction] = []
59-
var exportedClasses: [String: ExportedClass] = [:]
59+
/// The names of the exported classes, in the order they were written in the source file
60+
var exportedClassNames: [String] = []
61+
var exportedClassByName: [String: ExportedClass] = [:]
6062
var errors: [DiagnosticError] = []
6163

6264
enum State {
@@ -114,7 +116,7 @@ class ExportSwift {
114116
return .skipChildren
115117
case .classBody(let name):
116118
if let exportedFunction = visitFunction(node: node) {
117-
exportedClasses[name]?.methods.append(exportedFunction)
119+
exportedClassByName[name]?.methods.append(exportedFunction)
118120
}
119121
return .skipChildren
120122
}
@@ -217,7 +219,7 @@ class ExportSwift {
217219
parameters: parameters,
218220
effects: effects
219221
)
220-
exportedClasses[name]?.constructor = constructor
222+
exportedClassByName[name]?.constructor = constructor
221223
return .skipChildren
222224
}
223225

@@ -226,11 +228,12 @@ class ExportSwift {
226228
stateStack.push(state: .classBody(name: name))
227229

228230
guard node.attributes.hasJSAttribute() else { return .skipChildren }
229-
exportedClasses[name] = ExportedClass(
231+
exportedClassByName[name] = ExportedClass(
230232
name: name,
231233
constructor: nil,
232234
methods: []
233235
)
236+
exportedClassNames.append(name)
234237
return .visitChildren
235238
}
236239
override func visitPost(_ node: ClassDeclSyntax) {
@@ -242,7 +245,9 @@ class ExportSwift {
242245
let collector = APICollector(parent: self)
243246
collector.walk(sourceFile)
244247
exportedFunctions.append(contentsOf: collector.exportedFunctions)
245-
exportedClasses.append(contentsOf: collector.exportedClasses.values)
248+
exportedClasses.append(contentsOf: collector.exportedClassNames.map {
249+
collector.exportedClassByName[$0]!
250+
})
246251
return collector.errors
247252
}
248253

Plugins/BridgeJS/Sources/BridgeJSTool/BridgeJSTool.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ import SwiftParser
159159
)
160160
let progress = ProgressReporting(verbose: doubleDashOptions["verbose"] == "true")
161161
let exporter = ExportSwift(progress: progress)
162-
for inputFile in positionalArguments {
162+
for inputFile in positionalArguments.sorted() {
163163
let sourceURL = URL(fileURLWithPath: inputFile)
164164
guard sourceURL.pathExtension == "swift" else { continue }
165165
let sourceContent = try String(contentsOf: sourceURL, encoding: .utf8)

0 commit comments

Comments
 (0)