From 2b03bed2db195c132bcd68939f4f9f5a3f4fcf9f Mon Sep 17 00:00:00 2001 From: recloser <44084068+recloser@users.noreply.github.com> Date: Sun, 21 Oct 2018 21:18:10 +0200 Subject: [PATCH] Fix printing and comparing uninitialized strings --- lib/system/jssys.nim | 3 +++ tests/js/taddnilstr.nim | 4 ---- tests/js/tnilstrs.nim | 17 +++++++++++++++++ tests/js/tstrconcat.nim | 5 ----- 4 files changed, 20 insertions(+), 9 deletions(-) delete mode 100644 tests/js/taddnilstr.nim create mode 100644 tests/js/tnilstrs.nim delete mode 100644 tests/js/tstrconcat.nim diff --git a/lib/system/jssys.nim b/lib/system/jssys.nim index 836ac198dc..5ac0ca8b26 100644 --- a/lib/system/jssys.nim +++ b/lib/system/jssys.nim @@ -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; diff --git a/tests/js/taddnilstr.nim b/tests/js/taddnilstr.nim deleted file mode 100644 index f5b934fdda..0000000000 --- a/tests/js/taddnilstr.nim +++ /dev/null @@ -1,4 +0,0 @@ -var x = "foo".cstring -var y: string -add(y, x) -doAssert y == "foo" diff --git a/tests/js/tnilstrs.nim b/tests/js/tnilstrs.nim new file mode 100644 index 0000000000..c0048cb240 --- /dev/null +++ b/tests/js/tnilstrs.nim @@ -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" \ No newline at end of file diff --git a/tests/js/tstrconcat.nim b/tests/js/tstrconcat.nim deleted file mode 100644 index 37c8db6871..0000000000 --- a/tests/js/tstrconcat.nim +++ /dev/null @@ -1,5 +0,0 @@ -var x: string -var y = "foo" -add(x, y) -y[0] = 'm' -doAssert y == "moo" and x == "foo"