fixes #18134; registers formatBiggestFloat in VM (#21299)

fixes #18134; registers formatBiggestFloat in vmops

strformat supports float format in VM
This commit is contained in:
ringabout 2023-01-27 06:03:59 +08:00 • committed by GitHub
commit fc068ee06d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 61 deletions

View file

@ -38,6 +38,8 @@ when defined(nimPreviewSlimSystem):
else:
from std/formatfloat import addFloatRoundtrip, addFloatSprintf
from std/strutils import formatBiggestFloat, FloatFormatMode
# There are some useful procs in vmconv.
import vmconv, vmmarshal
@ -374,6 +376,10 @@ proc registerAdditionalOps*(c: PCtx) =
let x = a.getFloat(1)
addFloatSprintf(p.strVal, x)
registerCallback c, "stdlib.strutils.formatBiggestFloat", proc(a: VmArgs) =
setResult(a, formatBiggestFloat(a.getFloat(0), FloatFormatMode(a.getInt(1)),
a.getInt(2), chr(a.getInt(3))))
wrapIterator("stdlib.envvars.envPairsImplSeq"): envPairs()
registerCallback c, "stdlib.marshal.toVM", proc(a: VmArgs) =

View file

@ -2413,6 +2413,9 @@ func formatBiggestFloat*(f: BiggestFloat, format: FloatFormatMode = ffDefault,
doAssert x.formatBiggestFloat() == "123.4560000000000"
doAssert x.formatBiggestFloat(ffDecimal, 4) == "123.4560"
doAssert x.formatBiggestFloat(ffScientific, 2) == "1.23e+02"
when nimvm:
discard "implemented in the vmops"
else:
when defined(js):
var precision = precision
if precision == -1:

View file

@ -475,8 +475,10 @@ proc main() =
# Note: times.format adheres to the format protocol. Test that this
# works:
var dt = initDateTime(01, mJan, 2000, 00, 00, 00)
when nimvm:
discard
else:
var dt = dateTime(2000, mJan, 01, 00, 00, 00)
check &"{dt:yyyy-MM-dd}", "2000-01-01"
var tm = fromUnix(0)
@ -558,5 +560,6 @@ proc main() =
doAssert &"""{(if true: "'" & "'" & ')' else: "")}""" == "'')"
doAssert &"{(if true: \"\'\" & \"'\" & ')' else: \"\")}" == "'')"
doAssert fmt"""{(if true: "'" & ')' else: "")}""" == "')"
# xxx static: main()
static: main()
main()