|
1 | | -import struct SystemPackage.FileDescriptor |
| 1 | +// "FileSystem" trait can be turned off to support embedded platforms |
| 2 | +#if FileSystem |
2 | 3 |
|
3 | | -public final class FileHandleStream: ByteStream { |
4 | | - private(set) public var currentIndex: Int = 0 |
| 4 | + import struct SystemPackage.FileDescriptor |
| 5 | + import struct SystemPackage.FilePath |
5 | 6 |
|
6 | | - private let fileHandle: FileDescriptor |
7 | | - private let bufferLength: Int |
| 7 | + #if os(Windows) |
| 8 | + import ucrt |
| 9 | + #endif |
8 | 10 |
|
9 | | - private var endOffset: Int = 0 |
10 | | - private var startOffset: Int = 0 |
11 | | - private var bytes: [UInt8] = [] |
| 11 | + extension Parser where Stream == FileHandleStream { |
12 | 12 |
|
13 | | - public init(fileHandle: FileDescriptor, bufferLength: Int = 1024 * 8) throws { |
14 | | - self.fileHandle = fileHandle |
15 | | - self.bufferLength = bufferLength |
| 13 | + /// Initialize a new parser with the given file handle |
| 14 | + /// |
| 15 | + /// - Parameters: |
| 16 | + /// - fileHandle: The file handle to the WebAssembly binary file to parse |
| 17 | + /// - features: Enabled WebAssembly features for parsing |
| 18 | + public init(fileHandle: FileDescriptor, features: WasmFeatureSet = .default) throws { |
| 19 | + self.init(stream: try FileHandleStream(fileHandle: fileHandle), features: features) |
| 20 | + } |
16 | 21 |
|
17 | | - try readMoreIfNeeded() |
| 22 | + /// Initialize a new parser with the given file path |
| 23 | + /// |
| 24 | + /// - Parameters: |
| 25 | + /// - filePath: The file path to the WebAssembly binary file to parse |
| 26 | + /// - features: Enabled WebAssembly features for parsing |
| 27 | + public init(filePath: FilePath, features: WasmFeatureSet = .default) throws { |
| 28 | + #if os(Windows) |
| 29 | + // TODO: Upstream `O_BINARY` to `SystemPackage |
| 30 | + let accessMode = FileDescriptor.AccessMode( |
| 31 | + rawValue: FileDescriptor.AccessMode.readOnly.rawValue | O_BINARY |
| 32 | + ) |
| 33 | + #else |
| 34 | + let accessMode: FileDescriptor.AccessMode = .readOnly |
| 35 | + #endif |
| 36 | + let fileHandle = try FileDescriptor.open(filePath, accessMode) |
| 37 | + self.init(stream: try FileHandleStream(fileHandle: fileHandle), features: features) |
| 38 | + } |
18 | 39 | } |
19 | 40 |
|
20 | | - private func readMoreIfNeeded() throws(WasmParserError) { |
21 | | - guard Int(endOffset) == currentIndex else { return } |
22 | | - startOffset = currentIndex |
| 41 | + public final class FileHandleStream: ByteStream { |
| 42 | + private(set) public var currentIndex: Int = 0 |
23 | 43 |
|
24 | | - do { |
25 | | - let data = try fileHandle.read(upToCount: bufferLength) |
| 44 | + private let fileHandle: FileDescriptor |
| 45 | + private let bufferLength: Int |
26 | 46 |
|
27 | | - bytes = [UInt8](data) |
28 | | - } catch { |
29 | | - throw WasmParserError("I/O error: \(error)", offset: currentIndex) |
30 | | - } |
31 | | - endOffset = startOffset + bytes.count |
32 | | - } |
| 47 | + private var endOffset: Int = 0 |
| 48 | + private var startOffset: Int = 0 |
| 49 | + private var bytes: [UInt8] = [] |
| 50 | + |
| 51 | + public init(fileHandle: FileDescriptor, bufferLength: Int = 1024 * 8) throws { |
| 52 | + self.fileHandle = fileHandle |
| 53 | + self.bufferLength = bufferLength |
33 | 54 |
|
34 | | - @discardableResult |
35 | | - public func consumeAny() throws(WasmParserError) -> UInt8 { |
36 | | - guard let consumed = try peek() else { |
37 | | - throw WasmParserError(message: .unexpectedEnd, offset: currentIndex) |
| 55 | + try readMoreIfNeeded() |
38 | 56 | } |
39 | | - currentIndex = bytes.index(after: currentIndex) |
40 | | - return consumed |
41 | | - } |
42 | 57 |
|
43 | | - public func consume(count: Int) throws(WasmParserError) -> ArraySlice<UInt8> { |
44 | | - let bytesToRead = currentIndex + count - endOffset |
| 58 | + private func readMoreIfNeeded() throws(WasmParserError) { |
| 59 | + guard Int(endOffset) == currentIndex else { return } |
| 60 | + startOffset = currentIndex |
45 | 61 |
|
46 | | - guard bytesToRead > 0 else { |
47 | | - let bytesIndex = currentIndex - startOffset |
48 | | - let result = bytes[bytesIndex..<bytesIndex + count] |
49 | | - currentIndex = currentIndex + count |
50 | | - return result |
51 | | - } |
| 62 | + do { |
| 63 | + let data = try fileHandle.read(upToCount: bufferLength) |
52 | 64 |
|
53 | | - let data: [UInt8] |
54 | | - do { |
55 | | - data = try fileHandle.read(upToCount: bytesToRead) |
56 | | - } catch { |
57 | | - throw WasmParserError("I/O error: \(error)", offset: currentIndex) |
| 65 | + bytes = [UInt8](data) |
| 66 | + } catch { |
| 67 | + throw WasmParserError("I/O error: \(error)", offset: currentIndex) |
| 68 | + } |
| 69 | + endOffset = startOffset + bytes.count |
58 | 70 | } |
59 | | - guard data.count == bytesToRead else { |
60 | | - throw WasmParserError(kind: .parserUnexpectedEnd(expected: nil), offset: currentIndex) |
| 71 | + |
| 72 | + @discardableResult |
| 73 | + public func consumeAny() throws(WasmParserError) -> UInt8 { |
| 74 | + guard let consumed = try peek() else { |
| 75 | + throw WasmParserError(message: .unexpectedEnd, offset: currentIndex) |
| 76 | + } |
| 77 | + currentIndex = bytes.index(after: currentIndex) |
| 78 | + return consumed |
61 | 79 | } |
62 | 80 |
|
63 | | - bytes.append(contentsOf: [UInt8](data)) |
64 | | - endOffset = endOffset + data.count |
| 81 | + public func consume(count: Int) throws(WasmParserError) -> ArraySlice<UInt8> { |
| 82 | + let bytesToRead = currentIndex + count - endOffset |
| 83 | + |
| 84 | + guard bytesToRead > 0 else { |
| 85 | + let bytesIndex = currentIndex - startOffset |
| 86 | + let result = bytes[bytesIndex..<bytesIndex + count] |
| 87 | + currentIndex = currentIndex + count |
| 88 | + return result |
| 89 | + } |
| 90 | + |
| 91 | + let data: [UInt8] |
| 92 | + do { |
| 93 | + data = try fileHandle.read(upToCount: bytesToRead) |
| 94 | + } catch { |
| 95 | + throw WasmParserError("I/O error: \(error)", offset: currentIndex) |
| 96 | + } |
| 97 | + guard data.count == bytesToRead else { |
| 98 | + throw WasmParserError(kind: .parserUnexpectedEnd(expected: nil), offset: currentIndex) |
| 99 | + } |
| 100 | + |
| 101 | + bytes.append(contentsOf: [UInt8](data)) |
| 102 | + endOffset = endOffset + data.count |
65 | 103 |
|
66 | | - let bytesIndex = currentIndex - startOffset |
67 | | - let result = bytes[bytesIndex..<bytesIndex + count] |
| 104 | + let bytesIndex = currentIndex - startOffset |
| 105 | + let result = bytes[bytesIndex..<bytesIndex + count] |
68 | 106 |
|
69 | | - currentIndex = endOffset |
| 107 | + currentIndex = endOffset |
70 | 108 |
|
71 | | - return result |
72 | | - } |
| 109 | + return result |
| 110 | + } |
73 | 111 |
|
74 | | - public func peek() throws(WasmParserError) -> UInt8? { |
75 | | - try readMoreIfNeeded() |
| 112 | + public func peek() throws(WasmParserError) -> UInt8? { |
| 113 | + try readMoreIfNeeded() |
76 | 114 |
|
77 | | - let index = currentIndex - startOffset |
78 | | - guard bytes.indices.contains(index) else { |
79 | | - return nil |
80 | | - } |
| 115 | + let index = currentIndex - startOffset |
| 116 | + guard bytes.indices.contains(index) else { |
| 117 | + return nil |
| 118 | + } |
81 | 119 |
|
82 | | - return bytes[index] |
| 120 | + return bytes[index] |
| 121 | + } |
83 | 122 | } |
84 | | -} |
85 | 123 |
|
86 | | -extension FileDescriptor { |
87 | | - fileprivate func read(upToCount maxLength: Int) throws -> [UInt8] { |
88 | | - try [UInt8](unsafeUninitializedCapacity: maxLength) { buffer, outCount in |
89 | | - outCount = try read(into: UnsafeMutableRawBufferPointer(buffer)) |
| 124 | + extension FileDescriptor { |
| 125 | + fileprivate func read(upToCount maxLength: Int) throws -> [UInt8] { |
| 126 | + try [UInt8](unsafeUninitializedCapacity: maxLength) { buffer, outCount in |
| 127 | + outCount = try read(into: UnsafeMutableRawBufferPointer(buffer)) |
| 128 | + } |
90 | 129 | } |
91 | 130 | } |
92 | | -} |
| 131 | + |
| 132 | +#endif |
0 commit comments