mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-12-22 19:19:09 +00:00
compiler: provide filename to Compile()
This commit is contained in:
parent
d8db85ef55
commit
057e1c6e3c
9 changed files with 19 additions and 18 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue