compiler: support delete() builtin

This commit is contained in:
Evgenii Stratonikov 2020-09-06 15:49:41 +03:00
parent 18369c489e
commit 7483e3b054
3 changed files with 23 additions and 1 deletions

View file

@ -14,7 +14,7 @@ import (
var ( var (
// Go language builtin functions. // Go language builtin functions.
goBuiltins = []string{"len", "append", "panic", "make", "copy", "recover"} goBuiltins = []string{"len", "append", "panic", "make", "copy", "recover", "delete"}
// Custom builtin utility functions. // Custom builtin utility functions.
customBuiltins = []string{ customBuiltins = []string{
"FromAddress", "Equals", "FromAddress", "Equals",

View file

@ -1527,6 +1527,8 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) {
} }
emit.Opcode(c.prog.BinWriter, opcode.PUSHNULL) emit.Opcode(c.prog.BinWriter, opcode.PUSHNULL)
c.emitStoreByIndex(varGlobal, c.exceptionIndex) c.emitStoreByIndex(varGlobal, c.exceptionIndex)
case "delete":
emit.Opcode(c.prog.BinWriter, opcode.REMOVE)
case "ToInteger", "ToByteArray", "ToBool": case "ToInteger", "ToByteArray", "ToBool":
typ := stackitem.IntegerT typ := stackitem.IntegerT
switch name { switch name {

View file

@ -55,6 +55,26 @@ var mapTestCases = []testCase{
`, `,
[]byte("Valera"), []byte("Valera"),
}, },
{
"delete key",
`package foo
func Main() int {
m := map[int]int{1: 2, 3: 4}
delete(m, 1)
return len(m)
}`,
big.NewInt(1),
},
{
"delete missing key",
`package foo
func Main() int {
m := map[int]int{3: 4}
delete(m, 1)
return len(m)
}`,
big.NewInt(1),
},
} }
func TestMaps(t *testing.T) { func TestMaps(t *testing.T) {