igop

package module
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 27, 2022 License: Apache-2.0 Imports: 35 Imported by: 67

README

iGo+ The Go/Go+ Interpreter

Go1.14 Go1.15 Go1.16 Go1.17 Go1.18

ABI

support ABI0 and ABIInternal

unsupport features
  • Go1.18 type parameters
  • Go1.18 fuzzing
igop command line

Go version < 1.17:

go get -u github.com/goplus/igop/cmd/igop

Go version >= 1.17:

go install github.com/goplus/igop/cmd/igop@latest

Commands

igop             # repl mode
igop run         # interpret package
igop test        # test package
igop repl mode
igop                       # run repl mode, support Go/Go+
igop repl                  # run repl mode, support Go/Go+
igop repl -gop=false       # run repl mode, disable Go+ syntax
igop package

run go source

package main

import (
	"github.com/goplus/igop"
	_ "github.com/goplus/igop/pkg/fmt"
)

var source = `
package main

import "fmt"

func main() {
	fmt.Println("hello")
}
`

func main() {
	_, err := igop.RunFile("main.go", source, nil, 0)
	if err != nil {
		panic(err)
	}
}

run gop source

package main

import (
	"github.com/goplus/igop"
	_ "github.com/goplus/igop/gopbuild"
	_ "github.com/goplus/igop/pkg/fmt"
)

var source = `
println "Hello, Go+"
`

func main() {
	_, err := igop.RunFile("main.gop", source, nil, 0)
	if err != nil {
		panic(err)
	}
}

Documentation

Overview

Package ssa/interp defines an interpreter for the SSA representation of Go programs.

This interpreter is provided as an adjunct for testing the SSA construction algorithm. Its purpose is to provide a minimal metacircular implementation of the dynamic semantics of each SSA instruction. It is not, and will never be, a production-quality Go interpreter.

The following is a partial list of Go features that are currently unsupported or incomplete in the interpreter.

* Unsafe operations, including all uses of unsafe.Pointer, are impossible to support given the "boxed" value representation we have chosen.

* The reflect package is only partially implemented.

* The "testing" package is no longer supported because it depends on low-level details that change too often.

* "sync/atomic" operations are not atomic due to the "boxed" value representation: it is not possible to read, modify and write an interface value atomically. As a consequence, Mutexes are currently broken.

* recover is only partially implemented. Also, the interpreter makes no attempt to distinguish target panics from interpreter crashes.

* the sizes of the int, uint and uintptr types in the target program are assumed to be the same as those of the interpreter itself.

* all values occupy space, even those of types defined by the spec to have zero size, e.g. struct{}. This can cause asymptotic performance degradation.

* os.Exit is implemented using panic, causing deferred functions to run.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoPackage        = errors.New("no package")
	ErrPackage          = errors.New("package contain errors")
	ErrNotFoundMain     = errors.New("not found main package")
	ErrTestFailed       = errors.New("test failed")
	ErrNotFoundPackage  = errors.New("not found package")
	ErrNotFoundImporter = errors.New("not found provider for types.Importer")
	ErrGoexitDeadlock   = errors.New("fatal error: no goroutines (main called runtime.Goexit) - deadlock!")
	ErrNoFunction       = errors.New("no function")
	ErrNoCustomBuiltin  = errors.New("no custom builtin")
)

Functions

func AllMethod

func AllMethod(typ reflect.Type, enableUnexport bool) []reflect.Method

func CreateTestMainPackage deprecated

func CreateTestMainPackage(ctx *Context, pkg *ssa.Package) (*ssa.Package, error)

CreateTestMainPackage creates and returns a synthetic "testmain" package for the specified package if it defines tests, benchmarks or executable examples, or nil otherwise. The new package is named "main" and provides a function named "main" that runs the tests, similar to the one that would be created by the 'go test' tool.

Subsequent calls to prog.AllPackages include the new package. The package pkg must belong to the program prog.

Deprecated: Use golang.org/x/tools/go/packages to access synthetic testmain packages.

func Field

func Field(v interface{}, index int) (interface{}, error)

func FieldAddr

func FieldAddr(v interface{}, index int) (interface{}, error)

func FindTests deprecated

func FindTests(pkg *ssa.Package) (tests, benchmarks, examples []*ssa.Function, main *ssa.Function)

FindTests returns the Test, Benchmark, and Example functions (as defined by "go test") defined in the specified package, and its TestMain function, if any.

Deprecated: Use golang.org/x/tools/go/packages to access synthetic testmain packages.

func IntuitiveMethodSet

func IntuitiveMethodSet(T types.Type) []*types.Selection

golang.org/x/tools/go/types/typeutil.IntuitiveMethodSet

func IsConstNil

func IsConstNil(v ssa.Value) bool

func IsNil

func IsNil(v reflect.Value) bool

func PackageList added in v0.7.4

func PackageList() (list []string)

PackageList return all register packages

func ParseBuiltin added in v0.7.4

func ParseBuiltin(fset *token.FileSet, pkg string) (*ast.File, error)

func RegisterCustomBuiltin

func RegisterCustomBuiltin(key string, fn interface{}) error

func RegisterExternal

func RegisterExternal(key string, i interface{})

register external function for no function body

func RegisterFileProcess

func RegisterFileProcess(ext string, fn SourceProcessFunc)

func RegisterPackage

func RegisterPackage(pkg *Package)

RegisterPackage register pkg

func Run

func Run(path string, args []string, mode Mode) (exitCode int, err error)

func RunFile

func RunFile(filename string, src interface{}, args []string, mode Mode) (exitCode int, err error)

func RunTest

func RunTest(path string, args []string, mode Mode) error

func SetValue

func SetValue(v reflect.Value, x reflect.Value)

Types

type Context

type Context struct {
	Loader       Loader                                           // types loader
	FileSet      *token.FileSet                                   // file set
	Mode         Mode                                             // mode
	ParserMode   parser.Mode                                      // parser mode
	BuilderMode  ssa.BuilderMode                                  // ssa builder mode
	Sizes        types.Sizes                                      // types size for package unsafe
	BuildContext build.Context                                    // build context
	Lookup       func(root, path string) (dir string, found bool) // lookup external import
	// contains filtered or unexported fields
}

Context ssa context

func NewContext

func NewContext(mode Mode) *Context

NewContext create a new Context

func (*Context) AddImport added in v0.8.1

func (c *Context) AddImport(path string, filename string, src interface{}) error

func (*Context) AddImportDir added in v0.8.1

func (c *Context) AddImportDir(path string, dir string) error

func (*Context) BuildPackage

func (ctx *Context) BuildPackage(pkg *types.Package, files []*ast.File) (*ssa.Package, *types.Info, error)

func (*Context) ClearOverrideFunction

func (c *Context) ClearOverrideFunction(key string)

ClearOverrideFunction reset override function

func (*Context) IsEvalMode

func (c *Context) IsEvalMode() bool

func (*Context) LoadAstFile

func (c *Context) LoadAstFile(file *ast.File) (*ssa.Package, error)

func (*Context) LoadAstPackage

func (c *Context) LoadAstPackage(apkg *ast.Package) (*ssa.Package, error)

func (*Context) LoadDir

func (c *Context) LoadDir(path string) (pkgs []*ssa.Package, first error)

func (*Context) LoadFile

func (c *Context) LoadFile(filename string, src interface{}) (*ssa.Package, error)

func (*Context) NewInterp

func (c *Context) NewInterp(mainPkg *ssa.Package) (*Interp, error)

func (*Context) ParseFile

func (c *Context) ParseFile(filename string, src interface{}) (*ast.File, error)

func (*Context) Run

func (c *Context) Run(path string, args []string) (exitCode int, err error)

func (*Context) RunFile

func (c *Context) RunFile(filename string, src interface{}, args []string) (exitCode int, err error)

func (*Context) RunFunc

func (c *Context) RunFunc(mainPkg *ssa.Package, fnname string, args ...Value) (ret Value, err error)

func (*Context) RunPkg

func (c *Context) RunPkg(mainPkg *ssa.Package, input string, args []string) (exitCode int, err error)

func (*Context) RunTest

func (c *Context) RunTest(path string, args []string) error

func (*Context) SetDebug

func (c *Context) SetDebug(fn func(*DebugInfo))

func (*Context) SetLeastCallForEnablePool

func (c *Context) SetLeastCallForEnablePool(count int)

SetLeastCallForEnablePool set least call count for enable function pool, default 64

func (*Context) SetOverrideFunction

func (c *Context) SetOverrideFunction(key string, fn interface{})

SetOverrideFunction register external function to override function. match func fullname and signature

func (*Context) SetPrintOutput

func (c *Context) SetPrintOutput(output *bytes.Buffer)

set builtin print/println captured output

func (*Context) TestPkg

func (c *Context) TestPkg(pkgs []*ssa.Package, input string, args []string) error

type DebugInfo

type DebugInfo struct {
	*ssa.DebugRef
	// contains filtered or unexported fields
}

func (*DebugInfo) AsFunc

func (i *DebugInfo) AsFunc() (*types.Func, bool)

func (*DebugInfo) AsVar

func (i *DebugInfo) AsVar() (*types.Var, interface{}, bool)

func (*DebugInfo) Position

func (i *DebugInfo) Position() token.Position

type ExitError added in v0.7.4

type ExitError int

func (ExitError) Error added in v0.7.4

func (r ExitError) Error() string

type FindMethod

type FindMethod interface {
	FindMethod(mtyp reflect.Type, fn *types.Func) func([]reflect.Value) []reflect.Value
}

type Importer

type Importer struct {
	// contains filtered or unexported fields
}

func NewImporter

func NewImporter(ctx *Context) *Importer

func (*Importer) Import

func (i *Importer) Import(path string) (*types.Package, error)

type Interp

type Interp struct {
	// contains filtered or unexported fields
}

State shared between all interpreted goroutines.

func NewInterp

func NewInterp(ctx *Context, mainpkg *ssa.Package) (*Interp, error)

func (*Interp) Exit added in v0.7.4

func (i *Interp) Exit(code int)

func (*Interp) ExitCode

func (i *Interp) ExitCode() int

func (*Interp) FindMethod

func (i *Interp) FindMethod(mtyp reflect.Type, fn *types.Func) func([]reflect.Value) []reflect.Value

func (*Interp) GetConst

func (i *Interp) GetConst(key string) (constant.Value, bool)

func (*Interp) GetFunc

func (i *Interp) GetFunc(key string) (interface{}, bool)

func (*Interp) GetSymbol added in v0.7.4

func (i *Interp) GetSymbol(key string) (m ssa.Member, v interface{}, ok bool)

func (*Interp) GetType

func (i *Interp) GetType(key string) (reflect.Type, bool)

func (*Interp) GetVarAddr

func (i *Interp) GetVarAddr(key string) (interface{}, bool)

func (*Interp) MainPkg added in v0.7.4

func (i *Interp) MainPkg() *ssa.Package

func (*Interp) RunFunc

func (i *Interp) RunFunc(name string, args ...Value) (r Value, err error)

func (*Interp) RunInit

func (i *Interp) RunInit() (err error)

func (*Interp) RunMain

func (i *Interp) RunMain() (exitCode int, err error)

type Loader

type Loader interface {
	Import(path string) (*types.Package, error)
	Installed(path string) (*Package, bool)
	Packages() []*types.Package
	LookupReflect(typ types.Type) (reflect.Type, bool)
	LookupTypes(typ reflect.Type) (types.Type, bool)
	SetImport(path string, pkg *types.Package, load func() error) error
}

Loader types loader interface

func NewTypesLoader

func NewTypesLoader(mode Mode) Loader

NewTypesLoader install package and readonly

type Mode

type Mode uint

Mode is a bitmask of options affecting the interpreter.

const (
	DisableRecover       Mode = 1 << iota // Disable recover() in target programs; show interpreter crash instead.
	DisableCustomBuiltin                  // Disable load custom builtin func
	EnableDumpImports                     // print typesimports
	EnableDumpInstr                       // Print packages & SSA instruction code
	EnableTracing                         // Print a trace of all instructions as they are interpreted.
	EnablePrintAny                        // Enable builtin print for any type ( struct/array )
)

type Package

type Package struct {
	Name          string
	Path          string
	Interfaces    map[string]reflect.Type
	NamedTypes    map[string]reflect.Type
	AliasTypes    map[string]reflect.Type
	Vars          map[string]reflect.Value
	Funcs         map[string]reflect.Value
	TypedConsts   map[string]TypedConst
	UntypedConsts map[string]UntypedConst
	Deps          map[string]string
	// contains filtered or unexported fields
}

func LookupPackage

func LookupPackage(name string) (pkg *Package, ok bool)

LookupPackage lookup register pkgs

type Repl

type Repl struct {
	// contains filtered or unexported fields
}

func NewRepl

func NewRepl(ctx *Context) *Repl

func (*Repl) Eval

func (r *Repl) Eval(expr string) (tok token.Token, dump []string, err error)

func (*Repl) Interp added in v0.7.4

func (r *Repl) Interp() *Interp

func (*Repl) SetFileName

func (r *Repl) SetFileName(filename string)

func (*Repl) Source

func (r *Repl) Source() string

type SourceProcessFunc

type SourceProcessFunc func(ctx *Context, filename string, src interface{}) ([]byte, error)

type Tuple

type Tuple = tuple

type TypedConst

type TypedConst struct {
	Typ   reflect.Type
	Value constant.Value
}

type TypesLoader

type TypesLoader struct {
	// contains filtered or unexported fields
}

func (*TypesLoader) GetPackage

func (r *TypesLoader) GetPackage(pkg string) *types.Package

func (*TypesLoader) Import

func (r *TypesLoader) Import(path string) (*types.Package, error)

func (*TypesLoader) Insert

func (r *TypesLoader) Insert(v reflect.Value)

func (*TypesLoader) InsertAlias

func (r *TypesLoader) InsertAlias(p *types.Package, name string, rt reflect.Type)

func (*TypesLoader) InsertConst

func (r *TypesLoader) InsertConst(p *types.Package, name string, typ types.Type, c constant.Value)

func (*TypesLoader) InsertFunc

func (r *TypesLoader) InsertFunc(p *types.Package, name string, v reflect.Value)

func (*TypesLoader) InsertInterface

func (r *TypesLoader) InsertInterface(p *types.Package, name string, rt reflect.Type)

func (*TypesLoader) InsertNamedType

func (r *TypesLoader) InsertNamedType(p *types.Package, name string, rt reflect.Type)

func (*TypesLoader) InsertTypedConst

func (r *TypesLoader) InsertTypedConst(p *types.Package, name string, v TypedConst)

func (*TypesLoader) InsertUntypedConst

func (r *TypesLoader) InsertUntypedConst(p *types.Package, name string, v UntypedConst)

func (*TypesLoader) InsertVar

func (r *TypesLoader) InsertVar(p *types.Package, name string, v reflect.Value)

func (*TypesLoader) Installed

func (r *TypesLoader) Installed(path string) (pkg *Package, ok bool)

func (*TypesLoader) LookupPackage

func (r *TypesLoader) LookupPackage(pkgpath string) (*types.Package, bool)

func (*TypesLoader) LookupReflect

func (r *TypesLoader) LookupReflect(typ types.Type) (reflect.Type, bool)

func (*TypesLoader) LookupType

func (r *TypesLoader) LookupType(typ string) types.Type

func (*TypesLoader) LookupTypes

func (r *TypesLoader) LookupTypes(typ reflect.Type) (types.Type, bool)

func (*TypesLoader) Packages

func (r *TypesLoader) Packages() (pkgs []*types.Package)

func (*TypesLoader) SetImport added in v0.8.1

func (r *TypesLoader) SetImport(path string, pkg *types.Package, load func() error) error

func (*TypesLoader) ToType

func (r *TypesLoader) ToType(rt reflect.Type) types.Type

type TypesRecord

type TypesRecord struct {
	// contains filtered or unexported fields
}

func NewTypesRecord

func NewTypesRecord(loader Loader, finder FindMethod) *TypesRecord

func (*TypesRecord) Load

func (r *TypesRecord) Load(pkg *ssa.Package)

func (*TypesRecord) LoadType

func (r *TypesRecord) LoadType(typ types.Type) reflect.Type

func (*TypesRecord) LookupLocalTypes

func (r *TypesRecord) LookupLocalTypes(rt reflect.Type) (typ types.Type, ok bool)

func (*TypesRecord) LookupReflect

func (r *TypesRecord) LookupReflect(typ types.Type) (rt reflect.Type, ok bool)

func (*TypesRecord) LookupTypes

func (r *TypesRecord) LookupTypes(rt reflect.Type) (typ types.Type, ok bool)

func (*TypesRecord) ToType

func (r *TypesRecord) ToType(typ types.Type) reflect.Type

func (*TypesRecord) ToTypeList

func (r *TypesRecord) ToTypeList(tuple *types.Tuple) []reflect.Type

type UntypedConst

type UntypedConst struct {
	Typ   string
	Value constant.Value
}

type Value

type Value = value

Directories

Path Synopsis
cmd
internal/base
Package base defines shared xtype pieces of the gop command, in particular logging and the Command structure.
Package base defines shared xtype pieces of the gop command, in particular logging and the Command structure.
internal/help
Package help implements the “igop help” command.
Package help implements the “igop help” command.
internal/run
Package run implements the “gop run” command.
Package run implements the “gop run” command.
internal/test
Package test implements the “igop test” command.
Package test implements the “igop test” command.
pkg
fmt
io
log
net
os

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL