clang compilation error fixed (#5)

This commit is contained in:
Yuriy Glukhov 2018-04-04 18:47:31 +03:00 committed by Mamy Ratsimbazafy
parent 39b82b5397
commit 1d81a7c7ba
2 changed files with 14 additions and 25 deletions

12
nim.cfg
View File

@ -1,12 +0,0 @@
# TODO FIXME
# Default compiler on Mac is Clang.
# Currently it does not compile due to "Flexible array member with non-trivial destruction"
# See https://github.com/status-im/nimbus/issues/2 and https://github.com/status-im/nim-ttmath/issues/10
# As a workaround, forces GCC-7 on Mac
# GCC-7 is available through Homebrew
@if macosx:
cc:"gcc"
gcc.cpp.exe:"/usr/local/bin/g++-7"
gcc.cpp.linkerexe:"/usr/local/bin/g++-7"
@end

View File

@ -8,10 +8,18 @@ type
Value* = ref object
case kind*: ValueKind:
of VInt:
i*: Int256
Fi: array[32, byte] #Int256
of VBinary:
b*: seq[byte]
# TODO: The Int256 value is stored as array[32, byte], and we bitcast it
# back and forth. This is a hacky workaround for the problem that clang
# doesn't let you store ttmath types inside nim variant types (unions). Things
# should get better when we switch to mpint.
proc i*(v: Value): Int256 {.inline.} =
cast[ptr Int256](unsafeAddr v.Fi)[]
proc `$`*(value: Value): string =
case value.kind:
of VInt:
@ -19,23 +27,16 @@ proc `$`*(value: Value): string =
of VBinary:
&"Binary({value.b})"
proc vint*(i: int): Value =
Value(kind: VInt, i: i.int256)
proc toArr(i: Int256): array[32, byte] {.inline.} =
cast[ptr array[32, byte]](unsafeAddr i)[]
proc vint*(i: Int256): Value =
Value(kind: VInt, i: i)
Value(kind: VInt, Fi: i.toArr)
proc vint*(i: int): Value {.inline.} = vint(i.int256)
proc vbinary*(b: string): Value =
Value(kind: VBinary, b: b.mapIt(it.byte))
proc vbinary*(b: seq[byte]): Value =
Value(kind: VBinary, b: b)
proc `==`*(a: Value, b: Value): bool =
if a.kind != b.kind:
return false
case a.kind:
of VInt:
a.i == b.i
of VBinary:
a.b == b.b