compiler: allow to split main package across multiple files

This commit is contained in:
Evgenii Stratonikov 2020-08-10 18:23:45 +03:00
parent 553e57c2c4
commit a34ba92d46
6 changed files with 63 additions and 33 deletions

View file

@ -1,7 +1,6 @@
package smartcontract package smartcontract
import ( import (
"bytes"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors" "errors"
@ -563,12 +562,10 @@ func inspect(ctx *cli.Context) error {
if len(in) == 0 { if len(in) == 0 {
return cli.NewExitError(errNoInput, 1) return cli.NewExitError(errNoInput, 1)
} }
b, err := ioutil.ReadFile(in) var b []byte
if err != nil { var err error
return cli.NewExitError(err, 1)
}
if compile { if compile {
b, err = compiler.Compile(in, bytes.NewReader(b)) b, err = compiler.Compile(in, nil)
if err != nil { if err != nil {
return cli.NewExitError(fmt.Errorf("failed to compile: %w", err), 1) return cli.NewExitError(fmt.Errorf("failed to compile: %w", err), 1)
} }

View file

@ -1,8 +1,8 @@
package compiler package compiler
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"go/ast" "go/ast"
"go/parser" "go/parser"
@ -10,6 +10,7 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path"
"strings" "strings"
"github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract"
@ -85,11 +86,32 @@ func (c *codegen) fillImportMap(f *ast.File, pkg *types.Package) {
func getBuildInfo(name string, src interface{}) (*buildInfo, error) { func getBuildInfo(name string, src interface{}) (*buildInfo, error) {
conf := loader.Config{ParserMode: parser.ParseComments} conf := loader.Config{ParserMode: parser.ParseComments}
f, err := conf.ParseFile(name, src) if src != nil {
if err != nil { f, err := conf.ParseFile(name, src)
return nil, err if err != nil {
return nil, err
}
conf.CreateFromFiles("", f)
} else {
var names []string
if strings.HasSuffix(name, ".go") {
names = append(names, name)
} else {
ds, err := ioutil.ReadDir(name)
if err != nil {
return nil, fmt.Errorf("'%s' is neither Go source nor a directory", name)
}
for i := range ds {
if !ds[i].IsDir() && strings.HasSuffix(ds[i].Name(), ".go") {
names = append(names, path.Join(name, ds[i].Name()))
}
}
}
if len(names) == 0 {
return nil, errors.New("no files provided")
}
conf.CreateFromFilenames("", names...)
} }
conf.CreateFromFiles("", f)
prog, err := conf.Load() prog, err := conf.Load()
if err != nil { if err != nil {
@ -97,12 +119,14 @@ func getBuildInfo(name string, src interface{}) (*buildInfo, error) {
} }
return &buildInfo{ return &buildInfo{
initialPackage: f.Name.Name, initialPackage: prog.InitialPackages()[0].Pkg.Name(),
program: prog, program: prog,
}, nil }, nil
} }
// Compile compiles a Go program into bytecode that can run on the NEO virtual machine. // Compile compiles a Go program into bytecode that can run on the NEO virtual machine.
// If `r != nil`, `name` is interpreted as a filename, and `r` as file contents.
// Otherwise `name` is either file name or name of the directory containing source files.
func Compile(name string, r io.Reader) ([]byte, error) { func Compile(name string, r io.Reader) ([]byte, error) {
buf, _, err := CompileWithDebugInfo(name, r) buf, _, err := CompileWithDebugInfo(name, r)
if err != nil { if err != nil {
@ -123,21 +147,18 @@ func CompileWithDebugInfo(name string, r io.Reader) ([]byte, *DebugInfo, error)
// CompileAndSave will compile and save the file to disk in the NEF format. // CompileAndSave will compile and save the file to disk in the NEF format.
func CompileAndSave(src string, o *Options) ([]byte, error) { func CompileAndSave(src string, o *Options) ([]byte, error) {
if !strings.HasSuffix(src, ".go") {
return nil, fmt.Errorf("%s is not a Go file", src)
}
o.Outfile = strings.TrimSuffix(o.Outfile, fmt.Sprintf(".%s", fileExt)) o.Outfile = strings.TrimSuffix(o.Outfile, fmt.Sprintf(".%s", fileExt))
if len(o.Outfile) == 0 { if len(o.Outfile) == 0 {
o.Outfile = strings.TrimSuffix(src, ".go") if strings.HasSuffix(src, ".go") {
o.Outfile = strings.TrimSuffix(src, ".go")
} else {
o.Outfile = "out"
}
} }
if len(o.Ext) == 0 { if len(o.Ext) == 0 {
o.Ext = fileExt o.Ext = fileExt
} }
b, err := ioutil.ReadFile(src) b, di, err := CompileWithDebugInfo(src, nil)
if err != nil {
return nil, err
}
b, di, err := CompileWithDebugInfo(src, bytes.NewReader(b))
if err != nil { if err != nil {
return nil, fmt.Errorf("error while trying to compile smart contract file: %w", err) return nil, fmt.Errorf("error while trying to compile smart contract file: %w", err)
} }

View file

@ -24,6 +24,20 @@ func TestCompiler(t *testing.T) {
// CompileAndSave use config.Version for proper .nef generation. // CompileAndSave use config.Version for proper .nef generation.
config.Version = "0.90.0-test" config.Version = "0.90.0-test"
testCases := []compilerTestCase{ testCases := []compilerTestCase{
{
name: "TestCompileDirectory",
function: func(t *testing.T) {
const multiMainDir = "testdata/multi"
_, di, err := compiler.CompileWithDebugInfo(multiMainDir, nil)
require.NoError(t, err)
m := map[string]bool{}
for i := range di.Methods {
m[di.Methods[i].Name.Name] = true
}
require.Contains(t, m, "Func1")
require.Contains(t, m, "Func2")
},
},
{ {
name: "TestCompile", name: "TestCompile",
function: func(t *testing.T) { function: func(t *testing.T) {
@ -73,10 +87,6 @@ func filterFilename(infos []os.FileInfo) string {
} }
func compileFile(src string) error { func compileFile(src string) error {
file, err := os.Open(src) _, err := compiler.Compile(src, nil)
if err != nil {
return err
}
_, err = compiler.Compile("foo.go", file)
return err return err
} }

View file

@ -3,3 +3,7 @@ package multi
var SomeVar12 = 12 var SomeVar12 = 12
const SomeConst = 42 const SomeConst = 42
func Func1() bool {
return true
}

View file

@ -5,3 +5,7 @@ var SomeVar30 = 30
func Sum() int { func Sum() int {
return SomeVar12 + SomeVar30 return SomeVar12 + SomeVar30
} }
func Func2() bool {
return false
}

View file

@ -6,7 +6,6 @@ import (
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math/big" "math/big"
"os" "os"
"strconv" "strconv"
@ -306,12 +305,7 @@ func handleLoadGo(c *ishell.Context) {
c.Err(errors.New("missing parameter <file>")) c.Err(errors.New("missing parameter <file>"))
return return
} }
fb, err := ioutil.ReadFile(c.Args[0]) b, err := compiler.Compile(c.Args[0], nil)
if err != nil {
c.Err(err)
return
}
b, err := compiler.Compile(c.Args[0], bytes.NewReader(fb))
if err != nil { if err != nil {
c.Err(err) c.Err(err)
return return