Skip to content

Commit 64441fa

Browse files
committed
BridgeJS: Optional global namespace generation
1 parent 3ff42c3 commit 64441fa

26 files changed

+2029
-146
lines changed

Examples/PlayBridgeJS/Sources/PlayBridgeJS/main.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ import class Foundation.JSONDecoder
4040
children: [importSkeleton]
4141
)
4242
],
43-
sharedMemory: false
43+
sharedMemory: false,
44+
exposeToGlobal: true
4445
)
4546
let linked = try linker.link()
4647

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,10 +882,18 @@ public class ExportSwift {
882882
message: "Enum visibility must be at least internal"
883883
)
884884

885+
let tsFullPath: String
886+
if let namespace = namespaceResult.namespace, !namespace.isEmpty {
887+
tsFullPath = namespace.joined(separator: ".") + "." + name
888+
} else {
889+
tsFullPath = name
890+
}
891+
885892
// Create enum directly in dictionary
886893
let exportedEnum = ExportedEnum(
887894
name: name,
888895
swiftCallName: swiftCallName,
896+
tsFullPath: tsFullPath,
889897
explicitAccessControl: explicitAccessControl,
890898
cases: [], // Will be populated in visit(EnumCaseDeclSyntax)
891899
rawType: SwiftEnumRawType(rawType),

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 173 additions & 58 deletions
Large diffs are not rendered by default.

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ public struct ExportedEnum: Codable, Equatable, Sendable {
265265

266266
public let name: String
267267
public let swiftCallName: String
268+
public let tsFullPath: String
268269
public let explicitAccessControl: String?
269270
public var cases: [EnumCase]
270271
public let rawType: SwiftEnumRawType?
@@ -293,6 +294,7 @@ public struct ExportedEnum: Codable, Equatable, Sendable {
293294
public init(
294295
name: String,
295296
swiftCallName: String,
297+
tsFullPath: String,
296298
explicitAccessControl: String?,
297299
cases: [EnumCase],
298300
rawType: SwiftEnumRawType?,
@@ -303,6 +305,7 @@ public struct ExportedEnum: Codable, Equatable, Sendable {
303305
) {
304306
self.name = name
305307
self.swiftCallName = swiftCallName
308+
self.tsFullPath = tsFullPath
306309
self.explicitAccessControl = explicitAccessControl
307310
self.cases = cases
308311
self.rawType = rawType

Plugins/BridgeJS/Tests/BridgeJSToolTests/BridgeJSLinkTests.swift

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import Testing
5555
let encoder = JSONEncoder()
5656
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
5757
let outputSkeletonData = try encoder.encode(outputSkeleton)
58-
var bridgeJSLink = BridgeJSLink(sharedMemory: false)
58+
var bridgeJSLink = BridgeJSLink(sharedMemory: false, exposeToGlobal: true)
5959
try bridgeJSLink.addExportedSkeletonFile(data: outputSkeletonData)
6060
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Export")
6161
}
@@ -74,8 +74,54 @@ import Testing
7474
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
7575
let outputSkeletonData = try encoder.encode(importTS.skeleton)
7676

77-
var bridgeJSLink = BridgeJSLink(sharedMemory: false)
77+
var bridgeJSLink = BridgeJSLink(sharedMemory: false, exposeToGlobal: true)
7878
try bridgeJSLink.addImportedSkeletonFile(data: outputSkeletonData)
7979
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Import")
8080
}
81+
82+
@Test(arguments: [
83+
"Namespaces.swift",
84+
"StaticFunctions.swift",
85+
"StaticProperties.swift",
86+
"EnumNamespace.swift"
87+
])
88+
func testWithoutGlobal(inputFile: String) throws {
89+
let url = Self.inputsDirectory.appendingPathComponent(inputFile)
90+
let sourceFile = Parser.parse(source: try String(contentsOf: url, encoding: .utf8))
91+
let swiftAPI = ExportSwift(progress: .silent, moduleName: "TestModule")
92+
try swiftAPI.addSourceFile(sourceFile, inputFile)
93+
94+
let (_, outputSkeleton) = try #require(try swiftAPI.finalize())
95+
let encoder = JSONEncoder()
96+
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
97+
let outputSkeletonData = try encoder.encode(outputSkeleton)
98+
99+
var bridgeJSLink = BridgeJSLink(sharedMemory: false, exposeToGlobal: false)
100+
try bridgeJSLink.addExportedSkeletonFile(data: outputSkeletonData)
101+
102+
let (outputJs, outputDts) = try bridgeJSLink.link()
103+
104+
// Verify no global declarations
105+
#expect(!outputDts.contains("declare global"))
106+
#expect(!outputJs.contains("globalThis."))
107+
108+
// Save snapshots
109+
let name = url.deletingPathExtension().lastPathComponent + "_NoGlobal.Export"
110+
try assertSnapshot(
111+
name: name,
112+
filePath: #filePath,
113+
function: #function,
114+
sourceLocation: #_sourceLocation,
115+
input: outputJs.data(using: .utf8)!,
116+
fileExtension: "js"
117+
)
118+
try assertSnapshot(
119+
name: name,
120+
filePath: #filePath,
121+
function: #function,
122+
sourceLocation: #_sourceLocation,
123+
input: outputDts.data(using: .utf8)!,
124+
fileExtension: "d.ts"
125+
)
126+
}
81127
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.Export.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ export type APIResultObject = typeof APIResultValues;
4747

4848
export type ComplexResultObject = typeof ComplexResultValues;
4949

50-
export type ResultObject = typeof ResultValues;
50+
export type ResultObject = typeof Utilities.ResultValues;
5151

52-
export type NetworkingResultObject = typeof NetworkingResultValues;
52+
export type NetworkingResultObject = typeof API.NetworkingResultValues;
5353

5454
export type APIOptionalResultObject = typeof APIOptionalResultValues;
5555

@@ -89,7 +89,7 @@ export type Exports = {
8989
roundtripComplexResult(result: ComplexResultTag): ComplexResultTag;
9090
roundTripOptionalComplexResult(result: ComplexResultTag | null): ComplexResultTag | null;
9191
roundTripOptionalUtilitiesResult(result: Utilities.ResultTag | null): Utilities.ResultTag | null;
92-
roundTripOptionalNetworkingResult(result: NetworkingResultTag | null): NetworkingResultTag | null;
92+
roundTripOptionalNetworkingResult(result: API.NetworkingResultTag | null): API.NetworkingResultTag | null;
9393
roundTripOptionalAPIOptionalResult(result: APIOptionalResultTag | null): APIOptionalResultTag | null;
9494
APIResult: APIResultObject
9595
ComplexResult: ComplexResultObject

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumNamespace.Export.d.ts

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

7-
export type MethodObject = typeof MethodValues;
7+
export type MethodObject = typeof Networking.API.MethodValues;
88

9-
export type LogLevelObject = typeof LogLevelValues;
9+
export type LogLevelObject = typeof Configuration.LogLevelValues;
1010

11-
export type PortObject = typeof PortValues;
11+
export type PortObject = typeof Configuration.PortValues;
1212

13-
export type SupportedMethodObject = typeof SupportedMethodValues;
13+
export type SupportedMethodObject = typeof Networking.APIV2.Internal.SupportedMethodValues;
1414

1515
export {};
1616

@@ -48,7 +48,7 @@ declare global {
4848
namespace Internal {
4949
class TestServer {
5050
constructor();
51-
call(method: Internal.SupportedMethodTag): void;
51+
call(method: Networking.APIV2.Internal.SupportedMethodTag): void;
5252
}
5353
const SupportedMethodValues: {
5454
readonly Get: 0;
@@ -80,7 +80,7 @@ export interface HTTPServer extends SwiftHeapObject {
8080
call(method: Networking.API.MethodTag): void;
8181
}
8282
export interface TestServer extends SwiftHeapObject {
83-
call(method: Internal.SupportedMethodTag): void;
83+
call(method: Networking.APIV2.Internal.SupportedMethodTag): void;
8484
}
8585
export type Exports = {
8686
Configuration: {
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
export type MethodObject = typeof Networking.API.MethodValues;
8+
9+
export type LogLevelObject = typeof Configuration.LogLevelValues;
10+
11+
export type PortObject = typeof Configuration.PortValues;
12+
13+
export type SupportedMethodObject = typeof Networking.APIV2.Internal.SupportedMethodValues;
14+
15+
export namespace Configuration {
16+
const LogLevelValues: {
17+
readonly Debug: "debug";
18+
readonly Info: "info";
19+
readonly Warning: "warning";
20+
readonly Error: "error";
21+
};
22+
type LogLevelTag = typeof LogLevelValues[keyof typeof LogLevelValues];
23+
const PortValues: {
24+
readonly Http: 80;
25+
readonly Https: 443;
26+
readonly Development: 3000;
27+
};
28+
type PortTag = typeof PortValues[keyof typeof PortValues];
29+
}
30+
export namespace Networking {
31+
export namespace API {
32+
const MethodValues: {
33+
readonly Get: 0;
34+
readonly Post: 1;
35+
readonly Put: 2;
36+
readonly Delete: 3;
37+
};
38+
type MethodTag = typeof MethodValues[keyof typeof MethodValues];
39+
}
40+
export namespace APIV2 {
41+
export namespace Internal {
42+
const SupportedMethodValues: {
43+
readonly Get: 0;
44+
readonly Post: 1;
45+
};
46+
type SupportedMethodTag = typeof SupportedMethodValues[keyof typeof SupportedMethodValues];
47+
}
48+
}
49+
}
50+
/// Represents a Swift heap object like a class instance or an actor instance.
51+
export interface SwiftHeapObject {
52+
/// Release the heap object.
53+
///
54+
/// Note: Calling this method will release the heap object and it will no longer be accessible.
55+
release(): void;
56+
}
57+
export interface Converter extends SwiftHeapObject {
58+
toString(value: number): string;
59+
}
60+
export interface HTTPServer extends SwiftHeapObject {
61+
call(method: Networking.API.MethodTag): void;
62+
}
63+
export interface TestServer extends SwiftHeapObject {
64+
call(method: Networking.APIV2.Internal.SupportedMethodTag): void;
65+
}
66+
export type Exports = {
67+
Configuration: {
68+
LogLevel: LogLevelObject
69+
Port: PortObject
70+
},
71+
Networking: {
72+
API: {
73+
HTTPServer: {
74+
new(): HTTPServer;
75+
}
76+
Method: MethodObject
77+
},
78+
APIV2: {
79+
Internal: {
80+
TestServer: {
81+
new(): TestServer;
82+
}
83+
SupportedMethod: SupportedMethodObject
84+
},
85+
},
86+
},
87+
Utils: {
88+
Converter: {
89+
new(): Converter;
90+
}
91+
},
92+
}
93+
export type Imports = {
94+
}
95+
export function createInstantiator(options: {
96+
imports: Imports;
97+
}, swift: any): Promise<{
98+
addImports: (importObject: WebAssembly.Imports) => void;
99+
setInstance: (instance: WebAssembly.Instance) => void;
100+
createExports: (instance: WebAssembly.Instance) => Exports;
101+
}>;

0 commit comments

Comments
 (0)