Skip to content

Commit 3f6620f

Browse files
PackageToJS: Derive WASI and shared memory support from wasm imports
The SwiftBuild backend's build directory doesn't encode the target triple, so detect the traits derived from it by parsing the imports of the product binary instead. The imports are now parsed as a part of the packaging build graph.
1 parent 8f4c851 commit 3f6620f

16 files changed

Lines changed: 431 additions & 328 deletions

File tree

.github/workflows/test.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@ jobs:
1414
entry:
1515
- os: ubuntu-24.04
1616
toolchain:
17-
download-url: https://download.swift.org/development/ubuntu2404/swift-DEVELOPMENT-SNAPSHOT-2026-05-27-a/swift-DEVELOPMENT-SNAPSHOT-2026-05-27-a-ubuntu24.04.tar.gz
17+
download-url: https://download.swift.org/development/ubuntu2404/swift-DEVELOPMENT-SNAPSHOT-2026-08-11-a/swift-DEVELOPMENT-SNAPSHOT-2026-08-11-a-ubuntu24.04.tar.gz
1818
wasi-backend: Node
1919
target: "wasm32-unknown-wasip1"
20+
build-system: swiftbuild
2021
env: |
2122
JAVASCRIPTKIT_DISABLE_TRACING_TRAIT=1
2223
- os: ubuntu-24.04
2324
toolchain:
2425
download-url: https://download.swift.org/swift-6.3-branch/ubuntu2404/swift-6.3-DEVELOPMENT-SNAPSHOT-2026-03-05-a/swift-6.3-DEVELOPMENT-SNAPSHOT-2026-03-05-a-ubuntu24.04.tar.gz
2526
wasi-backend: Node
2627
target: "wasm32-unknown-wasip1"
28+
build-system: native
2729
- os: ubuntu-22.04
2830
toolchain:
29-
download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2026-05-27-a/swift-DEVELOPMENT-SNAPSHOT-2026-05-27-a-ubuntu22.04.tar.gz
31+
download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2026-08-11-a/swift-DEVELOPMENT-SNAPSHOT-2026-08-11-a-ubuntu22.04.tar.gz
3032
wasi-backend: Node
3133
target: "wasm32-unknown-wasip1-threads"
34+
build-system: swiftbuild
3235

3336
runs-on: ${{ matrix.entry.os }}
3437
env:
@@ -55,7 +58,7 @@ jobs:
5558
echo "SWIFT_BIN_PATH=$(dirname $(which swiftc))" >> $GITHUB_ENV
5659
- run: make bootstrap
5760
- run: npm run test:runtime
58-
- run: make unittest
61+
- run: make unittest BUILD_SYSTEM=${{ matrix.entry.build-system }}
5962
# Skip unit tests with uwasi because its proc_exit throws
6063
# unhandled promise rejection.
6164
if: ${{ matrix.entry.wasi-backend != 'MicroWASI' }}
@@ -144,7 +147,7 @@ jobs:
144147
- uses: actions/checkout@v7
145148
- uses: ./.github/actions/install-swift
146149
with:
147-
download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2026-05-27-a/swift-DEVELOPMENT-SNAPSHOT-2026-05-27-a-ubuntu22.04.tar.gz
150+
download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2026-08-11-a/swift-DEVELOPMENT-SNAPSHOT-2026-08-11-a-ubuntu22.04.tar.gz
148151
- run: make bootstrap
149152
- run: ./Utilities/bridge-js-generate.sh
150153
- name: Check if BridgeJS generated files are up-to-date
@@ -175,7 +178,7 @@ jobs:
175178
- uses: actions/checkout@v7
176179
- uses: ./.github/actions/install-swift
177180
with:
178-
download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2026-05-27-a/swift-DEVELOPMENT-SNAPSHOT-2026-05-27-a-ubuntu22.04.tar.gz
181+
download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2026-08-11-a/swift-DEVELOPMENT-SNAPSHOT-2026-08-11-a-ubuntu22.04.tar.gz
179182
- uses: swiftwasm/setup-swiftwasm@v2
180183
id: setup-wasm32-unknown-wasip1
181184
with: { target: wasm32-unknown-wasip1 }
@@ -198,7 +201,7 @@ jobs:
198201
- uses: actions/checkout@v7
199202
- uses: ./.github/actions/install-swift
200203
with:
201-
download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2026-05-27-a/swift-DEVELOPMENT-SNAPSHOT-2026-05-27-a-ubuntu22.04.tar.gz
204+
download-url: https://download.swift.org/development/ubuntu2204/swift-DEVELOPMENT-SNAPSHOT-2026-08-11-a/swift-DEVELOPMENT-SNAPSHOT-2026-08-11-a-ubuntu22.04.tar.gz
202205
- uses: swiftwasm/setup-swiftwasm@v2
203206
id: setup-wasm32-unknown-wasip1
204207
with: { target: wasm32-unknown-wasip1 }

Examples/PlayBridgeJS/Sources/PlayBridgeJS/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ import class Foundation.JSONDecoder
7070
let importTS = ImportTS(progress: .silent, moduleName: moduleName, skeleton: $0)
7171
return try importTS.finalize()
7272
}
73-
let linker = BridgeJSLink(skeletons: [skeleton], sharedMemory: false)
73+
let linker = BridgeJSLink(skeletons: [skeleton])
7474
let linked = try linker.link()
7575

7676
return PlayBridgeJSOutput(

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
SWIFT_SDK_ID ?=
2+
BUILD_SYSTEM ?= swiftbuild
23
ifeq ($(JAVASCRIPTKIT_DISABLE_TRACING_TRAIT),1)
34
TRACING_ARGS :=
45
else
@@ -16,7 +17,7 @@ unittest:
1617
echo "SWIFT_SDK_ID is not set. Run 'swift sdk list' and pass a matching SDK, e.g. 'make unittest SWIFT_SDK_ID=<id>'."; \
1718
exit 2; \
1819
}
19-
swift package --build-system native --swift-sdk "$(SWIFT_SDK_ID)" \
20+
swift package --build-system "$(BUILD_SYSTEM)" --swift-sdk "$(SWIFT_SDK_ID)" \
2021
$(TRACING_ARGS) \
2122
--disable-sandbox \
2223
js test --prelude ./Tests/prelude.mjs -Xnode --expose-gc

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import BridgeJSUtilities
99

1010
public struct BridgeJSLink {
1111
var skeletons: [BridgeJSSkeleton] = []
12-
let sharedMemory: Bool
1312
/// Whether to track the lifetime of Swift objects.
1413
///
1514
/// This is useful for debugging memory issues.
@@ -19,11 +18,9 @@ public struct BridgeJSLink {
1918
private let importedModuleRegistry = ImportedJSModuleRegistry()
2019

2120
public init(
22-
skeletons: [BridgeJSSkeleton] = [],
23-
sharedMemory: Bool = false
21+
skeletons: [BridgeJSSkeleton] = []
2422
) {
2523
self.skeletons = skeletons
26-
self.sharedMemory = sharedMemory
2724
}
2825

2926
/// The identity mode from the config file, resolved from skeletons.
@@ -1173,7 +1170,7 @@ public struct BridgeJSLink {
11731170
}
11741171

11751172
/// Generates JavaScript output using CodeFragmentPrinter for better maintainability
1176-
private func generateJavaScript(data: LinkData) throws -> String {
1173+
private func generateJavaScript(data: LinkData, sharedMemory: Bool) throws -> String {
11771174
let header = """
11781175
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
11791176
// DO NOT EDIT.
@@ -1346,7 +1343,7 @@ public struct BridgeJSLink {
13461343
return printer.lines.joined(separator: "\n")
13471344
}
13481345

1349-
public func link() throws -> (outputJs: String, outputDts: String) {
1346+
public func link(sharedMemory: Bool = false) throws -> (outputJs: String, outputDts: String) {
13501347
intrinsicRegistry.reset()
13511348
importedModuleRegistry.configure(skeletons: skeletons)
13521349
intrinsicRegistry.classNamespaces = skeletons.reduce(into: [:]) { result, unified in
@@ -1359,7 +1356,7 @@ public struct BridgeJSLink {
13591356
}
13601357
intrinsicRegistry.typeOwnerModules = collectTypeOwnerModules()
13611358
let data = try collectLinkData()
1362-
let outputJs = try generateJavaScript(data: data)
1359+
let outputJs = try generateJavaScript(data: data, sharedMemory: sharedMemory)
13631360
let outputDts = generateTypeScript(data: data)
13641361
return (outputJs, outputDts)
13651362
}

Plugins/BridgeJS/Sources/BridgeJSToolInternal/BridgeJSToolInternal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ import ArgumentParser
110110
let skeletonData = try readData(from: skeletonFile)
111111
skeletons.append(try JSONDecoder().decode(BridgeJSSkeleton.self, from: skeletonData))
112112
}
113-
let link = BridgeJSLink(skeletons: skeletons, sharedMemory: false)
113+
let link = BridgeJSLink(skeletons: skeletons)
114114
return try link.link()
115115
}
116116

Plugins/BridgeJS/Tests/BridgeJSToolTests/BridgeJSLinkTests.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ import Testing
7070
)
7171
importSwift.addSourceFile(sourceFile, inputFilePath: "\(name).swift")
7272
let importResult = try importSwift.finalize()
73-
var bridgeJSLink = BridgeJSLink(sharedMemory: false)
73+
var bridgeJSLink = BridgeJSLink()
7474
let encoder = JSONEncoder()
7575
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
7676
let unifiedData = try encoder.encode(importResult)
@@ -99,8 +99,7 @@ import Testing
9999
let bridgeJSLink: BridgeJSLink = BridgeJSLink(
100100
skeletons: [
101101
outputSkeleton
102-
],
103-
sharedMemory: false
102+
]
104103
)
105104
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Global")
106105
}
@@ -133,8 +132,7 @@ import Testing
133132
skeletons: [
134133
globalSkeleton,
135134
privateSkeleton,
136-
],
137-
sharedMemory: false
135+
]
138136
)
139137
try snapshot(bridgeJSLink: bridgeJSLink, name: "MixedModules")
140138
}
@@ -151,7 +149,7 @@ import Testing
151149
)
152150
importSwift.addSourceFile(sourceFile, inputFilePath: "\(name).swift")
153151
let importResult = try importSwift.finalize()
154-
var bridgeJSLink = BridgeJSLink(sharedMemory: false)
152+
var bridgeJSLink = BridgeJSLink()
155153
let encoder = JSONEncoder()
156154
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
157155
let unifiedData = try encoder.encode(importResult)
@@ -182,7 +180,7 @@ import Testing
182180
#expect(explicitlyUncachedClass?.identityMode == false)
183181

184182
// Verify generated JS via snapshot
185-
let bridgeJSLink = BridgeJSLink(skeletons: [outputSkeleton], sharedMemory: false)
183+
let bridgeJSLink = BridgeJSLink(skeletons: [outputSkeleton])
186184
try snapshot(bridgeJSLink: bridgeJSLink, name: "IdentityModeClass.PerClass")
187185
}
188186

@@ -206,7 +204,7 @@ import Testing
206204
#expect(explicitlyUncachedClass?.identityMode == false)
207205

208206
// Verify generated JS via snapshot
209-
let bridgeJSLink = BridgeJSLink(skeletons: [outputSkeleton], sharedMemory: false)
207+
let bridgeJSLink = BridgeJSLink(skeletons: [outputSkeleton])
210208
try snapshot(bridgeJSLink: bridgeJSLink, name: "IdentityModeClass.ConfigPointer")
211209
}
212210
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/ImportedJSModuleRegistryTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import Testing
3535
}
3636

3737
private func importLines(_ skeletons: [BridgeJSSkeleton]) throws -> [String] {
38-
var link = BridgeJSLink(sharedMemory: false)
38+
var link = BridgeJSLink()
3939
let encoder = JSONEncoder()
4040
for skeleton in skeletons {
4141
_ = try link.addSkeletonFile(data: try encoder.encode(skeleton))

0 commit comments

Comments
 (0)