* Fix negative arrays for JS, refs #13966 * small extra fix: no need to .slice() cstring in JS
This commit is contained in:
parent
3b5a504692
commit
800ce5b950
2 changed files with 10 additions and 4 deletions
|
|
@ -1161,9 +1161,9 @@ proc genArrayAddr(p: PProc, n: PNode, r: var TCompRes) =
|
||||||
first = firstOrd(p.config, typ[0])
|
first = firstOrd(p.config, typ[0])
|
||||||
if optBoundsCheck in p.options:
|
if optBoundsCheck in p.options:
|
||||||
useMagic(p, "chckIndx")
|
useMagic(p, "chckIndx")
|
||||||
r.res = "chckIndx($1, $2, ($3 != null ? $3.length : 0)+$2-1)-$2" % [b.res, rope(first), tmp]
|
r.res = "chckIndx($1, $2, ($3 != null ? $3.length : 0)+$2-1)-($2)" % [b.res, rope(first), tmp]
|
||||||
elif first != 0:
|
elif first != 0:
|
||||||
r.res = "($1)-$2" % [b.res, rope(first)]
|
r.res = "($1)-($2)" % [b.res, rope(first)]
|
||||||
else:
|
else:
|
||||||
r.res = b.res
|
r.res = b.res
|
||||||
r.kind = resExpr
|
r.kind = resExpr
|
||||||
|
|
@ -1897,8 +1897,8 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
|
||||||
let rhsIsLit = n[2].kind in nkStrKinds
|
let rhsIsLit = n[2].kind in nkStrKinds
|
||||||
let (a, tmp) = maybeMakeTemp(p, n[1], lhs)
|
let (a, tmp) = maybeMakeTemp(p, n[1], lhs)
|
||||||
if skipTypes(n[1].typ, abstractVarRange).kind == tyCString:
|
if skipTypes(n[1].typ, abstractVarRange).kind == tyCString:
|
||||||
r.res = "if ($1 != null) { $4 += $2; } else { $4 = $2$3; }" % [
|
r.res = "if ($1 != null) { $3 += $2; } else { $3 = $2; }" % [
|
||||||
a, rhs.rdLoc, if rhsIsLit: nil else: ~".slice()", tmp]
|
a, rhs.rdLoc, tmp]
|
||||||
else:
|
else:
|
||||||
r.res = "if ($1 != null) { $4 = ($4).concat($2); } else { $4 = $2$3; }" % [
|
r.res = "if ($1 != null) { $4 = ($4).concat($2); } else { $4 = $2$3; }" % [
|
||||||
lhs.rdLoc, rhs.rdLoc, if rhsIsLit: nil else: ~".slice()", tmp]
|
lhs.rdLoc, rhs.rdLoc, if rhsIsLit: nil else: ~".slice()", tmp]
|
||||||
|
|
|
||||||
|
|
@ -40,5 +40,11 @@ proc test_arrayboundscheck() =
|
||||||
except:
|
except:
|
||||||
echo "idx out of bounds: ", i
|
echo "idx out of bounds: ", i
|
||||||
|
|
||||||
|
# #13966
|
||||||
|
var negativeIndexed: array[-2..2, int] = [0, 1, 2, 3, 4]
|
||||||
|
negativeIndexed[-1] = 2
|
||||||
|
negativeIndexed[1] = 2
|
||||||
|
doAssert negativeIndexed == [0, 2, 2, 2, 4]
|
||||||
|
|
||||||
test_arrayboundscheck()
|
test_arrayboundscheck()
|
||||||
{.pop.}
|
{.pop.}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue