@@ -12,8 +12,6 @@ import WasmTypes
1212public struct Parser < Stream: ByteStream > {
1313 @usableFromInline
1414 let stream : Stream
15- public private( set) var hasDataCount : Bool = false
16- public let features : WasmFeatureSet
1715 let limits : ParsingLimits
1816 var orderTracking = OrderTracking ( )
1917
@@ -23,14 +21,14 @@ public struct Parser<Stream: ByteStream> {
2321 }
2422 private var nextParseTarget : NextParseTarget
2523
24+ public let features : WasmFeatureSet
2625 public var offset : Int {
2726 return stream. currentIndex
2827 }
2928
30- public init ( stream: Stream , features: WasmFeatureSet = . default, hasDataCount : Bool = false ) {
29+ public init ( stream: Stream , features: WasmFeatureSet = . default) {
3130 self . stream = stream
3231 self . features = features
33- self . hasDataCount = hasDataCount
3432 self . nextParseTarget = . header
3533 self . limits = . default
3634 }
@@ -121,7 +119,7 @@ extension Code {
121119 /// ````
122120 @inlinable
123121 public func parseExpression< V: InstructionVisitor > ( visitor: inout V ) throws {
124- let parser = Parser ( stream: StaticByteStream ( bytes: self . expression) , features: self . features, hasDataCount : self . hasDataCount )
122+ let parser = Parser ( stream: StaticByteStream ( bytes: self . expression) , features: self . features)
125123 var lastCode : InstructionCode ?
126124 while try ! parser. stream. hasReachedEnd ( ) {
127125 lastCode = try parser. parseInstruction ( visitor: & visitor)
@@ -153,8 +151,7 @@ public struct ExpressionParser {
153151 public init ( code: Code ) {
154152 self . parser = Parser (
155153 stream: StaticByteStream ( bytes: code. expression) ,
156- features: code. features,
157- hasDataCount: code. hasDataCount
154+ features: code. features
158155 )
159156 self . codeOffset = code. offset
160157 self . initialStreamOffset = self . parser. offset
@@ -166,7 +163,7 @@ public struct ExpressionParser {
166163 let shouldContinue = try ! parser. stream. hasReachedEnd ( )
167164 if !shouldContinue {
168165 guard lastCode == . end else {
169- throw parser . makeError ( . endOpcodeExpected)
166+ throw WasmParserError ( . endOpcodeExpected, offset : offset )
170167 }
171168 }
172169 return shouldContinue
@@ -212,6 +209,7 @@ public struct WasmParserError: Swift.Error {
212209 let message : Message
213210 let offset : Int
214211
212+ @usableFromInline
215213 init ( _ message: Message , offset: Int ) {
216214 self . message = message
217215 self . offset = offset
@@ -300,8 +298,6 @@ extension WasmParserError.Message {
300298
301299 static let sectionOutOfOrder = Self ( " Sections in the module are out of order " )
302300
303- @usableFromInline static let dataCountSectionRequired = Self ( " Data count section is required but not found " )
304-
305301 static func malformedLimit( _ byte: UInt8 ) -> Self {
306302 Self ( " Malformed limit: \( byte) " )
307303 }
@@ -874,24 +870,13 @@ extension Parser {
874870 case 7 : return try v. visitConversion ( . i64TruncSatF64U)
875871 case 8 :
876872 let dataIndex : DataIndex = try parseUnsigned ( )
877- // memory.init requires data count section
878- // https://webassembly.github.io/spec/core/binary/modules.html#data-count-section
879- guard hasDataCount else {
880- throw makeError ( . dataCountSectionRequired)
881- }
882-
883873 let zero = try stream. consumeAny ( )
884874 guard zero == 0x00 else {
885875 throw makeError ( . zeroExpected( actual: zero) )
886876 }
887877
888878 return try v. visitMemoryInit ( dataIndex: dataIndex)
889879 case 9 :
890- // memory.drop requires data count section
891- // https://webassembly.github.io/spec/core/binary/modules.html#data-count-section
892- guard hasDataCount else {
893- throw makeError ( . dataCountSectionRequired)
894- }
895880 return try v. visitDataDrop ( dataIndex: try parseUnsigned ( ) )
896881 case 10 :
897882 let ( zero1, zero2) = try ( stream. consumeAny ( ) , stream. consumeAny ( ) )
@@ -1140,7 +1125,7 @@ extension Parser {
11401125 )
11411126 return Code (
11421127 locals: locals, expression: expressionBytes,
1143- offset: expressionStart, hasDataCount : hasDataCount , features: features
1128+ offset: expressionStart, features: features
11441129 )
11451130 }
11461131 }
@@ -1327,7 +1312,6 @@ extension Parser {
13271312 payload = . dataSection( try parseDataSection ( ) )
13281313 case 12 :
13291314 order = . dataCount
1330- hasDataCount = true
13311315 payload = . dataCount( try parseDataCountSection ( ) )
13321316 default :
13331317 throw makeError ( . malformedSectionID( sectionID) )
0 commit comments