Skip to content

Commit

Permalink
feat: string <-> bigint, conditional bigint->int64/uint64
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed May 12, 2023
1 parent 89a4b4a commit 0a4c4ca
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions gnovm/pkg/gnolang/values_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,10 +808,33 @@ GNO_CASE:
}
case BigintKind:
switch k {
case IntKind, Int32Kind, Int64Kind, UintKind, Uint32Kind, Uint64Kind:
case IntKind, Int8Kind, Int16Kind, Int32Kind, UintKind, Uint8Kind, Uint16Kind, Uint32Kind:
panic(fmt.Sprintf(
"cannot convert %s to %s since it can lead loss of precision",
"cannot convert %s to %s",
tvk.String(), k.String()))
case Int64Kind:
x := tv.GetBigInt()
if x.IsInt64() {
tv.T = t
tv.SetInt64(x.Int64())
} else {
panic(fmt.Sprintf(
"cannot convert %s to %s",
tvk.String(), k.String()))
}
case Uint64Kind:
x := tv.GetBigInt()
if x.IsUint64() {
tv.T = t
tv.SetUint64(x.Uint64())
} else {
panic(fmt.Sprintf(
"cannot convert %s to %s",
tvk.String(), k.String()))
}
case StringKind:
tv.T = t
tv.SetString(StringValue(tv.GetBigInt().String()))
default:
panic(fmt.Sprintf(
"cannot convert %s to %s",
Expand All @@ -838,6 +861,21 @@ GNO_CASE:
"cannot convert %s to %s",
tvk.String(), t.String()))
}
case PrimitiveType:
switch k {
case BigintKind:
bi := new(big.Int)
bi, ok := bi.SetString(tv.GetString(), 10)
if !ok {
panic(fmt.Sprintf(
"cannot convert %s to %s",
tvk.String(), k.String(),
))
}
tv.V = BigintValue{V: bi}
tv.T = t

}
default:
panic(fmt.Sprintf(
"cannot convert %s to %s",
Expand Down

0 comments on commit 0a4c4ca

Please sign in to comment.