Refactor json macro (#12391)

* closes #12316
* make tjsonmacro work at js target
* closes #12289
* closes #11988
* also fixed gdb related stuff
This commit is contained in:
Arne Döring 2019-10-17 09:55:41 +02:00 • committed by Andreas Rumpf
commit 21cbfd72ec
9 changed files with 387 additions and 663 deletions

View file

@ -0,0 +1,73 @@
discard """
output: '''
hello some integer
hello range
hello tuple
hello seq
hello object
hello distinct
hello enum
'''
"""
# SomeInteger
proc foo[T : SomeInteger](arg: T)
proc foo[T : SomeInteger](arg: T) =
echo "hello some integer"
foo(123)
# range
proc foo2[T : range[0..100]](arg: T)
proc foo2[T : range[0..100]](arg: T) =
echo "hello range"
foo2(7)
# tuple
proc foo3[T : tuple](arg: T)
proc foo3[T : tuple](arg: T) =
echo "hello tuple"
foo3((a:123,b:321))
# seq
proc foo4[T: seq](arg: T)
proc foo4[T: seq](arg: T) =
echo "hello seq"
foo4(@[1,2,3])
# object
proc foo5[T : object](arg: T)
proc foo5[T : object](arg: T) =
echo "hello object"
type MyType = object
var mt: MyType
foo5(mt)
# distinct
proc foo6[T : distinct](arg: T)
proc foo6[T : distinct](arg: T) =
echo "hello distinct"
type MyDistinct = distinct string
var md: MyDistinct
foo6(md)
# enum
proc foo7[T : enum](arg: T)
proc foo7[T : enum](arg: T) =
echo "hello enum"
type MyEnum = enum
ValueA
foo7(ValueA)

View file

@ -1,6 +1,8 @@
discard """
output: ""
targets: "c js"
"""
import json, strutils, options, tables
when true:
@ -18,6 +20,11 @@ when true:
case kind*: ReplayEventKind
of FoodAppeared, FoodEaten:
foodPos*: Point[float]
case subKind*: bool
of true:
it: int
of false:
ot: float
of DirectionChanged:
playerPos*: float
@ -33,7 +40,9 @@ when true:
ReplayEvent(
time: 1.2345,
kind: FoodEaten,
foodPos: Point[float](x: 5.0, y: 1.0)
foodPos: Point[float](x: 5.0, y: 1.0),
subKind: true,
it: 7
)
],
test: 18827361,
@ -592,3 +601,34 @@ static:
doAssert t["fruit"]["color"].getInt == 10
doAssert t["emails"][0].getStr == "abc"
doAssert t["emails"][1].getStr == "123"
block:
# ref objects with cycles.
type
Misdirection = object
cycle: Cycle
Cycle = ref object
foo: string
cycle: Misdirection
let data = """
{"cycle": null}
"""
let dataParsed = parseJson(data)
let dataDeser = to(dataParsed, Misdirection)
block:
# ref object from #12316
type
Foo = ref Bar
Bar = object
discard "null".parseJson.to Foo
block:
# named array #12289
type Vec = array[2, int]
let arr = "[1,2]".parseJson.to Vec
doAssert arr == [1,2]

View file

@ -1,21 +0,0 @@
discard """
errormsg: "The `to` macro does not support ref objects with cycles."
file: "tjsonmacro_reject2.nim"
line: 10
"""
import json
type
Misdirection = object
cycle: Cycle
Cycle = ref object
foo: string
cycle: Misdirection
let data = """
{"cycle": null}
"""
let dataParsed = parseJson(data)
let dataDeser = to(dataParsed, Cycle)

View file

@ -1,3 +1,3 @@
Loading Nim Runtime support.
NimEnumPrinter: lookup global symbol 'NTI_z9cu80OJCfNgw9bUdzn5ZEzw_ failed for tyEnum_MyOtherEnum_z9cu80OJCfNgw9bUdzn5ZEzw.
NimEnumPrinter: lookup global symbol 'NTI__z9cu80OJCfNgw9bUdzn5ZEzw_ failed for tyEnum_MyOtherEnum__z9cu80OJCfNgw9bUdzn5ZEzw.
8