JS backend properly extends string with setLen (#21087)

* Add test case

* Extend string with '0' when setting length to be longer
This commit is contained in:
Jake Leahy
2022-12-13 18:43:37 +11:00
committed by GitHub
parent 1fefb8e92a
commit 7ae7832f76
2 changed files with 14 additions and 1 deletions

View File

@@ -2209,7 +2209,9 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
if optOverflowCheck notin p.options: binaryExpr(p, n, r, "", "$1 -= $2")
else: binaryExpr(p, n, r, "subInt", "$1 = subInt($3, $2)", true)
of mSetLengthStr:
binaryExpr(p, n, r, "mnewString", "($1.length = $2)")
binaryExpr(p, n, r, "mnewString",
"""if ($1.length < $2) { for (var i = $3.length; i < $4; ++i) $3.push(0); }
else {$3.length = $4; }""")
of mSetLengthSeq:
var x, y: TCompRes
gen(p, n[1], x)

11
tests/js/t20235.nim Normal file
View File

@@ -0,0 +1,11 @@
discard """
action: "run"
output: "0 4"
"""
proc main =
var s = ""
s.setLen(4)
echo s[0].ord, " ", s.len
main()