aboutsummaryrefslogtreecommitdiffstats
path: root/vm/closure.go
diff options
context:
space:
mode:
Diffstat (limited to 'vm/closure.go')
-rw-r--r--vm/closure.go72
1 files changed, 32 insertions, 40 deletions
diff --git a/vm/closure.go b/vm/closure.go
index ef9bbca93..bd5268f96 100644
--- a/vm/closure.go
+++ b/vm/closure.go
@@ -1,7 +1,5 @@
package vm
-// TODO Re write VM to use values instead of big integers?
-
import (
"math/big"
@@ -12,18 +10,16 @@ import (
type ClosureRef interface {
ReturnGas(*big.Int, *big.Int)
Address() []byte
- Object() *state.StateObject
+ SetCode([]byte)
GetStorage(*big.Int) *ethutil.Value
SetStorage(*big.Int, *ethutil.Value)
}
-// 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 +27,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 +41,16 @@ 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) GetValue(x uint64) *ethutil.Value {
+ return c.GetRangeValue(x, 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 +65,30 @@ 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)) {
+func (c *Closure) GetRangeValue(x, y uint64) *ethutil.Value {
+ if x >= uint64(len(c.Code)) || y >= uint64(len(c.Code)) {
return ethutil.NewValue(0)
}
- partial := c.Code[x.Int64() : x.Int64()+y.Int64()]
+ partial := c.Code[x : x+y]
return ethutil.NewValue(partial)
}
+/*
+ * State storage functions
+ */
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)
+func (c *Closure) GetStorage(x *big.Int) *ethutil.Value {
+ m := c.object.GetStorage(x)
+ if m == nil {
+ return ethutil.EmptyValue()
+ }
- return ret, c.UsedGas, err
+ return m
}
func (c *Closure) Return(ret []byte) []byte {
@@ -112,6 +98,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 +120,17 @@ func (c *Closure) ReturnGas(gas, price *big.Int) {
c.UsedGas.Sub(c.UsedGas, gas)
}
-func (c *Closure) Object() *state.StateObject {
- return c.object
-}
-
+/*
+ * Set / Get
+ */
func (c *Closure) Caller() ClosureRef {
return c.caller
}
-func (self *Closure) SetExecution(exe *Execution) {
- self.exe = exe
+func (c *Closure) Address() []byte {
+ return c.object.Address()
+}
+
+func (self *Closure) SetCode(code []byte) {
+ self.Code = code
}