This commit is contained in:
Araq 2014-11-28 18:15:06 +01:00
commit 51dd4b7601
2 changed files with 42 additions and 13 deletions

View file

@ -113,7 +113,7 @@ proc rdLoc(a: TCompRes): PRope {.inline.} =
result = ropef("$1[$2]", a.address, a.res) result = ropef("$1[$2]", a.address, a.res)
proc newProc(globals: PGlobals, module: BModule, procDef: PNode, proc newProc(globals: PGlobals, module: BModule, procDef: PNode,
options: TOptions): PProc = options: TOptions): PProc =
result = PProc( result = PProc(
blocks: @[], blocks: @[],
options: options, options: options,
@ -942,17 +942,14 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
case s.kind case s.kind
of skVar, skLet, skResult: of skVar, skLet, skResult:
r.kind = resExpr r.kind = resExpr
if mapType(n.typ) == etyObject: if mapType(n.sons[0].typ) == etyObject:
# make addr() a no-op: # make addr() a no-op:
r.typ = etyNone r.typ = etyNone
r.res = s.loc.r r.res = s.loc.r
r.address = nil r.address = nil
elif sfGlobal in s.flags: elif {sfGlobal, sfAddrTaken} * s.flags != {}:
# globals are always indirect accessible # for ease of code generation, we do not distinguish between
r.typ = etyBaseIndex # sfAddrTaken and sfGlobal.
r.address = toRope("Globals")
r.res = makeJSString(ropeToStr(s.loc.r))
elif sfAddrTaken in s.flags:
r.typ = etyBaseIndex r.typ = etyBaseIndex
r.address = s.loc.r r.address = s.loc.r
r.res = toRope("0") r.res = toRope("0")
@ -981,7 +978,7 @@ proc genSym(p: PProc, n: PNode, r: var TCompRes) =
of skVar, skLet, skParam, skTemp, skResult: of skVar, skLet, skParam, skTemp, skResult:
if s.loc.r == nil: if s.loc.r == nil:
internalError(n.info, "symbol has no generated name: " & s.name.s) internalError(n.info, "symbol has no generated name: " & s.name.s)
var k = mapType(s.typ) let k = mapType(s.typ)
if k == etyBaseIndex: if k == etyBaseIndex:
r.typ = etyBaseIndex r.typ = etyBaseIndex
if {sfAddrTaken, sfGlobal} * s.flags != {}: if {sfAddrTaken, sfGlobal} * s.flags != {}:
@ -990,7 +987,7 @@ proc genSym(p: PProc, n: PNode, r: var TCompRes) =
else: else:
r.address = s.loc.r r.address = s.loc.r
r.res = con(s.loc.r, "_Idx") 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]) r.res = ropef("$1[0]", [s.loc.r])
else: else:
r.res = s.loc.r r.res = s.loc.r
@ -1170,8 +1167,9 @@ proc createVar(p: PProc, typ: PType, indirect: bool): PRope =
internalError("createVar: " & $t.kind) internalError("createVar: " & $t.kind)
result = nil result = nil
proc isIndirect(v: PSym): bool = proc isIndirect(v: PSym): bool =
result = (sfAddrTaken in v.flags) and (mapType(v.typ) != etyObject) and result = {sfAddrTaken, sfGlobal} * v.flags != {} and
(mapType(v.typ) != etyObject) and
v.kind notin {skProc, skConverter, skMethod, skIterator, skClosureIterator} v.kind notin {skProc, skConverter, skMethod, skIterator, skClosureIterator}
proc genVarInit(p: PProc, v: PSym, n: PNode) = proc genVarInit(p: PProc, v: PSym, n: PNode) =
@ -1675,7 +1673,6 @@ proc newModule(module: PSym): BModule =
proc genHeader(): PRope = proc genHeader(): PRope =
result = ropef("/* Generated by the Nim Compiler v$1 */$n" & result = ropef("/* Generated by the Nim Compiler v$1 */$n" &
"/* (c) 2014 Andreas Rumpf */$n$n" & "/* (c) 2014 Andreas Rumpf */$n$n" &
"$nvar Globals = this;$n" &
"var framePtr = null;$n" & "var framePtr = null;$n" &
"var excHandler = null;$n" & "var excHandler = null;$n" &
"var lastJSError = null;$n", "var lastJSError = null;$n",

32
tests/js/tbyvar.nim Normal file
View file

@ -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()