Merge pull request #9460 from recloser/fix-printing-uninitialized-strings

Fix printing and comparing uninitialized strings on JS
This commit is contained in:
Arne Döring 2018-10-22 08:26:53 +02:00 • committed by GitHub
commit 0dd3038a39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 9 deletions

View file

@ -228,6 +228,7 @@ proc cstrToNimstr(c: cstring): string {.asmNoStackFrame, compilerproc.} =
proc toJSStr(s: string): cstring {.asmNoStackFrame, compilerproc.} =
asm """
if (`s` === null) return "";
var len = `s`.length;
var asciiPart = new Array(len);
var fcc = String.fromCharCode;
@ -330,6 +331,8 @@ proc cmp(x, y: string): int =
proc eqStrings(a, b: string): bool {.asmNoStackFrame, compilerProc.} =
asm """
if (`a` == `b`) return true;
if (`a` === null && `b`.length == 0) return true;
if (`b` === null && `a`.length == 0) return true;
if ((!`a`) || (!`b`)) return false;
var alen = `a`.length;
if (alen != `b`.length) return false;

View file

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

17
tests/js/tnilstrs.nim Normal file
View file

@ -0,0 +1,17 @@
block:
var x: string
var y = "foo"
echo x
doAssert x == ""
doAssert "" == x
add(x, y)
y[0] = 'm'
doAssert y == "moo" and x == "foo"
block:
var x = "foo".cstring
var y: string
add(y, x)
doAssert y == "foo"

View file

@ -1,5 +0,0 @@
var x: string
var y = "foo"
add(x, y)
y[0] = 'm'
doAssert y == "moo" and x == "foo"