From 3faaf9691f4f6b698e1dd7267d71fa01ef02da02 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 28 Jul 2023 13:38:59 +0900 Subject: [PATCH] fix: frame error --- examples/gno.land/r/demo/nested/r1/r1.gno | 17 +++++++ examples/gno.land/r/demo/nested/r2/r2.gno | 24 ++++++++++ examples/gno.land/r/demo/nested/r3/r3.gno | 17 +++++++ examples/gno.land/r/demo/nested/r4/r4.gno | 23 ++++++++++ examples/gno.land/r/demo/nested/r5/r5.gno | 14 ++++++ gnovm/cmd/gno/test.go | 8 +--- gnovm/pkg/gnolang/realm.go | 11 +---- gnovm/pkg/gnolang/values.go | 1 - gnovm/stdlibs/stdlibs.go | 11 ----- gnovm/tests/files/zrealm_crossrealm13.gno | 34 ++++++++++++++ gnovm/tests/imports.go | 56 ++++++++++++++++++----- 11 files changed, 177 insertions(+), 39 deletions(-) create mode 100644 examples/gno.land/r/demo/nested/r1/r1.gno create mode 100644 examples/gno.land/r/demo/nested/r2/r2.gno create mode 100644 examples/gno.land/r/demo/nested/r3/r3.gno create mode 100644 examples/gno.land/r/demo/nested/r4/r4.gno create mode 100644 examples/gno.land/r/demo/nested/r5/r5.gno create mode 100644 gnovm/tests/files/zrealm_crossrealm13.gno diff --git a/examples/gno.land/r/demo/nested/r1/r1.gno b/examples/gno.land/r/demo/nested/r1/r1.gno new file mode 100644 index 00000000000..4674dafd2e7 --- /dev/null +++ b/examples/gno.land/r/demo/nested/r1/r1.gno @@ -0,0 +1,17 @@ +package r1 + +import ( + r2 "gno.land/r/demo/nested/r2" + "std" +) + +func Stack() { + println("r1.GetOrigCaller():", std.GetOrigCaller()) + + pr := std.PrevRealm() + println("r1.PrevRealm.Addr():", pr.Addr()) + println("r1.PrevRealm.PkgPath():", pr.PkgPath()) + println() + + r2.Stack() +} diff --git a/examples/gno.land/r/demo/nested/r2/r2.gno b/examples/gno.land/r/demo/nested/r2/r2.gno new file mode 100644 index 00000000000..d535f4c91da --- /dev/null +++ b/examples/gno.land/r/demo/nested/r2/r2.gno @@ -0,0 +1,24 @@ +package r2 + +import ( + r3 "gno.land/r/demo/nested/r3" + "std" +) + +func Stack() { + orig := std.GetOrigCaller() // g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm + std.TestSetOrigCaller("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") // test1 + + println("r2.GetOrigCaller():", std.GetOrigCaller()) + + pr := std.PrevRealm() + + println("r2.PrevRealm.Addr():", pr.Addr()) + println("r2.PrevRealm.PkgPath():", pr.PkgPath()) + println() + + // recover + std.TestSetOrigCaller(orig) + + r3.Stack() +} diff --git a/examples/gno.land/r/demo/nested/r3/r3.gno b/examples/gno.land/r/demo/nested/r3/r3.gno new file mode 100644 index 00000000000..d801e31b40e --- /dev/null +++ b/examples/gno.land/r/demo/nested/r3/r3.gno @@ -0,0 +1,17 @@ +package r3 + +import ( + r4 "gno.land/r/demo/nested/r4" + "std" +) + +func Stack() { + println("r3.GetOrigCaller():", std.GetOrigCaller()) + + pr := std.PrevRealm() + println("r3.PrevRealm.Addr():", pr.Addr()) + println("r3.PrevRealm.PkgPath():", pr.PkgPath()) + println() + + r4.Stack() +} diff --git a/examples/gno.land/r/demo/nested/r4/r4.gno b/examples/gno.land/r/demo/nested/r4/r4.gno new file mode 100644 index 00000000000..7144c1a5f5f --- /dev/null +++ b/examples/gno.land/r/demo/nested/r4/r4.gno @@ -0,0 +1,23 @@ +package r4 + +import ( + r5 "gno.land/r/demo/nested/r5" + "std" +) + +func Stack() { + orig := std.PrevRealm().PkgPath() + std.TestSetPrevAddr("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") + + println("r4.GetOrigCaller():", std.GetOrigCaller()) + + pr := std.PrevRealm() + println("r4.PrevRealm.Addr():", pr.Addr()) + println("r4.PrevRealm.PkgPath():", pr.PkgPath()) + println() + + // recover + std.TestSetPrevRealm("gno.land/r/demo/nested/r4") + + r5.Stack() +} diff --git a/examples/gno.land/r/demo/nested/r5/r5.gno b/examples/gno.land/r/demo/nested/r5/r5.gno new file mode 100644 index 00000000000..9eca5dcec9d --- /dev/null +++ b/examples/gno.land/r/demo/nested/r5/r5.gno @@ -0,0 +1,14 @@ +package r5 + +import ( + "std" +) + +func Stack() { + println("r5.GetOrigCaller():", std.GetOrigCaller()) + + pr := std.PrevRealm() + println("r5.PrevRealm.Addr():", pr.Addr()) + println("r5.PrevRealm.PkgPath():", pr.PkgPath()) + println() +} diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index bd2d9a7f646..f5e180e694a 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -267,11 +267,6 @@ func gnoTestPkg( // testing with *_test.gno if len(unittestFiles) > 0 { - // pkgPath from command line - println("ORIG PKGPATH HERE:", pkgPath) - pkgPath = strings.Replace(pkgPath, "./", "", 1) - println("NEW PKGPATH HERE:", pkgPath) - memPkg := gno.ReadMemPackage(pkgPath, pkgPath) // tfiles, ifiles := gno.ParseMemPackageTests(memPkg) @@ -279,8 +274,7 @@ func gnoTestPkg( // run test files in pkg { - m := tests.TestMachine(testStore, stdout, pkgPath) - // m := tests.TestMachine(testStore, stdout, "main") + m := tests.TestMachine(testStore, stdout, "main") if printRuntimeMetrics { // from tm2/pkg/sdk/vm/keeper.go // XXX: make maxAllocTx configurable. diff --git a/gnovm/pkg/gnolang/realm.go b/gnovm/pkg/gnolang/realm.go index 971944ca6b7..40f770c7720 100644 --- a/gnovm/pkg/gnolang/realm.go +++ b/gnovm/pkg/gnolang/realm.go @@ -1053,7 +1053,6 @@ func copyTypeWithRefs(typ Type) Type { // Also checks for integrity of immediate children -- they must already be // persistent (real), and not dirty, or else this function panics. func copyValueWithRefs(parent Object, val Value) Value { - // XXX r3v4 switch cv := val.(type) { case nil: return nil @@ -1424,9 +1423,7 @@ func toRefValue(parent Object, val Value) RefValue { PkgPath: pv.PkgPath, } } else if !oo.GetIsReal() { - // panic("unexpected unreal object") - /// XXX r3v4 - // fmt.Println(fmt.Sprintf("%+v", oo)) + panic("unexpected unreal object") } else if oo.GetIsDirty() { // This can happen with some circular // references. @@ -1505,11 +1502,7 @@ func isUnsaved(oo Object) bool { func IsRealmPath(pkgPath string) bool { // TODO: make it more distinct to distinguish from normal paths. - // if strings.HasPrefix(pkgPath, "gno.land/r/") { - // if strings.Contains(pkgPath, "gno.land/r/") { // XXX r3v4 PANIC - // if strings.Contains(pkgPath, "gno.land/r/") || strings.Contains(pkgPath, ".gno.land/r/") { // XXX r3v4 PANIC - pkgPath = strings.Replace(pkgPath, "./gno.land/r/", "gno.land/r/", 1) - if strings.Contains(pkgPath, "gno.land/r/") || strings.Contains(pkgPath, ".gno.land/r/") { // XXX r3v4 + if strings.HasPrefix(pkgPath, "gno.land/r/") { return true } else { return false diff --git a/gnovm/pkg/gnolang/values.go b/gnovm/pkg/gnolang/values.go index 1ce472922d1..3de74ac0130 100644 --- a/gnovm/pkg/gnolang/values.go +++ b/gnovm/pkg/gnolang/values.go @@ -792,7 +792,6 @@ type PackageValue struct { } func (pv *PackageValue) IsRealm() bool { - // println("IsRealmPath", pv.PkgPath, IsRealmPath(pv.PkgPath)) return IsRealmPath(pv.PkgPath) } diff --git a/gnovm/stdlibs/stdlibs.go b/gnovm/stdlibs/stdlibs.go index c23a13243c5..fb230a0cf86 100644 --- a/gnovm/stdlibs/stdlibs.go +++ b/gnovm/stdlibs/stdlibs.go @@ -317,38 +317,27 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) { for i := m.NumFrames() - 1; i > 0; i-- { fr := m.Frames[i] - // println("#01 i:", i) - // println("#02 fr.LastPackage:", fr.LastPackage) - // XX println("#03 fr.LastPackage.IsRealm():", fr.LastPackage.IsRealm()) if fr.LastPackage == nil || !fr.LastPackage.IsRealm() { // Ignore non-realm frame - // println("#04 continue") continue } pkgPath := fr.LastPackage.PkgPath - // println("#05 pkgPath:", pkgPath) // The first realm we encounter will be the one calling // this function; to get the calling realm determine the first frame // where fr.LastPackage changes. if lastPkgPath == "" { lastPkgPath = pkgPath - // println("#07 lastPkgPath:", lastPkgPath) } else if lastPkgPath == pkgPath { - // println("#08 continue") continue } else { lastCaller = fr.LastPackage.GetPkgAddr().Bech32() lastPkgPath = pkgPath - // println("#09 lastCaller:", lastCaller) - // println("#10 lastPkgPath:", lastPkgPath) break } } // Empty the pkgPath if we return a user if ctx.OrigCaller == lastCaller { - // println("#11 ctx.OrigCalelr:", ctx.OrigCaller) - // println("#12 lastCaller:", lastCaller) lastPkgPath = "" } diff --git a/gnovm/tests/files/zrealm_crossrealm13.gno b/gnovm/tests/files/zrealm_crossrealm13.gno new file mode 100644 index 00000000000..d9ce65f3afb --- /dev/null +++ b/gnovm/tests/files/zrealm_crossrealm13.gno @@ -0,0 +1,34 @@ +// PKGPATH: gno.land/r/prevrealm_test +package prevrealm_test + +import ( + "fmt" + "std" + + r1 "gno.land/r/demo/nested/r1" +) + +func main() { + r1.Stack() +} + +// Output: +// r1.GetOrigCaller(): g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm +// r1.PrevRealm.Addr(): g1ngy6g5vkxnj75zjuf640npzvas4suja7gzg65l +// r1.PrevRealm.PkgPath(): gno.land/r/prevrealm_test +// +// r2.GetOrigCaller(): g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +// r2.PrevRealm.Addr(): g176s5yqvc2rlzjv0uurdvffjsddyec52umtvvyz +// r2.PrevRealm.PkgPath(): gno.land/r/demo/nested/r1 +// +// r3.GetOrigCaller(): g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm +// r3.PrevRealm.Addr(): g1h4kjvsxlp2w8km6sdggc86r7qd4wxgjz4herwp +// r3.PrevRealm.PkgPath(): gno.land/r/demo/nested/r2 +// +// r4.GetOrigCaller(): g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +// r4.PrevRealm.Addr(): g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +// r4.PrevRealm.PkgPath(): +// +// r5.GetOrigCaller(): g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5 +// r5.PrevRealm.Addr(): g1tnuhr8an4vufw8j800k2jdwsjgfekhkhcrc9wf +// r5.PrevRealm.PkgPath(): gno.land/r/demo/nested/r4 diff --git a/gnovm/tests/imports.go b/gnovm/tests/imports.go index f7b97721afb..87f524a60e9 100644 --- a/gnovm/tests/imports.go +++ b/gnovm/tests/imports.go @@ -577,24 +577,58 @@ func testPackageInjector(store gno.Store, pn *gno.PackageNode) { arg0 := m.LastBlock().GetParams1().TV realm := arg0.GetString() + // set pkgPath + for i := m.NumFrames() - 1; i > 0; i-- { + fr := m.Frames[i] + + if i != 2 { + fr.LastPackage = &gno.PackageValue{ + PkgPath: realm, + } + m.Frames[i] = fr + } + } + }, + ) + pn.DefineNative("TestSetPrevAddr", + gno.Flds( // params + "", "Address", + ), + gno.Flds( // results + ), + func(m *gno.Machine) { + arg0 := m.LastBlock().GetParams1().TV + addr := arg0.GetString() + + // overwrite context // TestSetOrigCaller + ctx := m.Context.(stdlibs.ExecContext) + ctx.OrigCaller = crypto.Bech32Address(addr) + m.Context = ctx + // Set PkgPath + lastPkgPath := "" for i := m.NumFrames() - 1; i > 0; i-- { fr := m.Frames[i] if fr.LastPackage == nil || !fr.LastPackage.IsRealm() { - if i != 1 { - fr.LastPackage = &gno.PackageValue{ - // PkgPath: "gno.land/r/this_is_dummy_pkg_path", // o - PkgPath: "", - } + // Ignore non-realm frame + continue + } - m.Frames[i] = fr - } else { - fr.LastPackage = &gno.PackageValue{ - PkgPath: realm, - } + pkgPath := fr.LastPackage.PkgPath + if lastPkgPath == "" { + lastPkgPath = pkgPath + } - m.Frames[i] = fr + if i != 2 { + fr.LastPackage = &gno.PackageValue{ + PkgPath: "", + } + m.Frames[i] = fr + } else { + fr.LastPackage = &gno.PackageValue{ + PkgPath: lastPkgPath, } + m.Frames[i] = fr } } },