diff --git a/PolkaVM/Sources/PolkaVM/Memory.swift b/PolkaVM/Sources/PolkaVM/Memory.swift index 76df3ed0..5ff9e6c3 100644 --- a/PolkaVM/Sources/PolkaVM/Memory.swift +++ b/PolkaVM/Sources/PolkaVM/Memory.swift @@ -67,7 +67,7 @@ public protocol Memory { func read(address: UInt32) throws -> UInt8 func read(address: UInt32, length: Int) throws -> Data func write(address: UInt32, value: UInt8) throws - func write(address: UInt32, values: some Sequence) throws + func write(address: UInt32, values: Data) throws func zero(pageIndex: UInt32, pages: Int) throws func void(pageIndex: UInt32, pages: Int) throws @@ -201,18 +201,17 @@ public class MemoryChunk { } } - public func write(address: UInt32, values: some Sequence) throws(MemoryError) { - let valuesData = Data(values) - guard startAddress <= address, address + UInt32(valuesData.count) <= endAddress else { + public func write(address: UInt32, values: Data) throws(MemoryError) { + guard startAddress <= address, address + UInt32(values.count) <= endAddress else { throw .exceedChunkBoundary(address) } let startIndex = Int(address - startAddress) + data.startIndex - let endIndex = startIndex + valuesData.count + let endIndex = startIndex + values.count try zeroPad(until: startAddress + UInt32(endIndex)) - data.replaceSubrange(startIndex ..< endIndex, with: valuesData) + data.replaceSubrange(startIndex ..< endIndex, with: values) } public func incrementEnd(size increment: UInt32) throws(MemoryError) { @@ -335,11 +334,11 @@ public class StandardMemory: Memory { guard isWritable(address: address, length: 1) else { throw .notWritable(address) } - try getChunk(address: address).write(address: address, values: [value]) + try getChunk(address: address).write(address: address, values: Data([value])) } - public func write(address: UInt32, values: some Sequence) throws(MemoryError) { - guard isWritable(address: address, length: Data(values).count) else { + public func write(address: UInt32, values: Data) throws(MemoryError) { + guard isWritable(address: address, length: values.count) else { throw .notWritable(address) } try getChunk(address: address).write(address: address, values: values) @@ -488,11 +487,11 @@ public class GeneralMemory: Memory { guard isWritable(address: address, length: 1) else { throw .notWritable(address) } - try getChunk(address: address).write(address: address, values: [value]) + try getChunk(address: address).write(address: address, values: Data([value])) } - public func write(address: UInt32, values: some Sequence) throws(MemoryError) { - guard isWritable(address: address, length: Data(values).count) else { + public func write(address: UInt32, values: Data) throws(MemoryError) { + guard isWritable(address: address, length: values.count) else { throw .notWritable(address) } try getChunk(address: address).write(address: address, values: values) diff --git a/PolkaVM/Sources/PolkaVM/VMState.swift b/PolkaVM/Sources/PolkaVM/VMState.swift index 78d1de5a..e43e3d99 100644 --- a/PolkaVM/Sources/PolkaVM/VMState.swift +++ b/PolkaVM/Sources/PolkaVM/VMState.swift @@ -65,7 +65,7 @@ public class VMState { } public func writeMemory(address: some FixedWidthInteger, values: some Sequence) throws { - try memory.write(address: UInt32(truncatingIfNeeded: address), values: values) + try memory.write(address: UInt32(truncatingIfNeeded: address), values: Data(values)) } public func sbrk(_ increment: UInt32) throws -> UInt32 { diff --git a/PolkaVM/Tests/PolkaVMTests/MemoryTests.swift b/PolkaVM/Tests/PolkaVMTests/MemoryTests.swift index 9f7e4469..9431dd51 100644 --- a/PolkaVM/Tests/PolkaVMTests/MemoryTests.swift +++ b/PolkaVM/Tests/PolkaVMTests/MemoryTests.swift @@ -80,7 +80,7 @@ enum MemoryTests { @Test func write() throws { let chunk = try MemoryChunk(startAddress: 0, endAddress: 10, data: Data()) - try chunk.write(address: 0, values: [1]) + try chunk.write(address: 0, values: Data([1])) #expect(chunk.data == Data([1])) try chunk.write(address: 1, values: Data([2])) #expect(chunk.data == Data([1, 2])) @@ -199,7 +199,7 @@ enum MemoryTests { #expect(memory.isWritable(address: stackStart, length: Int(stackEnd - stackStart)) == true) try memory.write(address: stackStart, value: 1) #expect(try memory.read(address: stackStart, length: 2) == Data([1, 0])) - try memory.write(address: stackEnd - 2, values: [1, 2]) + try memory.write(address: stackEnd - 2, values: Data([1, 2])) #expect(try memory.read(address: stackEnd - 4, length: 4) == Data([0, 0, 1, 2])) // argument @@ -260,9 +260,9 @@ enum MemoryTests { } @Test func write() throws { - try memory.write(address: 2, values: [9, 8]) + try memory.write(address: 2, values: Data([9, 8])) #expect(try memory.read(address: 0, length: 4) == Data([1, 2, 9, 8])) - #expect(throws: MemoryError.notWritable(4096)) { try memory.write(address: 4096, values: [0]) } + #expect(throws: MemoryError.notWritable(4096)) { try memory.write(address: 4096, values: Data([0])) } } @Test func sbrk() throws { @@ -271,7 +271,7 @@ enum MemoryTests { #expect(memory.isWritable(address: oldEnd, length: 512) == true) #expect(memory.isWritable(address: 0, length: Int(oldEnd)) == true) - try memory.write(address: oldEnd, values: [1, 2, 3]) + try memory.write(address: oldEnd, values: Data([1, 2, 3])) #expect(try memory.read(address: oldEnd - 1, length: 5) == Data([7, 1, 2, 3, 0])) }