Skip to content

Commit

Permalink
[backport:1.4] JS cstring null fixes (#16979)
Browse files Browse the repository at this point in the history
* [backport:1.4] JS cstring null fixes
* fix JS move string
* make it look cleaner
  • Loading branch information
metagn authored Feb 11, 2021
1 parent f3c39bf commit 81533a0
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 8 deletions.
22 changes: 19 additions & 3 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2011,7 +2011,9 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
gen(p, n[2], rhs)

if skipTypes(n[1].typ, abstractVarRange).kind == tyCString:
r.res = "$1 += $2;" % [lhs.rdLoc, rhs.rdLoc]
let (b, tmp) = maybeMakeTemp(p, n[2], rhs)
r.res = "if (null != $1) { if (null == $2) $2 = $3; else $2 += $3; }" %
[b, lhs.rdLoc, tmp]
else:
let (a, tmp) = maybeMakeTemp(p, n[1], lhs)
r.res = "$1.push.apply($3, $2);" % [a, rhs.rdLoc, tmp]
Expand Down Expand Up @@ -2062,9 +2064,23 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
of mDestroy: discard "ignore calls to the default destructor"
of mOrd: genOrd(p, n, r)
of mLengthStr, mLengthSeq, mLengthOpenArray, mLengthArray:
unaryExpr(p, n, r, "", "($1).length")
var x: TCompRes
gen(p, n[1], x)
if skipTypes(n[1].typ, abstractInst).kind == tyCString:
let (a, tmp) = maybeMakeTemp(p, n[1], x)
r.res = "(($1) == null ? 0 : ($2).length)" % [a, tmp]
else:
r.res = "($1).length" % [x.rdLoc]
r.kind = resExpr
of mHigh:
unaryExpr(p, n, r, "", "(($1).length-1)")
var x: TCompRes
gen(p, n[1], x)
if skipTypes(n[1].typ, abstractInst).kind == tyCString:
let (a, tmp) = maybeMakeTemp(p, n[1], x)
r.res = "(($1) == null ? -1 : ($2).length - 1)" % [a, tmp]
else:
r.res = "($1).length - 1" % [x.rdLoc]
r.kind = resExpr
of mInc:
if n[1].typ.skipTypes(abstractRange).kind in {tyUInt..tyUInt64}:
binaryUintExpr(p, n, r, "+", true)
Expand Down
18 changes: 16 additions & 2 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2029,8 +2029,14 @@ type
when NimStackTraceMsgs:
frameMsgLen*: int ## end position in frameMsgBuf for this frame.

when defined(js):
when defined(js) or defined(nimdoc):
proc add*(x: var string, y: cstring) {.asmNoStackFrame.} =
## Appends `y` to `x` in place.
runnableExamples:
var tmp = ""
tmp.add(cstring("ab"))
tmp.add(cstring("cd"))
doAssert tmp == "abcd"
asm """
if (`x` === null) { `x` = []; }
var off = `x`.length;
Expand All @@ -2039,7 +2045,15 @@ when defined(js):
`x`[off+i] = `y`.charCodeAt(i);
}
"""
proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".}
proc add*(x: var cstring, y: cstring) {.magic: "AppendStrStr".} =
## Appends `y` to `x` in place.
## Only implemented for JS backend.
runnableExamples:
when defined(js):
var tmp: cstring = ""
tmp.add(cstring("ab"))
tmp.add(cstring("cd"))
doAssert tmp == cstring("abcd")

elif hasAlloc:
{.push stackTrace: off, profiler: off.}
Expand Down
2 changes: 1 addition & 1 deletion lib/system/jssys.nim
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ proc genericReset(x: JSRef, ti: PNimType): JSRef {.compilerproc.} =
asm "`result` = {m_type: `ti`};"
else:
asm "`result` = {};"
of tySequence, tyOpenArray:
of tySequence, tyOpenArray, tyString:
asm """
`result` = [];
"""
Expand Down
10 changes: 9 additions & 1 deletion tests/js/tnilstrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ block:
var x = "foo".cstring
var y: string
add(y, x)
doAssert y == "foo"
doAssert y == "foo"

block:
type Foo = object
a: string
var foo = Foo(a: "foo")
var y = move foo.a
doAssert foo.a.len == 0
doAssert y == "foo"
25 changes: 24 additions & 1 deletion tests/system/tdollars.nim
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,35 @@ block: # `$`(SomeInteger)
testType int64
testType BiggestInt

block: # #14350 for JS
block: # #14350, #16674, #16686 for JS
var cstr: cstring
doAssert cstr == cstring(nil)
doAssert cstr == nil
doAssert cstr.isNil
doAssert cstr != cstring("")
doAssert cstr.len == 0

when defined(js):
cstr.add(cstring("abc"))
doAssert cstr == cstring("abc")

var nil1, nil2: cstring = nil

nil1.add(nil2)
doAssert nil1 == cstring(nil)
doAssert nil2 == cstring(nil)

nil1.add(cstring(""))
doAssert nil1 == cstring("")
doAssert nil2 == cstring(nil)

nil1.add(nil2)
doAssert nil1 == cstring("")
doAssert nil2 == cstring(nil)

nil2.add(nil1)
doAssert nil1 == cstring("")
doAssert nil2 == cstring("")


proc main()=
Expand Down

0 comments on commit 81533a0

Please sign in to comment.