From 7b46dcff8ca08d5c91a32be17428e002d0589f00 Mon Sep 17 00:00:00 2001 From: pcbeard Date: Fri, 13 Jun 2025 16:43:18 +0200 Subject: [PATCH] Support exploring ".wat" files Convert parseWasm() to be a global function instead of an an instance method of the Run command. This wraps WasmKit.parseWasm() and supports both ".wat" and ".wasm" files. --- Sources/CLI/Commands/Parse.swift | 20 ++++++++++++++++++++ Sources/CLI/Commands/Run.swift | 22 ++-------------------- 2 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 Sources/CLI/Commands/Parse.swift diff --git a/Sources/CLI/Commands/Parse.swift b/Sources/CLI/Commands/Parse.swift new file mode 100644 index 00000000..e1eaf3ca --- /dev/null +++ b/Sources/CLI/Commands/Parse.swift @@ -0,0 +1,20 @@ +import WAT +import WasmKit +import SystemPackage + +/// 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) + } +} diff --git a/Sources/CLI/Commands/Run.swift b/Sources/CLI/Commands/Run.swift index 3c690e81..e7461fae 100644 --- a/Sources/CLI/Commands/Run.swift +++ b/Sources/CLI/Commands/Run.swift @@ -1,6 +1,5 @@ import ArgumentParser import SystemPackage -import WAT import WasmKit import WasmKitWASI @@ -120,12 +119,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 self.parseWasm(filePath: FilePath(path)) + try parseWasm(filePath: FilePath(path)) } log("Finished parsing module: \(parseTime)", verbose: true) module = parsedModule } else { - module = try self.parseWasm(filePath: FilePath(path)) + module = try parseWasm(filePath: FilePath(path)) } let (interceptor, finalize) = try deriveInterceptor() @@ -149,23 +148,6 @@ 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] = []