Skip to content

Commit 779553d

Browse files
committed
BridgeJS: Fix nested type references in generated TS
1 parent 9c8cb0b commit 779553d

8 files changed

Lines changed: 511 additions & 12 deletions

File tree

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,6 +1704,24 @@ public struct BridgeJSLink {
17041704
}
17051705
}
17061706
return type.tsType
1707+
case .swiftStruct(let name):
1708+
for skeleton in exportedSkeletons {
1709+
for structDef in skeleton.structs {
1710+
if structDef.name == name || structDef.swiftCallName == name {
1711+
return structDef.tsFullPath
1712+
}
1713+
}
1714+
}
1715+
return type.tsType
1716+
case .swiftHeapObject(let name):
1717+
for skeleton in exportedSkeletons {
1718+
for klass in skeleton.classes {
1719+
if klass.name == name || klass.swiftCallName == name {
1720+
return klass.name
1721+
}
1722+
}
1723+
}
1724+
return type.tsType
17071725
case .alias(_, let underlying):
17081726
return resolveTypeScriptType(underlying, exportedSkeletons: exportedSkeletons)
17091727
case .nullable(let wrapped, let kind):
@@ -2903,7 +2921,7 @@ extension BridgeJSLink {
29032921
let namespaceEnumPaths = skeleton.enums
29042922
.filter { $0.enumType == .namespace }
29052923
.filter { !$0.staticProperties.isEmpty || !$0.staticMethods.isEmpty }
2906-
.map { ($0.namespace ?? []) + [$0.name] }
2924+
.map(\.tsPathComponents)
29072925

29082926
return itemNamespaces + namespaceEnumPaths
29092927
}
@@ -2961,15 +2979,13 @@ extension BridgeJSLink {
29612979
}
29622980
for enumDef in skeleton.enums where enumDef.enumType == .namespace {
29632981
for function in enumDef.staticMethods {
2964-
let fullNamespace = (enumDef.namespace ?? []) + [enumDef.name]
2965-
let namespacePath = fullNamespace.joined(separator: ".")
2982+
let namespacePath = enumDef.tsFullPath
29662983
printer.write(
29672984
"globalThis.\(namespacePath).\(function.resolvedJSName) = exports.\(namespacePath).\(function.resolvedJSName);"
29682985
)
29692986
}
29702987
for property in enumDef.staticProperties {
2971-
let fullNamespace = (enumDef.namespace ?? []) + [enumDef.name]
2972-
let namespacePath = fullNamespace.joined(separator: ".")
2988+
let namespacePath = enumDef.tsFullPath
29732989
let exportsPath = "exports.\(namespacePath)"
29742990

29752991
printer.write(
@@ -3082,15 +3098,15 @@ extension BridgeJSLink {
30823098

30833099
for klass in skeleton.classes {
30843100
var currentNode = rootNode
3085-
for part in (klass.namespace ?? []) + [klass.name] {
3101+
for part in klass.tsPathComponents {
30863102
currentNode = currentNode.addChild(part)
30873103
}
30883104
currentNode.content.declaration = .classType(klass)
30893105
}
30903106

30913107
for structDef in skeleton.structs {
30923108
var currentNode = rootNode
3093-
for part in (structDef.namespace ?? []) + [structDef.name] {
3109+
for part in structDef.tsPathComponents {
30943110
currentNode = currentNode.addChild(part)
30953111
}
30963112
currentNode.content.declaration = .structType(structDef)
@@ -3106,17 +3122,15 @@ extension BridgeJSLink {
31063122

31073123
for enumDef in skeleton.enums where enumDef.enumType == .namespace {
31083124
for property in enumDef.staticProperties {
3109-
let fullNamespace = (enumDef.namespace ?? []) + [enumDef.name]
31103125
var currentNode = rootNode
3111-
for part in fullNamespace {
3126+
for part in enumDef.tsPathComponents {
31123127
currentNode = currentNode.addChild(part)
31133128
}
31143129
currentNode.content.staticProperties.append(property)
31153130
}
31163131
for function in enumDef.staticMethods {
3117-
let fullNamespace = (enumDef.namespace ?? []) + [enumDef.name]
31183132
var currentNode = rootNode
3119-
for part in fullNamespace {
3133+
for part in enumDef.tsPathComponents {
31203134
currentNode = currentNode.addChild(part)
31213135
}
31223136
currentNode.content.functions.append(function)

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ extension NamespacedExportedType {
1414
}
1515
return name
1616
}
17+
18+
public var tsPathComponents: [String] {
19+
(namespace ?? []) + [name]
20+
}
21+
22+
public var tsFullPath: String {
23+
tsPathComponents.joined(separator: ".")
24+
}
1725
}
1826

1927
// MARK: - ABI Name Generation
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@JS enum Workshop {
2+
@JS class Bench {
3+
@JS init() {}
4+
}
5+
}
6+
7+
@JS func makeBench() -> Workshop.Bench {
8+
Workshop.Bench()
9+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{
2+
"exported" : {
3+
"aliases" : [
4+
5+
],
6+
"classes" : [
7+
{
8+
"constructor" : {
9+
"abiName" : "bjs_Workshop_Bench_init",
10+
"effects" : {
11+
"isAsync" : false,
12+
"isStatic" : false,
13+
"isThrows" : false
14+
},
15+
"parameters" : [
16+
17+
]
18+
},
19+
"methods" : [
20+
21+
],
22+
"name" : "Bench",
23+
"namespace" : [
24+
"Workshop"
25+
],
26+
"properties" : [
27+
28+
],
29+
"swiftCallName" : "Workshop.Bench"
30+
}
31+
],
32+
"enums" : [
33+
{
34+
"cases" : [
35+
36+
],
37+
"emitStyle" : "const",
38+
"name" : "Workshop",
39+
"staticMethods" : [
40+
41+
],
42+
"staticProperties" : [
43+
44+
],
45+
"swiftCallName" : "Workshop",
46+
"tsFullPath" : "Workshop"
47+
}
48+
],
49+
"exposeToGlobal" : false,
50+
"functions" : [
51+
{
52+
"abiName" : "bjs_makeBench",
53+
"effects" : {
54+
"isAsync" : false,
55+
"isStatic" : false,
56+
"isThrows" : false
57+
},
58+
"name" : "makeBench",
59+
"parameters" : [
60+
61+
],
62+
"returnType" : {
63+
"swiftHeapObject" : {
64+
"_0" : "Workshop.Bench"
65+
}
66+
}
67+
}
68+
],
69+
"protocols" : [
70+
71+
],
72+
"structs" : [
73+
74+
]
75+
},
76+
"moduleName" : "TestModule",
77+
"usedExternalModules" : [
78+
79+
]
80+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
@_expose(wasm, "bjs_makeBench")
2+
@_cdecl("bjs_makeBench")
3+
public func _bjs_makeBench() -> UnsafeMutableRawPointer {
4+
#if arch(wasm32)
5+
let ret = makeBench()
6+
return ret.bridgeJSLowerReturn()
7+
#else
8+
fatalError("Only available on WebAssembly")
9+
#endif
10+
}
11+
12+
@_expose(wasm, "bjs_Workshop_Bench_init")
13+
@_cdecl("bjs_Workshop_Bench_init")
14+
public func _bjs_Workshop_Bench_init() -> UnsafeMutableRawPointer {
15+
#if arch(wasm32)
16+
let ret = Workshop.Bench()
17+
return ret.bridgeJSLowerReturn()
18+
#else
19+
fatalError("Only available on WebAssembly")
20+
#endif
21+
}
22+
23+
@_expose(wasm, "bjs_Workshop_Bench_deinit")
24+
@_cdecl("bjs_Workshop_Bench_deinit")
25+
public func _bjs_Workshop_Bench_deinit(_ pointer: UnsafeMutableRawPointer) -> Void {
26+
#if arch(wasm32)
27+
Unmanaged<Workshop.Bench>.fromOpaque(pointer).release()
28+
#else
29+
fatalError("Only available on WebAssembly")
30+
#endif
31+
}
32+
33+
extension Workshop.Bench: ConvertibleToJSValue, _BridgedSwiftHeapObject, _BridgedSwiftProtocolExportable {
34+
var jsValue: JSValue {
35+
return .object(JSObject(id: UInt32(bitPattern: _bjs_Workshop_Bench_wrap(Unmanaged.passRetained(self).toOpaque()))))
36+
}
37+
consuming func bridgeJSLowerAsProtocolReturn() -> Int32 {
38+
_bjs_Workshop_Bench_wrap(Unmanaged.passRetained(self).toOpaque())
39+
}
40+
}
41+
42+
#if arch(wasm32)
43+
@_extern(wasm, module: "TestModule", name: "bjs_Workshop_Bench_wrap")
44+
fileprivate func _bjs_Workshop_Bench_wrap_extern(_ pointer: UnsafeMutableRawPointer) -> Int32
45+
#else
46+
fileprivate func _bjs_Workshop_Bench_wrap_extern(_ pointer: UnsafeMutableRawPointer) -> Int32 {
47+
fatalError("Only available on WebAssembly")
48+
}
49+
#endif
50+
@inline(never) fileprivate func _bjs_Workshop_Bench_wrap(_ pointer: UnsafeMutableRawPointer) -> Int32 {
51+
return _bjs_Workshop_Bench_wrap_extern(pointer)
52+
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/ExtensionScopeParity.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export interface Hammer extends SwiftHeapObject {
2828
export type Exports = {
2929
Signal: SignalObject
3030
Meta: {
31-
init(note: string): Signal.Meta;
31+
init(note: string): Meta;
3232
},
3333
app: {
3434
Toolbox: {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
/// Represents a Swift heap object like a class instance or an actor instance.
8+
export interface SwiftHeapObject {
9+
/// Release the heap object.
10+
///
11+
/// Note: Calling this method will release the heap object and it will no longer be accessible.
12+
release(): void;
13+
}
14+
export interface Bench extends SwiftHeapObject {
15+
}
16+
export type Exports = {
17+
makeBench(): Bench;
18+
Workshop: {
19+
Bench: {
20+
new(): Bench;
21+
},
22+
},
23+
}
24+
export type Imports = {
25+
}
26+
export function createInstantiator(options: {
27+
imports: Imports;
28+
}, swift: any): Promise<{
29+
addImports: (importObject: WebAssembly.Imports) => void;
30+
setInstance: (instance: WebAssembly.Instance) => void;
31+
createExports: (instance: WebAssembly.Instance) => Exports;
32+
}>;

0 commit comments

Comments
 (0)