vm: make slice opcodes emit Buffer

This commit is contained in:
Evgenii Stratonikov 2020-05-13 14:08:27 +03:00
parent c3f7832f3b
commit a3a3a77431
2 changed files with 11 additions and 11 deletions

View file

@ -642,7 +642,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
panic(fmt.Sprintf("too big item: %d", l)) panic(fmt.Sprintf("too big item: %d", l))
} }
ab := append(a, b...) ab := append(a, b...)
v.estack.PushVal(ab) v.estack.PushVal(NewBufferItem(ab))
case opcode.SUBSTR: case opcode.SUBSTR:
l := int(v.estack.Pop().BigInt().Int64()) l := int(v.estack.Pop().BigInt().Int64())
@ -658,7 +658,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
if last > len(s) { if last > len(s) {
panic("invalid offset") panic("invalid offset")
} }
v.estack.PushVal(s[o:last]) v.estack.PushVal(NewBufferItem(s[o:last]))
case opcode.LEFT: case opcode.LEFT:
l := int(v.estack.Pop().BigInt().Int64()) l := int(v.estack.Pop().BigInt().Int64())
@ -669,7 +669,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
if t := len(s); l > t { if t := len(s); l > t {
l = t l = t
} }
v.estack.PushVal(s[:l]) v.estack.PushVal(NewBufferItem(s[:l]))
case opcode.RIGHT: case opcode.RIGHT:
l := int(v.estack.Pop().BigInt().Int64()) l := int(v.estack.Pop().BigInt().Int64())
@ -677,7 +677,7 @@ func (v *VM) execute(ctx *Context, op opcode.Opcode, parameter []byte) (err erro
panic("negative length") panic("negative length")
} }
s := v.estack.Pop().Bytes() s := v.estack.Pop().Bytes()
v.estack.PushVal(s[len(s)-l:]) v.estack.PushVal(NewBufferItem(s[len(s)-l:]))
case opcode.DEPTH: case opcode.DEPTH:
v.estack.PushVal(v.estack.Len()) v.estack.PushVal(v.estack.Len())

View file

@ -1753,9 +1753,9 @@ func TestCAT(t *testing.T) {
arg := make([]byte, MaxItemSize/2+1) arg := make([]byte, MaxItemSize/2+1)
runWithArgs(t, prog, nil, arg, arg) runWithArgs(t, prog, nil, arg, arg)
}) })
t.Run("Good", getTestFuncForVM(prog, []byte("abcdef"), []byte("abc"), []byte("def"))) t.Run("Good", getTestFuncForVM(prog, NewBufferItem([]byte("abcdef")), []byte("abc"), []byte("def")))
t.Run("Int0ByteArray", getTestFuncForVM(prog, []byte{}, 0, []byte{})) t.Run("Int0ByteArray", getTestFuncForVM(prog, NewBufferItem([]byte{}), 0, []byte{}))
t.Run("ByteArrayInt1", getTestFuncForVM(prog, []byte{1}, []byte{}, 1)) t.Run("ByteArrayInt1", getTestFuncForVM(prog, NewBufferItem([]byte{1}), []byte{}, 1))
} }
func TestSUBSTR(t *testing.T) { func TestSUBSTR(t *testing.T) {
@ -1763,7 +1763,7 @@ func TestSUBSTR(t *testing.T) {
t.Run("NoArgument", getTestFuncForVM(prog, nil)) t.Run("NoArgument", getTestFuncForVM(prog, nil))
t.Run("OneArgument", getTestFuncForVM(prog, nil, 1)) t.Run("OneArgument", getTestFuncForVM(prog, nil, 1))
t.Run("TwoArguments", getTestFuncForVM(prog, nil, 0, 2)) t.Run("TwoArguments", getTestFuncForVM(prog, nil, 0, 2))
t.Run("Good", getTestFuncForVM(prog, []byte("bc"), []byte("abcdef"), 1, 2)) t.Run("Good", getTestFuncForVM(prog, NewBufferItem([]byte("bc")), []byte("abcdef"), 1, 2))
t.Run("BadOffset", getTestFuncForVM(prog, nil, []byte("abcdef"), 7, 1)) t.Run("BadOffset", getTestFuncForVM(prog, nil, []byte("abcdef"), 7, 1))
t.Run("BigLen", getTestFuncForVM(prog, nil, []byte("abcdef"), 1, 6)) t.Run("BigLen", getTestFuncForVM(prog, nil, []byte("abcdef"), 1, 6))
t.Run("NegativeOffset", getTestFuncForVM(prog, nil, []byte("abcdef"), -1, 3)) t.Run("NegativeOffset", getTestFuncForVM(prog, nil, []byte("abcdef"), -1, 3))
@ -1786,8 +1786,8 @@ func TestLEFT(t *testing.T) {
t.Run("NoArgument", getTestFuncForVM(prog, nil)) t.Run("NoArgument", getTestFuncForVM(prog, nil))
t.Run("NoString", getTestFuncForVM(prog, nil, 2)) t.Run("NoString", getTestFuncForVM(prog, nil, 2))
t.Run("NegativeLen", getTestFuncForVM(prog, nil, "abcdef", -1)) t.Run("NegativeLen", getTestFuncForVM(prog, nil, "abcdef", -1))
t.Run("Good", getTestFuncForVM(prog, "ab", "abcdef", 2)) t.Run("Good", getTestFuncForVM(prog, NewBufferItem([]byte("ab")), "abcdef", 2))
t.Run("GoodBigLen", getTestFuncForVM(prog, "abcdef", "abcdef", 8)) t.Run("GoodBigLen", getTestFuncForVM(prog, NewBufferItem([]byte("abcdef")), "abcdef", 8))
} }
func TestRIGHT(t *testing.T) { func TestRIGHT(t *testing.T) {
@ -1795,7 +1795,7 @@ func TestRIGHT(t *testing.T) {
t.Run("NoArgument", getTestFuncForVM(prog, nil)) t.Run("NoArgument", getTestFuncForVM(prog, nil))
t.Run("NoString", getTestFuncForVM(prog, nil, 2)) t.Run("NoString", getTestFuncForVM(prog, nil, 2))
t.Run("NegativeLen", getTestFuncForVM(prog, nil, "abcdef", -1)) t.Run("NegativeLen", getTestFuncForVM(prog, nil, "abcdef", -1))
t.Run("Good", getTestFuncForVM(prog, "ef", "abcdef", 2)) t.Run("Good", getTestFuncForVM(prog, NewBufferItem([]byte("ef")), "abcdef", 2))
t.Run("BadLen", getTestFuncForVM(prog, nil, "abcdef", 8)) t.Run("BadLen", getTestFuncForVM(prog, nil, "abcdef", 8))
} }