Skip to content

Parse .wat files in the run subcommand #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2025
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
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ let package = Package(
.executableTarget(
name: "CLI",
dependencies: [
"WAT",
"WasmKit",
"WasmKitWASI",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
Expand Down
2 changes: 1 addition & 1 deletion Sources/CLI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ add_executable(wasmkit-cli
)

target_link_wasmkit_libraries(wasmkit-cli PUBLIC
ArgumentParser WasmKitWASI)
ArgumentParser WAT WasmKitWASI)

install(TARGETS wasmkit-cli
RUNTIME DESTINATION bin)
22 changes: 20 additions & 2 deletions Sources/CLI/Commands/Run.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ArgumentParser
import SystemPackage
import WAT
import WasmKit
import WasmKitWASI

Expand Down Expand Up @@ -119,12 +120,12 @@ struct Run: ParsableCommand {
let module: Module
if verbose, #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
let (parsedModule, parseTime) = try measure {
try parseWasm(filePath: FilePath(path))
try self.parseWasm(filePath: FilePath(path))
}
log("Finished parsing module: \(parseTime)", verbose: true)
module = parsedModule
} else {
module = try parseWasm(filePath: FilePath(path))
module = try self.parseWasm(filePath: FilePath(path))
}

let (interceptor, finalize) = try deriveInterceptor()
Expand All @@ -148,6 +149,23 @@ struct Run: ParsableCommand {
}
}

/// Parses a `.wasm` or `.wat` module.
func parseWasm(filePath: FilePath) throws -> Module {
if filePath.extension == "wat", #available(macOS 11.0, iOS 14.0, macCatalyst 14.0, tvOS 14.0, visionOS 1.0, watchOS 7.0, *) {
let fileHandle = try FileDescriptor.open(filePath, .readOnly)
defer { try? fileHandle.close() }

let size = try fileHandle.seek(offset: 0, from: .end)

let wat = try String(unsafeUninitializedCapacity: Int(size)) {
try fileHandle.read(fromAbsoluteOffset: 0, into: .init($0))
}
return try WasmKit.parseWasm(bytes: wat2wasm(wat))
} else {
return try WasmKit.parseWasm(filePath: filePath)
}
}

/// Derives the runtime interceptor based on the command line arguments
func deriveInterceptor() throws -> (interceptor: EngineInterceptor?, finalize: () -> Void) {
var interceptors: [EngineInterceptor] = []
Expand Down