Skip to content

Commit fc546b2

Browse files
MaxDesiatovkateinoigakukun
authored andcommitted
Parse .wat files in the run subcommand
WasmKit already supports parsing of WAT (WebAssembly Text format), but its command-line interface does not act accordingly for files with `.wat` file extension. This change conditionally parses the text format on matching file extension instead of assuming that all given files are in binary format.
1 parent e66daf0 commit fc546b2

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ let package = Package(
2828
.executableTarget(
2929
name: "CLI",
3030
dependencies: [
31+
"WAT",
3132
"WasmKit",
3233
"WasmKitWASI",
3334
.product(name: "ArgumentParser", package: "swift-argument-parser"),

Sources/CLI/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ add_executable(wasmkit-cli
55
)
66

77
target_link_wasmkit_libraries(wasmkit-cli PUBLIC
8-
ArgumentParser WasmKitWASI)
8+
ArgumentParser WAT WasmKitWASI)
99

1010
install(TARGETS wasmkit-cli
1111
RUNTIME DESTINATION bin)

Sources/CLI/Commands/Run.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ArgumentParser
22
import SystemPackage
3+
import WAT
34
import WasmKit
45
import WasmKitWASI
56

@@ -119,12 +120,12 @@ struct Run: ParsableCommand {
119120
let module: Module
120121
if verbose, #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
121122
let (parsedModule, parseTime) = try measure {
122-
try parseWasm(filePath: FilePath(path))
123+
try self.parseWasm(filePath: FilePath(path))
123124
}
124125
log("Finished parsing module: \(parseTime)", verbose: true)
125126
module = parsedModule
126127
} else {
127-
module = try parseWasm(filePath: FilePath(path))
128+
module = try self.parseWasm(filePath: FilePath(path))
128129
}
129130

130131
let (interceptor, finalize) = try deriveInterceptor()
@@ -148,6 +149,23 @@ struct Run: ParsableCommand {
148149
}
149150
}
150151

152+
/// Parses a `.wasm` or `.wat` module.
153+
func parseWasm(filePath: FilePath) throws -> Module {
154+
if filePath.extension == "wat", #available(macOS 11.0, iOS 14.0, macCatalyst 14.0, tvOS 14.0, visionOS 1.0, watchOS 7.0, *) {
155+
let fileHandle = try FileDescriptor.open(filePath, .readOnly)
156+
defer { try? fileHandle.close() }
157+
158+
let size = try fileHandle.seek(offset: 0, from: .end)
159+
160+
let wat = try String(unsafeUninitializedCapacity: Int(size)) {
161+
try fileHandle.read(fromAbsoluteOffset: 0, into: .init($0))
162+
}
163+
return try WasmKit.parseWasm(bytes: wat2wasm(wat))
164+
} else {
165+
return try WasmKit.parseWasm(filePath: filePath)
166+
}
167+
}
168+
151169
/// Derives the runtime interceptor based on the command line arguments
152170
func deriveInterceptor() throws -> (interceptor: EngineInterceptor?, finalize: () -> Void) {
153171
var interceptors: [EngineInterceptor] = []

0 commit comments

Comments
 (0)