diff --git a/Sources/WasmParser/WasmParser.swift b/Sources/WasmParser/WasmParser.swift index 88861c37..e74a7acb 100644 --- a/Sources/WasmParser/WasmParser.swift +++ b/Sources/WasmParser/WasmParser.swift @@ -129,13 +129,19 @@ extension Code { // TODO: Move `doParseInstruction` under `ExpressionParser` struct @_documentation(visibility: internal) public struct ExpressionParser { + /// The byte offset of the code in the module + let codeOffset: Int + /// The initial byte offset of the code buffer stream + /// NOTE: This might be different from `codeOffset` if the code buffer + /// is not a part of the initial `FileHandleStream` buffer + let initialStreamOffset: Int @usableFromInline let parser: Parser @usableFromInline var lastCode: InstructionCode? public var offset: Int { - self.parser.currentIndex + self.codeOffset + self.parser.currentIndex - self.initialStreamOffset } public init(code: Code) { @@ -144,6 +150,8 @@ public struct ExpressionParser { features: code.features, hasDataCount: code.hasDataCount ) + self.codeOffset = code.offset + self.initialStreamOffset = self.parser.currentIndex } @inlinable @@ -1055,10 +1063,14 @@ extension Parser { let locals = localTypes.flatMap { (n: UInt32, type: ValueType) in return Array(repeating: type, count: Int(n)) } + let expressionStart = stream.currentIndex let expressionBytes = try stream.consume( - count: Int(size) - (stream.currentIndex - bodyStart) + count: Int(size) - (expressionStart - bodyStart) + ) + return Code( + locals: locals, expression: expressionBytes, + offset: expressionStart, hasDataCount: hasDataCount, features: features ) - return Code(locals: locals, expression: expressionBytes, hasDataCount: hasDataCount, features: features) } } diff --git a/Sources/WasmParser/WasmTypes.swift b/Sources/WasmParser/WasmTypes.swift index 7a315e96..f3bdf86b 100644 --- a/Sources/WasmParser/WasmTypes.swift +++ b/Sources/WasmParser/WasmTypes.swift @@ -11,6 +11,8 @@ public struct Code { // Parser state used to parse the expression body lazily @usableFromInline + internal let offset: Int + @usableFromInline internal let hasDataCount: Bool @usableFromInline internal let features: WasmFeatureSet