forked from TrueCloudLab/neoneo-go
refactor Add, Sub to return VMSTATE
add popTwoByteArrays helper function
This commit is contained in:
parent
c7e32e7eb3
commit
17c53d1081
1 changed files with 26 additions and 8 deletions
|
@ -7,35 +7,39 @@ import (
|
||||||
// Add adds two stack Items together.
|
// Add adds two stack Items together.
|
||||||
// Returns an error if either items cannot be casted to an integer
|
// Returns an error if either items cannot be casted to an integer
|
||||||
// or if integers cannot be added together
|
// or if integers cannot be added together
|
||||||
func Add(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) error {
|
func Add(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) (Vmstate, error) {
|
||||||
|
|
||||||
operandA, operandB, err := popTwoIntegers(ctx)
|
operandA, operandB, err := popTwoIntegers(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return FAULT, err
|
||||||
|
}
|
||||||
res, err := operandA.Add(operandB)
|
res, err := operandA.Add(operandB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return FAULT, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Estack.Push(res)
|
ctx.Estack.Push(res)
|
||||||
|
|
||||||
return nil
|
return NONE, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sub subtracts two stack Items.
|
// Sub subtracts two stack Items.
|
||||||
// Returns an error if either items cannot be casted to an integer
|
// Returns an error if either items cannot be casted to an integer
|
||||||
// or if integers cannot be subtracted together
|
// or if integers cannot be subtracted together
|
||||||
func Sub(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) error {
|
func Sub(op stack.Instruction, ctx *stack.Context, istack *stack.Invocation) (Vmstate, error) {
|
||||||
|
|
||||||
operandA, operandB, err := popTwoIntegers(ctx)
|
operandA, operandB, err := popTwoIntegers(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return FAULT, err
|
||||||
|
}
|
||||||
res, err := operandB.Sub(operandA)
|
res, err := operandB.Sub(operandA)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return HALT, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Estack.Push(res)
|
ctx.Estack.Push(res)
|
||||||
|
|
||||||
return nil
|
return NONE, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func popTwoIntegers(ctx *stack.Context) (*stack.Int, *stack.Int, error) {
|
func popTwoIntegers(ctx *stack.Context) (*stack.Int, *stack.Int, error) {
|
||||||
|
@ -50,3 +54,17 @@ func popTwoIntegers(ctx *stack.Context) (*stack.Int, *stack.Int, error) {
|
||||||
|
|
||||||
return operandA, operandB, nil
|
return operandA, operandB, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func popTwoByteArrays(ctx *stack.Context) (*stack.ByteArray, *stack.ByteArray, error) {
|
||||||
|
// Pop first stack item and cast as byte array
|
||||||
|
ba1, err := ctx.Estack.PopByteArray()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
// Pop second stack item and cast as byte array
|
||||||
|
ba2, err := ctx.Estack.PopByteArray()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return ba1, ba2, nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue