Skip to content

Commit

Permalink
replace OpCall with OpPreCall
Browse files Browse the repository at this point in the history
also add comments and small code reorg
  • Loading branch information
tbruyelle committed Mar 24, 2023
1 parent 46d1415 commit d59c320
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
30 changes: 14 additions & 16 deletions stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,34 +287,32 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
// 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()

ctx := m.Context.(ExecContext)
ctx.OrigCaller = ctx.OrigPkgAddr

backupContext := m.Context.(ExecContext)

// Push original context first, so it will be reset after the function
// call.
m.PushOp(gno.OpSetContext)
m.PushValue(gno.TypedValue{
T: &gno.InterfaceType{},
V: gno.ContextValue{
Context: backupContext,
Context: m.Context,
},
})
m.PushOp(gno.OpSetContext)

cx := gno.Call(gno.FuncT(nil, nil))
m.PushValue(gno.TypedValue{})
m.PushFrameCall(cx, fn, gno.TypedValue{})
m.PushOp(gno.OpCall)
// Push a function call, to execute the function in parameter.
m.PushOp(gno.OpPrecall)
m.PushExpr(gno.Call(gno.FuncT(nil, nil)))
tv := m.LastBlock().GetParams1().TV
m.PushValue(*tv)

// Push an alternate context with OrigCaller=OrigPkgAddr, this will
// affect the function call above
m.PushOp(gno.OpSetContext)
ctx := m.Context.(ExecContext)
ctx.OrigCaller = ctx.OrigPkgAddr
m.PushValue(gno.TypedValue{
T: &gno.InterfaceType{},
V: gno.ContextValue{
Context: ctx,
},
})
m.PushOp(gno.OpSetContext)
},
)
pn.DefineNative("GetCallerAt",
Expand Down
12 changes: 7 additions & 5 deletions tests/files/zrealm_std6.gno
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"std"
)

func main() {
println(std.GetOrigCaller())
std.ExecAsPkg(func() {
println(std.GetOrigCaller())
})
func printOrigCaller() {
println(std.GetOrigCaller())
}

func main() {
printOrigCaller()
std.ExecAsPkg(printOrigCaller)
printOrigCaller()
}

// Output:
// g1fahslvxakc058u0wa98ytfc9fd6trhe5qfh9ld
// g157y5v3k529jyzhjjz4fn49tzzhf4gess6v39xg
Expand Down

0 comments on commit d59c320

Please sign in to comment.