Merge pull request #622 from nspcc-dev/feature/byteconst

compiler: allow to convert string constants to []byte
This commit is contained in:
Roman Khimov 2020-01-27 15:56:07 +03:00 committed by GitHub
commit 1774b87a86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 5 deletions

View file

@ -54,3 +54,28 @@ func TestByteConversionDirectlyInFunctionCall(t *testing.T) {
` `
eval(t, src, []byte("foo")) eval(t, src, []byte("foo"))
} }
func TestByteConversionOfConstant(t *testing.T) {
src := `
package foo
const foo = "foo"
func Main() []byte {
b := []byte(foo)
return b
}
`
eval(t, src, []byte("foo"))
}
func TestByteConversionOfVariable(t *testing.T) {
src := `
package foo
func Main() []byte {
a := "fo"
a = a + "o"
b := []byte(a)
return b
}
`
eval(t, src, []byte("foo"))
}

View file

@ -349,6 +349,8 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
return nil return nil
} }
c.emitLoadConst(value) c.emitLoadConst(value)
} else if tv := c.typeInfo.Types[n]; tv.Value != nil {
c.emitLoadConst(tv)
} else { } else {
c.emitLoadLocal(n.Name) c.emitLoadLocal(n.Name)
} }
@ -481,11 +483,9 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
return nil return nil
} }
case *ast.ArrayType: case *ast.ArrayType:
// For now we will assume that there is only 1 argument passed which // For now we will assume that there are only byte slice conversions.
// will be a basic literal (string kind). This only to handle string // E.g. []byte("foobar") or []byte(scriptHash).
// to byte slice conversions. E.G. []byte("foobar") ast.Walk(c, n.Args[0])
arg := n.Args[0].(*ast.BasicLit)
c.emitLoadConst(c.typeInfo.Types[arg])
return nil return nil
} }
@ -672,6 +672,13 @@ func (c *codegen) getByteArray(expr ast.Expr) []byte {
buf[i] = byte(val) buf[i] = byte(val)
} }
return buf return buf
case *ast.CallExpr:
if tv := c.typeInfo.Types[t.Args[0]]; tv.Value != nil {
val := constant.StringVal(tv.Value)
return []byte(val)
}
return nil
default: default:
return nil return nil
} }

View file

@ -100,6 +100,27 @@ func TestAppCall(t *testing.T) {
_, err := compiler.Compile(strings.NewReader(src)) _, err := compiler.Compile(strings.NewReader(src))
require.Error(t, err) require.Error(t, err)
}) })
t.Run("convert from string constant", func(t *testing.T) {
src := `
package foo
import "github.com/CityOfZion/neo-go/pkg/interop/engine"
const scriptHash = ` + fmt.Sprintf("%#v", string(ih.BytesBE())) + `
func Main() int {
x := 13
y := 29
result := engine.AppCall([]byte(scriptHash), []interface{}{x, y})
return result.(int)
}
`
v := vmAndCompile(t, src)
v.SetScriptGetter(getScript)
require.NoError(t, v.Run())
assertResult(t, v, big.NewInt(42))
})
} }
func getAppCallScript(h string) string { func getAppCallScript(h string) string {