diff options
author | Yuriy Glukhov <yuriy.glukhov@gmail.com> | 2016-09-20 17:00:16 +0300 |
---|---|---|
committer | Yuriy Glukhov <yuriy.glukhov@gmail.com> | 2016-09-20 17:01:53 +0300 |
commit | 82b022ebc6621f573c516ea59e9db4426d87a64e (patch) | |
tree | aa8cf9145240dbf5639e8359c4b35a42f6940f44 | |
parent | 09651bec5e0988a5f712049fdc0f9b41e7e37e98 (diff) | |
download | Nim-82b022ebc6621f573c516ea59e9db4426d87a64e.tar.gz |
JS: Fixed ICE on ptr assignment
-rw-r--r-- | compiler/jsgen.nim | 20 | ||||
-rw-r--r-- | tests/js/taddr.nim | 7 | ||||
-rw-r--r-- | tests/js/tbyvar.nim | 8 |
3 files changed, 29 insertions, 6 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim index d17f5580e..e7fe8cc27 100644 --- a/compiler/jsgen.nim +++ b/compiler/jsgen.nim @@ -1471,13 +1471,21 @@ proc genVarInit(p: PProc, v: PSym, n: PNode) = useMagic(p, "nimCopy") s = "nimCopy(null, $1, $2)" % [a.res, genTypeInfo(p, n.typ)] of etyBaseIndex: - if (a.typ != etyBaseIndex): internalError(n.info, "genVarInit") - if {sfAddrTaken, sfGlobal} * v.flags != {}: - addf(p.body, "var $1 = [$2, $3];$n", - [v.loc.r, a.address, a.res]) + let targetBaseIndex = {sfAddrTaken, sfGlobal} * v.flags == {} + if a.typ == etyBaseIndex: + if targetBaseIndex: + addf(p.body, "var $1 = $2, $1_Idx = $3;$n", [ + v.loc.r, a.address, a.res]) + else: + addf(p.body, "var $1 = [$2, $3];$n", + [v.loc.r, a.address, a.res]) else: - addf(p.body, "var $1 = $2; var $1_Idx = $3;$n", [ - v.loc.r, a.address, a.res]) + if targetBaseIndex: + let tmp = p.getTemp + addf(p.body, "var $1 = $2, $3 = $1[0], $3_Idx = $1[1];$n", + [tmp, a.res, v.loc.r]) + else: + addf(p.body, "var $1 = $2;$n", [v.loc.r, a.res]) return else: s = a.res diff --git a/tests/js/taddr.nim b/tests/js/taddr.nim index 1fba30d55..d8ff4c11b 100644 --- a/tests/js/taddr.nim +++ b/tests/js/taddr.nim @@ -70,3 +70,10 @@ proc testPtr(p: pointer, a: int) = var i = 123 testPtr(addr i, 5) doAssert(i == 124) + +var someGlobal = 5 +proc getSomeGlobalPtr(): ptr int = addr someGlobal +let someGlobalPtr = getSomeGlobalPtr() +doAssert(someGlobalPtr[] == 5) +someGlobalPtr[] = 10 +doAssert(someGlobal == 10) diff --git a/tests/js/tbyvar.nim b/tests/js/tbyvar.nim index 9714cd56b..40aebd13b 100644 --- a/tests/js/tbyvar.nim +++ b/tests/js/tbyvar.nim @@ -41,3 +41,11 @@ proc bar(s: var seq[int], a: int) = foo(s) s.bar(5) doAssert(s == @[123, 1]) + +import tables +block: # Test get addr of byvar return value + var t = initTable[string, int]() + t["hi"] = 5 + let a = addr t["hi"] + a[] = 10 + doAssert(t["hi"] == 10) |