-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
29 lines (25 loc) · 830 Bytes
/
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
package script
import (
"fmt"
"reflect"
)
type function struct {
reflect.Value
}
func newFunction(fn interface{}) *function {
val := reflect.ValueOf(fn)
if val.Type().Kind() != reflect.Func {
panic(fmt.Errorf("not a function: %v", val.Type()))
}
if val.Type().NumOut() > 1 {
panic(fmt.Errorf("multiple return values not allowed: %v", val.Type()))
}
return &function{val}
}
// type of the function itself (when not called)
func (f *function) Type() reflect.Type { return f.Value.Type() }
func (f *function) NumIn() int { return f.Type().NumIn() }
func (f *function) In(i int) reflect.Type { return f.Type().In(i) }
func (f *function) Eval() interface{} { return f.Value.Interface() }
func (f *function) Child() []Expr { return nil }
func (f *function) Fix() Expr { return f }