trap

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 14 Imported by: 4

README

About

Trap is the core the xgo runtime.

When a function is called, it's control flow will first be transferred to Trap.

Then Trap check if there is any interceptor, including system defined interceptors like Mock and Trace, as well as user defined interceptors by calling Trap.AddInterceptor().

If any, it will then forward call to these interceptors, until all interceptors returned, or some interceptor returns trap.ErrAbort in the middle.

Inspect(f)

the trap.Inspect(fn) implements a way to retrieve func info. It has different internal paths for these function types:

  • package level function
    • PC lookup
  • struct method
    • -fm suffix check
    • struct.method proto existence check
    • dynamic retrieval
  • interface method
    • -fm suffix check
    • interface proto existence check
    • dynamic retrieval
  • closure
    • closure is registered using IR
    • PC lookup
  • generic function
    • fullName parsing to de-generic
    • generic template existence check
    • no dynamic retrieval since no recv, but is legal to do so
  • generic struct method
    • -fm suffix check
    • fullName parsing to de-generic
    • generic method template existence check
    • dynamic retrieval just for receiver
  • generic interface method
    • to be supported

Documentation

Index

Constants

View Source
const GenericImplIsClosure = false

Variables

View Source
var ErrAbort error = errors.New("abort trap interceptor")

Functions

func AddInterceptor

func AddInterceptor(interceptor *Interceptor) func()

func ClearLocalInterceptors

func ClearLocalInterceptors()

func GetTrappingPC added in v1.0.10

func GetTrappingPC() uintptr

func Inspect added in v1.0.8

func Inspect(f interface{}) (recvPtr interface{}, funcInfo *core.FuncInfo)

Inspect make a call to f to capture its receiver pointer if is is bound method It can be used to get the unwrapped innermost function of a method wrapper. if f is a bound method, then guranteed that recvPtr cannot be nil

func InspectPC added in v1.0.10

func InspectPC(f interface{}) (recvPtr interface{}, funcInfo *core.FuncInfo, funcPC uintptr, trappingPC uintptr)

func Skip

func Skip()

Skip serves as mark to tell xgo not insert trap instructions for the function that calls Skip() NOTE: the function body is intenionally leave empty as trap.Skip() is just a mark that makes sense at compile time.

func WithInterceptor

func WithInterceptor(interceptor *Interceptor, f func())

WithInterceptor executes given f with interceptor setup. It can be used from init phase safely. it clears the interceptor after f finishes. the interceptor will be added to head, so it will gets firstly invoked. f cannot be nil.

NOTE: the implementation uses addLocalInterceptor even from init because it will be soon cleared without causing concurrent issues.

Types

type Interceptor

type Interceptor struct {
	Pre  func(ctx context.Context, f *core.FuncInfo, args core.Object, result core.Object) (data interface{}, err error)
	Post func(ctx context.Context, f *core.FuncInfo, args core.Object, result core.Object, data interface{}) error
}

func GetAllInterceptors

func GetAllInterceptors() []*Interceptor

func GetInterceptors

func GetInterceptors() []*Interceptor

func GetLocalInterceptors

func GetLocalInterceptors() []*Interceptor

Jump to

Keyboard shortcuts

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