Skip to content

Commit 387347a

Browse files
Merge pull request #379 from swiftwasm/yt/fix-shared-memory-slice
Slice a bytes array when the underlying memory is shared
2 parents 2aec44b + 03f4d9a commit 387347a

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ struct BridgeJSLink {
44
/// The exported skeletons
55
var exportedSkeletons: [ExportedSkeleton] = []
66
var importedSkeletons: [ImportedModuleSkeleton] = []
7+
let sharedMemory: Bool
8+
9+
init(
10+
exportedSkeletons: [ExportedSkeleton] = [],
11+
importedSkeletons: [ImportedModuleSkeleton] = [],
12+
sharedMemory: Bool
13+
) {
14+
self.exportedSkeletons = exportedSkeletons
15+
self.importedSkeletons = importedSkeletons
16+
self.sharedMemory = sharedMemory
17+
}
718

819
mutating func addExportedSkeletonFile(data: Data) throws {
920
let skeleton = try JSONDecoder().decode(ExportedSkeleton.self, from: data)
@@ -118,7 +129,7 @@ struct BridgeJSLink {
118129
const bjs = {};
119130
importObject["bjs"] = bjs;
120131
bjs["return_string"] = function(ptr, len) {
121-
const bytes = new Uint8Array(memory.buffer, ptr, len);
132+
const bytes = new Uint8Array(memory.buffer, ptr, len)\(sharedMemory ? ".slice()" : "");
122133
tmpRetString = textDecoder.decode(bytes);
123134
}
124135
bjs["init_memory"] = function(sourceId, bytesPtr) {
@@ -127,7 +138,7 @@ struct BridgeJSLink {
127138
bytes.set(source);
128139
}
129140
bjs["make_jsstring"] = function(ptr, len) {
130-
const bytes = new Uint8Array(memory.buffer, ptr, len);
141+
const bytes = new Uint8Array(memory.buffer, ptr, len)\(sharedMemory ? ".slice()" : "");
131142
return swift.memory.retain(textDecoder.decode(bytes));
132143
}
133144
bjs["init_memory_with_result"] = function(ptr, len) {

Plugins/BridgeJS/Tests/BridgeJSToolTests/BridgeJSLinkTests.swift

Lines changed: 2 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()
58+
var bridgeJSLink = BridgeJSLink(sharedMemory: false)
5959
try bridgeJSLink.addExportedSkeletonFile(data: outputSkeletonData)
6060
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Export")
6161
}
@@ -73,7 +73,7 @@ import Testing
7373
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
7474
let outputSkeletonData = try encoder.encode(importTS.skeleton)
7575

76-
var bridgeJSLink = BridgeJSLink()
76+
var bridgeJSLink = BridgeJSLink(sharedMemory: false)
7777
try bridgeJSLink.addImportedSkeletonFile(data: outputSkeletonData)
7878
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Import")
7979
}

Plugins/PackageToJS/Sources/PackageToJS.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,8 @@ struct PackagingPlanner {
583583
let decoder = JSONDecoder()
584584
let data = try Data(contentsOf: URL(fileURLWithPath: scope.resolve(path: $0).path))
585585
return try decoder.decode(ImportedModuleSkeleton.self, from: data)
586-
}
586+
},
587+
sharedMemory: Self.isSharedMemoryEnabled(triple: triple)
587588
)
588589
let (outputJs, outputDts) = try link.link()
589590
try system.writeFile(atPath: scope.resolve(path: bridgeJs).path, content: Data(outputJs.utf8))
@@ -699,7 +700,7 @@ struct PackagingPlanner {
699700

700701
let inputPath = selfPackageDir.appending(path: file)
701702
let conditions: [String: Bool] = [
702-
"USE_SHARED_MEMORY": triple == "wasm32-unknown-wasip1-threads",
703+
"USE_SHARED_MEMORY": Self.isSharedMemoryEnabled(triple: triple),
703704
"IS_WASI": triple.hasPrefix("wasm32-unknown-wasi"),
704705
"USE_WASI_CDN": options.useCDN,
705706
"HAS_BRIDGE": exportedSkeletons.count > 0 || importedSkeletons.count > 0,
@@ -742,6 +743,10 @@ struct PackagingPlanner {
742743
try system.writeFile(atPath: $1.resolve(path: $0.output).path, content: Data(content.utf8))
743744
}
744745
}
746+
747+
private static func isSharedMemoryEnabled(triple: String) -> Bool {
748+
return triple == "wasm32-unknown-wasip1-threads"
749+
}
745750
}
746751

747752
// MARK: - Utilities

0 commit comments

Comments
 (0)