From 0a4c4cade8da3ad42cf218fefcd0eae471d51fdb Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 12 May 2023 16:58:48 +0900 Subject: [PATCH] feat: string <-> bigint, conditional bigint->int64/uint64 --- gnovm/pkg/gnolang/values_conversions.go | 42 +++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/gnovm/pkg/gnolang/values_conversions.go b/gnovm/pkg/gnolang/values_conversions.go index acd579ea672..f72f470d6b9 100644 --- a/gnovm/pkg/gnolang/values_conversions.go +++ b/gnovm/pkg/gnolang/values_conversions.go @@ -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", @@ -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",