Skip to content

Commit

Permalink
fix(gonative): add constant information when we define a go native value
Browse files Browse the repository at this point in the history
  • Loading branch information
omarsy committed Sep 25, 2024
1 parent 9897b66 commit 1ae5885
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 33 deletions.
15 changes: 12 additions & 3 deletions gnovm/pkg/gnolang/gonative.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Check warning on line 1271 in gnovm/pkg/gnolang/gonative.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/gonative.go#L1271

Added line #L1271 was not covered by tests
}
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)
}

// ----------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions gnovm/pkg/gnolang/preprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down
51 changes: 25 additions & 26 deletions gnovm/tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,15 @@ 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("November", time.November)
pkg.DefineGoNativeValue("UTC", time.UTC)
pkg.DefineGoNativeValue("Unix", time.Unix)
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("November", time.November)
pkg.DefineGoNativeConstValue("UTC", time.UTC)
pkg.DefineGoNativeConstValue("Unix", time.Unix)
pkg.DefineGoNativeType(reflect.TypeOf(time.Time{}))
pkg.DefineGoNativeType(reflect.TypeOf(time.Duration(0)))
pkg.DefineGoNativeType(reflect.TypeOf(time.Month(0)))
Expand All @@ -252,21 +252,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.
Expand Down Expand Up @@ -301,13 +300,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)
Expand Down

0 comments on commit 1ae5885

Please sign in to comment.