diff --git a/pkg/compiler/analysis.go b/pkg/compiler/analysis.go index 11be72e32..cd9e9148d 100644 --- a/pkg/compiler/analysis.go +++ b/pkg/compiler/analysis.go @@ -14,7 +14,7 @@ import ( var ( // 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. customBuiltins = []string{ "FromAddress", "Equals", diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index bf01115ad..be9e8ec2c 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -1527,6 +1527,8 @@ func (c *codegen) convertBuiltin(expr *ast.CallExpr) { } emit.Opcode(c.prog.BinWriter, opcode.PUSHNULL) c.emitStoreByIndex(varGlobal, c.exceptionIndex) + case "delete": + emit.Opcode(c.prog.BinWriter, opcode.REMOVE) case "ToInteger", "ToByteArray", "ToBool": typ := stackitem.IntegerT switch name { diff --git a/pkg/compiler/map_test.go b/pkg/compiler/map_test.go index be7453507..30fcf85a2 100644 --- a/pkg/compiler/map_test.go +++ b/pkg/compiler/map_test.go @@ -55,6 +55,26 @@ var mapTestCases = []testCase{ `, []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) {