Skip to content

Commit 0db2c80

Browse files
WasmParser: Remove consume(_ expected: Set<UInt8>) from ByteStream (#367)
We don't need `Set` allocation, and ByteStream should not be responsible for validation.
1 parent 5d33a56 commit 0db2c80

3 files changed

Lines changed: 8 additions & 48 deletions

File tree

Sources/WasmParser/Stream/ByteStream.swift

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@ public protocol ByteStream: ~Copyable {
22
var currentIndex: Int { get }
33

44
func consumeAny() throws(WasmParserError) -> UInt8
5-
func consume(_ expected: Set<UInt8>) throws(WasmParserError) -> UInt8
65
func consume(count: Int) throws(WasmParserError) -> ArraySlice<UInt8>
76

87
func peek() throws(WasmParserError) -> UInt8?
98
}
109

1110
extension ByteStream {
12-
func consume(_ expected: UInt8) throws(WasmParserError) -> UInt8 {
13-
try consume(Set([expected]))
14-
}
15-
1611
@usableFromInline
1712
func hasReachedEnd() throws(WasmParserError) -> Bool {
1813
try peek() == nil
@@ -43,25 +38,6 @@ public final class StaticByteStream: ByteStream {
4338
return consumed
4439
}
4540

46-
@discardableResult
47-
public func consume(_ expected: Set<UInt8>) throws(WasmParserError) -> UInt8 {
48-
guard bytes.indices.contains(currentIndex) else {
49-
throw WasmParserError(kind: .parserUnexpectedEnd(expected: Set(expected)), offset: currentIndex)
50-
}
51-
52-
let consumed = bytes[currentIndex]
53-
guard expected.contains(consumed) else {
54-
throw WasmParserError(
55-
kind: .parserUnexpectedByte(
56-
consumed,
57-
expected: Set(expected)
58-
), offset: currentIndex)
59-
}
60-
61-
currentIndex = bytes.index(after: currentIndex)
62-
return consumed
63-
}
64-
6541
public func consume(count: Int) throws(WasmParserError) -> ArraySlice<UInt8> {
6642
guard count > 0 else { return [] }
6743
let updatedIndex = currentIndex + count

Sources/WasmParser/Stream/FileHandleStream.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ public final class FileHandleStream: ByteStream {
4040
return consumed
4141
}
4242

43-
@discardableResult
44-
public func consume(_ expected: Set<UInt8>) throws(WasmParserError) -> UInt8 {
45-
guard let consumed = try peek() else {
46-
throw WasmParserError(kind: .parserUnexpectedEnd(expected: Set(expected)), offset: currentIndex)
47-
}
48-
guard expected.contains(consumed) else {
49-
throw WasmParserError(
50-
kind: .parserUnexpectedByte(
51-
consumed,
52-
expected: Set(expected)
53-
), offset: currentIndex)
54-
}
55-
currentIndex = bytes.index(after: currentIndex)
56-
return consumed
57-
}
58-
5943
public func consume(count: Int) throws(WasmParserError) -> ArraySlice<UInt8> {
6044
let bytesToRead = currentIndex + count - endOffset
6145

Sources/WasmParser/WasmParser.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -824,19 +824,19 @@ extension Parser {
824824
/// > Note:
825825
/// <https://webassembly.github.io/spec/core/binary/modules.html#binary-importdesc>
826826
func parseImportDescriptor() throws(WasmParserError) -> ImportDescriptor {
827-
let maxKind: UInt8 = features.contains(.exceptionHandling) ? 0x04 : 0x03
828-
let b = try stream.consume(Set(0x00...maxKind))
827+
let descriptorOffset = stream.currentIndex
828+
let b = try stream.consumeAny()
829829
switch b {
830830
case 0x00: return try .function(parseUnsigned())
831831
case 0x01: return try .table(parseTableType())
832832
case 0x02: return try .memory(parseMemoryType())
833833
case 0x03: return try .global(parseGlobalType())
834-
case 0x04:
834+
case 0x04 where features.contains(.exceptionHandling):
835835
let attribute: UInt8 = try parseUnsigned()
836836
guard attribute == 0 else { throw makeError(.invalidTagAttribute(attribute)) }
837837
return try .tag(parseUnsigned())
838838
default:
839-
preconditionFailure("should never reach here")
839+
throw WasmParserError(kind: .parserUnexpectedByte(b, expected: nil), offset: descriptorOffset)
840840
}
841841
}
842842

@@ -898,16 +898,16 @@ extension Parser {
898898
/// > Note:
899899
/// <https://webassembly.github.io/spec/core/binary/modules.html#binary-exportdesc>
900900
func parseExportDescriptor() throws(WasmParserError) -> ExportDescriptor {
901-
let maxKind: UInt8 = features.contains(.exceptionHandling) ? 0x04 : 0x03
902-
let b = try stream.consume(Set(0x00...maxKind))
901+
let descriptorOffset = stream.currentIndex
902+
let b = try stream.consumeAny()
903903
switch b {
904904
case 0x00: return try .function(parseUnsigned())
905905
case 0x01: return try .table(parseUnsigned())
906906
case 0x02: return try .memory(parseUnsigned())
907907
case 0x03: return try .global(parseUnsigned())
908-
case 0x04: return try .tag(parseUnsigned())
908+
case 0x04 where features.contains(.exceptionHandling): return try .tag(parseUnsigned())
909909
default:
910-
preconditionFailure("should never reach here")
910+
throw WasmParserError(kind: .parserUnexpectedByte(b, expected: nil), offset: descriptorOffset)
911911
}
912912
}
913913

0 commit comments

Comments
 (0)