Skip to content

Commit

Permalink
fix extra Data init
Browse files Browse the repository at this point in the history
  • Loading branch information
qiweiii committed Jan 24, 2025
1 parent ba9a6a1 commit 0b005dc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
23 changes: 11 additions & 12 deletions PolkaVM/Sources/PolkaVM/Memory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<UInt8>) throws
func write(address: UInt32, values: Data) throws

func zero(pageIndex: UInt32, pages: Int) throws
func void(pageIndex: UInt32, pages: Int) throws
Expand Down Expand Up @@ -201,18 +201,17 @@ public class MemoryChunk {
}
}

public func write(address: UInt32, values: some Sequence<UInt8>) 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) {
Expand Down Expand Up @@ -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<UInt8>) 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)
Expand Down Expand Up @@ -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]))

Check warning on line 490 in PolkaVM/Sources/PolkaVM/Memory.swift

View check run for this annotation

Codecov / codecov/patch

PolkaVM/Sources/PolkaVM/Memory.swift#L490

Added line #L490 was not covered by tests
}

public func write(address: UInt32, values: some Sequence<UInt8>) 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)
Expand Down
2 changes: 1 addition & 1 deletion PolkaVM/Sources/PolkaVM/VMState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class VMState {
}

public func writeMemory(address: some FixedWidthInteger, values: some Sequence<UInt8>) 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 {
Expand Down
10 changes: 5 additions & 5 deletions PolkaVM/Tests/PolkaVMTests/MemoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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]))
}

Expand Down

0 comments on commit 0b005dc

Please sign in to comment.