Skip to content

Commit

Permalink
fix: frame error
Browse files Browse the repository at this point in the history
  • Loading branch information
r3v4s committed Jul 28, 2023
1 parent ced9764 commit 3faaf96
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 39 deletions.
17 changes: 17 additions & 0 deletions examples/gno.land/r/demo/nested/r1/r1.gno
Original file line number Diff line number Diff line change
@@ -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()
}
24 changes: 24 additions & 0 deletions examples/gno.land/r/demo/nested/r2/r2.gno
Original file line number Diff line number Diff line change
@@ -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()
}
17 changes: 17 additions & 0 deletions examples/gno.land/r/demo/nested/r3/r3.gno
Original file line number Diff line number Diff line change
@@ -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()
}
23 changes: 23 additions & 0 deletions examples/gno.land/r/demo/nested/r4/r4.gno
Original file line number Diff line number Diff line change
@@ -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()
}
14 changes: 14 additions & 0 deletions examples/gno.land/r/demo/nested/r5/r5.gno
Original file line number Diff line number Diff line change
@@ -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()
}
8 changes: 1 addition & 7 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,20 +267,14 @@ 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)
tfiles, ifiles := parseMemPackageTests(memPkg)

// 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.
Expand Down
11 changes: 2 additions & 9 deletions gnovm/pkg/gnolang/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,6 @@ type PackageValue struct {
}

func (pv *PackageValue) IsRealm() bool {
// println("IsRealmPath", pv.PkgPath, IsRealmPath(pv.PkgPath))
return IsRealmPath(pv.PkgPath)
}

Expand Down
11 changes: 0 additions & 11 deletions gnovm/stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
}

Expand Down
34 changes: 34 additions & 0 deletions gnovm/tests/files/zrealm_crossrealm13.gno
Original file line number Diff line number Diff line change
@@ -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
56 changes: 45 additions & 11 deletions gnovm/tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
},
Expand Down

0 comments on commit 3faaf96

Please sign in to comment.