summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorLemonBoy <LemonBoy@users.noreply.github.com>2018-09-07 01:54:59 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-09-07 01:54:59 +0200
commitaf1e84f54da32b5e8e917edc533c6efa6846acc2 (patch)
tree7f5a361e855356ae0a4900a1e17b78956436c648
parentc033ff990acceac6d2095242fa17501e98555973 (diff)
downloadNim-af1e84f54da32b5e8e917edc533c6efa6846acc2.tar.gz
Automatic deref for everything but pointers in asm/emit (#8891)
Fixes #7249
-rw-r--r--compiler/jsgen.nim15
-rw-r--r--lib/system.nim6
-rw-r--r--tests/js/t7249.nim21
-rw-r--r--tests/js/tmangle.nim8
-rw-r--r--tests/js/tstringitems.nim2
5 files changed, 42 insertions, 10 deletions
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index 1b00ddbfa..da35002b7 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -764,11 +764,22 @@ proc genAsmOrEmitStmt(p: PProc, n: PNode) =
     of nkSym:
       let v = it.sym
       # for backwards compatibility we don't deref syms here :-(
-      if v.kind in {skVar, skLet, skTemp, skConst, skResult, skParam, skForVar}:
-        p.body.add mangleName(p.module, v)
+      if false:
+        discard
       else:
         var r: TCompRes
         gen(p, it, r)
+
+        if it.typ.kind == tyPointer:
+          # A fat pointer is disguised as an array
+          r.res = r.address
+          r.address = nil
+        elif r.typ == etyBaseIndex:
+          # Deference first
+          r.res = "$1[$2]" % [r.address, r.res]
+          r.address = nil
+          r.typ = etyNone
+
         p.body.add(r.rdLoc)
     else:
       var r: TCompRes
diff --git a/lib/system.nim b/lib/system.nim
index 3a18a715c..a7e215168 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -2741,12 +2741,12 @@ type
 when defined(JS):
   proc add*(x: var string, y: cstring) {.asmNoStackFrame.} =
     asm """
-      var len = `x`[0].length-1;
+      var len = `x`.length-1;
       for (var i = 0; i < `y`.length; ++i) {
-        `x`[0][len] = `y`.charCodeAt(i);
+        `x`[len] = `y`.charCodeAt(i);
         ++len;
       }
-      `x`[0][len] = 0
+      `x`[len] = 0
     """
   proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".}
 
diff --git a/tests/js/t7249.nim b/tests/js/t7249.nim
new file mode 100644
index 000000000..52eee2f7c
--- /dev/null
+++ b/tests/js/t7249.nim
@@ -0,0 +1,21 @@
+discard """
+  output: '''
+a -> 2
+a <- 2
+'''
+"""
+
+import jsffi
+
+var a = JsAssoc[cstring, int]{a: 2}
+
+for z, b in a:
+  echo z, " -> ", b
+
+proc f =
+  var a = JsAssoc[cstring, int]{a: 2}
+
+  for z, b in a:
+    echo z, " <- ", b
+
+f()
diff --git a/tests/js/tmangle.nim b/tests/js/tmangle.nim
index c4167ba39..c97bf7029 100644
--- a/tests/js/tmangle.nim
+++ b/tests/js/tmangle.nim
@@ -27,10 +27,10 @@ block:
   var global = T(a: 11, b: "foo")
   proc test(): bool =
     var obj = T(a: 11, b: "foo")
-    {. emit: [result, " = (", obj.addr[], "[0].a == 11);"] .}
-    {. emit: [result, " = ", result, " && (", obj.addr[], "[0].b == \"foo\");"] .}
-    {. emit: [result, " = ", result, " && (", global, "[0].a == 11);"] .}
-    {. emit: [result, " = ", result, " && (", global, "[0].b == \"foo\");"] .}
+    {. emit: [result, " = (", obj.addr[], ".a == 11);"] .}
+    {. emit: [result, " = ", result, " && (", obj.addr[], ".b == \"foo\");"] .}
+    {. emit: [result, " = ", result, " && (", global, ".a == 11);"] .}
+    {. emit: [result, " = ", result, " && (", global, ".b == \"foo\");"] .}
   echo test()
 
 # Test addr of field:
diff --git a/tests/js/tstringitems.nim b/tests/js/tstringitems.nim
index ff016642e..f09793dde 100644
--- a/tests/js/tstringitems.nim
+++ b/tests/js/tstringitems.nim
@@ -45,7 +45,7 @@ block: # Test compile-time binary data generation, invalid unicode
 block: # Test unicode strings
   const constStr = "Привет!"
   var jsStr : cstring
-  {.emit: """`jsStr`[0] = "Привет!";""".}
+  {.emit: """`jsStr` = "Привет!";""".}
 
   doAssert($jsStr == constStr)
   var runtimeStr = "При"