Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: std.CurrentRealm #893

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/gno.land/p/demo/tests/subtests/subtests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"std"
)

func CurrentRealmPath() string {
return std.CurrentRealmPath()
func GetCurrentRealm() std.Realm {
return std.CurrentRealm()
}

func GetPrevRealm() std.Realm {
Expand Down
4 changes: 2 additions & 2 deletions examples/gno.land/r/demo/tests/subtests/subtests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"std"
)

func CurrentRealmPath() string {
return std.CurrentRealmPath()
func GetCurrentRealm() std.Realm {
return std.CurrentRealm()
}

func GetPrevRealm() std.Realm {
Expand Down
38 changes: 38 additions & 0 deletions gnovm/stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,44 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
m.PushValue(res0)
},
)
pn.DefineNative("CurrentRealm",
gno.Flds( // params
),
gno.Flds( // results
"", "Realm",
),
func(m *gno.Machine) {
var (
ctx = m.Context.(ExecContext)
// Default lastCaller is OrigCaller, the signer of the tx
lastCaller = ctx.OrigCaller
lastPkgPath = ""
)

for i := m.NumFrames() - 1; i > 0; i-- {
fr := m.Frames[i]
if fr.LastPackage != nil && fr.LastPackage.IsRealm() {
lastCaller = fr.LastPackage.GetPkgAddr().Bech32()
lastPkgPath = fr.LastPackage.PkgPath
break
}
}

// Return the result
res0 := gno.Go2GnoValue(
m.Alloc,
m.Store,
reflect.ValueOf(Realm{
addr: lastCaller,
pkgPath: lastPkgPath,
}),
)

realmT := store.GetType(gno.DeclaredTypeID("std", "Realm"))
res0.T = realmT
m.PushValue(res0)
},
)
pn.DefineNative("PrevRealm",
gno.Flds( // params
),
Expand Down
8 changes: 8 additions & 0 deletions gnovm/stdlibs/stdshim/stdshim.gno
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func GetOrigSend() Coins {
return Coins{}
}

func CurrentRealm() Realm {
panic(shimWarn)
return Realm{
addr: Address(""),
pkgPath: "",
}
}

func PrevRealm() Realm {
panic(shimWarn)
return Realm{
Expand Down
37 changes: 37 additions & 0 deletions gnovm/tests/files/zrealm_crossrealm12.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// PKGPATH: gno.land/r/crossrealm_test
package crossrealm_test

import (
"std"
"fmt"

psubtests "gno.land/p/demo/tests/subtests"
rsubtests "gno.land/r/demo/tests/subtests"
)

func main() {
tests := []struct{
fn func() std.Realm
}{
{ std.CurrentRealm },
{ psubtests.GetCurrentRealm },
{ rsubtests.GetCurrentRealm },
}

for _, test := range tests {
r := test.fn()

if std.DerivePkgAddr(r.PkgPath()) != r.Addr() {
panic(fmt.Sprintf("ERROR: expected: %v, got: %v",
std.DerivePkgAddr(r.PkgPath()), r.Addr(),
))
}

println(r.PkgPath(), r.Addr())
}
}

// Output:
// gno.land/r/crossrealm_test g1vla5mffzum6060t99u4xhm8mnhgxr0sz4k574p
// gno.land/r/crossrealm_test g1vla5mffzum6060t99u4xhm8mnhgxr0sz4k574p
// gno.land/r/demo/tests/subtests g13g48xnr7lzxsrvny0uf6lhx0cfaxy4n0n5geuf