Merge branch 'devel' of github.com:nim-lang/Nim into devel
This commit is contained in:
commit
180a4b7657
6 changed files with 49 additions and 34 deletions
|
|
@ -928,8 +928,10 @@ proc genTry(p: BProc, t: PNode, d: var TLoc) =
|
||||||
for j in countup(0, blen - 2):
|
for j in countup(0, blen - 2):
|
||||||
assert(t.sons[i].sons[j].kind == nkType)
|
assert(t.sons[i].sons[j].kind == nkType)
|
||||||
if orExpr != nil: add(orExpr, "||")
|
if orExpr != nil: add(orExpr, "||")
|
||||||
appcg(p.module, orExpr,
|
let isObjFormat = if not p.module.compileToCpp:
|
||||||
"#isObj(#getCurrentException()->Sup.m_type, $1)",
|
"#isObj(#getCurrentException()->Sup.m_type, $1)"
|
||||||
|
else: "#isObj(#getCurrentException()->m_type, $1)"
|
||||||
|
appcg(p.module, orExpr, isObjFormat,
|
||||||
[genTypeInfo(p.module, t.sons[i].sons[j].typ)])
|
[genTypeInfo(p.module, t.sons[i].sons[j].typ)])
|
||||||
if i > 1: line(p, cpsStmts, "else ")
|
if i > 1: line(p, cpsStmts, "else ")
|
||||||
startBlock(p, "if ($1) {$n", [orExpr])
|
startBlock(p, "if ($1) {$n", [orExpr])
|
||||||
|
|
|
||||||
|
|
@ -891,10 +891,12 @@ proc genFieldAccess(p: PProc, n: PNode, r: var TCompRes) =
|
||||||
r.res = "$1['$2']" % [r.res, f.loc.r]
|
r.res = "$1['$2']" % [r.res, f.loc.r]
|
||||||
r.kind = resExpr
|
r.kind = resExpr
|
||||||
|
|
||||||
|
proc genAddr(p: PProc, n: PNode, r: var TCompRes)
|
||||||
|
|
||||||
proc genCheckedFieldAddr(p: PProc, n: PNode, r: var TCompRes) =
|
proc genCheckedFieldAddr(p: PProc, n: PNode, r: var TCompRes) =
|
||||||
let m = if n.kind == nkHiddenAddr: n.sons[0] else: n
|
let m = if n.kind == nkHiddenAddr: n.sons[0] else: n
|
||||||
internalAssert m.kind == nkCheckedFieldExpr
|
internalAssert m.kind == nkCheckedFieldExpr
|
||||||
genFieldAddr(p, m.sons[0], r) # XXX
|
genAddr(p, m, r) # XXX
|
||||||
|
|
||||||
proc genCheckedFieldAccess(p: PProc, n: PNode, r: var TCompRes) =
|
proc genCheckedFieldAccess(p: PProc, n: PNode, r: var TCompRes) =
|
||||||
genFieldAccess(p, n.sons[0], r) # XXX
|
genFieldAccess(p, n.sons[0], r) # XXX
|
||||||
|
|
|
||||||
|
|
@ -163,7 +163,11 @@ proc `[]`*[A](s: var HashSet[A], key: A): var A =
|
||||||
var hc: Hash
|
var hc: Hash
|
||||||
var index = rawGet(s, key, hc)
|
var index = rawGet(s, key, hc)
|
||||||
if index >= 0: result = s.data[index].key
|
if index >= 0: result = s.data[index].key
|
||||||
else: raise newException(KeyError, "key not found: " & $key)
|
else:
|
||||||
|
when compiles($key):
|
||||||
|
raise newException(KeyError, "key not found: " & $key)
|
||||||
|
else:
|
||||||
|
raise newException(KeyError, "key not found")
|
||||||
|
|
||||||
proc mget*[A](s: var HashSet[A], key: A): var A {.deprecated.} =
|
proc mget*[A](s: var HashSet[A], key: A): var A {.deprecated.} =
|
||||||
## returns the element that is actually stored in 's' which has the same
|
## returns the element that is actually stored in 's' which has the same
|
||||||
|
|
|
||||||
|
|
@ -1123,9 +1123,9 @@ else:
|
||||||
|
|
||||||
proc parseNativeJson(x: cstring): JSObject {.importc: "JSON.parse".}
|
proc parseNativeJson(x: cstring): JSObject {.importc: "JSON.parse".}
|
||||||
|
|
||||||
proc getVarType(x): JsonNodeKind =
|
proc getVarType(x: JSObject): JsonNodeKind =
|
||||||
result = JNull
|
result = JNull
|
||||||
proc getProtoName(y): cstring
|
proc getProtoName(y: JSObject): cstring
|
||||||
{.importc: "Object.prototype.toString.call".}
|
{.importc: "Object.prototype.toString.call".}
|
||||||
case $getProtoName(x) # TODO: Implicit returns fail here.
|
case $getProtoName(x) # TODO: Implicit returns fail here.
|
||||||
of "[object Array]": return JArray
|
of "[object Array]": return JArray
|
||||||
|
|
@ -1216,23 +1216,27 @@ when false:
|
||||||
# To get that we shall use, obj["json"]
|
# To get that we shall use, obj["json"]
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
var parsed = parseFile("tests/testdata/jsontest.json")
|
when not defined(js):
|
||||||
var parsed2 = parseFile("tests/testdata/jsontest2.json")
|
var parsed = parseFile("tests/testdata/jsontest.json")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
discard parsed["key2"][12123]
|
discard parsed["key2"][12123]
|
||||||
assert(false)
|
doAssert(false)
|
||||||
except IndexError: assert(true)
|
except IndexError: doAssert(true)
|
||||||
|
|
||||||
|
var parsed2 = parseFile("tests/testdata/jsontest2.json")
|
||||||
|
doAssert(parsed2{"repository", "description"}.str=="IRC Library for Haskell", "Couldn't fetch via multiply nested key using {}")
|
||||||
|
|
||||||
let testJson = parseJson"""{ "a": [1, 2, 3, 4], "b": "asd", "c": "\ud83c\udf83", "d": "\u00E6"}"""
|
let testJson = parseJson"""{ "a": [1, 2, 3, 4], "b": "asd", "c": "\ud83c\udf83", "d": "\u00E6"}"""
|
||||||
# nil passthrough
|
# nil passthrough
|
||||||
assert(testJson{"doesnt_exist"}{"anything"}.isNil)
|
doAssert(testJson{"doesnt_exist"}{"anything"}.isNil)
|
||||||
testJson{["e", "f"]} = %true
|
testJson{["e", "f"]} = %true
|
||||||
assert(testJson["e"]["f"].bval)
|
doAssert(testJson["e"]["f"].bval)
|
||||||
|
|
||||||
# make sure UTF-16 decoding works.
|
# make sure UTF-16 decoding works.
|
||||||
assert(testJson["c"].str == "🎃")
|
when not defined(js): # TODO: The following line asserts in JS
|
||||||
assert(testJson["d"].str == "æ")
|
doAssert(testJson["c"].str == "🎃")
|
||||||
|
doAssert(testJson["d"].str == "æ")
|
||||||
|
|
||||||
# make sure no memory leek when parsing invalid string
|
# make sure no memory leek when parsing invalid string
|
||||||
let startMemory = getOccupiedMem()
|
let startMemory = getOccupiedMem()
|
||||||
|
|
@ -1242,41 +1246,43 @@ when isMainModule:
|
||||||
except:
|
except:
|
||||||
discard
|
discard
|
||||||
# memory diff should less than 2M
|
# memory diff should less than 2M
|
||||||
assert(abs(getOccupiedMem() - startMemory) < 2 * 1024 * 1024)
|
doAssert(abs(getOccupiedMem() - startMemory) < 2 * 1024 * 1024)
|
||||||
|
|
||||||
|
|
||||||
# test `$`
|
# test `$`
|
||||||
let stringified = $testJson
|
let stringified = $testJson
|
||||||
let parsedAgain = parseJson(stringified)
|
let parsedAgain = parseJson(stringified)
|
||||||
assert(parsedAgain["b"].str == "asd")
|
doAssert(parsedAgain["b"].str == "asd")
|
||||||
|
|
||||||
|
parsedAgain["abc"] = %5
|
||||||
|
doAssert parsedAgain["abc"].num == 5
|
||||||
|
|
||||||
# Bounds checking
|
# Bounds checking
|
||||||
try:
|
try:
|
||||||
let a = testJson["a"][9]
|
let a = testJson["a"][9]
|
||||||
assert(false, "EInvalidIndex not thrown")
|
doAssert(false, "EInvalidIndex not thrown")
|
||||||
except IndexError:
|
except IndexError:
|
||||||
discard
|
discard
|
||||||
try:
|
try:
|
||||||
let a = testJson["a"][-1]
|
let a = testJson["a"][-1]
|
||||||
assert(false, "EInvalidIndex not thrown")
|
doAssert(false, "EInvalidIndex not thrown")
|
||||||
except IndexError:
|
except IndexError:
|
||||||
discard
|
discard
|
||||||
try:
|
try:
|
||||||
assert(testJson["a"][0].num == 1, "Index doesn't correspond to its value")
|
doAssert(testJson["a"][0].num == 1, "Index doesn't correspond to its value")
|
||||||
except:
|
except:
|
||||||
assert(false, "EInvalidIndex thrown for valid index")
|
doAssert(false, "EInvalidIndex thrown for valid index")
|
||||||
|
|
||||||
assert(testJson{"b"}.str=="asd", "Couldn't fetch a singly nested key with {}")
|
doAssert(testJson{"b"}.str=="asd", "Couldn't fetch a singly nested key with {}")
|
||||||
assert(isNil(testJson{"nonexistent"}), "Non-existent keys should return nil")
|
doAssert(isNil(testJson{"nonexistent"}), "Non-existent keys should return nil")
|
||||||
assert(parsed2{"repository", "description"}.str=="IRC Library for Haskell", "Couldn't fetch via multiply nested key using {}")
|
doAssert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil")
|
||||||
assert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil")
|
doAssert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil")
|
||||||
assert(isNil(testJson{"a", "b"}), "Indexing through a list should return nil")
|
doAssert(testJson{"a"}==parseJson"[1, 2, 3, 4]", "Didn't return a non-JObject when there was one to be found")
|
||||||
assert(testJson{"a"}==parseJson"[1, 2, 3, 4]", "Didn't return a non-JObject when there was one to be found")
|
doAssert(isNil(parseJson("[1, 2, 3]"){"foo"}), "Indexing directly into a list should return nil")
|
||||||
assert(isNil(parseJson("[1, 2, 3]"){"foo"}), "Indexing directly into a list should return nil")
|
|
||||||
|
|
||||||
# Generator:
|
# Generator:
|
||||||
var j = %* [{"name": "John", "age": 30}, {"name": "Susan", "age": 31}]
|
var j = %* [{"name": "John", "age": 30}, {"name": "Susan", "age": 31}]
|
||||||
assert j == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
|
doAssert j == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
|
||||||
|
|
||||||
var j2 = %*
|
var j2 = %*
|
||||||
[
|
[
|
||||||
|
|
@ -1289,7 +1295,7 @@ when isMainModule:
|
||||||
"age": 31
|
"age": 31
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
assert j2 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
|
doAssert j2 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
|
||||||
|
|
||||||
var name = "John"
|
var name = "John"
|
||||||
let herAge = 30
|
let herAge = 30
|
||||||
|
|
@ -1303,4 +1309,4 @@ when isMainModule:
|
||||||
, "age": hisAge
|
, "age": hisAge
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
assert j3 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
|
doAssert j3 == %[%{"name": %"John", "age": %30}, %{"name": %"Susan", "age": %31}]
|
||||||
|
|
|
||||||
|
|
@ -223,7 +223,7 @@ proc jsTests(r: var TResults, cat: Category, options: string) =
|
||||||
"varres/tvartup", "misc/tints", "misc/tunsignedinc"]:
|
"varres/tvartup", "misc/tints", "misc/tunsignedinc"]:
|
||||||
test "tests/" & testfile & ".nim"
|
test "tests/" & testfile & ".nim"
|
||||||
|
|
||||||
for testfile in ["pure/strutils"]:
|
for testfile in ["pure/strutils", "pure/json"]:
|
||||||
test "lib/" & testfile & ".nim"
|
test "lib/" & testfile & ".nim"
|
||||||
|
|
||||||
# ------------------------- manyloc -------------------------------------------
|
# ------------------------- manyloc -------------------------------------------
|
||||||
|
|
|
||||||
|
|
@ -135,7 +135,8 @@ General FAQ
|
||||||
- Sublime Text: Available via Package Control (`Repository <https://github.com/Varriount/NimLime>`_)
|
- Sublime Text: Available via Package Control (`Repository <https://github.com/Varriount/NimLime>`_)
|
||||||
- LiClipse: http://www.liclipse.com/ (Eclipse based plugin)
|
- LiClipse: http://www.liclipse.com/ (Eclipse based plugin)
|
||||||
- Howl: Included
|
- Howl: Included
|
||||||
|
- Notepad++: Available via `plugin <https://github.com/jangko/nppnim/releases>`_
|
||||||
|
|
||||||
|
|
||||||
.. container:: standout
|
.. container:: standout
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue