mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-05-03 23:02:27 +00:00
compiler: allow to use init
function
Process `init()` functions after global variables has been processed. It's body is saved into the `_initialize` method where all initialization is performed.
This commit is contained in:
parent
ef53a45e7a
commit
b771d2d024
3 changed files with 101 additions and 14 deletions
62
pkg/compiler/init_test.go
Normal file
62
pkg/compiler/init_test.go
Normal file
|
@ -0,0 +1,62 @@
|
|||
package compiler_test
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
t.Run("Simple", func(t *testing.T) {
|
||||
src := `package foo
|
||||
var a int
|
||||
func init() {
|
||||
a = 42
|
||||
}
|
||||
func Main() int {
|
||||
return a
|
||||
}`
|
||||
eval(t, src, big.NewInt(42))
|
||||
})
|
||||
t.Run("Multi", func(t *testing.T) {
|
||||
src := `package foo
|
||||
var m = map[int]int{}
|
||||
var a = 2
|
||||
func init() {
|
||||
m[1] = 11
|
||||
}
|
||||
func init() {
|
||||
a = 1
|
||||
m[3] = 30
|
||||
}
|
||||
func Main() int {
|
||||
return m[1] + m[3] + a
|
||||
}`
|
||||
eval(t, src, big.NewInt(42))
|
||||
})
|
||||
t.Run("WithCall", func(t *testing.T) {
|
||||
src := `package foo
|
||||
var m = map[int]int{}
|
||||
func init() {
|
||||
initMap(m)
|
||||
}
|
||||
func initMap(m map[int]int) {
|
||||
m[11] = 42
|
||||
}
|
||||
func Main() int {
|
||||
return m[11]
|
||||
}`
|
||||
eval(t, src, big.NewInt(42))
|
||||
})
|
||||
t.Run("InvalidSignature", func(t *testing.T) {
|
||||
src := `package foo
|
||||
type Foo int
|
||||
var a int
|
||||
func (f Foo) init() {
|
||||
a = 2
|
||||
}
|
||||
func Main() int {
|
||||
return a
|
||||
}`
|
||||
eval(t, src, big.NewInt(0))
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue