Skip to content

Commit 7cc9d65

Browse files
pcbeardkateinoigakukun
authored andcommitted
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.
1 parent a2200fc commit 7cc9d65

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

Sources/CLI/Commands/Parse.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import WAT
2+
import WasmKit
3+
import SystemPackage
4+
5+
/// Parses a `.wasm` or `.wat` module.
6+
func parseWasm(filePath: FilePath) throws -> Module {
7+
if filePath.extension == "wat", #available(macOS 11.0, iOS 14.0, macCatalyst 14.0, tvOS 14.0, visionOS 1.0, watchOS 7.0, *) {
8+
let fileHandle = try FileDescriptor.open(filePath, .readOnly)
9+
defer { try? fileHandle.close() }
10+
11+
let size = try fileHandle.seek(offset: 0, from: .end)
12+
13+
let wat = try String(unsafeUninitializedCapacity: Int(size)) {
14+
try fileHandle.read(fromAbsoluteOffset: 0, into: .init($0))
15+
}
16+
return try WasmKit.parseWasm(bytes: wat2wasm(wat))
17+
} else {
18+
return try WasmKit.parseWasm(filePath: filePath)
19+
}
20+
}

Sources/CLI/Commands/Run.swift

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

@@ -120,12 +119,12 @@ struct Run: ParsableCommand {
120119
let module: Module
121120
if verbose, #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) {
122121
let (parsedModule, parseTime) = try measure {
123-
try self.parseWasm(filePath: FilePath(path))
122+
try parseWasm(filePath: FilePath(path))
124123
}
125124
log("Finished parsing module: \(parseTime)", verbose: true)
126125
module = parsedModule
127126
} else {
128-
module = try self.parseWasm(filePath: FilePath(path))
127+
module = try parseWasm(filePath: FilePath(path))
129128
}
130129

131130
let (interceptor, finalize) = try deriveInterceptor()
@@ -149,23 +148,6 @@ struct Run: ParsableCommand {
149148
}
150149
}
151150

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-
169151
/// Derives the runtime interceptor based on the command line arguments
170152
func deriveInterceptor() throws -> (interceptor: EngineInterceptor?, finalize: () -> Void) {
171153
var interceptors: [EngineInterceptor] = []

0 commit comments

Comments
 (0)