From d7cc9016f3a457b0980259973f4b0089d25cd6bc Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Fri, 13 Apr 2018 23:41:31 +0200 Subject: [PATCH] fixes #4470 --- changelog.md | 4 +++- compiler/jsgen.nim | 2 ++ tests/js/tstring_assignment.nim | 12 +++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 89eb999d16..12297de412 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 727e9209d9..6841482cdb 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 bdd93e6b55..97ffa748fe 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..= 'a' and s[i] <= 'z': + result.add s[i] + +echo main("asdfasekjkleräöü")