Skip to content

Commit

Permalink
feat: std.CurrentRealm
Browse files Browse the repository at this point in the history
  • Loading branch information
albttx committed Jun 16, 2023
1 parent b6dc190 commit 2937a4c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 4 deletions.
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

0 comments on commit 2937a4c

Please sign in to comment.