Skip to content

Commit

Permalink
works w js too
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jul 19, 2021
1 parent 19c8226 commit d768c1c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ const # magic checked op; magic unchecked op;
mBoolToStr: ["nimBoolToStr", "nimBoolToStr"],
mIntToStr: ["cstrToNimstr", "cstrToNimstr"],
mInt64ToStr: ["cstrToNimstr", "cstrToNimstr"],
mFloatToStr: ["cstrToNimstr", "cstrToNimstr"],
mFloatToStr: ["cstrToNimstr", "cstrToNimstr"], # PRTEMP
mCStrToStr: ["cstrToNimstr", "cstrToNimstr"],
mStrToStr: ["", ""]]

Expand Down
4 changes: 2 additions & 2 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2472,8 +2472,8 @@ when defined(js) or defined(nimscript):
proc addInt*(result: var string; x: int64) =
result.add $x

proc addFloat*(result: var string; x: float) =
result.add $x
# proc addFloat*(result: var string; x: float) =
# result.add $x

proc quit*(errormsg: string, errorcode = QuitFailure) {.noreturn.} =
## A shorthand for `echo(errormsg); quit(errorcode)`.
Expand Down
10 changes: 3 additions & 7 deletions lib/system/dollars.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ proc `$`*(x: int64): string {.magic: "Int64ToStr", noSideEffect.}
## The stringify operator for an integer argument. Returns `x`
## converted to a decimal string.

proc `$`*(x: float): string {.magic: "FloatToStr", noSideEffect.}
## The stringify operator for a float argument. Returns `x`
## converted to a decimal string.

proc `$`*(x: float32): string {.magic: "FloatToStr", noSideEffect.}
## The stringify operator for a float32 argument. Returns `x`
## converted to a decimal string.
func `$`*(x: float | float32): string =
## Outplace version of `addFloat`.
result.addFloat(x)

proc `$`*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
## The stringify operator for a boolean argument. Returns `x`
Expand Down
41 changes: 37 additions & 4 deletions lib/system/formatfloat.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ proc addCstringN(result: var string, buf: cstring; buflen: int) =
let oldLen = result.len
let newLen = oldLen + buflen
result.setLen newLen
# when defined(js):
# # debugEcho()
# # for i in oldLen..<newLen:
# for i in 0..<buflen:
# result[oldLen + i] = buf[i]
# else:
# copyMem(result[oldLen].addr, buf, buflen)
c_memcpy(result[oldLen].addr, buf, buflen.csize_t)

Expand Down Expand Up @@ -99,6 +105,23 @@ proc addFloatSprintf*(result: var string; x: float) =
let n = writeFloatToBufferSprintf(buffer, x)
result.addCstringN(cstring(buffer[0].addr), n)

proc nimFloatToString(a: float): cstring =
## ensures the result doesn't print like an integer, i.e. return 2.0, not 2
# print `-0.0` properly
asm """
function nimOnlyDigitsOrMinus(n) {
return n.toString().match(/^-?\d+$/);
}
if (Number.isSafeInteger(`a`))
`result` = `a` === 0 && 1 / `a` < 0 ? "-0.0" : `a`+".0"
else {
`result` = `a`+""
if(nimOnlyDigitsOrMinus(`result`)){
`result` = `a`+".0"
}
}
"""

proc addFloat*(result: var string; x: float) {.inline.} =
## Converts float to its string representation and appends it to `result`.
##
Expand All @@ -107,10 +130,20 @@ proc addFloat*(result: var string; x: float) {.inline.} =
## a = "123"
## b = 45.67
## a.addFloat(b) # a <- "12345.67"
when defined(nimFpRoundtrips):
addFloatRoundtrip(result, x)
else:
addFloatSprintf(result, x)
template impl =
when defined(nimFpRoundtrips):
addFloatRoundtrip(result, x)
else:
addFloatSprintf(result, x)
when defined(js):
when nimvm: impl()
else:
# result.add $nimFloatToString(x)
# result.add cstrToNimstr(nimFloatToString(x))
let tmp = nimFloatToString(x)
for i in 0..<tmp.len:
result.add tmp[i]
else: impl()

proc nimFloatToStr(f: float): string {.compilerproc.} =
result = newStringOfCap(8)
Expand Down
6 changes: 3 additions & 3 deletions lib/system/repr_v2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ proc repr*(x: uint64): string {.noSideEffect.} =
## converted to a decimal string.
$x #Calls `$` from system/strmantle.nim

proc repr*(x: float): string {.magic: "FloatToStr", noSideEffect.}
## repr for a float argument. Returns `x`
## converted to a decimal string.
proc repr*(x: float): string =
## Same as $x
$x

proc repr*(x: bool): string {.magic: "BoolToStr", noSideEffect.}
## repr for a boolean argument. Returns `x`
Expand Down

0 comments on commit d768c1c

Please sign in to comment.