interp

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2021 License: Apache-2.0 Imports: 31 Imported by: 0

README

interp - Golang SSA interpreter

Go1.14 Go1.15 Go1.16

Go1.17

set env

GOEXPERIMENT=noregabi

igop command line
go get -u github.com/goplus/interp/cmd/igop

Commands

igop run         # interpret package [gop|go]
igop test        # test package [gop|go]
interp package

run gop source

package main

import (
	"github.com/goplus/interp"
	_ "github.com/goplus/interp/pkg"
)

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

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

run go source

package main

import (
	"github.com/goplus/interp"
	_ "github.com/goplus/interp/pkg"
)

var source = `
package main

import "fmt"

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

func main() {
	err := interp.RunFile(0,"main.go", source, nil)
	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")
)
View Source
var CapturedOutput *bytes.Buffer

If CapturedOutput is non-nil, all writes by the interpreted program to file descriptors 1 and 2 will also be written to CapturedOutput.

(The $GOROOT/test system requires that the test be considered a failure if "BUG" appears in the combined stdout/stderr output, even if it exits zero. This is a global variable shared by all interpreters in the same process.)

View Source
var (
	UnsafeSizes types.Sizes
)

Functions

func CreateTestMainPackage deprecated added in v0.1.4

func CreateTestMainPackage(pkg *ssa.Package) *ssa.Package

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 Interpret

func Interpret(mainpkg *ssa.Package, mode Mode, entry string) (exitCode int)

Interpret interprets the Go program whose main package is mainpkg. mode specifies various interpreter options. filename and args are the initial values of os.Args for the target program. sizes is the effective type-sizing function for this program.

Interpret returns the exit code of the program: 2 for panic (like gc does), or the argument to os.Exit for normal termination.

The SSA program must include the "runtime" package.

func IsConstNil

func IsConstNil(v ssa.Value) bool

func IsNil

func IsNil(v reflect.Value) bool

func LoadTest added in v0.1.4

func LoadTest(input string) (string, []*ssa.Package, error)

func RegisterExternal

func RegisterExternal(key string, i interface{})

register external interface

func RegisterExternals

func RegisterExternals(m map[string]interface{})

register external interface map

func RegisterFuncs added in v0.1.4

func RegisterFuncs(funcs ...interface{})

func RegisterPackage added in v0.1.4

func RegisterPackage(pkg string, ifacemap map[string]interface{}, typelist []interface{})

register extern package

func RegisterType

func RegisterType(key string, typ reflect.Type)

register external type

func RegisterTypeOf

func RegisterTypeOf(ptrs ...interface{})

register external type list

func Run

func Run(mode Mode, input string, args []string) error

func RunFile added in v0.1.4

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

func RunPkg added in v0.1.4

func RunPkg(mainPkg *ssa.Package, mode Mode, input string, entry string, args []string) error

func RunTest added in v0.1.4

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

func RunTestPkg added in v0.1.4

func RunTestPkg(pkgs []*ssa.Package, mode Mode, input string, args []string)

func SetValue

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

Types

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.
	EnableTracing                    // Print a trace of all instructions as they are interpreted.
	EnableDumpInstr                  //Print instr type & value
)

Directories

Path Synopsis
cmd
internal/base
Package base defines shared basic pieces of the gop command, in particular logging and the Command structure.
Package base defines shared basic 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.
internal
pkg
fmt
io
log
net
os

Jump to

Keyboard shortcuts

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