From 057e1c6e3c2c3ab087b4a6ee39395944e254eb36 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 10 Aug 2020 13:06:06 +0300 Subject: [PATCH] compiler: provide filename to `Compile()` --- cli/smartcontract/smart_contract.go | 2 +- pkg/compiler/compiler.go | 14 +++++++------- pkg/compiler/compiler_test.go | 2 +- pkg/compiler/debug_test.go | 4 ++-- pkg/compiler/global_test.go | 2 +- pkg/compiler/interop_test.go | 4 ++-- pkg/compiler/vm_test.go | 2 +- pkg/core/helper_test.go | 5 +++-- pkg/vm/cli/cli.go | 2 +- 9 files changed, 19 insertions(+), 18 deletions(-) diff --git a/cli/smartcontract/smart_contract.go b/cli/smartcontract/smart_contract.go index a6f62960e..4e0728354 100644 --- a/cli/smartcontract/smart_contract.go +++ b/cli/smartcontract/smart_contract.go @@ -568,7 +568,7 @@ func inspect(ctx *cli.Context) error { return cli.NewExitError(err, 1) } if compile { - b, err = compiler.Compile(bytes.NewReader(b)) + b, err = compiler.Compile(in, bytes.NewReader(b)) if err != nil { return cli.NewExitError(fmt.Errorf("failed to compile: %w", err), 1) } diff --git a/pkg/compiler/compiler.go b/pkg/compiler/compiler.go index b221b8d74..db3164053 100644 --- a/pkg/compiler/compiler.go +++ b/pkg/compiler/compiler.go @@ -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} - f, err := conf.ParseFile("", src) + f, err := conf.ParseFile(name, src) if err != nil { 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. -func Compile(r io.Reader) ([]byte, error) { - buf, _, err := CompileWithDebugInfo(r) +func Compile(name string, r io.Reader) ([]byte, error) { + buf, _, err := CompileWithDebugInfo(name, r) if err != nil { 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. -func CompileWithDebugInfo(r io.Reader) ([]byte, *DebugInfo, error) { - ctx, err := getBuildInfo(r) +func CompileWithDebugInfo(name string, r io.Reader) ([]byte, *DebugInfo, error) { + ctx, err := getBuildInfo(name, r) if err != nil { return nil, nil, err } @@ -138,7 +138,7 @@ func CompileAndSave(src string, o *Options) ([]byte, error) { if err != nil { return nil, err } - b, di, err := CompileWithDebugInfo(bytes.NewReader(b)) + b, di, err := CompileWithDebugInfo(src, bytes.NewReader(b)) if err != nil { return nil, fmt.Errorf("error while trying to compile smart contract file: %w", err) } diff --git a/pkg/compiler/compiler_test.go b/pkg/compiler/compiler_test.go index d3ab69f1b..2e114cbef 100644 --- a/pkg/compiler/compiler_test.go +++ b/pkg/compiler/compiler_test.go @@ -77,6 +77,6 @@ func compileFile(src string) error { if err != nil { return err } - _, err = compiler.Compile(file) + _, err = compiler.Compile("foo.go", file) return err } diff --git a/pkg/compiler/debug_test.go b/pkg/compiler/debug_test.go index 6c5a415cd..3134d4f17 100644 --- a/pkg/compiler/debug_test.go +++ b/pkg/compiler/debug_test.go @@ -44,7 +44,7 @@ func MethodStruct() struct{} { return struct{}{} } func unexportedMethod() int { return 1 } ` - info, err := getBuildInfo(src) + info, err := getBuildInfo("foo.go", src) require.NoError(t, err) pkg := info.program.Package(info.initialPackage) @@ -238,7 +238,7 @@ func TestSequencePoints(t *testing.T) { return false }` - info, err := getBuildInfo(src) + info, err := getBuildInfo("foo.go", src) require.NoError(t, err) pkg := info.program.Package(info.initialPackage) diff --git a/pkg/compiler/global_test.go b/pkg/compiler/global_test.go index 934972fec..2be4caf30 100644 --- a/pkg/compiler/global_test.go +++ b/pkg/compiler/global_test.go @@ -118,7 +118,7 @@ func TestContractWithNoMain(t *testing.T) { someLocal := 2 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) v := vm.New() invokeMethod(t, "Add3", b, v, di) diff --git a/pkg/compiler/interop_test.go b/pkg/compiler/interop_test.go index e0cceeb59..79c00d93f 100644 --- a/pkg/compiler/interop_test.go +++ b/pkg/compiler/interop_test.go @@ -63,7 +63,7 @@ func TestFromAddress(t *testing.T) { } 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) v := core.SpawnVM(ic) 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) m, err := di.ConvertToManifest(smartcontract.NoProperties) require.NoError(t, err) diff --git a/pkg/compiler/vm_test.go b/pkg/compiler/vm_test.go index ce862a93e..d4a317d36 100644 --- a/pkg/compiler/vm_test.go +++ b/pkg/compiler/vm_test.go @@ -72,7 +72,7 @@ func vmAndCompileInterop(t *testing.T, src string) (*vm.VM, *storagePlugin) { vm.GasLimit = -1 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) invokeMethod(t, testMainIdent, b, vm, di) diff --git a/pkg/core/helper_test.go b/pkg/core/helper_test.go index 9b6e2f9fa..fb4edb92e 100644 --- a/pkg/core/helper_test.go +++ b/pkg/core/helper_test.go @@ -225,9 +225,10 @@ func TestCreateBasicChain(t *testing.T) { require.NoError(t, err) // 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) - avm, di, err := compiler.CompileWithDebugInfo(bytes.NewReader(c)) + avm, di, err := compiler.CompileWithDebugInfo(name, bytes.NewReader(c)) require.NoError(t, err) t.Logf("contractHash: %s", hash.Hash160(avm).StringLE()) diff --git a/pkg/vm/cli/cli.go b/pkg/vm/cli/cli.go index c2109c6c6..cc1011cb8 100644 --- a/pkg/vm/cli/cli.go +++ b/pkg/vm/cli/cli.go @@ -311,7 +311,7 @@ func handleLoadGo(c *ishell.Context) { c.Err(err) return } - b, err := compiler.Compile(bytes.NewReader(fb)) + b, err := compiler.Compile(c.Args[0], bytes.NewReader(fb)) if err != nil { c.Err(err) return