2018-01-16 17:05:20 +00:00
|
|
|
import
|
2018-01-22 22:23:07 +00:00
|
|
|
sequtils, bigints,
|
2018-01-29 17:40:22 +00:00
|
|
|
../constants, ../errors, ../logging, ../validation, ../utils_numeric, ../utils/bytes
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Memory* = ref object
|
|
|
|
logger*: Logger
|
2018-01-29 17:40:22 +00:00
|
|
|
bytes*: seq[byte]
|
2018-01-16 17:05:20 +00:00
|
|
|
|
|
|
|
proc newMemory*: Memory =
|
|
|
|
new(result)
|
|
|
|
result.bytes = @[]
|
|
|
|
result.logger = logging.getLogger("evm.vm.memory.Memory")
|
|
|
|
|
|
|
|
proc len*(memory: Memory): int =
|
|
|
|
result = len(memory.bytes)
|
|
|
|
|
|
|
|
proc extend*(memory: var Memory; startPosition: Int256; size: Int256) =
|
|
|
|
if size == 0:
|
|
|
|
return
|
|
|
|
var newSize = ceil32(startPosition + size)
|
2018-01-22 22:23:07 +00:00
|
|
|
if newSize <= len(memory).int256:
|
2018-01-16 17:05:20 +00:00
|
|
|
return
|
2018-01-22 22:23:07 +00:00
|
|
|
var sizeToExtend = newSize - len(memory).int256
|
|
|
|
memory.bytes = memory.bytes.concat(repeat(0.byte, sizeToExtend.getInt))
|
2018-01-16 17:05:20 +00:00
|
|
|
|
2018-01-24 13:31:24 +00:00
|
|
|
proc newMemory*(size: Int256): Memory =
|
|
|
|
result = newMemory()
|
|
|
|
result.extend(0.int256, size)
|
|
|
|
|
2018-01-29 17:40:22 +00:00
|
|
|
proc read*(memory: var Memory, startPosition: Int256, size: Int256): seq[byte] =
|
|
|
|
result = memory.bytes[startPosition.getInt ..< (startPosition + size).getInt]
|
2018-01-22 22:23:07 +00:00
|
|
|
|
2018-01-29 17:40:22 +00:00
|
|
|
proc write*(memory: var Memory, startPosition: Int256, size: Int256, value: seq[byte]) =
|
2018-01-22 22:23:07 +00:00
|
|
|
echo value
|
2018-01-29 17:40:22 +00:00
|
|
|
|
|
|
|
template write*(memory: var Memory, startPosition: Int256, size: Int256, value: cstring) =
|
|
|
|
memory.write(startPosition, size, value.toBytes)
|