Skip to content

Commit

Permalink
feat: exec-as-pkg
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Bruyelle <thomas.bruyelle@gmail.com>
  • Loading branch information
albttx and tbruyelle committed Mar 24, 2023
1 parent 41d04a5 commit a180ce1
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 12 deletions.
7 changes: 7 additions & 0 deletions pkgs/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,6 +804,9 @@ const (
OpValueDecl Op = 0x90 // var/const ...
OpTypeDecl Op = 0x91 // type ...

/* ContextOp */
OpSetContext Op = 0xA1

/* Loop (sticky) operators (>= 0xD0) */
OpSticky Op = 0xD0 // not a real op.
OpBody Op = 0xD1 // if/block/switch/select.
Expand All @@ -813,6 +816,7 @@ const (
OpRangeIterMap Op = 0xD5
OpRangeIterArrayPtr Op = 0xD6
OpReturnCallDefers Op = 0xD7 // TODO rename?

)

//----------------------------------------
Expand Down Expand Up @@ -1275,6 +1279,9 @@ func (m *Machine) Run() {
case OpReturnCallDefers:
m.incrCPU(OpCPUReturnCallDefers)
m.doOpReturnCallDefers()
case OpSetContext:
m.incrCPU(1)
m.doOpSetContext()
default:
panic(fmt.Sprintf("unexpected opcode %s", op.String()))
}
Expand Down
5 changes: 5 additions & 0 deletions pkgs/gnolang/op_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,8 @@ func (m *Machine) doOpPanic2() {
m.PushOp(OpReturnCallDefers) // XXX rename, not return?
}
}

func (m *Machine) doOpSetContext() {
cv := m.PopValue().V.(ContextValue)
m.Context = cv.Context
}
13 changes: 13 additions & 0 deletions pkgs/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (*PackageValue) assertValue() {}
func (*NativeValue) assertValue() {}
func (*Block) assertValue() {}
func (RefValue) assertValue() {}
func (ContextValue) assertValue() {}

const (
nilStr = "nil"
Expand All @@ -62,6 +63,7 @@ var (
_ Value = &NativeValue{}
_ Value = &Block{}
_ Value = RefValue{}
_ Value = ContextValue{}
)

// ----------------------------------------
Expand Down Expand Up @@ -2184,6 +2186,17 @@ func (tv *TypedValue) GetSlice2(alloc *Allocator, low, high, max int) TypedValue
}
}

// ----------------------------------------
// ContextValue

type ContextValue struct {
Context interface{}
}

func (ctx ContextValue) String() string {
return fmt.Sprintf("%v", ctx.Context)
}

// ----------------------------------------
// Block
//
Expand Down
36 changes: 25 additions & 11 deletions stdlibs/stdlibs.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,32 @@ func InjectPackage(store gno.Store, pn *gno.PackageNode) {
// 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
*/
ctx := m.Context.(ExecContext)
ctx.OrigCaller = ctx.OrigPkgAddr

backupContext := m.Context.(ExecContext)

m.PushValue(gno.TypedValue{
T: &gno.InterfaceType{},
V: gno.ContextValue{
Context: backupContext,
},
})
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)

m.PushValue(gno.TypedValue{
T: &gno.InterfaceType{},
V: gno.ContextValue{
Context: ctx,
},
})
m.PushOp(gno.OpSetContext)
},
)
pn.DefineNative("GetCallerAt",
Expand Down
2 changes: 1 addition & 1 deletion tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestMachine(store gno.Store, stdout io.Writer, pkgPath string) *gno.Machine
func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAlloc int64, send std.Coins) *gno.Machine {
// FIXME: create a better package to manage this, with custom constructors
pkgAddr := gno.DerivePkgAddr(pkgPath) // the addr of the pkgPath called.
caller := gno.DerivePkgAddr(pkgPath) // NOTE: for the purpose of testing, the caller is generally the "main" package, same as pkgAddr.
caller := gno.DerivePkgAddr("caller") // NOTE: for the purpose of testing, the caller is generally the "main" package, same as pkgAddr.
pkgCoins := std.MustParseCoins("200000000ugnot").Add(send) // >= send.
banker := newTestBanker(pkgAddr.Bech32(), pkgCoins)
ctx := stdlibs.ExecContext{
Expand Down
20 changes: 20 additions & 0 deletions tests/files/zrealm_std6.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// PKGPATH: gno.land/r/std_test
package std_test

import (
"fmt"
"std"
)

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

// Output:
// g1fahslvxakc058u0wa98ytfc9fd6trhe5qfh9ld
// g157y5v3k529jyzhjjz4fn49tzzhf4gess6v39xg
// g1fahslvxakc058u0wa98ytfc9fd6trhe5qfh9ld

0 comments on commit a180ce1

Please sign in to comment.