From 34a4c159324fe8c523d4233792d29572a8970124 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 19 May 2020 17:02:26 +0300 Subject: [PATCH] compiler: support byte slice declaration --- pkg/compiler/codegen.go | 2 ++ pkg/compiler/slice_test.go | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/pkg/compiler/codegen.go b/pkg/compiler/codegen.go index b26c82cb4..6d4178636 100644 --- a/pkg/compiler/codegen.go +++ b/pkg/compiler/codegen.go @@ -233,6 +233,8 @@ func (c *codegen) emitDefault(t types.Type) { case *types.Slice: if isCompoundSlice(t) { emit.Opcode(c.prog.BinWriter, opcode.NEWARRAY0) + } else { + emit.Bytes(c.prog.BinWriter, []byte{}) } case *types.Struct: emit.Int(c.prog.BinWriter, int64(t.NumFields())) diff --git a/pkg/compiler/slice_test.go b/pkg/compiler/slice_test.go index 40600ea28..a853f6e00 100644 --- a/pkg/compiler/slice_test.go +++ b/pkg/compiler/slice_test.go @@ -130,6 +130,17 @@ var sliceTestCases = []testCase{ }`, []byte{2, 3}, }, + { + "declare byte slice", + `package foo + func Main() []byte { + var a []byte + a = append(a, 1) + a = append(a, 2) + return a + }`, + []byte{1, 2}, + }, { "declare compound slice", `package foo