Initial work for loadBytes and removed dumpBytes

This commit is contained in:
Nocturn9x 2021-11-12 16:35:55 +01:00
parent 3d4680e94d
commit df67e7db64
2 changed files with 12 additions and 11 deletions

View File

@ -25,7 +25,6 @@ import times
export ast
type
Serializer* = ref object
file: string
@ -80,7 +79,7 @@ proc dumpBytes*(self: Serializer, chunk: Chunk, file, filename: string): seq[byt
self.file = file
self.filename = filename
self.chunk = chunk
result.extend(self.toBytes("JAPL_BYTECODE"))
result.extend(self.toBytes(BYTECODE_MARKER))
result.add(byte(len(JAPL_BRANCH)))
result.extend(self.toBytes(JAPL_BRANCH))
if len(JAPL_COMMIT_HASH) != 40:
@ -126,16 +125,18 @@ proc dumpBytes*(self: Serializer, chunk: Chunk, file, filename: string): seq[byt
self.error(&"unknown constant kind in chunk table ({constant.kind})")
proc dumpHex*(self: Serializer, chunk: Chunk, file, filename: string): string =
## Wrapper of dumpBytes that returns a hex string (using strutils.toHex)
## instead of a seq[byte]
for b in self.dumpBytes(chunk, file, filename):
result.add(toHex(b))
proc loadBytes(self: Serializer, stream: seq[byte]): Serialized =
proc loadBytes*(self: Serializer, stream: seq[byte]): Serialized =
## Loads the result from dumpBytes to a Serializer object
## for use in the VM or for inspection
new(result)
result.chunk = newChunk()
var stream = stream
if stream[0..<len(BYTECODE_MARKER)] != self.toBytes(BYTECODE_MARKER):
self.error("malformed bytecode marker")
stream = stream[len(BYTECODE_MARKER)..^1]

View File

@ -14,7 +14,7 @@
import strformat
const BYTECODE_MARKER* = "JAPL_BYTECODE"
const MAP_LOAD_FACTOR* = 0.75 # Load factor for builtin hashmaps
const HEAP_GROW_FACTOR* = 2 # How much extra memory to allocate for dynamic arrays and garbage collection when resizing
const MAX_STACK_FRAMES* = 800 # The maximum number of stack frames at any one time. Acts as a recursion limiter (1 frame = 1 call)