Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ struct BridgeJSLink {
/// The exported skeletons
var exportedSkeletons: [ExportedSkeleton] = []
var importedSkeletons: [ImportedModuleSkeleton] = []
let sharedMemory: Bool

init(
exportedSkeletons: [ExportedSkeleton] = [],
importedSkeletons: [ImportedModuleSkeleton] = [],
sharedMemory: Bool
) {
self.exportedSkeletons = exportedSkeletons
self.importedSkeletons = importedSkeletons
self.sharedMemory = sharedMemory
}

mutating func addExportedSkeletonFile(data: Data) throws {
let skeleton = try JSONDecoder().decode(ExportedSkeleton.self, from: data)
Expand Down Expand Up @@ -118,7 +129,7 @@ struct BridgeJSLink {
const bjs = {};
importObject["bjs"] = bjs;
bjs["return_string"] = function(ptr, len) {
const bytes = new Uint8Array(memory.buffer, ptr, len);
const bytes = new Uint8Array(memory.buffer, ptr, len)\(sharedMemory ? ".slice()" : "");
tmpRetString = textDecoder.decode(bytes);
}
bjs["init_memory"] = function(sourceId, bytesPtr) {
Expand All @@ -127,7 +138,7 @@ struct BridgeJSLink {
bytes.set(source);
}
bjs["make_jsstring"] = function(ptr, len) {
const bytes = new Uint8Array(memory.buffer, ptr, len);
const bytes = new Uint8Array(memory.buffer, ptr, len)\(sharedMemory ? ".slice()" : "");
return swift.memory.retain(textDecoder.decode(bytes));
}
bjs["init_memory_with_result"] = function(ptr, len) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import Testing
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let outputSkeletonData = try encoder.encode(outputSkeleton)
var bridgeJSLink = BridgeJSLink()
var bridgeJSLink = BridgeJSLink(sharedMemory: false)
try bridgeJSLink.addExportedSkeletonFile(data: outputSkeletonData)
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Export")
}
Expand All @@ -73,7 +73,7 @@ import Testing
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
let outputSkeletonData = try encoder.encode(importTS.skeleton)

var bridgeJSLink = BridgeJSLink()
var bridgeJSLink = BridgeJSLink(sharedMemory: false)
try bridgeJSLink.addImportedSkeletonFile(data: outputSkeletonData)
try snapshot(bridgeJSLink: bridgeJSLink, name: name + ".Import")
}
Expand Down
9 changes: 7 additions & 2 deletions Plugins/PackageToJS/Sources/PackageToJS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,8 @@ struct PackagingPlanner {
let decoder = JSONDecoder()
let data = try Data(contentsOf: URL(fileURLWithPath: scope.resolve(path: $0).path))
return try decoder.decode(ImportedModuleSkeleton.self, from: data)
}
},
sharedMemory: Self.isSharedMemoryEnabled(triple: triple)
)
let (outputJs, outputDts) = try link.link()
try system.writeFile(atPath: scope.resolve(path: bridgeJs).path, content: Data(outputJs.utf8))
Expand Down Expand Up @@ -699,7 +700,7 @@ struct PackagingPlanner {

let inputPath = selfPackageDir.appending(path: file)
let conditions: [String: Bool] = [
"USE_SHARED_MEMORY": triple == "wasm32-unknown-wasip1-threads",
"USE_SHARED_MEMORY": Self.isSharedMemoryEnabled(triple: triple),
"IS_WASI": triple.hasPrefix("wasm32-unknown-wasi"),
"USE_WASI_CDN": options.useCDN,
"HAS_BRIDGE": exportedSkeletons.count > 0 || importedSkeletons.count > 0,
Expand Down Expand Up @@ -742,6 +743,10 @@ struct PackagingPlanner {
try system.writeFile(atPath: $1.resolve(path: $0.output).path, content: Data(content.utf8))
}
}

private static func isSharedMemoryEnabled(triple: String) -> Bool {
return triple == "wasm32-unknown-wasip1-threads"
}
}

// MARK: - Utilities
Expand Down