Fix add(string, cstring) when the lhs is null (#8951)

This commit is contained in:
LemonBoy 2018-09-12 10:10:00 +02:00 • committed by Andreas Rumpf
commit b195204549
2 changed files with 8 additions and 3 deletions

View file

@ -2741,10 +2741,11 @@ type
when defined(JS): when defined(JS):
proc add*(x: var string, y: cstring) {.asmNoStackFrame.} = proc add*(x: var string, y: cstring) {.asmNoStackFrame.} =
asm """ asm """
var len = `x`.length; if (`x` === null) { `x` = []; }
var off = `x`.length;
`x`.length += `y`.length;
for (var i = 0; i < `y`.length; ++i) { for (var i = 0; i < `y`.length; ++i) {
`x`[len] = `y`.charCodeAt(i); `x`[off+i] = `y`.charCodeAt(i);
++len;
} }
""" """
proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".} proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".}

4
tests/js/taddnilstr.nim Normal file
View file

@ -0,0 +1,4 @@
var x = "foo".cstring
var y: string
add(y, x)
doAssert y == "foo"