diff options
-rw-r--r-- | compiler/jsgen.nim | 23 | ||||
-rw-r--r-- | tests/js/tbyvar.nim | 32 |
2 files changed, 42 insertions, 13 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index 8fc2ec1d2..108c0fe10 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -113,7 +113,7 @@ proc rdLoc(a: TCompRes): PRope {.inline.} = result = ropef("$1[$2]", a.address, a.res) proc newProc(globals: PGlobals, module: BModule, procDef: PNode, - options: TOptions): PProc = + options: TOptions): PProc = result = PProc( blocks: @[], options: options, @@ -942,17 +942,14 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) = case s.kind of skVar, skLet, skResult: r.kind = resExpr - if mapType(n.typ) == etyObject: + if mapType(n.sons[0].typ) == etyObject: # make addr() a no-op: r.typ = etyNone r.res = s.loc.r r.address = nil - elif sfGlobal in s.flags: - # globals are always indirect accessible - r.typ = etyBaseIndex - r.address = toRope("Globals") - r.res = makeJSString(ropeToStr(s.loc.r)) - elif sfAddrTaken in s.flags: + elif {sfGlobal, sfAddrTaken} * s.flags != {}: + # for ease of code generation, we do not distinguish between + # sfAddrTaken and sfGlobal. r.typ = etyBaseIndex r.address = s.loc.r r.res = toRope("0") @@ -981,7 +978,7 @@ proc genSym(p: PProc, n: PNode, r: var TCompRes) = of skVar, skLet, skParam, skTemp, skResult: if s.loc.r == nil: internalError(n.info, "symbol has no generated name: " & s.name.s) - var k = mapType(s.typ) + let k = mapType(s.typ) if k == etyBaseIndex: r.typ = etyBaseIndex if {sfAddrTaken, sfGlobal} * s.flags != {}: @@ -990,7 +987,7 @@ proc genSym(p: PProc, n: PNode, r: var TCompRes) = else: r.address = s.loc.r r.res = con(s.loc.r, "_Idx") - elif k != etyObject and sfAddrTaken in s.flags: + elif k != etyObject and {sfAddrTaken, sfGlobal} * s.flags != {}: r.res = ropef("$1[0]", [s.loc.r]) else: r.res = s.loc.r @@ -1170,8 +1167,9 @@ proc createVar(p: PProc, typ: PType, indirect: bool): PRope = internalError("createVar: " & $t.kind) result = nil -proc isIndirect(v: PSym): bool = - result = (sfAddrTaken in v.flags) and (mapType(v.typ) != etyObject) and +proc isIndirect(v: PSym): bool = + result = {sfAddrTaken, sfGlobal} * v.flags != {} and + (mapType(v.typ) != etyObject) and v.kind notin {skProc, skConverter, skMethod, skIterator, skClosureIterator} proc genVarInit(p: PProc, v: PSym, n: PNode) = @@ -1675,7 +1673,6 @@ proc newModule(module: PSym): BModule = proc genHeader(): PRope = result = ropef("/* Generated by the Nim Compiler v$1 */$n" & "/* (c) 2014 Andreas Rumpf */$n$n" & - "$nvar Globals = this;$n" & "var framePtr = null;$n" & "var excHandler = null;$n" & "var lastJSError = null;$n", diff --git a/tests/js/tbyvar.nim b/tests/js/tbyvar.nim new file mode 100644 index 000000000..5ed2de1da --- /dev/null +++ b/tests/js/tbyvar.nim @@ -0,0 +1,32 @@ +discard """ + output: '''foo 12 +bar 12 +2 +foo 12 +bar 12 +2''' +""" + +# bug #1489 +proc foo(x: int) = echo "foo: ", x +proc bar(y: var int) = echo "bar: ", y + +var x = 12 +foo(x) +bar(x) + +# bug #1490 +var y = 1 +y *= 2 +echo y + +proc main = + var x = 12 + foo(x) + bar(x) + + var y = 1 + y *= 2 + echo y + +main() |