Initial work for loadBytes and removed dumpBytes
This commit is contained in:
parent
3d4680e94d
commit
df67e7db64
|
@ -25,7 +25,6 @@ import times
|
||||||
|
|
||||||
export ast
|
export ast
|
||||||
|
|
||||||
|
|
||||||
type
|
type
|
||||||
Serializer* = ref object
|
Serializer* = ref object
|
||||||
file: string
|
file: string
|
||||||
|
@ -80,7 +79,7 @@ proc dumpBytes*(self: Serializer, chunk: Chunk, file, filename: string): seq[byt
|
||||||
self.file = file
|
self.file = file
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.chunk = chunk
|
self.chunk = chunk
|
||||||
result.extend(self.toBytes("JAPL_BYTECODE"))
|
result.extend(self.toBytes(BYTECODE_MARKER))
|
||||||
result.add(byte(len(JAPL_BRANCH)))
|
result.add(byte(len(JAPL_BRANCH)))
|
||||||
result.extend(self.toBytes(JAPL_BRANCH))
|
result.extend(self.toBytes(JAPL_BRANCH))
|
||||||
if len(JAPL_COMMIT_HASH) != 40:
|
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})")
|
self.error(&"unknown constant kind in chunk table ({constant.kind})")
|
||||||
|
|
||||||
|
|
||||||
proc dumpHex*(self: Serializer, chunk: Chunk, file, filename: string): string =
|
proc loadBytes*(self: Serializer, stream: seq[byte]): Serialized =
|
||||||
## 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 =
|
|
||||||
## Loads the result from dumpBytes to a Serializer object
|
## Loads the result from dumpBytes to a Serializer object
|
||||||
## for use in the VM or for inspection
|
## 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]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
import strformat
|
import strformat
|
||||||
|
|
||||||
|
const BYTECODE_MARKER* = "JAPL_BYTECODE"
|
||||||
const MAP_LOAD_FACTOR* = 0.75 # Load factor for builtin hashmaps
|
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 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)
|
const MAX_STACK_FRAMES* = 800 # The maximum number of stack frames at any one time. Acts as a recursion limiter (1 frame = 1 call)
|
||||||
|
|
Loading…
Reference in New Issue