register callback for marshal in VM (#19578)
* register callback for marshal in VM * remove unrelated code * polish * more tests * more tests * add loadVM and toVM
This commit is contained in:
parent
6454472938
commit
8f9dd5b349
6 changed files with 96 additions and 59 deletions
|
|
@ -298,6 +298,9 @@ proc store*[T](s: Stream, data: T) =
|
|||
shallowCopy(d, data)
|
||||
storeAny(s, toAny(d), stored)
|
||||
|
||||
proc loadVM[T](typ: typedesc[T], x: T): string =
|
||||
discard "the implementation is in the compiler/vmops"
|
||||
|
||||
proc `$$`*[T](x: T): string =
|
||||
## Returns a string representation of `x` (serialization, marshalling).
|
||||
##
|
||||
|
|
@ -313,12 +316,18 @@ proc `$$`*[T](x: T): string =
|
|||
let y = $$x
|
||||
assert y == """{"id": 1, "bar": "baz"}"""
|
||||
|
||||
var stored = initIntSet()
|
||||
var d: T
|
||||
shallowCopy(d, x)
|
||||
var s = newStringStream()
|
||||
storeAny(s, toAny(d), stored)
|
||||
result = s.data
|
||||
when nimvm:
|
||||
result = loadVM(T, x)
|
||||
else:
|
||||
var stored = initIntSet()
|
||||
var d: T
|
||||
shallowCopy(d, x)
|
||||
var s = newStringStream()
|
||||
storeAny(s, toAny(d), stored)
|
||||
result = s.data
|
||||
|
||||
proc toVM[T](typ: typedesc[T], data: string): T =
|
||||
discard "the implementation is in the compiler/vmops"
|
||||
|
||||
proc to*[T](data: string): T =
|
||||
## Reads data and transforms it to a type `T` (deserialization, unmarshalling).
|
||||
|
|
@ -335,5 +344,8 @@ proc to*[T](data: string): T =
|
|||
assert z.id == 1
|
||||
assert z.bar == "baz"
|
||||
|
||||
var tab = initTable[BiggestInt, pointer]()
|
||||
loadAny(newStringStream(data), toAny(result), tab)
|
||||
when nimvm:
|
||||
result = toVM(T, data)
|
||||
else:
|
||||
var tab = initTable[BiggestInt, pointer]()
|
||||
loadAny(newStringStream(data), toAny(result), tab)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue