From 998c2ddf2041f7b5ba6760131ac2db865007500d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Mon, 1 Jul 2024 23:24:40 +0900 Subject: [PATCH] Fix module file reading on Windows `open` on Windows uses "text-mode", so we need to explicitly specify O_BINARY. --- Sources/WasmKit/ModuleParser.swift | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Sources/WasmKit/ModuleParser.swift b/Sources/WasmKit/ModuleParser.swift index 92a819d4..64eb94f1 100644 --- a/Sources/WasmKit/ModuleParser.swift +++ b/Sources/WasmKit/ModuleParser.swift @@ -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: 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)