-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
frame.go
214 lines (189 loc) · 5.3 KB
/
frame.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package eval
import (
"bufio"
"io"
"os"
"strings"
"sync"
"github.com/elves/elvish/util"
)
// Frame contains information of the current running function, aknin to a call
// 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
srcMeta *Source
local, up Ns
ports []*Port
begin, end int
traceback *stackTrace
background bool
}
// NewTopFrame creates a top-level Frame.
func NewTopFrame(ev *Evaler, src *Source, ports []*Port) *Frame {
return &Frame{
ev, src,
ev.Global, make(Ns),
ports,
0, len(src.code), nil, false,
}
}
// 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
}
// InputChan returns a channel from which input can be read.
func (fm *Frame) InputChan() chan interface{} {
return fm.ports[0].Chan
}
// InputFile returns a file from which input can be read.
func (fm *Frame) InputFile() *os.File {
return fm.ports[0].File
}
// OutputChan returns a channel onto which output can be written.
func (fm *Frame) OutputChan() chan<- interface{} {
return fm.ports[1].Chan
}
// OutputFile returns a file onto which output can be written.
func (fm *Frame) OutputFile() *os.File {
return fm.ports[1].File
}
// IterateInputs calls the passed function for each input element.
func (fm *Frame) IterateInputs(f func(interface{})) {
var w sync.WaitGroup
inputs := make(chan interface{})
w.Add(2)
go func() {
linesToChan(fm.ports[0].File, inputs)
w.Done()
}()
go func() {
for v := range fm.ports[0].Chan {
inputs <- v
}
w.Done()
}()
go func() {
w.Wait()
close(inputs)
}()
for v := range inputs {
f(v)
}
}
func linesToChan(r io.Reader, ch chan<- interface{}) {
filein := bufio.NewReader(r)
for {
line, err := filein.ReadString('\n')
if line != "" {
ch <- strings.TrimSuffix(line, "\n")
}
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.
func (fm *Frame) fork(name string) *Frame {
newPorts := make([]*Port, len(fm.ports))
for i, p := range fm.ports {
newPorts[i] = p.Fork()
}
return &Frame{
fm.Evaler, fm.srcMeta,
fm.local, fm.up,
newPorts,
fm.begin, fm.end, fm.traceback, fm.background,
}
}
// Eval evaluates an op. It does so in a protected environment so that
// exceptions thrown are wrapped in an Error.
func (fm *Frame) Eval(op Op) (err error) {
defer catch(&err, fm)
e := op.Exec(fm)
if e != nil {
if exc, ok := e.(*Exception); ok {
return exc
}
return fm.makeException(e)
}
return nil
}
// Call calls a function with the given arguments and options. It does so in a
// protected environment so that exceptions thrown are wrapped in an Error.
func (fm *Frame) Call(f Callable, args []interface{}, opts map[string]interface{}) (err error) {
defer catch(&err, fm)
e := f.Call(fm, args, opts)
if e != nil {
if exc, ok := e.(*Exception); ok {
return exc
}
return fm.makeException(e)
}
return nil
}
// CaptureOutput calls a function with the given arguments and options,
// capturing and returning the output. It does so in a protected environment so
// that exceptions thrown are wrapped in an Error.
func (fm *Frame) CaptureOutput(fn Callable, args []interface{}, opts map[string]interface{}) (vs []interface{}, err error) {
// XXX There is no source.
opFunc := func(f *Frame) error {
return fn.Call(f, args, opts)
}
return pcaptureOutput(fm, Op{funcOp(opFunc), -1, -1})
}
// CallWithOutputCallback calls a function with the given arguments and options,
// feeding the outputs to the given callbacks. It does so in a protected
// environment so that exceptions thrown are wrapped in an Error.
func (fm *Frame) CallWithOutputCallback(fn Callable, args []interface{}, opts map[string]interface{}, valuesCb func(<-chan interface{}), bytesCb func(*os.File)) error {
// XXX There is no source.
opFunc := func(f *Frame) error {
return fn.Call(f, args, opts)
}
return pcaptureOutputInner(fm, Op{funcOp(opFunc), -1, -1}, valuesCb, bytesCb)
}
func catch(perr *error, fm *Frame) {
// NOTE: We have to duplicate instead of calling util.Catch here, since
// recover can only catch a panic when called directly from a deferred
// function.
r := recover()
if r == nil {
return
}
if exc, ok := r.(util.Thrown); ok {
err := exc.Wrapped
if _, ok := err.(*Exception); !ok {
err = fm.makeException(err)
}
*perr = err
} else if r != nil {
panic(r)
}
}
// makeException turns an error into an Exception by adding traceback.
func (fm *Frame) makeException(e error) *Exception {
return &Exception{e, fm.addTraceback()}
}
func (fm *Frame) addTraceback() *stackTrace {
return &stackTrace{
entry: &util.SourceRange{
Name: fm.srcMeta.describePath(), Source: fm.srcMeta.code,
Begin: fm.begin, End: fm.end,
},
next: fm.traceback,
}
}
// errorpf stops the ec.eval immediately by panicking with a diagnostic message.
// The panic is supposed to be caught by ec.eval.
func (fm *Frame) errorpf(begin, end int, format string, args ...interface{}) {
fm.begin, fm.end = begin, end
throwf(format, args...)
}