diff --git a/examples/gno.land/r/demo/mirror/doc.gno b/examples/gno.land/r/demo/mirror/doc.gno new file mode 100644 index 00000000000..40fdbd5bc26 --- /dev/null +++ b/examples/gno.land/r/demo/mirror/doc.gno @@ -0,0 +1,3 @@ +// Package mirror demonstrates that users can pass realm functions +// as arguments to other realms. +package mirror diff --git a/examples/gno.land/r/demo/mirror/gno.mod b/examples/gno.land/r/demo/mirror/gno.mod new file mode 100644 index 00000000000..2bf27fd6916 --- /dev/null +++ b/examples/gno.land/r/demo/mirror/gno.mod @@ -0,0 +1,3 @@ +module gno.land/r/demo/mirror + +require gno.land/p/demo/avl v0.0.0-latest diff --git a/examples/gno.land/r/demo/mirror/mirror.gno b/examples/gno.land/r/demo/mirror/mirror.gno new file mode 100644 index 00000000000..770fddc4fda --- /dev/null +++ b/examples/gno.land/r/demo/mirror/mirror.gno @@ -0,0 +1,33 @@ +package mirror + +import ( + "gno.land/p/demo/avl" +) + +var store avl.Tree + +func Register(pkgpath string, rndr func(string) string) { + if store.Has(pkgpath) { + return + } + + if rndr == nil { + return + } + + store.Set(pkgpath, rndr) +} + +func Render(path string) string { + if raw, ok := store.Get(path); ok { + return raw.(func(string) string)("") + } + + if store.Size() == 0 { + return "None are fair." + } + + return "Mirror, mirror on the wall, which realm's the fairest of them all?" +} + +// Credits to @jeronimoalbi diff --git a/examples/gno.land/r/leon/home/gno.mod b/examples/gno.land/r/leon/home/gno.mod index 4649cf4abe6..7288c176050 100644 --- a/examples/gno.land/r/leon/home/gno.mod +++ b/examples/gno.land/r/leon/home/gno.mod @@ -5,5 +5,6 @@ require ( gno.land/r/demo/art/gnoface v0.0.0-latest gno.land/r/demo/art/millipede v0.0.0-latest gno.land/r/demo/hof v0.0.0-latest + gno.land/r/demo/mirror v0.0.0-latest gno.land/r/leon/config v0.0.0-latest ) diff --git a/examples/gno.land/r/leon/home/home.gno b/examples/gno.land/r/leon/home/home.gno index aea8b43e9cd..632b3f14a62 100644 --- a/examples/gno.land/r/leon/home/home.gno +++ b/examples/gno.land/r/leon/home/home.gno @@ -9,6 +9,7 @@ import ( "gno.land/r/demo/art/gnoface" "gno.land/r/demo/art/millipede" "gno.land/r/demo/hof" + "gno.land/r/demo/mirror" "gno.land/r/leon/config" ) @@ -34,6 +35,7 @@ TODO import r/gh } hof.Register() + mirror.Register(std.CurrentRealm().PkgPath(), Render) } func UpdatePFP(url, caption string) {