github.com/goplus/igop@v0.25.0/cmd/internal/run/run.go (about) 1 /* 2 Copyright 2021 The GoPlus Authors (goplus.org) 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package run implements the “gop run” command. 18 package run 19 20 import ( 21 "context" 22 "fmt" 23 "log" 24 "os" 25 "path/filepath" 26 27 "github.com/goplus/igop" 28 "github.com/goplus/igop/cmd/internal/base" 29 "github.com/goplus/igop/cmd/internal/load" 30 "golang.org/x/tools/go/ssa" 31 ) 32 33 // ----------------------------------------------------------------------------- 34 35 // Cmd - igop run 36 var Cmd = &base.Command{ 37 UsageLine: "igop run [build flags] [package] [arguments...]", 38 Short: "run a Go/Go+ package", 39 } 40 41 var ( 42 flag = &Cmd.Flag 43 ) 44 45 func init() { 46 Cmd.Run = runCmd 47 base.AddBuildFlags(Cmd, base.OmitModFlag|base.OmitSSAFlag|base.OmitSSATraceFlag| 48 base.OmitVFlag|base.OmitExperimentalGCFlag) 49 } 50 51 func runCmd(cmd *base.Command, args []string) { 52 err := flag.Parse(args) 53 if err != nil { 54 os.Exit(2) 55 } 56 paths := flag.Args() 57 if len(paths) == 0 { 58 paths = []string{"."} 59 } 60 args = paths[1:] 61 path, _ := filepath.Abs(paths[0]) 62 isDir, err := load.IsDir(path) 63 if err != nil { 64 log.Fatalln("input arg check failed:", err) 65 } 66 var mode igop.Mode 67 if base.BuildSSA { 68 mode |= igop.EnableDumpInstr 69 } 70 if base.DebugSSATrace { 71 mode |= igop.EnableTracing 72 } 73 if base.BuildX { 74 mode |= igop.EnableDumpImports 75 } 76 if base.ExperimentalGC { 77 mode |= igop.ExperimentalSupportGC 78 } 79 ctx := igop.NewContext(mode) 80 ctx.BuildContext = base.BuildContext 81 ctx.RunContext = context.TODO() 82 var pkg *ssa.Package 83 var input string 84 if isDir { 85 if load.SupportGop && load.IsGopProject(path) { 86 err := load.BuildGopDir(ctx, path) 87 if err != nil { 88 fmt.Fprintln(os.Stderr, err) 89 os.Exit(2) 90 } 91 } 92 pkg, err = ctx.LoadDir(path, false) 93 input = path 94 } else { 95 pkg, err = ctx.LoadFile(path, nil) 96 input, _ = filepath.Split(path) 97 } 98 if err != nil { 99 fmt.Fprintln(os.Stderr, err) 100 os.Exit(2) 101 } 102 interp, err := ctx.NewInterp(pkg) 103 if err != nil { 104 fmt.Fprintln(os.Stderr, err) 105 os.Exit(2) 106 } 107 if base.BuildV { 108 fmt.Println(pkg.Pkg.Path()) 109 } 110 code, err := ctx.RunInterp(interp, input, args) 111 if err != nil { 112 if e, ok := err.(igop.PanicError); ok { 113 fmt.Fprintf(os.Stderr, "panic: %v\n\n%s\n", e.Error(), e.Stack()) 114 } else { 115 fmt.Fprintln(os.Stderr, err) 116 } 117 os.Exit(2) 118 } 119 os.Exit(code) 120 }