| 1 | package interp |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "go/ast" |
| 6 | "go/token" |
| 7 | "io/fs" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "runtime" |
| 11 | "runtime/debug" |
| 12 | ) |
| 13 | |
| 14 | // A Program is Go code that has been parsed and compiled. |
| 15 | type Program struct { |
| 16 | pkgName string |
| 17 | root *node |
| 18 | init []*node |
| 19 | } |
| 20 | |
| 21 | // PackageName returns name used in a package clause. |
| 22 | func (p *Program) PackageName() string { |
| 23 | return p.pkgName |
| 24 | } |
| 25 | |
| 26 | // FileSet is the fileset that must be used for parsing Go that will be passed |
| 27 | // to interp.CompileAST(). |
| 28 | func (interp *Interpreter) FileSet() *token.FileSet { |
| 29 | return interp.fset |
| 30 | } |
| 31 | |
| 32 | // Compile parses and compiles a Go code represented as a string. |
| 33 | func (interp *Interpreter) Compile(src string) (*Program, error) { |
| 34 | return interp.compileSrc(src, "", true) |
| 35 | } |
| 36 | |
| 37 | // CompilePath parses and compiles a Go code located at the given path. |
| 38 | func (interp *Interpreter) CompilePath(path string) (*Program, error) { |
| 39 | path = filepath.ToSlash(path) // Ensure path is in Unix format. Since we work with fs.FS, we need to use Unix path. |
| 40 | if !isFile(interp.filesystem, path) { |
| 41 | _, err := interp.importSrc(mainID, path, NoTest) |
| 42 | return nil, err |
| 43 | } |
| 44 | |
| 45 | b, err := fs.ReadFile(interp.filesystem, path) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | return interp.compileSrc(string(b), path, false) |
| 50 | } |
| 51 | |
| 52 | func (interp *Interpreter) compileSrc(src, name string, inc bool) (*Program, error) { |
| 53 | if name != "" { |
| 54 | interp.name = name |
| 55 | } |
| 56 | if interp.name == "" { |
| 57 | interp.name = DefaultSourceName |
| 58 | } |
| 59 | |
| 60 | // Parse source to AST. |
| 61 | n, err := interp.parse(src, interp.name, inc) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | return interp.CompileAST(n) |
| 67 | } |
| 68 | |
| 69 | // CompileAST builds a Program for the given Go code AST. Files and block |
| 70 | // statements can be compiled, as can most expressions. Var declaration nodes |
| 71 | // cannot be compiled. |
| 72 | // |
| 73 | // WARNING: The node must have been parsed using interp.FileSet(). Results are |
| 74 | // unpredictable otherwise. |
| 75 | func (interp *Interpreter) CompileAST(n ast.Node) (*Program, error) { |
| 76 | // Convert AST. |
| 77 | pkgName, root, err := interp.ast(n) |
| 78 | if err != nil || root == nil { |
| 79 | return nil, err |
| 80 | } |
| 81 | |
| 82 | if interp.astDot { |
| 83 | dotCmd := interp.dotCmd |
| 84 | if dotCmd == "" { |
| 85 | dotCmd = defaultDotCmd(interp.name, "yaegi-ast-") |
| 86 | } |
| 87 | root.astDot(dotWriter(dotCmd), interp.name) |
| 88 | if interp.noRun { |
| 89 | return nil, err |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Perform global types analysis. |
| 94 | if err = interp.gtaRetry([]*node{root}, pkgName, pkgName); err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | |
| 98 | // Annotate AST with CFG informations. |
| 99 | initNodes, err := interp.cfg(root, nil, pkgName, pkgName) |
| 100 | if err != nil { |
| 101 | if interp.cfgDot { |
| 102 | dotCmd := interp.dotCmd |
| 103 | if dotCmd == "" { |
| 104 | dotCmd = defaultDotCmd(interp.name, "yaegi-cfg-") |
| 105 | } |
| 106 | root.cfgDot(dotWriter(dotCmd)) |
| 107 | } |
| 108 | return nil, err |
| 109 | } |
| 110 | |
| 111 | if root.kind != fileStmt { |
| 112 | // REPL may skip package statement. |
| 113 | setExec(root.start) |
| 114 | } |
| 115 | interp.mutex.Lock() |
| 116 | gs := interp.scopes[pkgName] |
| 117 | if interp.universe.sym[pkgName] == nil { |
| 118 | // Make the package visible under a path identical to its name. |
| 119 | interp.srcPkg[pkgName] = gs.sym |
| 120 | interp.universe.sym[pkgName] = &symbol{kind: pkgSym, typ: &itype{cat: srcPkgT, path: pkgName}} |
| 121 | interp.pkgNames[pkgName] = pkgName |
| 122 | } |
| 123 | interp.mutex.Unlock() |
| 124 | |
| 125 | // Add main to list of functions to run, after all inits. |
| 126 | if m := gs.sym[mainID]; pkgName == mainID && m != nil { |
| 127 | initNodes = append(initNodes, m.node) |
| 128 | } |
| 129 | |
| 130 | if interp.cfgDot { |
| 131 | dotCmd := interp.dotCmd |
| 132 | if dotCmd == "" { |
| 133 | dotCmd = defaultDotCmd(interp.name, "yaegi-cfg-") |
| 134 | } |
| 135 | root.cfgDot(dotWriter(dotCmd)) |
| 136 | } |
| 137 | |
| 138 | return &Program{pkgName, root, initNodes}, nil |
| 139 | } |
| 140 | |
| 141 | // Execute executes compiled Go code. |
| 142 | func (interp *Interpreter) Execute(p *Program) (res reflect.Value, err error) { |
| 143 | defer func() { |
| 144 | r := recover() |
| 145 | if r != nil { |
| 146 | var pc [64]uintptr // 64 frames should be enough. |
| 147 | n := runtime.Callers(1, pc[:]) |
| 148 | err = Panic{Value: r, Callers: pc[:n], Stack: debug.Stack()} |
| 149 | } |
| 150 | }() |
| 151 | |
| 152 | // Generate node exec closures. |
| 153 | if err = genRun(p.root); err != nil { |
| 154 | return res, err |
| 155 | } |
| 156 | |
| 157 | // Init interpreter execution memory frame. |
| 158 | interp.frame.setrunid(interp.runid()) |
| 159 | interp.frame.mutex.Lock() |
| 160 | interp.resizeFrame() |
| 161 | interp.frame.mutex.Unlock() |
| 162 | |
| 163 | // Execute node closures. |
| 164 | interp.run(p.root, nil) |
| 165 | |
| 166 | // Wire and execute global vars. |
| 167 | n, err := genGlobalVars([]*node{p.root}, interp.scopes[p.pkgName]) |
| 168 | if err != nil { |
| 169 | return res, err |
| 170 | } |
| 171 | interp.run(n, nil) |
| 172 | |
| 173 | for _, n := range p.init { |
| 174 | interp.run(n, interp.frame) |
| 175 | } |
| 176 | v := genValue(p.root) |
| 177 | res = v(interp.frame) |
| 178 | |
| 179 | // If result is an interpreter node, wrap it in a runtime callable function. |
| 180 | if res.IsValid() { |
| 181 | if n, ok := res.Interface().(*node); ok { |
| 182 | res = genFunctionWrapper(n)(interp.frame) |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return res, err |
| 187 | } |
| 188 | |
| 189 | // ExecuteWithContext executes compiled Go code. |
| 190 | func (interp *Interpreter) ExecuteWithContext(ctx context.Context, p *Program) (res reflect.Value, err error) { |
| 191 | interp.mutex.Lock() |
| 192 | interp.done = make(chan struct{}) |
| 193 | interp.cancelChan = !interp.opt.fastChan |
| 194 | interp.mutex.Unlock() |
| 195 | |
| 196 | done := make(chan struct{}) |
| 197 | go func() { |
| 198 | defer close(done) |
| 199 | res, err = interp.Execute(p) |
| 200 | }() |
| 201 | |
| 202 | select { |
| 203 | case <-ctx.Done(): |
| 204 | interp.stop() |
| 205 | return reflect.Value{}, ctx.Err() |
| 206 | case <-done: |
| 207 | } |
| 208 | return res, err |
| 209 | } |