forked from TrueCloudLab/neoneo-go
compiler: allow to convert string constants to []byte
Also load constant directly into stack, not by name.
This commit is contained in:
parent
1cfad9ccb8
commit
d65d6ab08d
2 changed files with 30 additions and 5 deletions
|
@ -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"))
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue