Skip to content

Commit

Permalink
Fix module file reading on Windows
Browse files Browse the repository at this point in the history
`open` on Windows uses "text-mode", so we need to explicitly specify
O_BINARY.
  • Loading branch information
kateinoigakukun committed Jul 1, 2024
1 parent 2670f82 commit 998c2dd
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Sources/WasmKit/ModuleParser.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
import SystemPackage
import WasmParser

#if os(Windows)
import ucrt

extension FileDescriptor.AccessMode {
static var binary: FileDescriptor.AccessMode {
FileDescriptor.AccessMode(rawValue: O_BINARY)
}
}

#endif


/// Parse a given file as a WebAssembly binary format file
/// > Note: <https://webassembly.github.io/spec/core/binary/index.html>
public func parseWasm(filePath: FilePath, features: WasmFeatureSet = .default) throws -> Module {
let fileHandle = try FileDescriptor.open(filePath, .readOnly)
#if os(Windows)
// TODO: Upstream `O_BINARY` to `SystemPackage
let accessMode = FileDescriptor.AccessMode(
rawValue: FileDescriptor.AccessMode.readOnly.rawValue | FileDescriptor.AccessMode.binary.rawValue
)
#else
let accessMode: FileDescriptor.AccessMode = .readOnly
#endif
let fileHandle = try FileDescriptor.open(filePath, accessMode)
defer { try? fileHandle.close() }
let stream = try FileHandleStream(fileHandle: fileHandle)
let module = try parseModule(stream: stream, features: features)
Expand Down

0 comments on commit 998c2dd

Please sign in to comment.