2016-02-19 05:52:05 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-08-31 02:52:27 +08:00
|
|
|
"unsafe"
|
|
|
|
|
2020-04-13 07:47:33 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/errs"
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/eval/vals"
|
|
|
|
"github.com/elves/elvish/pkg/eval/vars"
|
|
|
|
"github.com/elves/elvish/pkg/parse"
|
2017-08-31 02:52:27 +08:00
|
|
|
"github.com/xiaq/persistent/hash"
|
2016-02-19 05:52:05 +08:00
|
|
|
)
|
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// Closure is a closure defined in Elvish script. Each closure has its unique
|
|
|
|
// identity.
|
2016-02-19 05:52:05 +08:00
|
|
|
type Closure struct {
|
|
|
|
ArgNames []string
|
2016-02-26 08:54:27 +08:00
|
|
|
// The name for the rest argument. If empty, the function has fixed arity.
|
2017-08-06 04:27:44 +08:00
|
|
|
RestArg string
|
|
|
|
OptNames []string
|
2018-01-30 01:39:41 +08:00
|
|
|
OptDefaults []interface{}
|
2018-09-29 03:45:11 +08:00
|
|
|
Op effectOp
|
2017-12-24 07:51:32 +08:00
|
|
|
Captured Ns
|
2020-04-26 02:22:38 +08:00
|
|
|
SrcMeta parse.Source
|
2020-06-29 07:32:00 +08:00
|
|
|
DefFrom int
|
|
|
|
DefTo int
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 01:46:21 +08:00
|
|
|
var _ Callable = &Closure{}
|
2016-09-15 05:34:10 +08:00
|
|
|
|
2017-06-11 07:04:53 +08:00
|
|
|
// Kind returns "fn".
|
2016-02-19 05:52:05 +08:00
|
|
|
func (*Closure) Kind() string {
|
|
|
|
return "fn"
|
|
|
|
}
|
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// Equal compares by address.
|
2017-08-31 01:47:50 +08:00
|
|
|
func (c *Closure) Equal(rhs interface{}) bool {
|
2017-07-14 08:15:14 +08:00
|
|
|
return c == rhs
|
|
|
|
}
|
|
|
|
|
2019-04-19 05:15:34 +08:00
|
|
|
// Hash returns the hash of the address of the closure.
|
2017-08-31 02:52:27 +08:00
|
|
|
func (c *Closure) Hash() uint32 {
|
|
|
|
return hash.Pointer(unsafe.Pointer(c))
|
|
|
|
}
|
|
|
|
|
2017-06-11 07:04:53 +08:00
|
|
|
// Repr returns an opaque representation "<closure 0x23333333>".
|
2016-02-20 03:11:31 +08:00
|
|
|
func (c *Closure) Repr(int) string {
|
2016-10-12 02:03:25 +08:00
|
|
|
return fmt.Sprintf("<closure %p>", c)
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
|
|
|
|
2019-04-20 01:26:27 +08:00
|
|
|
func listOfStrings(ss []string) vals.List {
|
|
|
|
list := vals.EmptyList
|
|
|
|
for _, s := range ss {
|
|
|
|
list = list.Cons(s)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2016-02-19 05:52:05 +08:00
|
|
|
// Call calls a closure.
|
2018-03-01 10:17:56 +08:00
|
|
|
func (c *Closure) Call(fm *Frame, args []interface{}, opts map[string]interface{}) error {
|
2016-02-26 08:54:27 +08:00
|
|
|
if c.RestArg != "" {
|
2020-04-13 07:47:33 +08:00
|
|
|
if len(args) < len(c.ArgNames) {
|
|
|
|
return errs.ArityMismatch{
|
|
|
|
What: "arguments here",
|
|
|
|
ValidLow: len(c.ArgNames), ValidHigh: -1, Actual: len(args)}
|
2016-02-26 08:54:27 +08:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-13 07:47:33 +08:00
|
|
|
if len(args) != len(c.ArgNames) {
|
|
|
|
return errs.ArityMismatch{
|
|
|
|
What: "arguments here",
|
|
|
|
ValidLow: len(c.ArgNames), ValidHigh: len(c.ArgNames), Actual: len(args)}
|
2016-02-26 08:54:27 +08:00
|
|
|
}
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This evalCtx is dedicated to the current form, so we modify it in place.
|
|
|
|
// BUG(xiaq): When evaluating closures, async access to global variables
|
|
|
|
// and ports can be problematic.
|
|
|
|
|
|
|
|
// Make upvalue namespace and capture variables.
|
2017-09-20 20:43:28 +08:00
|
|
|
// TODO(xiaq): Is it safe to simply assign ec.up = c.Captured?
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.up = make(Ns)
|
2017-12-24 07:51:32 +08:00
|
|
|
for name, variable := range c.Captured {
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.up[name] = variable
|
2017-09-20 20:43:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate local scope with arguments, possibly a rest argument, and
|
|
|
|
// options.
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.local = make(Ns)
|
2016-02-26 08:54:27 +08:00
|
|
|
for i, name := range c.ArgNames {
|
2018-11-18 22:13:03 +08:00
|
|
|
fm.local[name] = vars.FromInit(args[i])
|
2016-02-26 08:54:27 +08:00
|
|
|
}
|
2017-08-06 01:54:16 +08:00
|
|
|
if c.RestArg != "" {
|
2018-11-18 22:13:03 +08:00
|
|
|
fm.local[c.RestArg] = vars.FromInit(vals.MakeList(args[len(c.ArgNames):]...))
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
2018-01-14 23:48:31 +08:00
|
|
|
optUsed := make(map[string]struct{})
|
2017-08-06 04:27:44 +08:00
|
|
|
for i, name := range c.OptNames {
|
|
|
|
v, ok := opts[name]
|
2018-01-14 23:48:31 +08:00
|
|
|
if ok {
|
|
|
|
optUsed[name] = struct{}{}
|
|
|
|
} else {
|
2017-08-06 04:27:44 +08:00
|
|
|
v = c.OptDefaults[i]
|
|
|
|
}
|
2018-11-18 22:13:03 +08:00
|
|
|
fm.local[name] = vars.FromInit(v)
|
2017-08-06 04:27:44 +08:00
|
|
|
}
|
2018-01-14 23:48:31 +08:00
|
|
|
for name := range opts {
|
|
|
|
_, used := optUsed[name]
|
|
|
|
if !used {
|
2018-01-21 19:05:36 +08:00
|
|
|
return fmt.Errorf("unknown option %s", parse.Quote(name))
|
2018-01-14 23:48:31 +08:00
|
|
|
}
|
2016-09-15 06:00:52 +08:00
|
|
|
}
|
2016-02-19 05:52:05 +08:00
|
|
|
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.srcMeta = c.SrcMeta
|
2018-09-29 03:45:11 +08:00
|
|
|
return c.Op.exec(fm)
|
2016-02-19 05:52:05 +08:00
|
|
|
}
|
2020-06-29 07:32:00 +08:00
|
|
|
|
|
|
|
func (c *Closure) Fields() vals.StructMap { return closureFields{c} }
|
|
|
|
|
|
|
|
type closureFields struct{ c *Closure }
|
|
|
|
|
|
|
|
func (closureFields) IsStructMap() {}
|
|
|
|
|
|
|
|
func (cf closureFields) ArgNames() vals.List { return listOfStrings(cf.c.ArgNames) }
|
|
|
|
func (cf closureFields) RestArg() string { return cf.c.RestArg }
|
|
|
|
func (cf closureFields) OptNames() vals.List { return listOfStrings(cf.c.OptNames) }
|
|
|
|
func (cf closureFields) Src() parse.Source { return cf.c.SrcMeta }
|
|
|
|
|
|
|
|
func (cf closureFields) OptDefaults() vals.List {
|
|
|
|
return vals.MakeList(cf.c.OptDefaults...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf closureFields) Body() string {
|
|
|
|
return cf.c.SrcMeta.Code[cf.c.Op.From:cf.c.Op.To]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cf closureFields) Def() string {
|
|
|
|
return cf.c.SrcMeta.Code[cf.c.DefFrom:cf.c.DefTo]
|
|
|
|
}
|