fsh / integers

Ergonomic GMP Integers for Nim
MIT License
10 stars 1 forks source link

Unable to compile code #2

Closed C-NERD closed 1 year ago

C-NERD commented 1 year ago

when i tried to compile a sample code

when not defined(js):

    import pkg / integers
    from std / strutils import parseInt

else:

    import std / jsbigints

type

    int128* = object

        when not defined(js):

            value : Integer

        else:

            value : JsBigInt

const HexChars = "0123456789ABCDEF"
when not defined(js):

    const 
        #HighestInt128 = newInteger("170141183460469231731687303715884105727", 10)
        HighestUInt128 = newInteger("340282366920938463463374607431768211455", 10)

    template `int`(data : Integer) : untyped = parseInt($data)

else:

    const 
        #HighestInt128 = big(cstring("170141183460469231731687303715884105727"))
        HighestUInt128 = big(cstring("340282366920938463463374607431768211455"))

    template `int`(data : JsBigInt) : untyped = int(toNumber(data))

proc `'i128`*(data : string) : int128 =

    when not defined(js):

        result = int128(value : newInteger(data))

    else:

        result = int128(value : big(cstring(data)))

proc toHex*(data : int128) : string =

    var n = data.value
    result = newString(32)
    when not defined(js):

        let handleNegative = data.value < 0
        for j in countdown(31, 0):

            result[j] = HexChars[int(n and 0xF)]
            n = n shr 4
            # handle negative overflow
            if n == 0 and handleNegative: n = HighestUInt128

    else:

        let handleNegative = data.value < 0'big
        for j in countdown(31, 0):

            result[j] = HexChars[int(n and 0xF)]
            n = n shr 4'big
            # handle negative overflow
            if n == 0'big and handleNegative: n = HighestUInt128

when isMainModule:

    echo toHex(100000000000000000'128)

I get the error

Hint: used config file '/home/cnerd/.choosenim/toolchains/nim-1.6.14/config/nim.cfg' [Conf]
Hint: used config file '/home/cnerd/.choosenim/toolchains/nim-1.6.14/config/config.nims' [Conf]
Hint: used config file '/home/cnerd/Documents/software/bcs/config.nims' [Conf]
.......................................................................................
/home/cnerd/.nimble/pkgs2/integers-0.1.1-0cd987d8c999f1a34bf3b62448a5880bd2558e0b/integers/core.nim(57, 11) Error: cannot 'importc' variable at compile time; mpz_init_set_str
C-NERD commented 1 year ago

apparently the problem is at

const 
        #HighestInt128 = newInteger("170141183460469231731687303715884105727", 10)
        HighestUInt128 = newInteger("340282366920938463463374607431768211455", 10)

the library cannot initialize the wrapped function on compiled time

fsh commented 1 year ago

Sorry for slow response, but yes, external FFI doesn't seem to work out of the box at compile-time. I tried playing around with --experimental:compiletimeFFI but I still can't get Nim to accept it.

It is fixed by changing the variable declaration from const (compile-time) to let (run-time const).

One additional note: the int template is converting to strings to convert back to integers: there's already a n.getOr(default_value_if_it_doesnt_fit) which will return an integer of equal type to the argument. Alternatively there's n.getUnsafe(int).