pkg/eval: Remove New*Source functions.

All call sites of such functions are replaced with simple &Source{...}
expressions.
This commit is contained in:
Qi Xiao 2020-04-25 13:22:07 +01:00
parent c112223611
commit 53a1eca9af
20 changed files with 30 additions and 64 deletions

View File

@ -401,7 +401,7 @@ func adaptMatcherMap(nt notifier, ev *eval.Evaler, m vals.Map) complete.Filterer
{}, // Will be replaced in CaptureOutput
{File: os.Stderr},
}
fm := eval.NewTopFrame(ev, eval.NewInternalGoSource("[editor matcher]"), ports)
fm := eval.NewTopFrame(ev, &eval.Source{Name: "[editor matcher]"}, ports)
outputs, err := fm.CaptureOutput(func(fm *eval.Frame) error {
return matcher.Call(fm, []interface{}{seed}, eval.NoOpts)
})
@ -473,7 +473,7 @@ func adaptArgGeneratorMap(ev *eval.Evaler, m vals.Map) complete.ArgGenerator {
}
}
}
fm := eval.NewTopFrame(ev, eval.NewInternalGoSource("[editor arg generator]"), ports)
fm := eval.NewTopFrame(ev, &eval.Source{Name: "[editor arg generator]"}, ports)
f := func(fm *eval.Frame) error {
return gen.Call(fm, argValues, eval.NoOpts)
}

View File

@ -64,7 +64,7 @@ func callHooks(ev *eval.Evaler, name string, hook vals.List, args ...interface{}
// unexported from eval.
ports := []*eval.Port{
{File: os.Stdin}, {File: os.Stdout}, {File: os.Stderr}}
fm := eval.NewTopFrame(ev, eval.NewInternalGoSource(name), ports)
fm := eval.NewTopFrame(ev, &eval.Source{Name: name}, ports)
fn.Call(fm, args, eval.NoOpts)
}
}

View File

@ -76,8 +76,7 @@ func evalDefaultBinding(ev *eval.Evaler, ns eval.Ns) {
if err != nil {
panic(err)
}
src := eval.NewInternalElvishSource(
true, "[default bindings]", defaultBindingsElv)
src := &eval.Source{Name: "[default bindings]", Code: defaultBindingsElv}
op, err := ev.CompileWithGlobal(n, src, ns)
if err != nil {
panic(err)

View File

@ -19,7 +19,7 @@ func initHighlighter(appSpec *cli.AppSpec, ev *eval.Evaler) {
}
func check(ev *eval.Evaler, n *parse.Chunk) error {
src := eval.NewInteractiveSource("[tty]", n.SourceText())
src := &eval.Source{Name: "[tty]", Code: n.SourceText()}
_, err := ev.Compile(n, src)
return err
}

View File

@ -39,7 +39,7 @@ func initInstant(app cli.App, ev *eval.Evaler, ns eval.Ns) {
func instantStart(app cli.App, ev *eval.Evaler, binding cli.Handler) {
execute := func(code string) ([]string, error) {
src := eval.NewInteractiveSource("[instant]", code)
src := &eval.Source{Name: "[instant]", Code: code}
op, err := ev.ParseAndCompile(src)
if err != nil {
return nil, err

View File

@ -57,7 +57,7 @@ func indexLayeredBindings(k ui.Key, bindings ...BindingMap) eval.Callable {
return nil
}
var bindingSource = eval.NewInternalGoSource("[editor binding]")
var bindingSource = &eval.Source{Name: "[editor binding]"}
func callWithNotifyPorts(nt notifier, ev *eval.Evaler, f eval.Callable, args ...interface{}) {
// TODO(xiaq): Use CallWithOutputCallback when it supports redirecting the

View File

@ -35,7 +35,7 @@ func minibufSubmit(app cli.App, ev *eval.Evaler) {
}
cli.SetAddon(app, nil)
code := codeArea.CopyState().Buffer.Content
src := eval.NewInteractiveSource("[minibuf]", code)
src := &eval.Source{Name: "[minibuf]", Code: code}
op, err := ev.ParseAndCompile(src)
if err != nil {
app.Notify(err.Error())

View File

@ -169,7 +169,7 @@ func callForStyledText(nt notifier, ev *eval.Evaler, fn eval.Callable, args ...i
{File: os.Stderr},
}
// XXX There is no source to pass to NewTopEvalCtx.
fm := eval.NewTopFrame(ev, eval.NewInternalGoSource("[prompt]"), ports)
fm := eval.NewTopFrame(ev, &eval.Source{Name: "[prompt]"}, ports)
f := func(fm *eval.Frame) error { return fn.Call(fm, args, eval.NoOpts) }
err := fm.PipeOutput(f, valuesCb, bytesCb)

View File

@ -105,7 +105,7 @@ func feedInput(ttyCtrl apptest.TTYCtrl, s string) {
func evals(ev *eval.Evaler, codes ...string) {
for _, code := range codes {
src := eval.NewInternalElvishSource(true, "[test]", code)
src := &eval.Source{Name: "[test]", Code: code}
op, err := ev.ParseAndCompile(src)
if err != nil {
panic(fmt.Errorf("parse and compile %q: %s", code, err))

View File

@ -276,7 +276,7 @@ func source(fm *Frame, fname string) error {
scriptGlobal.set(name)
}
op, err := compile(fm.Builtin.static(),
scriptGlobal, n, NewScriptSource(path, code))
scriptGlobal, n, &Source{Name: path, Code: code, IsFile: true})
if err != nil {
return err
}

View File

@ -275,7 +275,8 @@ func loadModule(fm *Frame, r diag.Ranger, spec string) (Ns, error) {
return ns, nil
}
if code, ok := fm.bundled[spec]; ok {
return evalModule(fm, r, spec, NewInternalElvishSource(false, spec, code))
return evalModule(fm, r, spec,
&Source{Name: "[bundled " + spec + "]", Code: code})
}
if fm.libDir == "" {
return nil, noSuchModule{spec}
@ -294,7 +295,7 @@ func loadModuleFile(fm *Frame, r diag.Ranger, spec, path string) (Ns, error) {
}
return nil, err
}
return evalModule(fm, r, path, NewModuleSource(path, code))
return evalModule(fm, r, path, &Source{Name: path, Code: code, IsFile: true})
}
func evalModule(fm *Frame, r diag.Ranger, key string, src *Source) (Ns, error) {

View File

@ -185,8 +185,7 @@ func adaptChdirHook(name string, ev *Evaler, pfns *vector.Vector) func(string) {
fmt.Fprintln(os.Stderr, name, "hook must be callable")
continue
}
fm := NewTopFrame(ev,
NewInternalGoSource("["+name+" hook]"), ports[:])
fm := NewTopFrame(ev, &Source{Name: "[hook " + name + "]"}, ports[:])
err := fn.Call(fm, []interface{}{path}, NoOpts)
if err != nil {
// TODO: Stack trace

View File

@ -50,7 +50,7 @@ func TestMultipleEval(t *testing.T) {
func TestConcurrentEval(t *testing.T) {
// Run this test with "go test -race".
ev := NewEvaler()
src := NewInternalElvishSource(true, "[test]", "")
src := &Source{Name: "[test]"}
op, err := ev.ParseAndCompile(src)
if err != nil {
panic(err)
@ -97,7 +97,7 @@ func BenchmarkOutputCaptureMixed(b *testing.B) {
func benchmarkOutputCapture(n int, f func(*Frame)) {
ev := NewEvaler()
defer ev.Close()
fm := NewTopFrame(ev, NewInternalGoSource("[benchmark]"), []*Port{{}, {}, {}})
fm := NewTopFrame(ev, &Source{Name: "[benchmark]"}, []*Port{{}, {}, {}})
for i := 0; i < n; i++ {
captureOutput(fm, func(fm *Frame) error {
f(fm)

View File

@ -75,7 +75,7 @@ func (ev *Evaler) PurelyEvalPrimary(pn *parse.Primary) interface{} {
if sigil != "" {
return nil
}
ec := NewTopFrame(ev, NewInternalGoSource("[purely eval]"), nil)
ec := NewTopFrame(ev, &Source{Name: "[purely-eval]"}, nil)
variable := ec.ResolveVar(qname)
if variable != nil {
return variable.Get()

View File

@ -10,38 +10,11 @@ import (
// Source describes a piece of source code.
type Source struct {
Name string
Code string
Name string
Code string
IsFile bool
}
// NewInteractiveSource returns a Source for a piece of code entered
// interactively.
func NewInteractiveSource(name, code string) *Source {
return &Source{Name: "[tty]", Code: code}
}
// NewScriptSource returns a Source for a piece of code used as a script.
func NewScriptSource(path, code string) *Source {
return &Source{Name: path, Code: code, IsFile: true}
}
// NewModuleSource returns a Source for a piece of code used as a module.
func NewModuleSource(path, code string) *Source {
return &Source{Name: path, Code: code, IsFile: true}
}
// NewInternalGoSource returns a Source for use as a placeholder when calling Elvish
// functions from Go code. It has no associated code.
func NewInternalGoSource(name string) *Source {
return &Source{Name: name}
}
func NewInternalElvishSource(root bool, name, code string) *Source {
return &Source{Name: name, Code: code}
}
func (src *Source) Kind() string {
return "map"
}

View File

@ -8,23 +8,17 @@ import (
)
func TestSourceAsValue(t *testing.T) {
vals.TestValue(t, NewInteractiveSource("[tty]", "echo")).
vals.TestValue(t, &Source{Name: "[tty]", Code: "echo"}).
Kind("map").
Hash(hash.DJB(hash.String("[tty]"), hash.String("echo"), 0)).
Equal(NewInteractiveSource("[tty]", "echo")).
NotEqual(NewInteractiveSource("[tty]", "put")).
Equal(&Source{Name: "[tty]", Code: "echo"}).
NotEqual(&Source{Name: "[tty]", Code: "put"}).
Repr("<src name:'[tty]' code:... is-file:$false>").
AllKeys("name", "code", "is-file").
Index("name", "[tty]").
Index("code", "echo").
Index("is-file", false)
vals.TestValue(t, NewInternalGoSource("[test]")).
Index("is-file", false)
vals.TestValue(t, NewInternalElvishSource(true, "[test]", "echo")).
Index("is-file", false)
vals.TestValue(t, NewScriptSource("/fake/path", "echo")).
vals.TestValue(t, &Source{IsFile: true}).
Index("is-file", true)
}

View File

@ -255,8 +255,7 @@ func evalAndCollect(t *testing.T, ev *Evaler, texts []string) result {
}()
for i, text := range texts {
name := fmt.Sprintf("test%d.elv", i)
src := NewInternalElvishSource(true, name, text)
src := &Source{Name: fmt.Sprintf("test%d.elv", i), Code: text}
n, err := parse.AsChunk(src.Name, src.Code)
if err != nil {

View File

@ -79,7 +79,7 @@ func Interact(fds [3]*os.File, cfg *InteractConfig) {
// No error; reset cooldown.
cooldown = time.Second
src := eval.NewInteractiveSource("[tty]", line)
src := &eval.Source{Name: fmt.Sprintf("[tty %v]", cmdNum), Code: line}
op, err := ev.ParseAndCompile(src)
if err == nil {
err = evalInTTY(ev, op, fds)
@ -103,7 +103,7 @@ func sourceRC(fds [3]*os.File, ev *eval.Evaler, rcPath string) error {
}
return err
}
src := eval.NewScriptSource(absPath, code)
src := &eval.Source{Name: absPath, Code: code, IsFile: true}
op, err := ev.ParseAndCompile(src)
if err != nil {
return err

View File

@ -50,7 +50,8 @@ func Script(fds [3]*os.File, args []string, cfg *ScriptConfig) int {
}
}
op, err := ev.ParseAndCompile(eval.NewScriptSource(name, code))
op, err := ev.ParseAndCompile(
&eval.Source{Name: name, Code: code, IsFile: true})
if err != nil {
if cfg.CompileOnly && cfg.JSON {
fmt.Fprintf(fds[1], "%s\n", errorToJSON(err))

View File

@ -114,7 +114,7 @@ const (
func evalAndCollect(ev *eval.Evaler, code string) (
outBytes []byte, outValues []interface{}, errBytes []byte, err error) {
op, err := ev.ParseAndCompile(eval.NewInteractiveSource("[web]", code))
op, err := ev.ParseAndCompile(&eval.Source{Name: "[web]", Code: code})
if err != nil {
return
}