2017-12-28 03:59:42 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-09-28 01:50:53 +08:00
|
|
|
"fmt"
|
2017-12-28 03:59:42 +08:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2019-12-24 04:00:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/diag"
|
2020-04-26 01:26:17 +08:00
|
|
|
"github.com/elves/elvish/pkg/parse"
|
2020-04-27 04:24:30 +08:00
|
|
|
"github.com/elves/elvish/pkg/prog"
|
2020-09-05 04:13:59 +08:00
|
|
|
"github.com/elves/elvish/pkg/strutil"
|
2017-12-28 03:59:42 +08:00
|
|
|
)
|
|
|
|
|
2020-09-11 10:07:19 +08:00
|
|
|
// Frame contains information of the current running function, akin to a call
|
2017-12-28 03:59:42 +08:00
|
|
|
// frame in native CPU execution. A Frame is only modified during and very
|
|
|
|
// shortly after creation; new Frame's are "forked" when needed.
|
|
|
|
type Frame struct {
|
|
|
|
*Evaler
|
2020-03-29 07:56:47 +08:00
|
|
|
|
2020-04-26 02:22:38 +08:00
|
|
|
srcMeta parse.Source
|
2017-12-28 03:59:42 +08:00
|
|
|
|
|
|
|
local, up Ns
|
2020-03-29 07:56:47 +08:00
|
|
|
|
2020-04-16 06:03:58 +08:00
|
|
|
intCh <-chan struct{}
|
2020-03-29 07:56:47 +08:00
|
|
|
ports []*Port
|
2017-12-28 03:59:42 +08:00
|
|
|
|
2020-09-03 12:54:32 +08:00
|
|
|
traceback *StackTrace
|
2017-12-28 03:59:42 +08:00
|
|
|
|
|
|
|
background bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTopFrame creates a top-level Frame.
|
2018-10-08 23:05:27 +08:00
|
|
|
//
|
|
|
|
// TODO(xiaq): This should be a method on the Evaler.
|
2020-04-26 02:22:38 +08:00
|
|
|
func NewTopFrame(ev *Evaler, src parse.Source, ports []*Port) *Frame {
|
2017-12-28 03:59:42 +08:00
|
|
|
return &Frame{
|
2018-01-05 12:07:11 +08:00
|
|
|
ev, src,
|
2017-12-28 03:59:42 +08:00
|
|
|
ev.Global, make(Ns),
|
2020-03-29 07:56:47 +08:00
|
|
|
nil, ports,
|
2020-03-29 06:03:42 +08:00
|
|
|
nil, false,
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 21:07:42 +08:00
|
|
|
// SetLocal changes the local scope of the Frame.
|
|
|
|
func (fm *Frame) SetLocal(ns Ns) {
|
|
|
|
fm.local = ns
|
|
|
|
}
|
|
|
|
|
2018-03-01 10:03:04 +08:00
|
|
|
// Close releases resources allocated for this frame. It always returns a nil
|
|
|
|
// error. It may be called only once.
|
|
|
|
func (fm *Frame) Close() error {
|
|
|
|
for _, port := range fm.ports {
|
|
|
|
port.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-28 03:59:42 +08:00
|
|
|
// InputChan returns a channel from which input can be read.
|
2018-03-01 10:17:56 +08:00
|
|
|
func (fm *Frame) InputChan() chan interface{} {
|
|
|
|
return fm.ports[0].Chan
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// InputFile returns a file from which input can be read.
|
2018-03-01 10:17:56 +08:00
|
|
|
func (fm *Frame) InputFile() *os.File {
|
|
|
|
return fm.ports[0].File
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// OutputChan returns a channel onto which output can be written.
|
2018-03-01 10:17:56 +08:00
|
|
|
func (fm *Frame) OutputChan() chan<- interface{} {
|
|
|
|
return fm.ports[1].Chan
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// OutputFile returns a file onto which output can be written.
|
2018-03-01 10:17:56 +08:00
|
|
|
func (fm *Frame) OutputFile() *os.File {
|
|
|
|
return fm.ports[1].File
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IterateInputs calls the passed function for each input element.
|
2018-03-01 10:17:56 +08:00
|
|
|
func (fm *Frame) IterateInputs(f func(interface{})) {
|
2017-12-28 03:59:42 +08:00
|
|
|
var w sync.WaitGroup
|
2018-01-30 01:39:41 +08:00
|
|
|
inputs := make(chan interface{})
|
2017-12-28 03:59:42 +08:00
|
|
|
|
|
|
|
w.Add(2)
|
|
|
|
go func() {
|
2018-03-01 10:17:56 +08:00
|
|
|
linesToChan(fm.ports[0].File, inputs)
|
2017-12-28 03:59:42 +08:00
|
|
|
w.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
2018-03-01 10:17:56 +08:00
|
|
|
for v := range fm.ports[0].Chan {
|
2017-12-28 03:59:42 +08:00
|
|
|
inputs <- v
|
|
|
|
}
|
|
|
|
w.Done()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
w.Wait()
|
|
|
|
close(inputs)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for v := range inputs {
|
|
|
|
f(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 01:39:41 +08:00
|
|
|
func linesToChan(r io.Reader, ch chan<- interface{}) {
|
2017-12-28 03:59:42 +08:00
|
|
|
filein := bufio.NewReader(r)
|
|
|
|
for {
|
|
|
|
line, err := filein.ReadString('\n')
|
|
|
|
if line != "" {
|
2020-09-05 04:13:59 +08:00
|
|
|
ch <- strutil.ChopLineEnding(line)
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
logger.Println("error on reading:", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fork returns a modified copy of ec. The ports are forked, and the name is
|
|
|
|
// changed to the given value. Other fields are copied shallowly.
|
2018-03-01 10:17:56 +08:00
|
|
|
func (fm *Frame) fork(name string) *Frame {
|
|
|
|
newPorts := make([]*Port, len(fm.ports))
|
|
|
|
for i, p := range fm.ports {
|
2020-01-12 08:42:18 +08:00
|
|
|
if p != nil {
|
|
|
|
newPorts[i] = p.Fork()
|
|
|
|
}
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
return &Frame{
|
2020-04-09 06:33:22 +08:00
|
|
|
fm.Evaler, fm.srcMeta,
|
2018-03-01 10:17:56 +08:00
|
|
|
fm.local, fm.up,
|
2020-03-29 07:56:47 +08:00
|
|
|
fm.intCh, newPorts,
|
2020-03-29 06:03:42 +08:00
|
|
|
fm.traceback, fm.background,
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-12 19:57:41 +08:00
|
|
|
// Eval evaluates an Op. It is like eval except that it sets fm.srcMeta
|
2018-10-10 19:43:42 +08:00
|
|
|
// temporarily to op.src during the evaluation.
|
2018-10-12 19:57:41 +08:00
|
|
|
func (fm *Frame) Eval(op Op) error {
|
2018-10-10 19:43:42 +08:00
|
|
|
oldSrc := fm.srcMeta
|
2018-10-10 21:07:42 +08:00
|
|
|
fm.srcMeta = op.Src
|
2018-10-10 19:43:42 +08:00
|
|
|
defer func() {
|
|
|
|
fm.srcMeta = oldSrc
|
|
|
|
}()
|
2020-04-10 04:01:14 +08:00
|
|
|
return op.Inner.exec(fm)
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:12:25 +08:00
|
|
|
// CaptureOutput captures the output of a given callback that operates on a Frame.
|
|
|
|
func (fm *Frame) CaptureOutput(f func(*Frame) error) ([]interface{}, error) {
|
|
|
|
return captureOutput(fm, f)
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
|
2020-04-10 08:12:25 +08:00
|
|
|
// PipeOutput calls a callback with output piped to the given output handlers.
|
|
|
|
func (fm *Frame) PipeOutput(f func(*Frame) error, valuesCb func(<-chan interface{}), bytesCb func(*os.File)) error {
|
|
|
|
return pipeOutput(fm, f, valuesCb, bytesCb)
|
2019-12-08 08:29:19 +08:00
|
|
|
}
|
|
|
|
|
2020-09-03 12:54:32 +08:00
|
|
|
func (fm *Frame) addTraceback(r diag.Ranger) *StackTrace {
|
|
|
|
return &StackTrace{
|
|
|
|
Head: diag.NewContext(fm.srcMeta.Name, fm.srcMeta.Code, r.Range()),
|
|
|
|
Next: fm.traceback,
|
2020-04-09 06:33:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an Exception with specified range and cause.
|
|
|
|
func (fm *Frame) errorp(r diag.Ranger, e error) error {
|
2020-01-13 07:00:45 +08:00
|
|
|
switch e := e.(type) {
|
|
|
|
case nil:
|
|
|
|
return nil
|
|
|
|
case *Exception:
|
|
|
|
return e
|
|
|
|
default:
|
2020-09-03 12:54:32 +08:00
|
|
|
return &Exception{e, &StackTrace{
|
|
|
|
Head: diag.NewContext(fm.srcMeta.Name, fm.srcMeta.Code, r.Range()),
|
|
|
|
Next: fm.traceback,
|
2020-04-09 06:33:22 +08:00
|
|
|
}}
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 06:33:22 +08:00
|
|
|
// Returns an Exception with specified range and error text.
|
|
|
|
func (fm *Frame) errorpf(r diag.Ranger, format string, args ...interface{}) error {
|
|
|
|
return fm.errorp(r, fmt.Errorf(format, args...))
|
2017-12-28 03:59:42 +08:00
|
|
|
}
|
2020-04-26 08:13:05 +08:00
|
|
|
|
|
|
|
// Deprecate shows a deprecation message. The message is not shown if the same
|
|
|
|
// deprecation message has been shown for the same location before.
|
2020-08-16 05:52:50 +08:00
|
|
|
func (fm *Frame) Deprecate(msg string, ctx *diag.Context) {
|
|
|
|
if ctx == nil {
|
2020-09-03 12:54:32 +08:00
|
|
|
ctx = fm.traceback.Head
|
2020-08-16 05:52:50 +08:00
|
|
|
}
|
|
|
|
dep := deprecation{ctx.Name, ctx.Ranging, msg}
|
2020-04-27 04:24:30 +08:00
|
|
|
if prog.ShowDeprecations && fm.deprecations.register(dep) {
|
2020-04-26 08:13:05 +08:00
|
|
|
err := diag.Error{
|
2020-08-16 05:52:50 +08:00
|
|
|
Type: "deprecation", Message: dep.message, Context: *ctx}
|
2020-04-26 08:13:05 +08:00
|
|
|
fm.ports[2].File.WriteString(err.Show("") + "\n")
|
|
|
|
}
|
|
|
|
}
|