github.com/lsg2020/go-forceexport@v1.0.5/forceexport.go (about)

     1  package forceexport
     2  
     3  import (
     4  	go114 "github.com/lsg2020/go-forceexport/go114"
     5  	go116 "github.com/lsg2020/go-forceexport/go116"
     6  	"regexp"
     7  	"runtime"
     8  )
     9  
    10  var GoVersion string
    11  
    12  func init() {
    13  	reg := regexp.MustCompile(`go\d+\.\d+`)
    14  	GoVersion = reg.FindString(runtime.Version())
    15  }
    16  
    17  // GetFunc gets the function defined by the given fully-qualified name. The
    18  // outFuncPtr parameter should be a pointer to a function with the appropriate
    19  // type (e.g. the address of a local variable), and is set to a new function
    20  // value that calls the specified function. If the specified function does not
    21  // exist, outFuncPtr is not set and an error is returned.
    22  func GetFunc(outFuncPtr interface{}, name string) (err error) {
    23  	switch GoVersion {
    24  	case "go1.14", "go1.15":
    25  		err = go114.GetFunc(outFuncPtr, name)
    26  	case "go1.16":
    27  		err = go116.GetFunc(outFuncPtr, name)
    28  	default:
    29  		panic("Not suitable for " + GoVersion)
    30  	}
    31  	return
    32  }
    33  
    34  // CreateFuncForCodePtr is given a code pointer and creates a function value
    35  // that uses that pointer. The outFun argument should be a pointer to a function
    36  // of the proper type (e.g. the address of a local variable), and will be set to
    37  // the result function value.
    38  func CreateFuncForCodePtr(outFuncPtr interface{}, codePtr uintptr) {
    39  	switch GoVersion {
    40  	case "go1.14", "go1.15":
    41  		go114.CreateFuncForCodePtr(outFuncPtr, codePtr)
    42  	case "go1.16":
    43  		go116.CreateFuncForCodePtr(outFuncPtr, codePtr)
    44  	default:
    45  		panic("Not suitable for " + GoVersion)
    46  	}
    47  }
    48  
    49  // FindFuncWithName searches through the moduledata table created by the linker
    50  // and returns the function's code pointer. If the function was not found, it
    51  // returns an error. Since the data structures here are not exported, we copy
    52  // them below (and they need to stay in sync or else things will fail
    53  // catastrophically).
    54  func FindFuncWithName(name string) (p uintptr, err error) {
    55  	switch GoVersion {
    56  	case "go1.14", "go1.15":
    57  		p, err = go114.FindFuncWithName(name)
    58  	case "go1.16":
    59  		p, err = go116.FindFuncWithName(name)
    60  	default:
    61  		panic("Not suitable for " + GoVersion)
    62  	}
    63  	return
    64  }
    65  
    66  // GetAllFuncName returns all function names from the moduledata table. It's used for debug.
    67  // When you got
    68  func GetAllFuncName() (names []string) {
    69  	switch GoVersion {
    70  	case "go1.14", "go1.15":
    71  		names = go114.GetAllFuncName()
    72  	case "go1.16":
    73  		names = go116.GetAllFuncName()
    74  	default:
    75  		panic("Not suitable for " + GoVersion)
    76  	}
    77  	return
    78  }