summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-04-13 23:41:31 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-04-13 23:41:31 +0200
commitd7cc9016f3a457b0980259973f4b0089d25cd6bc (patch)
treeb21e5dd071f192df08b37d67313fc00f68af2b1f
parenta3cf1cff5b06d7484449dc4131e75ab894d2f1ed (diff)
downloadNim-d7cc9016f3a457b0980259973f4b0089d25cd6bc.tar.gz
fixes #4470
-rw-r--r--changelog.md4
-rw-r--r--compiler/jsgen.nim2
-rw-r--r--tests/js/tstring_assignment.nim12
3 files changed, 16 insertions, 2 deletions
diff --git a/changelog.md b/changelog.md
index 89eb999d1..12297de41 100644
--- a/changelog.md
+++ b/changelog.md
@@ -9,6 +9,8 @@
   trailing comma. The underlying AST is ``nnkTupleConst(newLit 1)`` for this
   example. ``nnkTupleConstr`` is a new node kind your macros need to be able
   to deal with!
+- Indexing into a ``cstring`` for the JS target is now mapped
+  to ``charCodeAt``.
 
 #### Breaking changes in the standard library
 
@@ -68,7 +70,7 @@
   target. To use it, compile your code with `--hotReloading:on` and use a
   helper library such as LiveReload or BrowserSync.
 
-- Added ``macros.getProjectPath`` and ``ospaths.putEnv`` procs to Nim's virtual 
+- Added ``macros.getProjectPath`` and ``ospaths.putEnv`` procs to Nim's virtual
   machine.
 
 ### Bugfixes
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index 727e9209d..6841482cd 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -1109,6 +1109,8 @@ proc genArrayAccess(p: PProc, n: PNode, r: var TCompRes) =
       r.res = "ord(@$1[$2])" % [r.address, r.res]
     else:
       r.res = "$1[$2]" % [r.address, r.res]
+  elif ty.kind == tyCString:
+    r.res = "$1.charCodeAt($2)" % [r.address, r.res]
   else:
     r.res = "$1[$2]" % [r.address, r.res]
   r.address = nil
diff --git a/tests/js/tstring_assignment.nim b/tests/js/tstring_assignment.nim
index bdd93e6b5..97ffa748f 100644
--- a/tests/js/tstring_assignment.nim
+++ b/tests/js/tstring_assignment.nim
@@ -1,5 +1,6 @@
 discard """
-  output: '''true'''
+  output: '''true
+asdfasekjkler'''
 """
 
 # bug #4471
@@ -9,3 +10,12 @@ when true:
   s2.setLen(0)
   # fails - s1.len == 0
   echo s1.len == 3
+
+# bug #4470
+proc main(s: cstring): string =
+  result = newString(0)
+  for i in 0..<s.len:
+    if s[i] >= 'a' and s[i] <= 'z':
+      result.add s[i]
+
+echo main("asdfasekjkleräöü")