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

exp: add std.ExecAsPkg #335

Closed
wants to merge 2 commits into from
Closed
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
28 changes: 28 additions & 0 deletions stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,34 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
m.PushValue(res0)
},
)
pn.DefineNative("ExecAsPkg",
// TODO: rename UnsafeExecAsPkg?
gno.Flds( // params
"fn", gno.FuncT(nil, nil),
),
gno.Flds( // results
),
func(m *gno.Machine) {
// TODO: limit stack size: 1? 32?.
// TODO: limit usage in goroutines?
// TODO: build a new sub-context instead of patching the root one.
// to support multiple concurrent contexts and advanced flows.
arg0 := m.LastBlock().GetParams1().TV
fn := arg0.GetFunc()
println("#####################", arg0, fn.GetType(m.Store).String())
//gno.Call(fn, "nil")
//m.Eval(gno.Call(fn))
// m.Eval(gno.Call(gno.FuncT(nil, nil), fn))
println("@@@@@@@@@@")
//println(fn)

/*
ctx := m.Context.(stdlibs.ExecContext)
ctx.OrigCaller = crypto.Bech32Address(addr)
m.Context = ctx
*/
},
)
pn.DefineNative("GetCallerAt",
gno.Flds( // params
"n", "int",
Expand Down
4 changes: 4 additions & 0 deletions stdlibs/stdshim/stdshim.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ func DecodeBech32(addr Address) (prefix string, bytes []byte, ok bool) {
panic(shimWarn)
return "", nil, false
}

func ExecAsPkg(fn func()) {
panic(shimWarn)
}
16 changes: 10 additions & 6 deletions tests/files2/zrealm_std3.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import (
"std"
)

func foo() {
println("foo", std.CurrentRealmPath())
func displayOrigCaller() {
println(std.GetOrigCaller())
}

func main() {
println("main", std.CurrentRealmPath())
foo()
displayOrigCaller()
std.ExecAsPkg(func() {
displayOrigCaller()
})
displayOrigCaller()
}

// Output:
// main gno.land/r/std_test
// foo gno.land/r/std_test
// g157y5v3k529jyzhjjz4fn49tzzhf4gess6v39xg
// TODO: should be the address package
// g157y5v3k529jyzhjjz4fn49tzzhf4gess6v39xg