2013-10-01 21:34:49 +08:00
|
|
|
package eval
|
|
|
|
|
2019-04-19 05:57:14 +08:00
|
|
|
// Misc builtin functions.
|
2014-04-02 10:51:09 +08:00
|
|
|
|
2013-10-02 22:10:07 +08:00
|
|
|
import (
|
2020-08-18 13:40:30 +08:00
|
|
|
"errors"
|
2014-01-16 09:24:14 +08:00
|
|
|
"fmt"
|
2016-11-06 17:47:20 +08:00
|
|
|
"net"
|
2020-08-17 06:02:51 +08:00
|
|
|
"sync"
|
2016-02-14 04:05:35 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/diag"
|
2021-12-29 09:16:44 +08:00
|
|
|
"src.elv.sh/pkg/eval/errs"
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/eval/vals"
|
|
|
|
"src.elv.sh/pkg/parse"
|
2013-10-02 22:10:07 +08:00
|
|
|
)
|
|
|
|
|
2021-05-22 11:51:03 +08:00
|
|
|
var (
|
2022-11-20 23:06:45 +08:00
|
|
|
ErrNegativeSleepDuration = errors.New("sleep duration must be >= zero")
|
|
|
|
ErrInvalidSleepDuration = errors.New("invalid sleep duration")
|
2021-05-22 11:51:03 +08:00
|
|
|
)
|
|
|
|
|
2017-12-17 13:20:03 +08:00
|
|
|
// Builtins that have not been put into their own groups go here.
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
func init() {
|
2022-03-20 23:50:25 +08:00
|
|
|
addBuiltinFns(map[string]any{
|
2020-08-17 01:46:29 +08:00
|
|
|
"kind-of": kindOf,
|
|
|
|
"constantly": constantly,
|
|
|
|
|
2022-01-04 02:29:48 +08:00
|
|
|
// Introspection
|
|
|
|
"call": call,
|
2020-08-17 01:46:29 +08:00
|
|
|
"resolve": resolve,
|
2020-08-17 06:02:51 +08:00
|
|
|
"eval": eval,
|
2020-08-17 07:11:25 +08:00
|
|
|
"use-mod": useMod,
|
2020-08-17 01:46:29 +08:00
|
|
|
|
2021-01-24 23:32:24 +08:00
|
|
|
"deprecate": deprecate,
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
"-ifaddrs": _ifaddrs,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-08 03:20:00 +08:00
|
|
|
var nopGoFn = NewGoFn("nop", nop)
|
|
|
|
|
2022-03-20 23:50:25 +08:00
|
|
|
func nop(opts RawOptions, args ...any) {
|
2020-08-17 01:46:29 +08:00
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2022-03-20 23:50:25 +08:00
|
|
|
func kindOf(fm *Frame, args ...any) error {
|
2021-06-18 07:11:51 +08:00
|
|
|
out := fm.ValueOutput()
|
2020-08-17 01:46:29 +08:00
|
|
|
for _, a := range args {
|
2021-06-18 07:11:51 +08:00
|
|
|
err := out.Put(vals.Kind(a))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-17 01:46:29 +08:00
|
|
|
}
|
2021-06-18 07:11:51 +08:00
|
|
|
return nil
|
2020-08-17 01:46:29 +08:00
|
|
|
}
|
|
|
|
|
2022-03-20 23:50:25 +08:00
|
|
|
func constantly(args ...any) Callable {
|
2020-08-17 01:46:29 +08:00
|
|
|
// TODO(xiaq): Repr of this function is not right.
|
|
|
|
return NewGoFn(
|
|
|
|
"created by constantly",
|
2021-06-18 07:11:51 +08:00
|
|
|
func(fm *Frame) error {
|
|
|
|
out := fm.ValueOutput()
|
2020-08-17 01:46:29 +08:00
|
|
|
for _, v := range args {
|
2021-06-18 07:11:51 +08:00
|
|
|
err := out.Put(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-17 01:46:29 +08:00
|
|
|
}
|
2021-06-18 07:11:51 +08:00
|
|
|
return nil
|
2020-08-17 01:46:29 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-29 09:16:44 +08:00
|
|
|
func call(fm *Frame, fn Callable, argsVal vals.List, optsVal vals.Map) error {
|
2022-03-20 23:50:25 +08:00
|
|
|
args := make([]any, 0, argsVal.Len())
|
2021-12-29 09:16:44 +08:00
|
|
|
for it := argsVal.Iterator(); it.HasElem(); it.Next() {
|
|
|
|
args = append(args, it.Elem())
|
|
|
|
}
|
2022-03-20 23:50:25 +08:00
|
|
|
opts := make(map[string]any, optsVal.Len())
|
2021-12-29 09:16:44 +08:00
|
|
|
for it := optsVal.Iterator(); it.HasElem(); it.Next() {
|
|
|
|
k, v := it.Elem()
|
|
|
|
ks, ok := k.(string)
|
|
|
|
if !ok {
|
|
|
|
return errs.BadValue{What: "option key",
|
|
|
|
Valid: "string", Actual: vals.Kind(k)}
|
|
|
|
}
|
|
|
|
opts[ks] = v
|
|
|
|
}
|
2021-12-30 23:01:43 +08:00
|
|
|
return fn.Call(fm.Fork("-call"), args, opts)
|
2021-12-29 09:16:44 +08:00
|
|
|
}
|
|
|
|
|
2020-08-17 01:46:29 +08:00
|
|
|
func resolve(fm *Frame, head string) string {
|
2020-12-26 05:30:57 +08:00
|
|
|
special, fnRef := resolveCmdHeadInternally(fm, head, nil)
|
|
|
|
switch {
|
|
|
|
case special != nil:
|
2020-08-17 01:46:29 +08:00
|
|
|
return "special"
|
2020-12-26 05:30:57 +08:00
|
|
|
case fnRef != nil:
|
|
|
|
return "$" + head + FnSuffix
|
|
|
|
default:
|
|
|
|
return "(external " + parse.Quote(head) + ")"
|
2020-08-17 01:46:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 22:59:00 +08:00
|
|
|
type evalOpts struct {
|
|
|
|
Ns *Ns
|
|
|
|
OnEnd Callable
|
|
|
|
}
|
2020-08-17 06:02:51 +08:00
|
|
|
|
|
|
|
func (*evalOpts) SetDefaultOptions() {}
|
|
|
|
|
|
|
|
func eval(fm *Frame, opts evalOpts, code string) error {
|
|
|
|
src := parse.Source{Name: fmt.Sprintf("[eval %d]", nextEvalCount()), Code: code}
|
|
|
|
ns := opts.Ns
|
|
|
|
if ns == nil {
|
2021-01-10 08:54:02 +08:00
|
|
|
ns = CombineNs(fm.up, fm.local)
|
2021-01-09 22:59:00 +08:00
|
|
|
}
|
|
|
|
// The stacktrace already contains the line that calls "eval", so we pass
|
|
|
|
// nil as the second argument.
|
|
|
|
newNs, exc := fm.Eval(src, nil, ns)
|
|
|
|
if opts.OnEnd != nil {
|
2021-12-30 23:01:43 +08:00
|
|
|
newFm := fm.Fork("on-end callback of eval")
|
2022-03-20 23:50:25 +08:00
|
|
|
errCb := opts.OnEnd.Call(newFm, []any{newNs}, NoOpts)
|
2021-01-09 22:59:00 +08:00
|
|
|
if exc == nil {
|
|
|
|
return errCb
|
|
|
|
}
|
2020-08-17 06:02:51 +08:00
|
|
|
}
|
2021-01-09 22:59:00 +08:00
|
|
|
return exc
|
2020-08-17 06:02:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Used to generate unique names for each source passed to eval.
|
|
|
|
var (
|
|
|
|
evalCount int
|
|
|
|
evalCountMutex sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
func nextEvalCount() int {
|
|
|
|
evalCountMutex.Lock()
|
|
|
|
defer evalCountMutex.Unlock()
|
|
|
|
evalCount++
|
|
|
|
return evalCount
|
|
|
|
}
|
|
|
|
|
2020-12-25 01:39:51 +08:00
|
|
|
func useMod(fm *Frame, spec string) (*Ns, error) {
|
2021-01-09 22:59:00 +08:00
|
|
|
return use(fm, spec, nil)
|
2020-08-17 07:11:25 +08:00
|
|
|
}
|
|
|
|
|
2021-01-24 23:32:24 +08:00
|
|
|
func deprecate(fm *Frame, msg string) {
|
|
|
|
var ctx *diag.Context
|
|
|
|
if fm.traceback.Next != nil {
|
|
|
|
ctx = fm.traceback.Next.Head
|
|
|
|
}
|
|
|
|
fm.Deprecate(msg, ctx, 0)
|
|
|
|
}
|
|
|
|
|
2018-02-05 15:35:49 +08:00
|
|
|
func _ifaddrs(fm *Frame) error {
|
2016-11-06 17:47:20 +08:00
|
|
|
addrs, err := net.InterfaceAddrs()
|
2018-02-05 15:35:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-18 07:11:51 +08:00
|
|
|
out := fm.ValueOutput()
|
2016-11-06 17:47:20 +08:00
|
|
|
for _, addr := range addrs {
|
2021-06-18 07:11:51 +08:00
|
|
|
err := out.Put(addr.String())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-11-06 17:47:20 +08:00
|
|
|
}
|
2018-02-05 15:35:49 +08:00
|
|
|
return nil
|
2016-11-06 17:47:20 +08:00
|
|
|
}
|