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
parent 87955eaf30
commit b195204549
2 changed files with 8 additions and 3 deletions

View File

@@ -2741,10 +2741,11 @@ type
when defined(JS):
proc add*(x: var string, y: cstring) {.asmNoStackFrame.} =
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) {
`x`[len] = `y`.charCodeAt(i);
++len;
`x`[off+i] = `y`.charCodeAt(i);
}
"""
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"