summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2019-07-12 10:11:59 +0200
committerAraq <rumpf_a@web.de>2019-07-12 10:11:59 +0200
commiteaf4b42ff99b4e7bd77232dc53035e93682aaa73 (patch)
treee3624c91f4e2b778d58f0909dde6dfaa04b498a8
parent511b6ae27c1aae27c3f7bb8aed83245a752d4f4c (diff)
downloadNim-eaf4b42ff99b4e7bd77232dc53035e93682aaa73.tar.gz
JS codegen: supports toOpenArray [bugfix]
-rw-r--r--changelog_v020.md1
-rw-r--r--compiler/jsgen.nim12
-rw-r--r--lib/system.nim21
-rw-r--r--tests/js/tstringitems.nim10
4 files changed, 30 insertions, 14 deletions
diff --git a/changelog_v020.md b/changelog_v020.md
index dbee829fb..346ccda89 100644
--- a/changelog_v020.md
+++ b/changelog_v020.md
@@ -26,6 +26,7 @@
 
 ## Library additions
 
+- `toOpenArray` is now available for the JS target.
 
 ## Library changes
 
diff --git a/compiler/jsgen.nim b/compiler/jsgen.nim
index 84909b745..0c04b339c 100644
--- a/compiler/jsgen.nim
+++ b/compiler/jsgen.nim
@@ -482,7 +482,7 @@ template binaryExpr(p: PProc, n: PNode, r: var TCompRes, magic, frmt: string) =
     a, tmp = x.rdLoc
     b, tmp2 = y.rdLoc
   when "$3" in frmt: (a, tmp) = maybeMakeTemp(p, n[1], x)
-  when "$4" in frmt: (a, tmp) = maybeMakeTemp(p, n[1], x)
+  when "$4" in frmt: (b, tmp2) = maybeMakeTemp(p, n[2], y)
 
   r.res = frmt % [a, b, tmp, tmp2]
   r.kind = resExpr
@@ -2040,8 +2040,14 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
   of mParseBiggestFloat:
     useMagic(p, "nimParseBiggestFloat")
     genCall(p, n, r)
-  of mArray:
-    genCall(p, n, r)
+  of mSlice:
+    # arr.slice([begin[, end]]): 'end' is exclusive
+    var x, y, z: TCompRes
+    gen(p, n.sons[1], x)
+    gen(p, n.sons[2], y)
+    gen(p, n.sons[3], z)
+    r.res = "($1.slice($2, $3+1))" % [x.rdLoc, y.rdLoc, z.rdLoc]
+    r.kind = resExpr
   else:
     genCall(p, n, r)
     #else internalError(p.config, e.info, 'genMagic: ' + magicToStr[op]);
diff --git a/lib/system.nim b/lib/system.nim
index 966375445..61fb6ec03 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -4484,18 +4484,19 @@ when defined(windows) and appType == "console" and defined(nimSetUtf8CodePage) a
   discard setConsoleOutputCP(65001) # 65001 - utf-8 codepage
 
 when not defined(js):
-  proc toOpenArray*[T](x: seq[T]; first, last: int): openArray[T] {.
-    magic: "Slice".}
-  proc toOpenArray*[T](x: openArray[T]; first, last: int): openArray[T] {.
-    magic: "Slice".}
   proc toOpenArray*[T](x: ptr UncheckedArray[T]; first, last: int): openArray[T] {.
     magic: "Slice".}
-  proc toOpenArray*[I, T](x: array[I, T]; first, last: I): openArray[T] {.
-    magic: "Slice".}
-  proc toOpenArray*(x: string; first, last: int): openArray[char] {.
-    magic: "Slice".}
-  proc toOpenArrayByte*(x: string; first, last: int): openArray[byte] {.
-    magic: "Slice".}
+
+proc toOpenArray*[T](x: seq[T]; first, last: int): openArray[T] {.
+  magic: "Slice".}
+proc toOpenArray*[T](x: openArray[T]; first, last: int): openArray[T] {.
+  magic: "Slice".}
+proc toOpenArray*[I, T](x: array[I, T]; first, last: I): openArray[T] {.
+  magic: "Slice".}
+proc toOpenArray*(x: string; first, last: int): openArray[char] {.
+  magic: "Slice".}
+proc toOpenArrayByte*(x: string; first, last: int): openArray[byte] {.
+  magic: "Slice".}
 
 type
   ForLoopStmt* {.compilerproc.} = object ## \
diff --git a/tests/js/tstringitems.nim b/tests/js/tstringitems.nim
index f09793dde..16df04149 100644
--- a/tests/js/tstringitems.nim
+++ b/tests/js/tstringitems.nim
@@ -1,6 +1,9 @@
 discard """
   output: '''Hello
-Hello'''
+Hello
+c
+d
+e'''
 """
 
 block: # bug #2581
@@ -85,3 +88,8 @@ block: # String cmp
   doAssert(cmp("foo", "foobar") == -3)
   doAssert(cmp("fooz", "foog") == 19)
   doAssert(cmp("foog", "fooz") == -19)
+
+proc main(x: openArray[char]) =
+  for c in x: echo c
+
+main(toOpenArray(['a', 'b', 'c', 'd', 'e'], 2, 4))