2016-02-19 05:52:05 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-08-20 20:15:00 +08:00
|
|
|
"strconv"
|
2017-08-31 02:52:27 +08:00
|
|
|
"unsafe"
|
|
|
|
|
2020-08-22 05:28:54 +08:00
|
|
|
"github.com/elves/elvish/pkg/diag"
|
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
|
2020-08-20 20:15:00 +08:00
|
|
|
// The index of the rest argument. -1 if there is no rest argument.
|
|
|
|
RestArg int
|
2017-08-06 04:27:44 +08:00
|
|
|
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-08-22 05:28:54 +08:00
|
|
|
DefRange diag.Ranging
|
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 {
|
2020-08-20 20:15:00 +08:00
|
|
|
if c.RestArg != -1 {
|
|
|
|
if len(args) < len(c.ArgNames)-1 {
|
2020-04-13 07:47:33 +08:00
|
|
|
return errs.ArityMismatch{
|
|
|
|
What: "arguments here",
|
2020-08-20 20:15:00 +08:00
|
|
|
ValidLow: len(c.ArgNames) - 1, 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
|
|
|
}
|
|
|
|
|
2020-08-20 20:15:00 +08:00
|
|
|
// Populate local scope with arguments and options.
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.local = make(Ns)
|
2020-08-20 20:15:00 +08:00
|
|
|
if c.RestArg == -1 {
|
|
|
|
for i, name := range c.ArgNames {
|
|
|
|
fm.local[name] = vars.FromInit(args[i])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := 0; i < c.RestArg; i++ {
|
|
|
|
fm.local[c.ArgNames[i]] = vars.FromInit(args[i])
|
|
|
|
}
|
|
|
|
restOff := len(args) - len(c.ArgNames)
|
|
|
|
fm.local[c.ArgNames[c.RestArg]] = vars.FromInit(
|
|
|
|
vals.MakeList(args[c.RestArg : c.RestArg+restOff+1]...))
|
|
|
|
for i := c.RestArg + 1; i < len(c.ArgNames); i++ {
|
|
|
|
fm.local[c.ArgNames[i]] = vars.FromInit(args[i+restOff])
|
|
|
|
}
|
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) }
|
2020-08-20 20:15:00 +08:00
|
|
|
func (cf closureFields) RestArg() string { return strconv.Itoa(cf.c.RestArg) }
|
2020-06-29 07:32:00 +08:00
|
|
|
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 {
|
2020-08-22 05:28:54 +08:00
|
|
|
r := cf.c.Op.(diag.Ranger).Range()
|
|
|
|
return cf.c.SrcMeta.Code[r.From:r.To]
|
2020-06-29 07:32:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (cf closureFields) Def() string {
|
2020-08-22 05:28:54 +08:00
|
|
|
return cf.c.SrcMeta.Code[cf.c.DefRange.From:cf.c.DefRange.To]
|
2020-06-29 07:32:00 +08:00
|
|
|
}
|