/
code_block_function.go
87 lines (78 loc) · 2.12 KB
/
code_block_function.go
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
76
77
78
79
80
81
82
83
84
85
86
87
package compiler
import "reflect"
// FunctionInfo contains the function information.
type FunctionInfo struct {
Id uint32
Name string
Params []Token
Results []Token
// tail function
Tails map[string]FuncTail
Variadic bool
CanWrite bool // If the function can update DB
}
// ParamsCount returns the number of parameters of the function.
func (e *FunctionInfo) ParamsCount() int {
count := 0
for i := 0; i < len(e.Params); i++ {
count++
}
return count
}
// HasTails returns whether the function has tails function.
func (e *FunctionInfo) HasTails() bool {
return e != nil && e.Tails != nil
}
// FuncTail contains the tail function information
type FuncTail struct {
Name string
Params []Token
Offset []int
Variadic bool
}
// IsParamEmpty checks if the parameter at the given index is empty (nil).
func (f FuncTail) IsParamEmpty(i int) bool {
return f.Params[i] == UNKNOWN
}
// ExtFuncInfo is the structure for the extended golang function.
type ExtFuncInfo struct {
Name string
Params []reflect.Type
Results []reflect.Type
Auto []string
Variadic bool
Func any
CanWrite bool // If the function can update DB
}
// Call executes the function with the provided parameters.
// It takes a slice of any type as an argument which represents the parameters to be passed to the function.
// It returns a slice of any type which represents the return values of the function.
func (e *ExtFuncInfo) Call(params []any) (ret []any) {
foo := reflect.ValueOf(e.Func)
var result []reflect.Value
if e.Variadic {
pars := make([]reflect.Value, len(e.Params))
pars[len(pars)-1] = reflect.ValueOf(params[len(pars)-1:])
result = foo.CallSlice(pars)
} else {
pars := make([]reflect.Value, len(params))
for i := 0; i < len(params); i++ {
pars[i] = reflect.ValueOf(params[i])
}
result = foo.Call(pars)
}
for _, value := range result {
ret = append(ret, value.Interface())
}
return ret
}
// AutoParamsCount returns the number of auto params.
func (e *ExtFuncInfo) AutoParamsCount() int {
count := 0
for i := 0; i < len(e.Params); i++ {
if len(e.Auto[i]) > 0 {
count++
}
}
return count
}