diff options
-rw-r--r-- | compiler/jsgen.nim | 27 | ||||
-rw-r--r-- | tests/js/tbasics.nim | 14 |
2 files changed, 30 insertions, 11 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index e3ca6830c..627cc6cdf 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -1804,6 +1804,10 @@ proc genOf(p: PProc, n: PNode, r: var TCompRes) = r.res = "isObj($1.m_type, $2)" % [x.res, genTypeInfo(p, t)] r.kind = resExpr +proc genDefault(p: PProc, n: PNode; r: var TCompRes) = + r.res = createVar(p, n.typ, indirect = false) + r.kind = resExpr + proc genReset(p: PProc, n: PNode) = var x: TCompRes useMagic(p, "genericReset") @@ -1938,6 +1942,7 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) = of mNewSeq: genNewSeq(p, n) of mNewSeqOfCap: unaryExpr(p, n, r, "", "[]") of mOf: genOf(p, n, r) + of mDefault: genDefault(p, n, r) of mReset: genReset(p, n) of mEcho: genEcho(p, n, r) of mNLen..mNError, mSlurp, mStaticExec: @@ -2138,7 +2143,7 @@ proc genProcBody(p: PProc, prc: PSym): Rope = if hasFrameInfo(p): add(result, frameDestroy(p)) -proc optionaLine(p: Rope): Rope = +proc optionalLine(p: Rope): Rope = if p == nil: return nil else: @@ -2182,11 +2187,11 @@ proc genProc(oldProc: PProc, prc: PSym): Rope = [ returnType, name, header, - optionaLine(p.globals), - optionaLine(p.locals), - optionaLine(resultAsgn), - optionaLine(genProcBody(p, prc)), - optionaLine(p.indentLine(returnStmt))] + optionalLine(p.globals), + optionalLine(p.locals), + optionalLine(resultAsgn), + optionalLine(genProcBody(p, prc)), + optionalLine(p.indentLine(returnStmt))] else: result = ~"\L" @@ -2203,11 +2208,11 @@ proc genProc(oldProc: PProc, prc: PSym): Rope = def = "function $#($#) {$n$#$#$#$#$#" % [ name, header, - optionaLine(p.globals), - optionaLine(p.locals), - optionaLine(resultAsgn), - optionaLine(genProcBody(p, prc)), - optionaLine(p.indentLine(returnStmt))] + optionalLine(p.globals), + optionalLine(p.locals), + optionalLine(resultAsgn), + optionalLine(genProcBody(p, prc)), + optionalLine(p.indentLine(returnStmt))] dec p.extraIndent result.add p.indentLine(def) diff --git a/tests/js/tbasics.nim b/tests/js/tbasics.nim index 33616776f..b297bb037 100644 --- a/tests/js/tbasics.nim +++ b/tests/js/tbasics.nim @@ -46,3 +46,17 @@ proc test2 = echo int(val) test2() + + +var someGlobal = default(array[5, int]) +for x in someGlobal: doAssert(x == 0) + +proc tdefault = + var x = default(int) + doAssert(x == 0) + proc inner(v: openarray[string]) = + doAssert(v.len == 0) + + inner(default(seq[string])) + +tdefault() |