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

Calling a realm that uses std.PrevRealm() has different behavior in var and init blocks #941

Closed
n0izn0iz opened this issue Jul 1, 2023 · 2 comments
Labels
🐞 bug Something isn't working

Comments

@n0izn0iz
Copy link
Contributor

n0izn0iz commented Jul 1, 2023

Calling a realm that uses std.PrevRealm() has different behavior in var and init blocks

Description

Calling a realm that uses std.PrevRealm has not the same behavior if called in a realm's var block or in the init block

Your environment

  • macos: 13.3.1
  • go: version go1.20.4 darwin/arm64
  • gno: master at d684985

Steps to reproduce

  • Deploy this realm
package var_prev_realm

import (
	"std"

	"gno.land/r/demo/tests"
)

var (
	varRealm  = tests.GetPrevRealm()
	initRealm std.Realm
)

func init() {
	initRealm = tests.GetPrevRealm()
}

func Render(path string) string {
	s := ""
	s += "Var block realm:\n\nPackage path: " + varRealm.PkgPath() + "\n\nAddress: " + varRealm.Addr().String() + "\n\n"
	s += "Init block realm:\n\nPackage path: " + initRealm.PkgPath() + "\n\nAddress: " + initRealm.Addr().String() + "\n\n"
	return s
}
  • Inspect render

Expected behaviour

tests.GetPrevRealm() returns the same whether called in var or init block

Actual behaviour

Screenshot 2023-07-01 at 15 59 27

Additional info

Not sure if this is a bug or intended behavior, would love to know the rationale if this is intended

@leohhhn
Copy link
Contributor

leohhhn commented Sep 12, 2024

Hey @n0izn0iz not sure this is relevant anymore.
With this code:

package mypkg

import (
	"std"

	"gno.land/p/demo/ufmt"
	"gno.land/r/demo/tests"
)

var (
	toplvl  = std.PrevRealm().Addr()
	initlvl std.Address

	toplvltest  = tests.GetPrevRealm().Addr()
	initlvltest std.Address
)

func init() {
	initlvl = std.PrevRealm().Addr()
	initlvltest = tests.GetPrevRealm().Addr()
}

func Render(_ string) string {
	out := ufmt.Sprintf("### prevrealm in this realm (=deployer):\n\ntoplvl: %s\n\n initlvl: %s\n\n", toplvl, initlvl)
	out += ufmt.Sprintf("### prevrealm in tests (=current realm addr):\n\ntoplvl using test: %s\n\n initlvl using test: %s\n\n", toplvltest, initlvltest)

	return out
}

We get the following output:
Screenshot 2024-09-12 at 14 13 45

Above is tested locally, and here you can see it deployed: https://gno.land/r/leon/issues/mypkg

Possibly this was fixed somewhere in between. Can you see if your issue is still relevant, and close it if it's not?

@n0izn0iz
Copy link
Contributor Author

looks good indeed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working
Projects
Development

No branches or pull requests

4 participants