fix nim js cmp fails at CT (#16473)

This commit is contained in:
flywind
2020-12-27 03:13:57 -06:00
committed by GitHub
parent 4cf605dcf6
commit 3f9a2ebea5
4 changed files with 49 additions and 24 deletions

View File

@@ -2394,14 +2394,8 @@ when notJSnotNims:
""".}
when defined(js):
when not defined(nimscript):
include "system/jssys"
include "system/reprjs"
else:
proc cmp(x, y: string): int =
if x == y: return 0
if x < y: return -1
return 1
include "system/jssys"
include "system/reprjs"
when defined(js) or defined(nimscript):
proc addInt*(result: var string; x: int64) =

View File

@@ -340,7 +340,12 @@ proc cmpStrings(a, b: string): int {.asmNoStackFrame, compilerproc.} =
"""
proc cmp(x, y: string): int =
return cmpStrings(x, y)
when nimvm:
if x == y: result = 0
elif x < y: result = -1
else: result = 1
else:
result = cmpStrings(x, y)
proc eqStrings(a, b: string): bool {.asmNoStackFrame, compilerproc.} =
asm """

20
tests/misc/tstrtabs.nim Normal file
View File

@@ -0,0 +1,20 @@
discard """
targets: "c cpp js"
"""
import std/strtabs
proc fun()=
let ret = newStringTable(modeCaseSensitive)
ret["foo"] = "bar"
doAssert $ret == "{foo: bar}"
let b = ret["foo"]
doAssert b == "bar"
proc main()=
static: fun()
fun()
main()

View File

@@ -1,16 +1,14 @@
discard """
output: '''OK
@[@[], @[], @[], @[], @[]]
'''
targets: "c cpp js"
"""
const characters = "abcdefghijklmnopqrstuvwxyz"
const numbers = "1234567890"
var s: string
proc test_string_slice() =
# test "slice of length == len(characters)":
# replace characters completely by numbers
var s: string
s = characters
s[0..^1] = numbers
doAssert s == numbers
@@ -51,11 +49,13 @@ proc test_string_slice() =
s[2..0] = numbers
doAssert s == "ab1234567890cdefghijklmnopqrstuvwxyz"
# bug #6223
doAssertRaises(IndexDefect):
discard s[0..999]
when nimvm:
discard
else:
# bug #6223
doAssertRaises(IndexDefect):
discard s[0..999]
echo("OK")
proc test_string_cmp() =
let world = "hello\0world"
@@ -76,9 +76,6 @@ proc test_string_cmp() =
doAssert cmp(world, hello) > 0
doAssert cmp(world, goodbye) > 0
test_string_slice()
test_string_cmp()
#--------------------------
# bug #7816
@@ -87,9 +84,9 @@ import sequtils
proc tester[T](x: T) =
let test = toSeq(0..4).map(i => newSeq[int]())
echo test
doAssert $test == "@[@[], @[], @[], @[], @[]]"
tester(1)
# #14497
func reverse*(a: string): string =
@@ -97,4 +94,13 @@ func reverse*(a: string): string =
for i in 0 ..< a.len div 2:
swap(result[i], result[^(i + 1)])
doAssert reverse("hello") == "olleh"
proc main() =
test_string_slice()
test_string_cmp()
tester(1)
doAssert reverse("hello") == "olleh"
static: main()
main()