compiler: provide filename to Compile()

This commit is contained in:
Evgenii Stratonikov 2020-08-10 13:06:06 +03:00
parent d8db85ef55
commit 057e1c6e3c
9 changed files with 19 additions and 18 deletions

View file

@ -568,7 +568,7 @@ func inspect(ctx *cli.Context) error {
return cli.NewExitError(err, 1) return cli.NewExitError(err, 1)
} }
if compile { if compile {
b, err = compiler.Compile(bytes.NewReader(b)) b, err = compiler.Compile(in, bytes.NewReader(b))
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

@ -84,9 +84,9 @@ func (c *codegen) fillImportMap(f *ast.File, pkg *types.Package) {
} }
} }
func getBuildInfo(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("", src) f, err := conf.ParseFile(name, src)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -104,8 +104,8 @@ func getBuildInfo(src interface{}) (*buildInfo, error) {
} }
// 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.
func Compile(r io.Reader) ([]byte, error) { func Compile(name string, r io.Reader) ([]byte, error) {
buf, _, err := CompileWithDebugInfo(r) buf, _, err := CompileWithDebugInfo(name, r)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -114,8 +114,8 @@ func Compile(r io.Reader) ([]byte, error) {
} }
// CompileWithDebugInfo compiles a Go program into bytecode and emits debug info. // CompileWithDebugInfo compiles a Go program into bytecode and emits debug info.
func CompileWithDebugInfo(r io.Reader) ([]byte, *DebugInfo, error) { func CompileWithDebugInfo(name string, r io.Reader) ([]byte, *DebugInfo, error) {
ctx, err := getBuildInfo(r) ctx, err := getBuildInfo(name, r)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -138,7 +138,7 @@ func CompileAndSave(src string, o *Options) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
b, di, err := CompileWithDebugInfo(bytes.NewReader(b)) 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

@ -77,6 +77,6 @@ func compileFile(src string) error {
if err != nil { if err != nil {
return err return err
} }
_, err = compiler.Compile(file) _, err = compiler.Compile("foo.go", file)
return err return err
} }

View file

@ -44,7 +44,7 @@ func MethodStruct() struct{} { return struct{}{} }
func unexportedMethod() int { return 1 } func unexportedMethod() int { return 1 }
` `
info, err := getBuildInfo(src) info, err := getBuildInfo("foo.go", src)
require.NoError(t, err) require.NoError(t, err)
pkg := info.program.Package(info.initialPackage) pkg := info.program.Package(info.initialPackage)
@ -238,7 +238,7 @@ func TestSequencePoints(t *testing.T) {
return false return false
}` }`
info, err := getBuildInfo(src) info, err := getBuildInfo("foo.go", src)
require.NoError(t, err) require.NoError(t, err)
pkg := info.program.Package(info.initialPackage) pkg := info.program.Package(info.initialPackage)

View file

@ -118,7 +118,7 @@ func TestContractWithNoMain(t *testing.T) {
someLocal := 2 someLocal := 2
return someGlobal + someLocal + a return someGlobal + someLocal + a
}` }`
b, di, err := compiler.CompileWithDebugInfo(strings.NewReader(src)) b, di, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(src))
require.NoError(t, err) require.NoError(t, err)
v := vm.New() v := vm.New()
invokeMethod(t, "Add3", b, v, di) invokeMethod(t, "Add3", b, v, di)

View file

@ -63,7 +63,7 @@ func TestFromAddress(t *testing.T) {
} }
func spawnVM(t *testing.T, ic *interop.Context, src string) *vm.VM { func spawnVM(t *testing.T, ic *interop.Context, src string) *vm.VM {
b, di, err := compiler.CompileWithDebugInfo(strings.NewReader(src)) b, di, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(src))
require.NoError(t, err) require.NoError(t, err)
v := core.SpawnVM(ic) v := core.SpawnVM(ic)
invokeMethod(t, testMainIdent, b, v, di) invokeMethod(t, testMainIdent, b, v, di)
@ -86,7 +86,7 @@ func TestAppCall(t *testing.T) {
} }
` `
inner, di, err := compiler.CompileWithDebugInfo(strings.NewReader(srcInner)) inner, di, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(srcInner))
require.NoError(t, err) require.NoError(t, err)
m, err := di.ConvertToManifest(smartcontract.NoProperties) m, err := di.ConvertToManifest(smartcontract.NoProperties)
require.NoError(t, err) require.NoError(t, err)

View file

@ -72,7 +72,7 @@ func vmAndCompileInterop(t *testing.T, src string) (*vm.VM, *storagePlugin) {
vm.GasLimit = -1 vm.GasLimit = -1
vm.SyscallHandler = storePlugin.syscallHandler vm.SyscallHandler = storePlugin.syscallHandler
b, di, err := compiler.CompileWithDebugInfo(strings.NewReader(src)) b, di, err := compiler.CompileWithDebugInfo("foo.go", strings.NewReader(src))
require.NoError(t, err) require.NoError(t, err)
invokeMethod(t, testMainIdent, b, vm, di) invokeMethod(t, testMainIdent, b, vm, di)

View file

@ -225,9 +225,10 @@ func TestCreateBasicChain(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
// Push some contract into the chain. // Push some contract into the chain.
c, err := ioutil.ReadFile(prefix + "test_contract.go") name := prefix + "test_contract.go"
c, err := ioutil.ReadFile(name)
require.NoError(t, err) require.NoError(t, err)
avm, di, err := compiler.CompileWithDebugInfo(bytes.NewReader(c)) avm, di, err := compiler.CompileWithDebugInfo(name, bytes.NewReader(c))
require.NoError(t, err) require.NoError(t, err)
t.Logf("contractHash: %s", hash.Hash160(avm).StringLE()) t.Logf("contractHash: %s", hash.Hash160(avm).StringLE())

View file

@ -311,7 +311,7 @@ func handleLoadGo(c *ishell.Context) {
c.Err(err) c.Err(err)
return return
} }
b, err := compiler.Compile(bytes.NewReader(fb)) b, err := compiler.Compile(c.Args[0], bytes.NewReader(fb))
if err != nil { if err != nil {
c.Err(err) c.Err(err)
return return