Skip to content

Support exploring ".wat" files #186

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions Sources/CLI/Commands/Parse.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
22 changes: 2 additions & 20 deletions Sources/CLI/Commands/Run.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import ArgumentParser
import SystemPackage
import WAT
import WasmKit
import WasmKitWASI

Expand Down Expand Up @@ -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()
Expand All @@ -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] = []
Expand Down
Loading