JS codegen: supports toOpenArray [bugfix]

This commit is contained in:
Araq
2019-07-12 10:11:59 +02:00
parent 511b6ae27c
commit eaf4b42ff9
4 changed files with 30 additions and 14 deletions

View File

@@ -26,6 +26,7 @@
## Library additions
- `toOpenArray` is now available for the JS target.
## Library changes

View File

@@ -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]);

View File

@@ -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 ## \

View File

@@ -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))