From 1ee47f218b75f6fd05196c782d4b3c09e2bcba0e Mon Sep 17 00:00:00 2001 From: Omar Sy Date: Wed, 25 Sep 2024 16:12:26 +0200 Subject: [PATCH] fix(gonative): add constant information when we define a go native value --- gnovm/pkg/gnolang/gonative.go | 15 +++++-- gnovm/pkg/gnolang/preprocess_test.go | 8 ++-- gnovm/tests/imports.go | 67 ++++++++++++++-------------- 3 files changed, 49 insertions(+), 41 deletions(-) diff --git a/gnovm/pkg/gnolang/gonative.go b/gnovm/pkg/gnolang/gonative.go index 6127fa42b07..450fa446b6a 100644 --- a/gnovm/pkg/gnolang/gonative.go +++ b/gnovm/pkg/gnolang/gonative.go @@ -1258,16 +1258,25 @@ func (x *PackageNode) DefineGoNativeType(rt reflect.Type) { x.Define(Name(name), asValue(nt)) } -func (x *PackageNode) DefineGoNativeValue(n Name, nv interface{}) { +func (x *PackageNode) DefineGoNativeValue(name Name, nv interface{}) { + x.defineGoNativeValue(false, name, nv) +} + +func (x *PackageNode) DefineGoNativeConstValue(name Name, nv interface{}) { + x.defineGoNativeValue(true, name, nv) +} + +func (x *PackageNode) defineGoNativeValue(isConst bool, n Name, nv interface{}) { if debug { - debug.Printf("*PackageNode.DefineGoNativeValue(%s)\n", reflect.ValueOf(nv).String()) + debug.Printf("*PackageNode.defineGoNativeValue(%s)\n", reflect.ValueOf(nv).String()) } rv := reflect.ValueOf(nv) // rv is not settable, so create something that is. rt := rv.Type() rv2 := reflect.New(rt).Elem() rv2.Set(rv) - x.Define(n, go2GnoValue(nilAllocator, rv2)) + tv := go2GnoValue(nilAllocator, rv2) + x.Define2(isConst, n, tv.T, tv) } // ---------------------------------------- diff --git a/gnovm/pkg/gnolang/preprocess_test.go b/gnovm/pkg/gnolang/preprocess_test.go index 73e1318b062..53ad97dd972 100644 --- a/gnovm/pkg/gnolang/preprocess_test.go +++ b/gnovm/pkg/gnolang/preprocess_test.go @@ -11,8 +11,8 @@ import ( func TestPreprocess_BinaryExpressionOneNative(t *testing.T) { pn := NewPackageNode("time", "time", nil) - pn.DefineGoNativeValue("Millisecond", time.Millisecond) - pn.DefineGoNativeValue("Second", time.Second) + pn.DefineGoNativeConstValue("Millisecond", time.Millisecond) + pn.DefineGoNativeConstValue("Second", time.Second) pn.DefineGoNativeType(reflect.TypeOf(time.Duration(0))) pv := pn.NewPackage() store := gonativeTestStore(pn, pv) @@ -37,8 +37,8 @@ func main() { func TestPreprocess_BinaryExpressionBothNative(t *testing.T) { pn := NewPackageNode("time", "time", nil) - pn.DefineGoNativeValue("March", time.March) - pn.DefineGoNativeValue("Wednesday", time.Wednesday) + pn.DefineGoNativeConstValue("March", time.March) + pn.DefineGoNativeConstValue("Wednesday", time.Wednesday) pn.DefineGoNativeType(reflect.TypeOf(time.Month(0))) pn.DefineGoNativeType(reflect.TypeOf(time.Weekday(0))) pv := pn.NewPackage() diff --git a/gnovm/tests/imports.go b/gnovm/tests/imports.go index 30f410fa8d5..2c0b8a25e89 100644 --- a/gnovm/tests/imports.go +++ b/gnovm/tests/imports.go @@ -222,23 +222,23 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri return pkg, pkg.NewPackage() case "time": pkg := gno.NewPackageNode("time", pkgPath, nil) - pkg.DefineGoNativeValue("Millisecond", time.Millisecond) - pkg.DefineGoNativeValue("Second", time.Second) - pkg.DefineGoNativeValue("Minute", time.Minute) - pkg.DefineGoNativeValue("Hour", time.Hour) - pkg.DefineGoNativeValue("Date", time.Date) - pkg.DefineGoNativeValue("Now", func() time.Time { return time.Unix(0, 0).UTC() }) // deterministic - pkg.DefineGoNativeValue("January", time.January) - pkg.DefineGoNativeValue("February", time.February) - pkg.DefineGoNativeValue("March", time.March) - pkg.DefineGoNativeValue("April", time.April) - pkg.DefineGoNativeValue("May", time.May) - pkg.DefineGoNativeValue("June", time.June) - pkg.DefineGoNativeValue("July", time.July) - pkg.DefineGoNativeValue("August", time.August) - pkg.DefineGoNativeValue("September", time.September) - pkg.DefineGoNativeValue("November", time.November) - pkg.DefineGoNativeValue("December", time.December) + pkg.DefineGoNativeConstValue("Millisecond", time.Millisecond) + pkg.DefineGoNativeConstValue("Second", time.Second) + pkg.DefineGoNativeConstValue("Minute", time.Minute) + pkg.DefineGoNativeConstValue("Hour", time.Hour) + pkg.DefineGoNativeConstValue("Date", time.Date) + pkg.DefineGoNativeConstValue("Now", func() time.Time { return time.Unix(0, 0).UTC() }) // deterministic + pkg.DefineGoNativeConstValue("January", time.January) + pkg.DefineGoNativeConstValue("February", time.February) + pkg.DefineGoNativeConstValue("March", time.March) + pkg.DefineGoNativeConstValue("April", time.April) + pkg.DefineGoNativeConstValue("May", time.May) + pkg.DefineGoNativeConstValue("June", time.June) + pkg.DefineGoNativeConstValue("July", time.July) + pkg.DefineGoNativeConstValue("August", time.August) + pkg.DefineGoNativeConstValue("September", time.September) + pkg.DefineGoNativeConstValue("November", time.November) + pkg.DefineGoNativeConstValue("December", time.December) pkg.DefineGoNativeValue("UTC", time.UTC) pkg.DefineGoNativeValue("Unix", time.Unix) pkg.DefineGoNativeType(reflect.TypeOf(time.Time{})) @@ -263,21 +263,20 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri pkg := gno.NewPackageNode("math", pkgPath, nil) pkg.DefineGoNativeValue("Abs", math.Abs) pkg.DefineGoNativeValue("Cos", math.Cos) - pkg.DefineGoNativeValue("Pi", math.Pi) + pkg.DefineGoNativeConstValue("Pi", math.Pi) pkg.DefineGoNativeValue("Float64bits", math.Float64bits) - pkg.DefineGoNativeValue("Pi", math.Pi) - pkg.DefineGoNativeValue("MaxFloat32", math.MaxFloat32) - pkg.DefineGoNativeValue("MaxFloat64", math.MaxFloat64) - pkg.DefineGoNativeValue("MaxUint32", uint32(math.MaxUint32)) - pkg.DefineGoNativeValue("MaxUint64", uint64(math.MaxUint64)) - pkg.DefineGoNativeValue("MinInt8", math.MinInt8) - pkg.DefineGoNativeValue("MinInt16", math.MinInt16) - pkg.DefineGoNativeValue("MinInt32", math.MinInt32) - pkg.DefineGoNativeValue("MinInt64", int64(math.MinInt64)) - pkg.DefineGoNativeValue("MaxInt8", math.MaxInt8) - pkg.DefineGoNativeValue("MaxInt16", math.MaxInt16) - pkg.DefineGoNativeValue("MaxInt32", math.MaxInt32) - pkg.DefineGoNativeValue("MaxInt64", int64(math.MaxInt64)) + pkg.DefineGoNativeConstValue("MaxFloat32", math.MaxFloat32) + pkg.DefineGoNativeConstValue("MaxFloat64", math.MaxFloat64) + pkg.DefineGoNativeConstValue("MaxUint32", uint32(math.MaxUint32)) + pkg.DefineGoNativeConstValue("MaxUint64", uint64(math.MaxUint64)) + pkg.DefineGoNativeConstValue("MinInt8", math.MinInt8) + pkg.DefineGoNativeConstValue("MinInt16", math.MinInt16) + pkg.DefineGoNativeConstValue("MinInt32", math.MinInt32) + pkg.DefineGoNativeConstValue("MinInt64", int64(math.MinInt64)) + pkg.DefineGoNativeConstValue("MaxInt8", math.MaxInt8) + pkg.DefineGoNativeConstValue("MaxInt16", math.MaxInt16) + pkg.DefineGoNativeConstValue("MaxInt32", math.MaxInt32) + pkg.DefineGoNativeConstValue("MaxInt64", int64(math.MaxInt64)) return pkg, pkg.NewPackage() case "math/rand": // XXX only expose for tests. @@ -312,13 +311,13 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri return pkg, pkg.NewPackage() case "compress/flate": pkg := gno.NewPackageNode("flate", pkgPath, nil) - pkg.DefineGoNativeValue("BestSpeed", flate.BestSpeed) + pkg.DefineGoNativeConstValue("BestSpeed", flate.BestSpeed) return pkg, pkg.NewPackage() case "compress/gzip": pkg := gno.NewPackageNode("gzip", pkgPath, nil) pkg.DefineGoNativeType(reflect.TypeOf(gzip.Writer{})) - pkg.DefineGoNativeValue("BestCompression", gzip.BestCompression) - pkg.DefineGoNativeValue("BestSpeed", gzip.BestSpeed) + pkg.DefineGoNativeConstValue("BestCompression", gzip.BestCompression) + pkg.DefineGoNativeConstValue("BestSpeed", gzip.BestSpeed) return pkg, pkg.NewPackage() case "context": pkg := gno.NewPackageNode("context", pkgPath, nil)