-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework the memory reader api to require a buffer
- Loading branch information
1 parent
8420e45
commit 70af207
Showing
7 changed files
with
182 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 33 additions & 1 deletion
34
...nMain/kotlin/io/github/charlietap/chasm/executor/memory/read/MemoryInstanceBytesReader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,39 @@ | ||
@file:Suppress("NOTHING_TO_INLINE") | ||
|
||
package io.github.charlietap.chasm.executor.memory.read | ||
|
||
import com.github.michaelbull.result.Result | ||
import io.github.charlietap.chasm.executor.memory.ByteArrayLinearMemory | ||
import io.github.charlietap.chasm.executor.memory.LinearMemoryInteractor | ||
import io.github.charlietap.chasm.executor.memory.LinearMemoryInteractorImpl | ||
import io.github.charlietap.chasm.executor.runtime.error.InvocationError | ||
import io.github.charlietap.chasm.executor.runtime.instance.MemoryInstance | ||
|
||
typealias MemoryInstanceBytesReader = (MemoryInstance, Int, Int) -> Result<ByteArray, InvocationError.MemoryOperationOutOfBounds> | ||
typealias MemoryInstanceBytesReader = (MemoryInstance, ByteArray, Int, Int, Int) -> Result<ByteArray, InvocationError.MemoryOperationOutOfBounds> | ||
|
||
fun MemoryInstanceBytesReader( | ||
instance: MemoryInstance, | ||
buffer: ByteArray, | ||
memoryPointer: Int, | ||
bytesToRead: Int, | ||
bufferPointer: Int, | ||
): Result<ByteArray, InvocationError.MemoryOperationOutOfBounds> = | ||
MemoryInstanceBytesReader( | ||
instance = instance, | ||
buffer = buffer, | ||
memoryPointer = memoryPointer, | ||
bytesToRead = bytesToRead, | ||
bufferPointer = bufferPointer, | ||
linearMemoryInteractor = ::LinearMemoryInteractorImpl, | ||
) | ||
|
||
internal inline fun MemoryInstanceBytesReader( | ||
instance: MemoryInstance, | ||
buffer: ByteArray, | ||
memoryPointer: Int, | ||
bytesToRead: Int, | ||
bufferPointer: Int, | ||
linearMemoryInteractor: LinearMemoryInteractor<ByteArray>, | ||
): Result<ByteArray, InvocationError.MemoryOperationOutOfBounds> = linearMemoryInteractor(instance.data, memoryPointer, bytesToRead) { | ||
(instance.data as ByteArrayLinearMemory).memory.copyInto(buffer, bufferPointer, memoryPointer, memoryPointer + bytesToRead) | ||
} |
31 changes: 0 additions & 31 deletions
31
...n/kotlin/io/github/charlietap/chasm/executor/memory/read/MemoryInstanceBytesReaderImpl.kt
This file was deleted.
Oops, something went wrong.
83 changes: 83 additions & 0 deletions
83
...st/kotlin/io/github/charlietap/chasm/executor/memory/read/MemoryInstanceByteReaderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package io.github.charlietap.chasm.executor.memory.read | ||
|
||
import com.github.michaelbull.result.Err | ||
import com.github.michaelbull.result.Ok | ||
import io.github.charlietap.chasm.executor.memory.ByteArrayLinearMemory | ||
import io.github.charlietap.chasm.executor.memory.LinearMemoryInteractorImpl | ||
import io.github.charlietap.chasm.executor.runtime.error.InvocationError | ||
import io.github.charlietap.chasm.executor.runtime.memory.LinearMemory | ||
import io.github.charlietap.chasm.fixture.instance.memoryInstance | ||
import kotlin.test.Test | ||
import kotlin.test.assertContentEquals | ||
import kotlin.test.assertEquals | ||
|
||
class MemoryInstanceByteReaderTest { | ||
|
||
@Test | ||
fun `can read bytes from linear memory using a buffer `() { | ||
|
||
val memoryArray = byteArrayOf(0, 0, 117, 118, 0, 0) | ||
val memory = ByteArrayLinearMemory(min = LinearMemory.Pages(1), max = LinearMemory.Pages(1), memory = memoryArray) | ||
val instance = memoryInstance(data = memory) | ||
|
||
val memoryPointer = 2 | ||
val buffer = byteArrayOf(0, 0, 0, 0) | ||
val bufferPointer = 2 | ||
|
||
val actual = MemoryInstanceBytesReader( | ||
instance = instance, | ||
buffer = buffer, | ||
memoryPointer = memoryPointer, | ||
bytesToRead = 2, | ||
bufferPointer = bufferPointer, | ||
linearMemoryInteractor = ::LinearMemoryInteractorImpl, | ||
) | ||
|
||
assertEquals(Ok(buffer), actual) | ||
assertContentEquals(byteArrayOf(0, 0, 117, 118), buffer) | ||
} | ||
|
||
@Test | ||
fun `error if returned if read is out of the memory's bounds `() { | ||
|
||
val memory = ByteArrayLinearMemory(min = LinearMemory.Pages(1)) | ||
val instance = memoryInstance(data = memory) | ||
|
||
val memoryPointer = LinearMemory.PAGE_SIZE | ||
val buffer = byteArrayOf(0) | ||
val bufferPointer = 0 | ||
|
||
val actual = MemoryInstanceBytesReader( | ||
instance = instance, | ||
buffer = buffer, | ||
memoryPointer = memoryPointer, | ||
bytesToRead = 1, | ||
bufferPointer = bufferPointer, | ||
linearMemoryInteractor = ::LinearMemoryInteractorImpl, | ||
) | ||
|
||
assertEquals(Err(InvocationError.MemoryOperationOutOfBounds), actual) | ||
} | ||
|
||
@Test | ||
fun `error if returned if read is out of the buffer's bounds `() { | ||
|
||
val memory = ByteArrayLinearMemory(min = LinearMemory.Pages(1)) | ||
val instance = memoryInstance(data = memory) | ||
|
||
val memoryPointer = 0 | ||
val buffer = byteArrayOf(0) | ||
val bufferPointer = 1 | ||
|
||
val actual = MemoryInstanceBytesReader( | ||
instance = instance, | ||
buffer = buffer, | ||
memoryPointer = memoryPointer, | ||
bytesToRead = 1, | ||
bufferPointer = bufferPointer, | ||
linearMemoryInteractor = ::LinearMemoryInteractorImpl, | ||
) | ||
|
||
assertEquals(Err(InvocationError.MemoryOperationOutOfBounds), actual) | ||
} | ||
} |