ixgo

package module
v0.52.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2025 License: Apache-2.0 Imports: 38 Imported by: 1

README ¶

iXGo The Go/XGo Interpreter

Go1.18 Go1.19 Go1.20 Go1.21 Go1.22 Go1.23 Go1.24 Go Reference

Go Version
  • Go1.18 ~ Go1.24
  • macOS Linux Windows WebAssembly GopherJS and more.
ABI

support ABI0 and ABIInternal

  • ABI0 stack-based ABI

  • ABIInternal register-based Go calling convention proposal

    • Go1.17: amd64
    • Go1.18: amd64 arm64 ppc64/ppc64le
    • Go1.19: amd64 arm64 ppc64/ppc64le riscv64
    • Go1.20: amd64 arm64 ppc64/ppc64le riscv64
    • Go1.21: amd64 arm64 ppc64/ppc64le riscv64
    • Go1.22: amd64 arm64 ppc64/ppc64le riscv64 loong64
    • Go1.23: amd64 arm64 ppc64/ppc64le riscv64 loong64
    • Go1.24: amd64 arm64 ppc64/ppc64le riscv64 loong64
Generics
  • support typeparams (Go1.18 ~ Go1.24)
  • support alias typeparams (Go1.24)
runtime.GC

ixgo.Mode ExperimentalSupportGC

experimental support runtime.GC and runtime.SetFinalizer

install ixgo command line

Go version <= 1.22:

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

Go version >= 1.23

go install -ldflags="-checklinkname=0" github.com/goplus/ixgo/ixgo@v0.3.0
install ixgo export command line
go install github.com/goplus/ixgo/cmd/qexp@latest
ixgo command
ixgo             # ixgo repl mode
ixgo run         # run a Go/XGo package
ixgo build       # compile a Go/XGo package
ixgo test        # test a package
ixgo verson      # print version
ixgo export      # export Go package to ixgo builtin package
ixgo run mode
Usage: ixgo run [build flags] [package] [arguments...]
  -exp-gc
    	experimental support runtime.GC
  -mod value
    	module download mode to use: readonly, vendor, or mod.
  -ssa
    	print SSA instruction code
  -ssa-trace
    	trace SSA interpreter code
  -tags value
    	a comma-separated list of build tags to consider satisfied during the build
  -v	print the names of packages as they are compiled.
  -x	print the commands.
ixgo repl mode
ixgo                       # run repl mode, support Go/XGo
ixgo repl                  # run repl mode, support Go/XGo
ixgo repl -go              # run repl mode, disable XGo syntax
ixgo test unsupport features
  • test -fuzz
  • test -cover
ixgo demo
The XGo Playground (WebAssembly)
The XGo REPL Playground (WebAssembly)
ispx

https://github.com/goplus/ispx

run simple Go source demo
package main

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

var source = `
package main

import "fmt"

func main() {
	fmt.Println("Hello, World")
}
`

func main() {
	_, err := ixgo.RunFile("main.go", source, nil, 0)
	if err != nil {
		panic(err)
	}
}
run simple XGo source demo
package main

import (
	"github.com/goplus/ixgo"
	_ "github.com/goplus/ixgo/gopbuild"
)

var source = `
fields := [
	"engineering",
	"STEM education", 
	"and data science",
]

echo "The XGo language for", fields.join(", ")
`

func main() {
	_, err := ixgo.RunFile("main.xgo", source, nil, 0)
	if err != nil {
		panic(err)
	}
}
ixgo more demo

https://github.com/visualfc/ixgo_demo

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

View Source
var (
	ErrNotFoundMain    = errors.New("not found main package")
	ErrTestFailed      = errors.New("test failed")
	ErrNotFoundPackage = errors.New("not found package")
	ErrGoexitDeadlock  = errors.New("fatal error: no goroutines (main called runtime.Goexit) - deadlock!")
	ErrNoFunction      = errors.New("no function")
	ErrNoTestFiles     = errors.New("[no test files]")
)

Functions ¶

func IcallStat ¶

func IcallStat() (capacity int, allocate int, aviable int)

IcallStat return reflectx icall allocate stat

func IsConstNil ¶

func IsConstNil(v ssa.Value) bool

func IsNil ¶

func IsNil(v reflect.Value) bool

func PackageList ¶

func PackageList() (list []string)

PackageList return all register packages

func ParseBuiltin ¶

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{})

RegisterExternal is register external variable address or func

func RegisterFileProcess ¶

func RegisterFileProcess(ext string, fn SourceProcessFunc)

func RegisterPackage ¶

func RegisterPackage(pkg *Package)

RegisterPackage register pkg

func RegisterPatch ¶

func RegisterPatch(path string, src interface{})

RegisterPatch register path with "pkg@patch"

func ResetAllIcall ¶

func ResetAllIcall()

ResetAllIcall is reset all reflectx icall, all interp methods invalid.

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
	BuildContext build.Context   // build context, default build.Default
	RunContext   context.Context // run context, default unset

	FileSet *token.FileSet // file set

	Lookup func(root, path string) (dir string, found bool) // lookup external import

	Mode        Mode            // mode
	BuilderMode ssa.BuilderMode // ssa builder mode
	// contains filtered or unexported fields
}

Context ssa context

func NewContext ¶

func NewContext(mode Mode) *Context

NewContext create a new Context

func (*Context) AddImport ¶

func (ctx *Context) AddImport(path string, dir string) (err error)

func (*Context) AddImportFile ¶

func (ctx *Context) AddImportFile(path string, filename string, src interface{}) (err error)

func (*Context) IsEvalMode ¶

func (ctx *Context) IsEvalMode() bool

func (*Context) LoadAstFile ¶

func (ctx *Context) LoadAstFile(path string, file *ast.File) (*ssa.Package, error)

func (*Context) LoadAstPackage ¶

func (ctx *Context) LoadAstPackage(path string, apkg *ast.Package) (*ssa.Package, error)

func (*Context) LoadDir ¶

func (ctx *Context) LoadDir(dir string, test bool) (pkg *ssa.Package, err error)

func (*Context) LoadDirEx ¶

func (ctx *Context) LoadDirEx(dir string, test bool, getImportPath func(pkgName string, dir string) (string, bool)) (pkg *ssa.Package, err error)

func (*Context) LoadFS ¶

func (ctx *Context) LoadFS(fsys fs.FS, test bool, getImportPath func(pkgName string, dir string) (string, bool)) (pkg *ssa.Package, err error)

func (*Context) LoadFile ¶

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

func (*Context) LoadFileSystem ¶

func (ctx *Context) LoadFileSystem(fsys FileSystem, test bool, getImportPath func(pkgName string, dir string) (string, bool)) (pkg *ssa.Package, err error)

func (*Context) LoadInterp ¶

func (ctx *Context) LoadInterp(filename string, src interface{}) (*Interp, error)

func (*Context) NewInterp ¶

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

func (*Context) ParseFile ¶

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

func (*Context) RegisterExternal ¶

func (ctx *Context) RegisterExternal(key string, i interface{})

RegisterExternal register external value must variable address or func.

func (*Context) Run ¶

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

func (*Context) RunFile ¶

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

func (*Context) RunFunc ¶

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

func (*Context) RunInterp ¶

func (ctx *Context) RunInterp(interp *Interp, input string, args []string) (exitCode int, err error)

func (*Context) RunPkg ¶

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

func (*Context) RunTest ¶

func (ctx *Context) RunTest(dir string, args []string) error

func (*Context) SetDebug ¶

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

func (*Context) SetEvalMode ¶

func (ctx *Context) SetEvalMode(b bool)

func (*Context) SetLeastCallForEnablePool ¶

func (ctx *Context) SetLeastCallForEnablePool(count int)

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

func (*Context) SetPanic ¶

func (ctx *Context) SetPanic(fn func(*PanicInfo))

func (*Context) SetPrintOutput ¶

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

SetPrintOutput is captured builtin print/println output

func (*Context) SetUnsafeSizes ¶

func (ctx *Context) SetUnsafeSizes(sizes types.Sizes)

SetUnsafeSizes set the sizing functions for package unsafe.

func (*Context) SourcePackage ¶

func (ctx *Context) SourcePackage(path string) *SourcePackage

func (*Context) TestPkg ¶

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

func (*Context) UnsafeRelease ¶

func (ctx *Context) UnsafeRelease()

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 Eval ¶

type Eval struct {
	Value interface{} // constant.Value or interface{}
	Type  reflect.Type
}

func (*Eval) String ¶

func (e *Eval) String() string

type ExitError ¶

type ExitError int

func (ExitError) Error ¶

func (r ExitError) Error() string

type FatalError ¶

type FatalError struct {
	Value value
	// contains filtered or unexported fields
}

run func fatal error

func (FatalError) Error ¶

func (p FatalError) Error() string

func (FatalError) Stack ¶

func (p FatalError) Stack() []byte

type FileSystem ¶

type FileSystem interface {
	ReadDir(dirname string) ([]fs.DirEntry, error)
	ReadFile(filename string) ([]byte, error)
	Join(elem ...string) string

	// Base returns the last element of path.
	// Trailing path separators are removed before extracting the last element.
	// If the path is empty, Base returns ".".
	// If the path consists entirely of separators, Base returns a single separator.
	Base(filename string) string

	// Abs returns an absolute representation of path.
	Abs(path string) (string, error)
}

FileSystem represents a file system.

type FindMethod ¶

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

type Frame ¶

type Frame = frame

func (*Frame) CallerFrames ¶

func (fr *Frame) CallerFrames() (frames []runtime.Frame)

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
}

func NewInterp ¶

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

func (*Interp) Abort ¶

func (i *Interp) Abort()

func (*Interp) Exit ¶

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 ¶

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) IcallAlloc ¶

func (i *Interp) IcallAlloc() int

icall allocate

func (*Interp) MainPkg ¶

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

func (*Interp) ResetIcall ¶

func (i *Interp) ResetIcall()

ResetIcall is reset reflectx icall, all methods invalid.

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)

func (*Interp) UnsafeRelease ¶

func (i *Interp) UnsafeRelease()

UnsafeRelease is unsafe release interp. interp all invalid.

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(ctx *Context, 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 import packages
	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 )
	EnableNoStrict                                  // Enable no strict mode
	ExperimentalSupportGC                           // experimental support runtime.GC
	SupportMultipleInterp                           // Support multiple interp, must manual release interp reflectx icall.
	CheckGopOverloadFunc                            // Check and skip gop overload func
	DisableAutoLoadPatchs                           // Disable automatic loading of package patches.
	DisableDynamicFuncCallAnalysis                  // Disable dynamic func call analysis and optimization.
	OptionLoadRutimeImethod                         // Option load runtime imethod for less imethod and memory space.
	OptionLoadAllImethod                            // Option load all imethod.
	OptionLoadDefaultImethod       = 0              // Option load default imethod.
)

type Package ¶

type Package struct {
	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 // path -> name
	Name          string
	Path          string
	Source        string
}

func LookupPackage ¶

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

LookupPackage lookup register pkgs

type PanicError ¶

type PanicError struct {
	Value value
	// contains filtered or unexported fields
}

If the target program panics, the interpreter panics with this type.

func (PanicError) Error ¶

func (p PanicError) Error() string

func (PanicError) Stack ¶

func (p PanicError) Stack() []byte

type PanicInfo ¶

type PanicInfo struct {
	*Frame

	Error error // PanicError / FatalError / PlainError / RuntimeError
	// contains filtered or unexported fields
}

func (*PanicInfo) Position ¶

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

type PlainError ¶

type PlainError string

func (PlainError) Error ¶

func (e PlainError) Error() string

func (PlainError) RuntimeError ¶

func (e PlainError) RuntimeError()

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 []*Eval, err error)

func (*Repl) Interp ¶

func (r *Repl) Interp() *Interp

func (*Repl) SetFileName ¶

func (r *Repl) SetFileName(filename string)

func (*Repl) Source ¶

func (r *Repl) Source() string

type RuntimeError ¶

type RuntimeError string

func (RuntimeError) Error ¶

func (e RuntimeError) Error() string

func (RuntimeError) RuntimeError ¶

func (e RuntimeError) RuntimeError()

type SourcePackage ¶

type SourcePackage struct {
	Context  *Context
	Package  *types.Package
	Info     *types.Info
	Importer types.Importer
	Files    []*ast.File
	Links    []*load.LinkSym
	Dir      string
	Register bool // register package
}

func (*SourcePackage) Load ¶

func (sp *SourcePackage) Load() (err error)

func (*SourcePackage) Loaded ¶

func (sp *SourcePackage) Loaded() bool

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 ¶

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(rctx *reflectx.Context, loader Loader, finder FindMethod, nested map[*types.Named]int) *TypesRecord

func (*TypesRecord) EnterInstance ¶

func (r *TypesRecord) EnterInstance(fn *ssa.Function)

func (*TypesRecord) LeaveInstance ¶

func (r *TypesRecord) LeaveInstance(fn *ssa.Function)

func (*TypesRecord) Load ¶

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

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, nested bool)

func (*TypesRecord) LookupTypes ¶

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

func (*TypesRecord) Release ¶

func (r *TypesRecord) Release()

func (*TypesRecord) ToType ¶

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

func (*TypesRecord) ToTypeList ¶

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

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/build
Package build implements the “ixgo build” command.
Package build implements the “ixgo build” command.
internal/help
Package help implements the “ixgo help” command.
Package help implements the “ixgo help” command.
internal/run
Package run implements the “gop run” command.
Package run implements the “gop run” command.
internal/test
Package test implements the “ixgo test” command.
Package test implements the “ixgo test” command.
internal/version
Package build implements the “ixgo version” command.
Package build implements the “ixgo version” command.
internal
ixgo module
pkg
_go123/iter
Package iter provides basic definitions and operations related to iterators over sequences.
Package iter provides basic definitions and operations related to iterators over sequences.
_go124/iter
Package iter provides basic definitions and operations related to iterators over sequences.
Package iter provides basic definitions and operations related to iterators over sequences.
cmp
fmt
io
log
net
os
x
race
Package race contains helper functions for manually instrumenting code for the race detector.
Package race contains helper functions for manually instrumenting code for the race detector.
testdeps
Package testdeps provides access to dependencies needed by test execution.
Package testdeps provides access to dependencies needed by test execution.
testlog
Package testlog provides a back-channel communication path between tests and package os, so that cmd/go can see which environment variables and files a test consults.
Package testlog provides a back-channel communication path between tests and package os, so that cmd/go can see which environment variables and files a test consults.
pkg

Jump to

Keyboard shortcuts

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