package interp

Import Path
	github.com/traefik/yaegi/interp (on go.dev)

Dependency Relation
	imports 33 packages, and imported by one package

Involved Source Files ast.go build.go cfg.go debugger.go Package interp provides a complete Go interpreter. For the Go language itself, refer to the official Go specification https://golang.org/ref/spec. # Importing packages Packages can be imported in source or binary form, using the standard Go import statement. In source form, packages are searched first in the vendor directory, the preferred way to store source dependencies. If not found in vendor, sources modules will be searched in GOPATH. Go modules are not supported yet by yaegi. Binary form packages are compiled and linked with the interpreter executable, and exposed to scripts with the Use method. The extract subcommand of yaegi can be used to generate package wrappers. # Custom build tags Custom build tags allow to control which files in imported source packages are interpreted, in the same way as the "-tags" option of the "go build" command. Setting a custom build tag spans globally for all future imports of the session. A build tag is a line comment that begins // yaegi:tags that lists the build constraints to be satisfied by the further imports of source packages. For example the following custom build tag // yaegi:tags noasm Will ensure that an import of a package will exclude files containing // +build !noasm And include files containing // +build noasm dot.go generic.go gta.go hooks.go interp.go op.go program.go realfs.go run.go scope.go src.go type.go typecheck.go typestring.go use.go value.go
Code Examples package main import ( "fmt" "log" "github.com/traefik/yaegi/interp" ) func main() { // Create a new interpreter context i := interp.New(interp.Options{}) // Run some code: define a new function _, err := i.Eval("func f(i int) int { return 2 * i }") if err != nil { log.Fatal(err) } // Access the interpreted f function with Eval v, err := i.Eval("f") if err != nil { log.Fatal(err) } // Returned v is a reflect.Value, so we can use its interface f, ok := v.Interface().(func(int) int) if !ok { log.Fatal("type assertion failed") } // Use interpreted f as it was pre-compiled fmt.Println(f(2)) }
Package-Level Type Names (total 16)
/* sort by: | */
Breakpoint is the result of attempting to set a breakpoint. Position indicates the source position of the breakpoint. Valid indicates whether the breakpoint was successfully set. func (*Debugger).SetBreakpoints(target BreakpointTarget, requests ...BreakpointRequest) []Breakpoint
BreakpointRequest is a request to set a breakpoint. func FunctionBreakpoint(name string) BreakpointRequest func LineBreakpoint(line int) BreakpointRequest func (*Debugger).SetBreakpoints(target BreakpointTarget, requests ...BreakpointRequest) []Breakpoint
BreakpointTarget is the target of a request to set breakpoints. func AllBreakpointTarget() BreakpointTarget func PathBreakpointTarget(path string) BreakpointTarget func ProgramBreakpointTarget(prog *Program) BreakpointTarget func (*Debugger).SetBreakpoints(target BreakpointTarget, requests ...BreakpointRequest) []Breakpoint
A DebugEvent is an event generated by a debugger. FrameDepth returns the number of call frames in the stack trace. Frames returns the call frames in the range [start, end). GoRoutine returns the ID of the Go routine that generated the event. Reason returns the reason for the event.
DebugEventReason is the reason a debug event occurred. func (*DebugEvent).Reason() DebugEventReason func (*Debugger).Interrupt(id int, reason DebugEventReason) bool func (*Debugger).Step(id int, reason DebugEventReason) error const DebugBreak const DebugEnterGoRoutine const DebugEntry const DebugExitGoRoutine const DebugPause const DebugStepInto const DebugStepOut const DebugStepOver const DebugTerminate
DebugFrame provides access to stack frame information while debugging a program. Name returns the name of the stack frame. For function calls to named functions, this is the function name. Position returns the current position of the frame. This is effectively the program counter/link register. May return `Position{}`. Program returns the program associated with the current position of the frame. May return nil. Scopes returns the variable scopes of the frame. *DebugFrame : github.com/polarsignals/frostdb/query/logicalplan.Named func (*DebugEvent).Frames(start, end int) []*DebugFrame
DebugFrameScope provides access to scoped variables while debugging a program. IsClosure returns true if this is the capture scope of a closure. Variables returns the names and values of the variables of the scope. func (*DebugFrame).Scopes() []*DebugFrameScope
A Debugger can be used to debug a Yaegi program. Continue continues execution of the specified Go routine. Continue returns ErrNotLive if there is no Go routine with the corresponding ID, or if it is not live. GoRoutines returns an array of live Go routines. Interrupt issues a stepInto, stepOver, or stepOut request to a running Go routine. Interrupt returns ErrRunning if the Go routine is running. Interrupt returns ErrNotLive if there is no Go routine with the corresponding ID, or if it is not live. SetBreakpoints sets breakpoints for the given target. The returned array has an entry for every request, in order. If a given breakpoint request cannot be satisfied, the corresponding entry will be marked invalid. If the target cannot be found, all entries will be marked invalid. Step issues a stepInto, stepOver, or stepOut request to a stopped Go routine. Step returns ErrRunning if the Go routine is running. Step returns ErrNotLive if there is no Go routine with the corresponding ID, or if it is not live. Terminate attempts to terminate the program. Wait blocks until all Go routines launched by the program have terminated. Wait returns the results of `(*Interpreter).Execute`. func (*Interpreter).Debug(ctx context.Context, prog *Program, events func(*DebugEvent), opts *DebugOptions) *Debugger
DebugGoRoutine provides access to information about a Go routine while debugging a program. ID returns the ID of the Go routine. Name returns "Goroutine {ID}". *DebugGoRoutine : github.com/polarsignals/frostdb/query/logicalplan.Named func (*Debugger).GoRoutines() []*DebugGoRoutine
DebugOptions are the debugger options. If true, Go routine IDs start at 1 instead of 0. func (*Interpreter).Debug(ctx context.Context, prog *Program, events func(*DebugEvent), opts *DebugOptions) *Debugger
DebugVariable is the name and value of a variable from a debug session. Name string Value reflect.Value func (*DebugFrameScope).Variables() []*DebugVariable
Exports stores the map of binary packages per package path. The package path is the path joined from the import path and the package name as specified in source files by the "package" statement. func (*Interpreter).Symbols(importPath string) Exports func (*Interpreter).Use(values Exports) error func github.com/pancsta/asyncmachine-go/pkg/integrations/yaegi.Exec[G](mach am.Api, code fstest.MapFS, host G, symbols Exports, ioOut, ioErr io.Writer) (*yhost.Ret, error) var Symbols
Interpreter contains global resources and state. Compile parses and compiles a Go code represented as a string. CompileAST builds a Program for the given Go code AST. Files and block statements can be compiled, as can most expressions. Var declaration nodes cannot be compiled. WARNING: The node must have been parsed using interp.FileSet(). Results are unpredictable otherwise. CompilePath parses and compiles a Go code located at the given path. Debug initializes a debugger for the given program. The program will not start running until Step or Continue has been called. If Step is called with DebugEntry, an entry event will be generated before the first statement is executed. Otherwise, the debugger will behave as usual. Eval evaluates Go code represented as a string. Eval returns the last result computed by the interpreter, and a non nil error in case of failure. EvalPath evaluates Go code located at path and returns the last result computed by the interpreter, and a non nil error in case of failure. The main function of the main package is executed if present. EvalPathWithContext evaluates Go code located at path and returns the last result computed by the interpreter, and a non nil error in case of failure. The main function of the main package is executed if present. EvalTest evaluates Go code located at path, including test files with "_test.go" suffix. A non nil error is returned in case of failure. The main function, test functions and benchmark functions are internally compiled but not executed. Test functions can be retrieved using the Symbol() method. EvalWithContext evaluates Go code represented as a string. It returns a map on current interpreted package exported symbols. Execute executes compiled Go code. ExecuteWithContext executes compiled Go code. FileSet is the fileset that must be used for parsing Go that will be passed to interp.CompileAST(). Globals returns a map of global variables and constants in the main package. ImportUsed automatically imports pre-compiled packages included by Use(). This is mainly useful for REPLs, or single command lines. In case of an ambiguous default package name, for example "rand" for crypto/rand and math/rand, the package name is constructed by replacing the last "/" by a "_", producing crypto_rand and math_rand. ImportUsed should not be called more than once, and not after a first Eval, as it may rename packages. REPL performs a Read-Eval-Print-Loop on input reader. Results are printed to the output writer of the Interpreter, provided as option at creation time. Errors are printed to the similarly defined errors writer. The last interpreter result value and error are returned. Symbols returns a map of interpreter exported symbol values for the given import path. If the argument is the empty string, all known symbols are returned. Use loads binary runtime symbols in the interpreter context so they can be used in interpreted code. func New(options Options) *Interpreter var Self *Interpreter
Options are the interpreter options. Cmdline args, defaults to os.Args. BuildTags sets build constraints for the interpreter. Environment of interpreter. Entries are in the form "key=values". GoPath sets GOPATH for the interpreter. SourcecodeFilesystem is where the _sourcecode_ is loaded from and does NOT affect the filesystem of scripts when they run. It can be any fs.FS compliant filesystem (e.g. embed.FS, or fstest.MapFS for testing) See example/fs/fs_test.go for an example. Stderr io.Writer Standard input, output and error streams. They default to os.Stdin, os.Stdout and os.Stderr respectively. Stdout io.Writer Unrestricted allows to run non sandboxed stdlib symbols such as os/exec and environment func New(options Options) *Interpreter
Panic is an error recovered from a panic call in interpreted code. Callers is the call stack obtained from the recover call. It may be used as the parameter to runtime.CallersFrames. Stack is the call stack buffer for debug. Value is the recovered value of a call to panic. ( Panic) Error() string Panic : error
A Program is Go code that has been parsed and compiled. PackageName returns name used in a package clause. func (*DebugFrame).Program() *Program func (*Interpreter).Compile(src string) (*Program, error) func (*Interpreter).CompileAST(n ast.Node) (*Program, error) func (*Interpreter).CompilePath(path string) (*Program, error) func ProgramBreakpointTarget(prog *Program) BreakpointTarget func (*Interpreter).Debug(ctx context.Context, prog *Program, events func(*DebugEvent), opts *DebugOptions) *Debugger func (*Interpreter).Execute(p *Program) (res reflect.Value, err error) func (*Interpreter).ExecuteWithContext(ctx context.Context, p *Program) (res reflect.Value, err error)
Package-Level Functions (total 6)
AllBreakpointTarget is used to set breakpoints on all compiled code. Do not use with LineBreakpoint.
FunctionBreakpoint requests a breakpoint on the named function.
LineBreakpoint requests a breakpoint on the given line.
New returns a new interpreter.
PathBreakpointTarget is used to set breapoints on compiled code by path. This can be used to set breakpoints on code compiled with EvalPath, or source packages loaded by Yaegi.
ProgramBreakpointTarget is used to set breakpoints on a Program.
Package-Level Variables (total 5)
ErrNotLive indicates that the specified ID does not refer to a (live) Go routine.
ErrNotRunning indicates that the specified Go routine is running.
ErrRunning indicates that the specified Go routine is running.
Self points to the current interpreter if accessed from within itself, or is nil.
Symbols exposes interpreter values.
Package-Level Constants (total 12)
DebugBreak is emitted when a debug target hits a breakpoint.
DebugEnterGoRoutine is emitted when a Go routine is entered.
DebugEntry is emitted when a debug target starts executing. Can be used with Step to produce a corresponding event when execution starts.
DebugExitGoRoutine is emitted when a Go routine is exited.
DebugPause is emitted when a pause request is completed. Can be used with Interrupt to request a pause.
DebugStepInto is emitted when a stepInto request is completed. Can be used with Step or Interrupt to request a stepInto.
DebugStepOut is emitted when a stepOut request is completed. Can be used with Step or Interrupt to request a stepOut.
DebugStepOver is emitted when a stepOver request is completed. Can be used with Step or Interrupt to request a stepOver.
DebugTerminate is emitted when a debug target terminates. Can be used with Interrupt to attempt to terminate the program.
DefaultSourceName is the name used by default when the name of the input source file has not been specified for an Eval. TODO(mpl): something even more special as a name?
NoTest is the value to pass to EvalPath to skip evaluation of test functions.
Test is the value to pass to EvalPath to activate evaluation of test functions.