compiler: support removing slice elements

Go-way of removing elements from slice is via `append` builtin.
There is a separate opcode for removing elements from
Arrays, which is cheaper and supported in this commit.
This commit is contained in:
Evgenii Stratonikov 2020-09-15 10:05:41 +03:00
parent 78948ef7af
commit bcc11cbd74
4 changed files with 36 additions and 1 deletions

View file

@ -17,7 +17,7 @@ var (
goBuiltins = []string{"len", "append", "panic", "make", "copy", "recover", "delete"}
// Custom builtin utility functions.
customBuiltins = []string{
"FromAddress", "Equals",
"FromAddress", "Equals", "Remove",
"ToBool", "ToByteArray", "ToInteger",
}
)

View file

@ -1542,6 +1542,12 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
typ = stackitem.BooleanT
}
c.emitConvert(typ)
case "Remove":
if !isCompoundSlice(c.typeOf(expr.Args[0])) {
c.prog.Err = errors.New("`Remove` supports only non-byte slices")
return
}
emit.Opcode(c.prog.BinWriter, opcode.REMOVE)
case "Equals":
emit.Opcode(c.prog.BinWriter, opcode.EQUAL)
case "FromAddress":

View file

@ -327,6 +327,30 @@ func TestSubsliceCompound(t *testing.T) {
require.Error(t, err)
}
func TestRemove(t *testing.T) {
t.Run("Valid", func(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/util"
func Main() int {
a := []int{11, 22, 33}
util.Remove(a, 1)
return len(a) + a[0] + a[1]
}`
eval(t, src, big.NewInt(46))
})
t.Run("ByteSlice", func(t *testing.T) {
src := `package foo
import "github.com/nspcc-dev/neo-go/pkg/interop/util"
func Main() int {
a := []byte{11, 22, 33}
util.Remove(a, 1)
return len(a)
}`
_, err := compiler.Compile("", strings.NewReader(src))
require.Error(t, err)
})
}
func TestJumps(t *testing.T) {
src := `
package foo

View file

@ -17,3 +17,8 @@ func FromAddress(address string) []byte {
func Equals(a, b interface{}) bool {
return false
}
// Remove removes element with index i from slice.
// This is done in place and slice must have type other than `[]byte`.
func Remove(slice interface{}, i int) {
}