compiler: initialize packages according to go spec

`init()` functions should be called during package initialization,
after global variables were processed.
This commit is contained in:
Evgenii Stratonikov 2020-08-05 11:14:43 +03:00
parent 6f2759be3a
commit 439d9ff94d
4 changed files with 37 additions and 5 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"golang.org/x/tools/go/loader"
)
var (
@ -47,8 +48,20 @@ func (c *codegen) traverseGlobals() int {
return 0
}
emit.Instruction(c.prog.BinWriter, opcode.INITSSLOT, []byte{byte(n)})
c.ForEachFile(c.convertGlobals)
c.ForEachFile(c.convertInitFuncs)
c.ForEachPackage(func(pkg *loader.PackageInfo) {
for _, f := range pkg.Files {
c.fillImportMap(f, pkg.Pkg)
c.convertGlobals(f, pkg.Pkg)
}
for _, f := range pkg.Files {
c.fillImportMap(f, pkg.Pkg)
c.convertInitFuncs(f, pkg.Pkg)
}
// because we reuse `convertFuncDecl` for init funcs,
// we need to cleare scope, so that global variables
// encountered after will be recognized as globals.
c.scope = nil
})
}
return n
}