fix: std/marshal unmarshaling of ref objects (#22983)

Fixes #16496 

![Marshal doesn't properly unmarshal *most* ref objects; the exceptions
being nil
ones](https://github-production-user-asset-6210df.s3.amazonaws.com/4764481/285471431-a39ee2c5-5670-4b12-aa10-7a10ba6b5b96.gif)
Test case added.

Note that this test (t9754) does pass locally, but there are tons of
failures by default on OS X arm64, mostly around the bohem GC, so it's
pretty spammy, and could easily have missed something. If there are
better instructions please do let me know.

---------

Co-authored-by: John Viega <viega@Johns-MacBook-Pro.local>
Co-authored-by: John Viega <viega@Johns-MBP.localdomain>
Co-authored-by: ringabout <43030857+ringabout@users.noreply.github.com>
This commit is contained in:
John Viega
2023-11-26 00:32:32 -05:00
committed by GitHub
parent 26f2ea149c
commit 5b2fcabff5
2 changed files with 43 additions and 2 deletions

View File

@@ -210,7 +210,8 @@ proc loadAny(p: var JsonParser, a: Any, t: var Table[BiggestInt, pointer]) =
setPointer(a, nil)
next(p)
of jsonInt:
setPointer(a, t.getOrDefault(p.getInt))
var raw = t.getOrDefault(p.getInt)
setPointer(a, addr raw)
next(p)
of jsonArrayStart:
next(p)

View File

@@ -3,7 +3,7 @@ discard """
"""
import std/marshal
import std/[assertions, objectdollar]
import std/[assertions, objectdollar, streams]
# TODO: add static tests
@@ -166,6 +166,46 @@ block:
let a: ref A = new(B)
doAssert $$a[] == "{}" # not "{f: 0}"
# bug #16496
block:
type
A = ref object
data: seq[int]
B = ref object
x: A
let o = A(data: @[1, 2, 3, 4])
let s1 = @[B(x: o), B(x: o)]
let m = $$ s1
let s2 = to[seq[B]](m)
doAssert s2[0].x.data == s2[1].x.data
doAssert s1[0].x.data == s2[1].x.data
block:
type
Obj = ref object
i: int
b: bool
let
strm = newStringStream()
var
o = Obj(i: 1, b: false)
t1 = @[o, o]
t2: seq[Obj]
doAssert t1[0] == t1[1]
strm.store(t1)
strm.setPosition(0)
strm.load(t2)
strm.close()
doAssert t2[0] == t2[1]
template checkMarshal(data: typed) =
let orig = data
let m = $$orig