pkg/eval: Remove access methods for exported fields.

The exported fields are intended to be mutated directly.
This commit is contained in:
Qi Xiao 2022-06-18 19:01:31 +01:00
parent d48b6f1843
commit 75ee261fcb
6 changed files with 7 additions and 32 deletions

View File

@ -118,7 +118,7 @@ func initLocation(ed *Editor, ev *eval.Evaler, st storedefs.Store, commonBinding
})
startMode(ed.app, w, err)
}))
ev.AddAfterChdir(func(string) {
ev.AfterChdir = append(ev.AfterChdir, func(string) {
wd, err := os.Getwd()
if err != nil {
// TODO(xiaq): Surface the error.

View File

@ -18,8 +18,8 @@ func TestChdir(t *testing.T) {
ev := NewEvaler()
argDirInBefore, argDirInAfter := "", ""
ev.AddBeforeChdir(func(dir string) { argDirInBefore = dir })
ev.AddAfterChdir(func(dir string) { argDirInAfter = dir })
ev.BeforeChdir = append(ev.BeforeChdir, func(dir string) { argDirInBefore = dir })
ev.AfterChdir = append(ev.AfterChdir, func(dir string) { argDirInAfter = dir })
back := saveWd()
defer back()

View File

@ -280,32 +280,6 @@ func (ev *Evaler) addNumBgJobs(delta int) {
ev.numBgJobs += delta
}
// SetArgs sets the value of the $args variable to a list of strings, built from
// the given slice. This method must be called before the Evaler is used to
// evaluate any code.
func (ev *Evaler) SetArgs(args []string) {
ev.Args = vals.MakeListSlice(args)
}
// AddBeforeChdir adds a function to run before changing directory. This method
// must be called before the Evaler is used to evaluate any code.
func (ev *Evaler) AddBeforeChdir(f func(string)) {
ev.BeforeChdir = append(ev.BeforeChdir, f)
}
// AddAfterChdir adds a function to run after changing directory. This method
// must be called before the Evaler is used to evaluate any code.
func (ev *Evaler) AddAfterChdir(f func(string)) {
ev.AfterChdir = append(ev.AfterChdir, f)
}
// AddBeforeExit adds a function to run before the Elvish process exits or gets
// replaced (via "exec" on UNIX). This method must be called before the Evaler
// is used to evaluate any code.
func (ev *Evaler) AddBeforeExit(f func()) {
ev.BeforeExit = append(ev.BeforeExit, f)
}
// Chdir changes the current directory, and updates $E:PWD on success
//
// It runs the functions in beforeChdir immediately before changing the

View File

@ -34,7 +34,7 @@ func TestArgs(t *testing.T) {
Test(t,
That("put $args").Puts(vals.EmptyList))
TestWithSetup(t,
func(ev *Evaler) { ev.SetArgs([]string{"foo", "bar"}) },
func(ev *Evaler) { ev.Args = vals.MakeList("foo", "bar") },
That("put $args").Puts(vals.MakeList("foo", "bar")))
}

View File

@ -67,7 +67,7 @@ func interact(ev *eval.Evaler, fds [3]*os.File, cfg *interactCfg) {
daemonClient = cl
// Even if error is not nil, we install daemon-related functionalities
// anyway. Daemon may eventually come online and become functional.
ev.AddBeforeExit(func() { cl.Close() })
ev.BeforeExit = append(ev.BeforeExit, func() { cl.Close() })
ev.AddModule("store", store.Ns(cl))
ev.AddModule("daemon", daemon.Ns(cl))
}

View File

@ -10,6 +10,7 @@ import (
"src.elv.sh/pkg/diag"
"src.elv.sh/pkg/eval"
"src.elv.sh/pkg/eval/vals"
"src.elv.sh/pkg/parse"
)
@ -23,7 +24,7 @@ type scriptCfg struct {
// Executes a shell script.
func script(ev *eval.Evaler, fds [3]*os.File, args []string, cfg *scriptCfg) int {
arg0 := args[0]
ev.SetArgs(args[1:])
ev.Args = vals.MakeListSlice(args[1:])
var name, code string
if cfg.Cmd {