mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-18 17:08:32 +00:00
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:
@@ -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
11
tests/js/t20235.nim
Normal 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()
|
||||
Reference in New Issue
Block a user