diff options
Diffstat (limited to 'vm/closure.go')
-rw-r--r-- | vm/closure.go | 77 |
1 files changed, 20 insertions, 57 deletions
diff --git a/vm/closure.go b/vm/closure.go index ef9bbca93..97b31ada0 100644 --- a/vm/closure.go +++ b/vm/closure.go @@ -1,29 +1,22 @@ package vm -// TODO Re write VM to use values instead of big integers? - import ( "math/big" - "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/state" ) type ClosureRef interface { ReturnGas(*big.Int, *big.Int) Address() []byte - Object() *state.StateObject - GetStorage(*big.Int) *ethutil.Value - SetStorage(*big.Int, *ethutil.Value) + SetCode([]byte) } -// Basic inline closure object which implement the 'closure' interface type Closure struct { caller ClosureRef - object *state.StateObject + object ClosureRef Code []byte message *state.Message - exe *Execution Gas, UsedGas, Price *big.Int @@ -31,7 +24,7 @@ type Closure struct { } // Create a new closure for the given data items -func NewClosure(msg *state.Message, caller ClosureRef, object *state.StateObject, code []byte, gas, price *big.Int) *Closure { +func NewClosure(msg *state.Message, caller ClosureRef, object ClosureRef, code []byte, gas, price *big.Int) *Closure { c := &Closure{message: msg, caller: caller, object: object, Code: code, Args: nil} // Gas should be a pointer so it can safely be reduced through the run @@ -45,26 +38,12 @@ func NewClosure(msg *state.Message, caller ClosureRef, object *state.StateObject return c } -// Retuns the x element in data slice -func (c *Closure) GetStorage(x *big.Int) *ethutil.Value { - m := c.object.GetStorage(x) - if m == nil { - return ethutil.EmptyValue() - } - - return m -} - -func (c *Closure) Get(x *big.Int) *ethutil.Value { - return c.Gets(x, big.NewInt(1)) -} - -func (c *Closure) GetOp(x int) OpCode { +func (c *Closure) GetOp(x uint64) OpCode { return OpCode(c.GetByte(x)) } -func (c *Closure) GetByte(x int) byte { - if x < len(c.Code) { +func (c *Closure) GetByte(x uint64) byte { + if x < uint64(len(c.Code)) { return c.Code[x] } @@ -79,30 +58,12 @@ func (c *Closure) GetBytes(x, y int) []byte { return c.Code[x : x+y] } -func (c *Closure) Gets(x, y *big.Int) *ethutil.Value { - if x.Int64() >= int64(len(c.Code)) || y.Int64() >= int64(len(c.Code)) { - return ethutil.NewValue(0) +func (c *Closure) GetRangeValue(x, y uint64) []byte { + if x >= uint64(len(c.Code)) || y >= uint64(len(c.Code)) { + return nil } - partial := c.Code[x.Int64() : x.Int64()+y.Int64()] - - return ethutil.NewValue(partial) -} - -func (c *Closure) SetStorage(x *big.Int, val *ethutil.Value) { - c.object.SetStorage(x, val) -} - -func (c *Closure) Address() []byte { - return c.object.Address() -} - -func (c *Closure) Call(vm VirtualMachine, args []byte) ([]byte, *big.Int, error) { - c.Args = args - - ret, err := vm.RunClosure(c) - - return ret, c.UsedGas, err + return c.Code[x : x+y] } func (c *Closure) Return(ret []byte) []byte { @@ -112,6 +73,9 @@ func (c *Closure) Return(ret []byte) []byte { return ret } +/* + * Gas functions + */ func (c *Closure) UseGas(gas *big.Int) bool { if c.Gas.Cmp(gas) < 0 { return false @@ -131,14 +95,13 @@ func (c *Closure) ReturnGas(gas, price *big.Int) { c.UsedGas.Sub(c.UsedGas, gas) } -func (c *Closure) Object() *state.StateObject { - return c.object -} - -func (c *Closure) Caller() ClosureRef { - return c.caller +/* + * Set / Get + */ +func (c *Closure) Address() []byte { + return c.object.Address() } -func (self *Closure) SetExecution(exe *Execution) { - self.exe = exe +func (self *Closure) SetCode(code []byte) { + self.Code = code } |