Skip to content

Commit b176aef

Browse files
BridgeJS: Fix PlayBridgeJS build
1 parent 06aacb9 commit b176aef

File tree

9 files changed

+53
-66
lines changed

9 files changed

+53
-66
lines changed

Examples/PlayBridgeJS/Sources/JavaScript/app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ export class BridgeJSPlayground {
250250
// Process the TypeScript definitions to generate skeleton
251251
const processor = new TypeProcessor(tsProgram.getTypeChecker(), diagnosticEngine);
252252

253-
const skeleton = processor.processTypeDeclarations(tsProgram, virtualFilePath);
253+
const { content } = processor.processTypeDeclarations(tsProgram, virtualFilePath);
254254

255-
return JSON.stringify(skeleton);
255+
return content;
256256
}
257257
}
258258
}
@@ -340,4 +340,4 @@ export class BridgeJSPlayground {
340340
this.progressBar.classList.add('hidden');
341341
}
342342
}
343-
}
343+
}

Examples/PlayBridgeJS/Sources/JavaScript/editor.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ export class EditorSystem {
3838
modelUri: 'Playground.d.ts'
3939
},
4040
{
41-
key: 'import-glue',
42-
id: 'importGlueOutput',
41+
key: 'swift-import-macros',
42+
id: 'swiftImportMacrosOutput',
4343
language: 'swift',
44-
placeholder: '// Import Swift Glue will appear here...',
44+
placeholder: '// Import Swift Macros will appear here...',
4545
readOnly: true,
46-
modelUri: 'ImportTS.swift'
46+
modelUri: 'BridgeJS.Macros.swift'
4747
},
4848
{
49-
key: 'export-glue',
50-
id: 'exportGlueOutput',
49+
key: 'swift-glue',
50+
id: 'swiftGlueOutput',
5151
language: 'swift',
52-
placeholder: '// Export Swift Glue will appear here...',
52+
placeholder: '// Swift Glue will appear here...',
5353
readOnly: true,
54-
modelUri: 'ExportTS.swift'
54+
modelUri: 'BridgeJS.swift'
5555
},
5656
{
5757
key: 'js-generated',
@@ -206,8 +206,8 @@ export class EditorSystem {
206206

207207
updateOutputs(result) {
208208
const outputMap = {
209-
'import-glue': () => result.importSwiftGlue(),
210-
'export-glue': () => result.exportSwiftGlue(),
209+
'swift-glue': () => result.swiftGlue(),
210+
'swift-import-macros': () => result.importSwiftMacroDecls(),
211211
'js-generated': () => result.outputJs(),
212212
'dts-generated': () => result.outputDts()
213213
};
@@ -238,4 +238,4 @@ export class EditorSystem {
238238
getActiveTabs() {
239239
return this.activeTabs;
240240
}
241-
}
241+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../../../../Plugins/BridgeJS/Sources/TS2Skeleton/JavaScript/src/processor.js
1+
../../../../Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,22 @@ public func _bjs_PlayBridgeJSOutput_outputDts(_ _self: UnsafeMutableRawPointer)
9090
#endif
9191
}
9292

93-
@_expose(wasm, "bjs_PlayBridgeJSOutput_importSwiftGlue")
94-
@_cdecl("bjs_PlayBridgeJSOutput_importSwiftGlue")
95-
public func _bjs_PlayBridgeJSOutput_importSwiftGlue(_ _self: UnsafeMutableRawPointer) -> Void {
93+
@_expose(wasm, "bjs_PlayBridgeJSOutput_importSwiftMacroDecls")
94+
@_cdecl("bjs_PlayBridgeJSOutput_importSwiftMacroDecls")
95+
public func _bjs_PlayBridgeJSOutput_importSwiftMacroDecls(_ _self: UnsafeMutableRawPointer) -> Void {
9696
#if arch(wasm32)
97-
let ret = PlayBridgeJSOutput.bridgeJSLiftParameter(_self).importSwiftGlue()
97+
let ret = PlayBridgeJSOutput.bridgeJSLiftParameter(_self).importSwiftMacroDecls()
9898
return ret.bridgeJSLowerReturn()
9999
#else
100100
fatalError("Only available on WebAssembly")
101101
#endif
102102
}
103103

104-
@_expose(wasm, "bjs_PlayBridgeJSOutput_exportSwiftGlue")
105-
@_cdecl("bjs_PlayBridgeJSOutput_exportSwiftGlue")
106-
public func _bjs_PlayBridgeJSOutput_exportSwiftGlue(_ _self: UnsafeMutableRawPointer) -> Void {
104+
@_expose(wasm, "bjs_PlayBridgeJSOutput_swiftGlue")
105+
@_cdecl("bjs_PlayBridgeJSOutput_swiftGlue")
106+
public func _bjs_PlayBridgeJSOutput_swiftGlue(_ _self: UnsafeMutableRawPointer) -> Void {
107107
#if arch(wasm32)
108-
let ret = PlayBridgeJSOutput.bridgeJSLiftParameter(_self).exportSwiftGlue()
108+
let ret = PlayBridgeJSOutput.bridgeJSLiftParameter(_self).swiftGlue()
109109
return ret.bridgeJSLowerReturn()
110110
#else
111111
fatalError("Only available on WebAssembly")

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@
9292
}
9393
},
9494
{
95-
"abiName" : "bjs_PlayBridgeJSOutput_importSwiftGlue",
95+
"abiName" : "bjs_PlayBridgeJSOutput_importSwiftMacroDecls",
9696
"effects" : {
9797
"isAsync" : false,
9898
"isStatic" : false,
9999
"isThrows" : false
100100
},
101-
"name" : "importSwiftGlue",
101+
"name" : "importSwiftMacroDecls",
102102
"parameters" : [
103103

104104
],
@@ -109,13 +109,13 @@
109109
}
110110
},
111111
{
112-
"abiName" : "bjs_PlayBridgeJSOutput_exportSwiftGlue",
112+
"abiName" : "bjs_PlayBridgeJSOutput_swiftGlue",
113113
"effects" : {
114114
"isAsync" : false,
115115
"isStatic" : false,
116116
"isThrows" : false
117117
},
118-
"name" : "exportSwiftGlue",
118+
"name" : "swiftGlue",
119119
"parameters" : [
120120

121121
],

Examples/PlayBridgeJS/Sources/PlayBridgeJS/main.swift

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,49 +22,45 @@ import class Foundation.JSONDecoder
2222
let sourceFile = Parser.parse(source: swiftSource)
2323
try exportSwift.addSourceFile(sourceFile, "Playground.swift")
2424
let exportResult = try exportSwift.finalize()
25-
var importTS = ImportTS(progress: .silent, moduleName: moduleName)
2625
let ts2swift = try createTS2Swift()
27-
let skeletonJSONString = try ts2swift.convert(dtsSource)
28-
let decoder = JSONDecoder()
29-
let importSkeleton = try decoder.decode(
30-
ImportedFileSkeleton.self,
31-
from: skeletonJSONString.data(using: .utf8)!
32-
)
33-
importTS.addSkeleton(importSkeleton)
34-
let importSwiftGlue = try importTS.finalize()
35-
26+
let importSwiftMacroDecls = try ts2swift.convert(dtsSource)
27+
let importSwift = ImportSwiftMacros(progress: .silent, moduleName: moduleName)
28+
let importSourceFile = Parser.parse(source: importSwiftMacroDecls)
29+
importSwift.addSourceFile(importSourceFile, "Playground.Macros.swift")
30+
importSwift.addSourceFile(sourceFile, "Playground.swift")
31+
let importResult = try importSwift.finalize()
3632
let skeleton = BridgeJSSkeleton(
3733
moduleName: moduleName,
3834
exported: exportResult.map { $0.outputSkeleton },
39-
imported: ImportedModuleSkeleton(children: [importSkeleton])
35+
imported: importResult.outputSkeleton
4036
)
4137
let linker = BridgeJSLink(skeletons: [skeleton], sharedMemory: false)
4238
let linked = try linker.link()
4339

4440
return PlayBridgeJSOutput(
4541
outputJs: linked.outputJs,
4642
outputDts: linked.outputDts,
47-
importSwiftGlue: importSwiftGlue ?? "",
48-
exportSwiftGlue: exportResult?.outputSwift ?? ""
43+
importSwiftMacroDecls: importSwiftMacroDecls,
44+
swiftGlue: (importResult.outputSwift ?? "") + "\n\n" + (exportResult?.outputSwift ?? "")
4945
)
5046
}
5147
}
5248

5349
@JS class PlayBridgeJSOutput {
5450
let _outputJs: String
5551
let _outputDts: String
56-
let _importSwiftGlue: String
57-
let _exportSwiftGlue: String
52+
let _importSwiftMacroDecls: String
53+
let _swiftGlue: String
5854

59-
init(outputJs: String, outputDts: String, importSwiftGlue: String, exportSwiftGlue: String) {
55+
init(outputJs: String, outputDts: String, importSwiftMacroDecls: String, swiftGlue: String) {
6056
self._outputJs = outputJs
6157
self._outputDts = outputDts
62-
self._importSwiftGlue = importSwiftGlue
63-
self._exportSwiftGlue = exportSwiftGlue
58+
self._importSwiftMacroDecls = importSwiftMacroDecls
59+
self._swiftGlue = swiftGlue
6460
}
6561

6662
@JS func outputJs() -> String { self._outputJs }
6763
@JS func outputDts() -> String { self._outputDts }
68-
@JS func importSwiftGlue() -> String { self._importSwiftGlue }
69-
@JS func exportSwiftGlue() -> String { self._exportSwiftGlue }
64+
@JS func importSwiftMacroDecls() -> String { self._importSwiftMacroDecls }
65+
@JS func swiftGlue() -> String { self._swiftGlue }
7066
}

Examples/PlayBridgeJS/index.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,15 @@ <h2 class="output-title">Output</h2>
8484
</div>
8585
<div class="tab-group">
8686
<button class="tab-button active" data-tab="dts-generated">Playground.d.ts</button>
87+
<button class="tab-button" data-tab="swift-import-macros">BridgeJS.Macros.swift</button>
8788
<button class="tab-button" data-tab="js-generated">Playground.js</button>
88-
<button class="tab-button" data-tab="import-glue">ImportTS.swift</button>
89-
<button class="tab-button" data-tab="export-glue">ExportSwift.swift</button>
89+
<button class="tab-button" data-tab="swift-glue">BridgeJS.swift</button>
9090
</div>
91-
<div id="importGlueOutputTab" class="tab-content active">
92-
<div id="importGlueOutput" class="editor-container"></div>
91+
<div id="swiftImportMacrosOutputTab" class="tab-content active">
92+
<div id="swiftImportMacrosOutput" class="editor-container"></div>
9393
</div>
94-
<div id="exportGlueOutputTab" class="tab-content">
95-
<div id="exportGlueOutput" class="editor-container"></div>
94+
<div id="swiftGlueOutputTab" class="tab-content">
95+
<div id="swiftGlueOutput" class="editor-container"></div>
9696
</div>
9797
<div id="jsOutputTab" class="tab-content">
9898
<div id="jsOutput" class="editor-container"></div>

Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
// @ts-check
77
import ts from 'typescript';
8-
import path from 'path';
98

109
/** @typedef {import('./index.d.ts').Parameter} Parameter */
1110
/** @typedef {import('./index.d.ts').BridgeType} BridgeType */
@@ -64,11 +63,9 @@ export class TypeProcessor {
6463
* @returns {{ content: string, hasAny: boolean }} Rendered Swift code
6564
*/
6665
processTypeDeclarations(program, inputFilePath) {
67-
const normalizedInput = path.resolve(inputFilePath);
68-
const sourceFiles = program.getSourceFiles().filter(sf => {
69-
if (!sf.isDeclarationFile) return true;
70-
return path.resolve(sf.fileName) === normalizedInput;
71-
});
66+
const sourceFiles = program.getSourceFiles().filter(
67+
sf => !sf.isDeclarationFile || sf.fileName === inputFilePath
68+
);
7269

7370
// Add prelude
7471
this.swiftLines.push(

Plugins/BridgeJS/Tests/BridgeJSToolTests/ImportTSTests.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import SwiftParser
2222
let nodePath = try #require(which("node"))
2323
let tsconfigPath = url.deletingLastPathComponent().appendingPathComponent("tsconfig.json")
2424

25-
// Use the new workflow: invokeTS2Swift -> ImportSwiftMacros -> ImportTS
2625
let swiftSource = try invokeTS2Swift(
2726
dtsFile: url.path,
2827
tsconfigPath: tsconfigPath.path,
@@ -42,12 +41,7 @@ import SwiftParser
4241
importSwift.addSourceFile(sourceFile, "\(name).Macros.swift")
4342
let importResult = try importSwift.finalize()
4443

45-
var api = ImportTS(progress: .silent, moduleName: "Check")
46-
for child in importResult.outputSkeleton.children {
47-
api.addSkeleton(child)
48-
}
49-
50-
let outputSwift = try #require(try api.finalize())
44+
let outputSwift = try #require(importResult.outputSwift)
5145
try assertSnapshot(
5246
name: name,
5347
filePath: #filePath,

0 commit comments

Comments
 (0)