-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload.go
More file actions
75 lines (65 loc) · 1.7 KB
/
load.go
File metadata and controls
75 lines (65 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package golinker
import (
"fmt"
"reflect"
"sync"
"unsafe"
"github.com/pkujhd/goloader"
)
func TypeOf(i interface{}) reflect.Type {
return reflect.TypeOf(i)
}
type Ptr = unsafe.Pointer
type CodeModule = goloader.CodeModule
// Var represents an exported package-level variable (of pointer type).
type Var struct {
// The variable's name.
// eg. var GlobalVar *string
Name string
// Ptr is the unsafe.Pointer of the pointer-type variable.
// eg. Ptr(&GlobalVar)
Ptr Ptr
}
// Load returns a function that lazy-loads a CodeModule specific to a package.
// It should be stored in a variable in the package. It will always return the same
// CodeModule. The CodeModule must not be unloaded unless you know that you will
// never use the package again. The CodeModule is derived from the Linker.
//
// ptrs represents package-level variables which will be initialized to point
// to the equivalent variable in the "backing-package".
func Load(fullPackageName string, pattern string, ptrs ...Var) func() *CodeModule {
var (
once sync.Once
valid bool
p interface{}
result *CodeModule
)
g := func() {
defer func() {
p = recover()
if !valid {
panic(p)
}
}()
result = func() *CodeModule {
codeModule, err := goloader.Load(linker(), symPtr)
if err != nil {
panic(fmt.Sprintf(`%s: %s: Load error: %s`, pkgname, fullPackageName, err.Error()))
}
for _, p := range ptrs {
name := fullPackageName + "." + fmt.Sprintf(pattern, p.Name)
q := (*(*func() unsafe.Pointer)(SymbolPtr(name, codeModule)))()
*(*unsafe.Pointer)(p.Ptr) = q
}
return codeModule
}()
valid = true
}
return func() *CodeModule {
once.Do(g)
if !valid {
panic(p)
}
return result
}
}