fixes #6035
This commit is contained in:
parent
57f4abf6f4
commit
4de989d1c5
3 changed files with 25 additions and 3 deletions
|
|
@ -1446,7 +1446,7 @@ proc createRecordVarAux(p: PProc, rec: PNode, excludedFieldIDs: IntSet, output:
|
||||||
|
|
||||||
proc createObjInitList(p: PProc, typ: PType, excludedFieldIDs: IntSet, output: var Rope) =
|
proc createObjInitList(p: PProc, typ: PType, excludedFieldIDs: IntSet, output: var Rope) =
|
||||||
var t = typ
|
var t = typ
|
||||||
if tfFinal notin t.flags or t.sons[0] != nil:
|
if objHasTypeField(t):
|
||||||
if output.len > 0: output.add(", ")
|
if output.len > 0: output.add(", ")
|
||||||
addf(output, "m_type: $1" | "'m_type' => $#", [genTypeInfo(p, t)])
|
addf(output, "m_type: $1" | "'m_type' => $#", [genTypeInfo(p, t)])
|
||||||
while t != nil:
|
while t != nil:
|
||||||
|
|
|
||||||
|
|
@ -67,9 +67,13 @@ proc genObjectFields(p: PProc, typ: PType, n: PNode): Rope =
|
||||||
rope(lengthOrd(field.typ)), makeJSString(field.name.s), result]
|
rope(lengthOrd(field.typ)), makeJSString(field.name.s), result]
|
||||||
else: internalError(n.info, "genObjectFields")
|
else: internalError(n.info, "genObjectFields")
|
||||||
|
|
||||||
|
proc objHasTypeField(t: PType): bool {.inline.} =
|
||||||
|
tfInheritable in t.flags or t.sons[0] != nil
|
||||||
|
|
||||||
proc genObjectInfo(p: PProc, typ: PType, name: Rope) =
|
proc genObjectInfo(p: PProc, typ: PType, name: Rope) =
|
||||||
|
let kind = if objHasTypeField(typ): tyObject else: tyTuple
|
||||||
var s = ("var $1 = {size: 0, kind: $2, base: null, node: null, " &
|
var s = ("var $1 = {size: 0, kind: $2, base: null, node: null, " &
|
||||||
"finalizer: null};$n") % [name, rope(ord(typ.kind))]
|
"finalizer: null};$n") % [name, rope(ord(kind))]
|
||||||
prepend(p.g.typeInfo, s)
|
prepend(p.g.typeInfo, s)
|
||||||
addf(p.g.typeInfo, "var NNI$1 = $2;$n",
|
addf(p.g.typeInfo, "var NNI$1 = $2;$n",
|
||||||
[rope(typ.id), genObjectFields(p, typ, typ.n)])
|
[rope(typ.id), genObjectFields(p, typ, typ.n)])
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
discard """
|
discard """
|
||||||
action: run
|
output: '''{"columns":[{"t":null},{"t":null}]}
|
||||||
|
{"columns":[{"t":null},{"t":null}]}
|
||||||
|
'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
## Tests javascript object generation
|
## Tests javascript object generation
|
||||||
|
|
@ -36,3 +38,19 @@ doAssert test.name == "Jorden"
|
||||||
doAssert knight.age == 19
|
doAssert knight.age == 19
|
||||||
doAssert knight.item.price == 50
|
doAssert knight.item.price == 50
|
||||||
doAssert recurse1.next.next.data == 3
|
doAssert recurse1.next.next.data == 3
|
||||||
|
|
||||||
|
# bug #6035
|
||||||
|
proc toJson*[T](data: T): cstring {.importc: "JSON.stringify".}
|
||||||
|
|
||||||
|
type
|
||||||
|
Column = object
|
||||||
|
t: ref Column
|
||||||
|
|
||||||
|
Test2 = object
|
||||||
|
columns: seq[Column]
|
||||||
|
|
||||||
|
var test1 = Test2(columns: @[Column(t: nil), Column(t: nil)])
|
||||||
|
let test2 = test1
|
||||||
|
|
||||||
|
echo toJSON(test1)
|
||||||
|
echo toJSON(test2)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue