2018-01-01 23:12:34 +08:00
|
|
|
// Package eval handles evaluation of parsed Elvish code and provides runtime
|
|
|
|
// facilities.
|
2013-09-18 22:51:48 +08:00
|
|
|
package eval
|
|
|
|
|
|
|
|
import (
|
2023-05-08 04:44:28 +08:00
|
|
|
"context"
|
2014-01-27 18:55:45 +08:00
|
|
|
"fmt"
|
2020-04-26 20:14:51 +08:00
|
|
|
"io"
|
2014-01-22 22:45:33 +08:00
|
|
|
"os"
|
2018-10-10 00:39:24 +08:00
|
|
|
"strconv"
|
2021-01-04 23:55:26 +08:00
|
|
|
"sync"
|
2014-02-10 11:33:53 +08:00
|
|
|
|
2021-01-27 09:28:38 +08:00
|
|
|
"src.elv.sh/pkg/env"
|
|
|
|
"src.elv.sh/pkg/eval/vals"
|
|
|
|
"src.elv.sh/pkg/eval/vars"
|
|
|
|
"src.elv.sh/pkg/logutil"
|
|
|
|
"src.elv.sh/pkg/parse"
|
2013-09-18 22:51:48 +08:00
|
|
|
)
|
|
|
|
|
2020-09-03 12:27:18 +08:00
|
|
|
var logger = logutil.GetLogger("[eval] ")
|
2016-02-08 03:39:03 +08:00
|
|
|
|
2017-12-24 07:51:32 +08:00
|
|
|
const (
|
|
|
|
// FnSuffix is the suffix for the variable names of functions. Defining a
|
|
|
|
// function "foo" is equivalent to setting a variable named "foo~", and vice
|
|
|
|
// versa.
|
|
|
|
FnSuffix = "~"
|
|
|
|
// NsSuffix is the suffix for the variable names of namespaces. Defining a
|
|
|
|
// namespace foo is equivalent to setting a variable named "foo:", and vice
|
|
|
|
// versa.
|
|
|
|
NsSuffix = ":"
|
|
|
|
)
|
2015-02-24 17:21:27 +08:00
|
|
|
|
2017-10-01 09:05:13 +08:00
|
|
|
const (
|
2018-06-02 02:06:34 +08:00
|
|
|
defaultValuePrefix = "▶ "
|
|
|
|
defaultNotifyBgJobSuccess = true
|
2017-10-01 09:05:13 +08:00
|
|
|
)
|
|
|
|
|
2021-01-04 23:55:26 +08:00
|
|
|
// Evaler provides methods for evaluating code, and maintains state that is
|
|
|
|
// persisted between evaluation of different pieces of code. An Evaler is safe
|
|
|
|
// to use concurrently.
|
2015-02-25 07:37:18 +08:00
|
|
|
type Evaler struct {
|
2021-10-05 07:52:28 +08:00
|
|
|
// The following fields must only be set before the Evaler is used to
|
|
|
|
// evaluate any code; mutating them afterwards may cause race conditions.
|
|
|
|
|
2021-10-04 06:57:26 +08:00
|
|
|
// Command-line arguments, exposed as $args.
|
2021-10-05 07:52:28 +08:00
|
|
|
Args vals.List
|
2021-10-04 06:57:26 +08:00
|
|
|
// Hooks to run before exit or exec.
|
2022-06-20 04:45:36 +08:00
|
|
|
PreExitHooks []func()
|
2021-10-05 06:27:06 +08:00
|
|
|
// Chdir hooks, exposed indirectly as $before-chdir and $after-chdir.
|
2021-10-05 07:52:28 +08:00
|
|
|
BeforeChdir, AfterChdir []func(string)
|
2021-10-04 06:57:26 +08:00
|
|
|
// Directories to search libraries.
|
2021-10-05 07:52:28 +08:00
|
|
|
LibDirs []string
|
2021-10-04 06:57:26 +08:00
|
|
|
// Source code of internal bundled modules indexed by use specs.
|
2021-10-05 07:52:28 +08:00
|
|
|
BundledModules map[string]string
|
2021-10-05 06:57:00 +08:00
|
|
|
// Callback to notify the success or failure of background jobs. Must not be
|
|
|
|
// mutated once the Evaler is used to evaluate any code.
|
|
|
|
BgJobNotify func(string)
|
2022-08-08 06:42:43 +08:00
|
|
|
// Path to the rc file, and path to the rc file actually evaluated. These
|
|
|
|
// are not used by the Evaler itself right now; they are here so that they
|
|
|
|
// can be exposed to the runtime: module.
|
|
|
|
RcPath, EffectiveRcPath string
|
2021-10-04 06:57:26 +08:00
|
|
|
|
|
|
|
mu sync.RWMutex
|
2021-10-05 07:52:28 +08:00
|
|
|
// Mutations to fields below must be guarded by mutex.
|
2021-01-04 23:55:26 +08:00
|
|
|
//
|
|
|
|
// Note that this is *not* a GIL; most state mutations when executing Elvish
|
|
|
|
// code is localized and do not need to hold this mutex.
|
|
|
|
//
|
|
|
|
// TODO: Actually guard all mutations by this mutex.
|
|
|
|
|
2021-01-05 07:54:13 +08:00
|
|
|
global, builtin *Ns
|
2018-09-29 01:54:36 +08:00
|
|
|
|
2021-01-05 07:54:13 +08:00
|
|
|
deprecations deprecationRegistry
|
2018-09-29 03:09:02 +08:00
|
|
|
|
2020-01-11 10:27:43 +08:00
|
|
|
// Internal modules are indexed by use specs. External modules are indexed by
|
|
|
|
// absolute paths.
|
2020-12-25 01:39:51 +08:00
|
|
|
modules map[string]*Ns
|
2018-09-29 03:09:02 +08:00
|
|
|
|
2021-01-05 07:54:13 +08:00
|
|
|
// Various states and configs exposed to Elvish code.
|
|
|
|
//
|
|
|
|
// The prefix to prepend to value outputs when writing them to terminal,
|
|
|
|
// exposed as $value-out-prefix.
|
|
|
|
valuePrefix string
|
|
|
|
// Whether to notify the success of background jobs, exposed as
|
|
|
|
// $notify-bg-job-sucess.
|
|
|
|
notifyBgJobSuccess bool
|
|
|
|
// The current number of background jobs, exposed as $num-bg-jobs.
|
|
|
|
numBgJobs int
|
2017-12-18 23:22:59 +08:00
|
|
|
}
|
|
|
|
|
2015-02-25 19:21:06 +08:00
|
|
|
// NewEvaler creates a new Evaler.
|
2017-12-23 07:45:47 +08:00
|
|
|
func NewEvaler() *Evaler {
|
2020-12-25 01:39:51 +08:00
|
|
|
builtin := builtinNs.Ns()
|
2021-12-17 08:21:20 +08:00
|
|
|
beforeChdirElvish, afterChdirElvish := vals.EmptyList, vals.EmptyList
|
2017-06-19 18:58:58 +08:00
|
|
|
|
2017-10-01 09:05:13 +08:00
|
|
|
ev := &Evaler{
|
2021-01-05 07:54:13 +08:00
|
|
|
global: new(Ns),
|
|
|
|
builtin: builtin,
|
2020-04-26 08:13:05 +08:00
|
|
|
|
|
|
|
deprecations: newDeprecationRegistry(),
|
2021-01-05 07:54:13 +08:00
|
|
|
|
2021-10-29 05:28:13 +08:00
|
|
|
modules: make(map[string]*Ns),
|
|
|
|
BundledModules: make(map[string]string),
|
2021-01-05 07:54:13 +08:00
|
|
|
|
|
|
|
valuePrefix: defaultValuePrefix,
|
|
|
|
notifyBgJobSuccess: defaultNotifyBgJobSuccess,
|
|
|
|
numBgJobs: 0,
|
2021-10-05 07:52:28 +08:00
|
|
|
Args: vals.EmptyList,
|
2017-06-25 00:08:47 +08:00
|
|
|
}
|
2017-12-28 04:43:43 +08:00
|
|
|
|
2021-10-05 07:52:28 +08:00
|
|
|
ev.BeforeChdir = []func(string){
|
2021-01-05 07:54:13 +08:00
|
|
|
adaptChdirHook("before-chdir", ev, &beforeChdirElvish)}
|
2021-10-05 07:52:28 +08:00
|
|
|
ev.AfterChdir = []func(string){
|
2021-01-05 07:54:13 +08:00
|
|
|
adaptChdirHook("after-chdir", ev, &afterChdirElvish)}
|
|
|
|
|
2021-10-24 04:44:11 +08:00
|
|
|
ev.ExtendBuiltin(BuildNs().
|
|
|
|
AddVar("pwd", NewPwdVar(ev)).
|
|
|
|
AddVar("before-chdir", vars.FromPtr(&beforeChdirElvish)).
|
|
|
|
AddVar("after-chdir", vars.FromPtr(&afterChdirElvish)).
|
|
|
|
AddVar("value-out-indicator",
|
|
|
|
vars.FromPtrWithMutex(&ev.valuePrefix, &ev.mu)).
|
|
|
|
AddVar("notify-bg-job-success",
|
|
|
|
vars.FromPtrWithMutex(&ev.notifyBgJobSuccess, &ev.mu)).
|
|
|
|
AddVar("num-bg-jobs",
|
2022-03-20 23:50:25 +08:00
|
|
|
vars.FromGet(func() any { return strconv.Itoa(ev.getNumBgJobs()) })).
|
|
|
|
AddVar("args", vars.FromGet(func() any { return ev.Args })))
|
2018-03-01 10:35:32 +08:00
|
|
|
|
2021-10-29 05:28:13 +08:00
|
|
|
// Install the "builtin" module after extension is complete.
|
|
|
|
ev.modules["builtin"] = ev.builtin
|
|
|
|
|
2017-10-01 09:05:13 +08:00
|
|
|
return ev
|
2016-09-12 00:14:56 +08:00
|
|
|
}
|
|
|
|
|
2021-12-17 08:21:20 +08:00
|
|
|
func adaptChdirHook(name string, ev *Evaler, pfns *vals.List) func(string) {
|
2018-03-22 04:51:08 +08:00
|
|
|
return func(path string) {
|
2021-01-05 07:54:13 +08:00
|
|
|
ports, cleanup := PortsFromStdFiles(ev.ValuePrefix())
|
2020-04-16 07:11:23 +08:00
|
|
|
defer cleanup()
|
2022-03-20 23:50:25 +08:00
|
|
|
callCfg := CallCfg{Args: []any{path}, From: "[hook " + name + "]"}
|
2021-01-04 02:51:29 +08:00
|
|
|
evalCfg := EvalCfg{Ports: ports[:]}
|
2018-03-22 04:51:08 +08:00
|
|
|
for it := (*pfns).Iterator(); it.HasElem(); it.Next() {
|
|
|
|
fn, ok := it.Elem().(Callable)
|
|
|
|
if !ok {
|
|
|
|
fmt.Fprintln(os.Stderr, name, "hook must be callable")
|
|
|
|
continue
|
|
|
|
}
|
2021-01-04 02:51:29 +08:00
|
|
|
err := ev.Call(fn, callCfg, evalCfg)
|
2018-03-22 04:51:08 +08:00
|
|
|
if err != nil {
|
|
|
|
// TODO: Stack trace
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 04:45:36 +08:00
|
|
|
// PreExit runs all pre-exit hooks.
|
|
|
|
func (ev *Evaler) PreExit() {
|
|
|
|
for _, hook := range ev.PreExitHooks {
|
|
|
|
hook()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:54:13 +08:00
|
|
|
// Access methods.
|
|
|
|
|
|
|
|
// Global returns the global Ns.
|
|
|
|
func (ev *Evaler) Global() *Ns {
|
|
|
|
ev.mu.RLock()
|
|
|
|
defer ev.mu.RUnlock()
|
|
|
|
return ev.global
|
2018-03-22 04:51:08 +08:00
|
|
|
}
|
|
|
|
|
2021-10-24 04:44:11 +08:00
|
|
|
// ExtendGlobal extends the global namespace with the given namespace.
|
|
|
|
func (ev *Evaler) ExtendGlobal(ns Nser) {
|
2021-01-05 07:54:13 +08:00
|
|
|
ev.mu.Lock()
|
|
|
|
defer ev.mu.Unlock()
|
2021-10-24 04:44:11 +08:00
|
|
|
ev.global = CombineNs(ev.global, ns.Ns())
|
2021-01-05 07:54:13 +08:00
|
|
|
}
|
|
|
|
|
2022-12-28 20:42:31 +08:00
|
|
|
// DeleteFromGlobal deletes names from the global namespace.
|
|
|
|
func (ev *Evaler) DeleteFromGlobal(names map[string]struct{}) {
|
2022-12-09 05:38:16 +08:00
|
|
|
ev.mu.Lock()
|
|
|
|
defer ev.mu.Unlock()
|
|
|
|
g := ev.global.clone()
|
|
|
|
for i := range g.infos {
|
2022-12-28 20:42:31 +08:00
|
|
|
if _, ok := names[g.infos[i].name]; ok {
|
2022-12-09 05:38:16 +08:00
|
|
|
g.infos[i].deleted = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ev.global = g
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:54:13 +08:00
|
|
|
// Builtin returns the builtin Ns.
|
|
|
|
func (ev *Evaler) Builtin() *Ns {
|
|
|
|
ev.mu.RLock()
|
|
|
|
defer ev.mu.RUnlock()
|
|
|
|
return ev.builtin
|
|
|
|
}
|
|
|
|
|
2021-10-24 04:44:11 +08:00
|
|
|
// ExtendBuiltin extends the builtin namespace with the given namespace.
|
|
|
|
func (ev *Evaler) ExtendBuiltin(ns Nser) {
|
2021-01-05 08:48:25 +08:00
|
|
|
ev.mu.Lock()
|
|
|
|
defer ev.mu.Unlock()
|
2021-10-24 04:44:11 +08:00
|
|
|
ev.builtin = CombineNs(ev.builtin, ns.Ns())
|
2021-01-05 08:48:25 +08:00
|
|
|
}
|
|
|
|
|
2022-04-12 04:39:29 +08:00
|
|
|
// ReplaceBuiltin replaces the builtin namespace. It should only be used in
|
|
|
|
// tests.
|
|
|
|
func (ev *Evaler) ReplaceBuiltin(ns *Ns) {
|
|
|
|
ev.mu.Lock()
|
|
|
|
defer ev.mu.Unlock()
|
|
|
|
ev.builtin = ns
|
|
|
|
}
|
|
|
|
|
2021-01-05 07:54:13 +08:00
|
|
|
func (ev *Evaler) registerDeprecation(d deprecation) bool {
|
|
|
|
ev.mu.Lock()
|
|
|
|
defer ev.mu.Unlock()
|
|
|
|
return ev.deprecations.register(d)
|
2018-03-22 04:51:08 +08:00
|
|
|
}
|
|
|
|
|
2021-01-05 07:54:13 +08:00
|
|
|
// AddModule add an internal module so that it can be used with "use $name" from
|
|
|
|
// script.
|
|
|
|
func (ev *Evaler) AddModule(name string, mod *Ns) {
|
|
|
|
ev.mu.Lock()
|
|
|
|
defer ev.mu.Unlock()
|
2018-01-04 05:49:44 +08:00
|
|
|
ev.modules[name] = mod
|
2017-12-23 07:45:47 +08:00
|
|
|
}
|
|
|
|
|
2021-01-05 07:54:13 +08:00
|
|
|
// ValuePrefix returns the prefix to prepend to value outputs when writing them
|
|
|
|
// to terminal.
|
|
|
|
func (ev *Evaler) ValuePrefix() string {
|
|
|
|
ev.mu.RLock()
|
|
|
|
defer ev.mu.RUnlock()
|
|
|
|
return ev.valuePrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *Evaler) getNotifyBgJobSuccess() bool {
|
|
|
|
ev.mu.RLock()
|
|
|
|
defer ev.mu.RUnlock()
|
|
|
|
return ev.notifyBgJobSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *Evaler) getNumBgJobs() int {
|
|
|
|
ev.mu.RLock()
|
|
|
|
defer ev.mu.RUnlock()
|
|
|
|
return ev.numBgJobs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ev *Evaler) addNumBgJobs(delta int) {
|
|
|
|
ev.mu.Lock()
|
|
|
|
defer ev.mu.Unlock()
|
|
|
|
ev.numBgJobs += delta
|
|
|
|
}
|
|
|
|
|
2021-10-04 06:16:20 +08:00
|
|
|
// Chdir changes the current directory, and updates $E:PWD on success
|
|
|
|
//
|
2021-01-05 12:07:35 +08:00
|
|
|
// It runs the functions in beforeChdir immediately before changing the
|
|
|
|
// directory, and the functions in afterChdir immediately after (if chdir was
|
|
|
|
// successful). It returns nil as long as the directory changing part succeeds.
|
|
|
|
func (ev *Evaler) Chdir(path string) error {
|
2021-10-05 07:52:28 +08:00
|
|
|
for _, hook := range ev.BeforeChdir {
|
2021-01-05 12:07:35 +08:00
|
|
|
hook(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := os.Chdir(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-05 07:52:28 +08:00
|
|
|
for _, hook := range ev.AfterChdir {
|
2021-01-05 12:07:35 +08:00
|
|
|
hook(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
logger.Println("getwd after cd:", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
os.Setenv(env.PWD, pwd)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 02:51:29 +08:00
|
|
|
// EvalCfg keeps configuration for the (*Evaler).Eval method.
|
|
|
|
type EvalCfg struct {
|
2023-05-08 04:44:28 +08:00
|
|
|
// Context that can be used to cancel the evaluation.
|
|
|
|
Interrupts context.Context
|
2021-01-04 02:51:29 +08:00
|
|
|
// Ports to use in evaluation. The first 3 elements, if not specified
|
|
|
|
// (either being nil or Ports containing fewer than 3 elements),
|
2021-01-05 12:07:35 +08:00
|
|
|
// will be filled with DummyInputPort, DummyOutputPort and
|
|
|
|
// DummyOutputPort respectively.
|
2021-01-04 02:51:29 +08:00
|
|
|
Ports []*Port
|
|
|
|
// Whether the Eval method should try to put the Elvish in the foreground
|
|
|
|
// after the code is executed.
|
|
|
|
PutInFg bool
|
|
|
|
// If not nil, used the given global namespace, instead of Evaler's own.
|
|
|
|
Global *Ns
|
|
|
|
}
|
|
|
|
|
2021-01-17 08:36:18 +08:00
|
|
|
func (cfg *EvalCfg) fillDefaults() {
|
2021-01-04 02:51:29 +08:00
|
|
|
if len(cfg.Ports) < 3 {
|
|
|
|
cfg.Ports = append(cfg.Ports, make([]*Port, 3-len(cfg.Ports))...)
|
|
|
|
}
|
|
|
|
if cfg.Ports[0] == nil {
|
2021-01-05 12:07:35 +08:00
|
|
|
cfg.Ports[0] = DummyInputPort
|
2021-01-04 02:51:29 +08:00
|
|
|
}
|
|
|
|
if cfg.Ports[1] == nil {
|
2021-01-05 12:07:35 +08:00
|
|
|
cfg.Ports[1] = DummyOutputPort
|
2021-01-04 02:51:29 +08:00
|
|
|
}
|
|
|
|
if cfg.Ports[2] == nil {
|
2021-01-05 12:07:35 +08:00
|
|
|
cfg.Ports[2] = DummyOutputPort
|
2021-01-04 02:51:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 08:10:26 +08:00
|
|
|
// Eval evaluates a piece of source code with the given configuration. The
|
|
|
|
// returned error may be a parse error, compilation error or exception.
|
|
|
|
func (ev *Evaler) Eval(src parse.Source, cfg EvalCfg) error {
|
2021-01-17 08:36:18 +08:00
|
|
|
cfg.fillDefaults()
|
2021-01-04 01:20:56 +08:00
|
|
|
errFile := cfg.Ports[2].File
|
|
|
|
|
2021-02-01 22:17:57 +08:00
|
|
|
tree, err := parse.Parse(src, parse.Config{WarningWriter: errFile})
|
2021-01-04 01:20:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-17 08:36:18 +08:00
|
|
|
|
|
|
|
ev.mu.Lock()
|
|
|
|
b := ev.builtin
|
|
|
|
defaultGlobal := cfg.Global == nil
|
|
|
|
if defaultGlobal {
|
|
|
|
// If cfg.Global is nil, use the Evaler's default global, and also
|
|
|
|
// mutate the default global.
|
|
|
|
cfg.Global = ev.global
|
|
|
|
// Continue to hold the mutex; it will be released when ev.global gets
|
|
|
|
// mutated.
|
|
|
|
} else {
|
|
|
|
ev.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-12-11 22:21:09 +08:00
|
|
|
op, _, err := compile(b.static(), cfg.Global.static(), nil, tree, errFile)
|
2021-01-02 08:10:26 +08:00
|
|
|
if err != nil {
|
2021-01-17 08:36:18 +08:00
|
|
|
if defaultGlobal {
|
|
|
|
ev.mu.Unlock()
|
|
|
|
}
|
2021-01-02 08:10:26 +08:00
|
|
|
return err
|
|
|
|
}
|
2021-01-17 08:36:18 +08:00
|
|
|
|
2021-01-04 22:17:27 +08:00
|
|
|
fm, cleanup := ev.prepareFrame(src, cfg)
|
|
|
|
defer cleanup()
|
2021-01-17 08:36:18 +08:00
|
|
|
|
2021-01-24 22:10:45 +08:00
|
|
|
newLocal, exec := op.prepare(fm)
|
2021-01-17 08:36:18 +08:00
|
|
|
if defaultGlobal {
|
2021-01-24 22:10:45 +08:00
|
|
|
ev.global = newLocal
|
2021-01-17 08:36:18 +08:00
|
|
|
ev.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2021-01-24 22:10:45 +08:00
|
|
|
return exec()
|
2021-01-02 08:10:26 +08:00
|
|
|
}
|
|
|
|
|
2021-01-04 02:51:29 +08:00
|
|
|
// CallCfg keeps configuration for the (*Evaler).Call method.
|
|
|
|
type CallCfg struct {
|
2022-07-05 10:00:29 +08:00
|
|
|
// Arguments to pass to the function.
|
2022-03-20 23:50:25 +08:00
|
|
|
Args []any
|
2021-01-04 02:51:29 +08:00
|
|
|
// Options to pass to the function.
|
2022-03-20 23:50:25 +08:00
|
|
|
Opts map[string]any
|
2021-01-04 02:51:29 +08:00
|
|
|
// The name of the internal source that is calling the function.
|
|
|
|
From string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cfg *CallCfg) fillDefaults() {
|
|
|
|
if cfg.Opts == nil {
|
|
|
|
cfg.Opts = NoOpts
|
2021-01-02 10:22:44 +08:00
|
|
|
}
|
2021-01-04 02:51:29 +08:00
|
|
|
if cfg.From == "" {
|
|
|
|
cfg.From = "[internal]"
|
2021-01-02 10:22:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 02:51:29 +08:00
|
|
|
// Call calls a given function.
|
|
|
|
func (ev *Evaler) Call(f Callable, callCfg CallCfg, evalCfg EvalCfg) error {
|
|
|
|
callCfg.fillDefaults()
|
2021-01-17 08:36:18 +08:00
|
|
|
evalCfg.fillDefaults()
|
|
|
|
if evalCfg.Global == nil {
|
|
|
|
evalCfg.Global = ev.Global()
|
|
|
|
}
|
2021-01-04 22:17:27 +08:00
|
|
|
fm, cleanup := ev.prepareFrame(parse.Source{Name: callCfg.From}, evalCfg)
|
|
|
|
defer cleanup()
|
|
|
|
return f.Call(fm, callCfg.Args, callCfg.Opts)
|
2021-01-04 02:51:29 +08:00
|
|
|
}
|
|
|
|
|
2021-01-04 22:17:27 +08:00
|
|
|
func (ev *Evaler) prepareFrame(src parse.Source, cfg EvalCfg) (*Frame, func()) {
|
2023-05-08 04:44:28 +08:00
|
|
|
intCtx := cfg.Interrupts
|
|
|
|
if intCtx == nil {
|
|
|
|
intCtx = context.Background()
|
2020-04-16 07:11:23 +08:00
|
|
|
}
|
2021-01-04 22:17:27 +08:00
|
|
|
|
2021-08-02 04:38:30 +08:00
|
|
|
ports := fillDefaultDummyPorts(cfg.Ports)
|
|
|
|
|
2023-05-08 04:44:28 +08:00
|
|
|
fm := &Frame{ev, src, cfg.Global, new(Ns), nil, intCtx, ports, nil, false}
|
2021-01-04 22:17:27 +08:00
|
|
|
return fm, func() {
|
|
|
|
if cfg.PutInFg {
|
2020-04-16 07:11:23 +08:00
|
|
|
err := putSelfInFg()
|
|
|
|
if err != nil {
|
2021-08-02 04:38:30 +08:00
|
|
|
fmt.Fprintln(ports[2].File,
|
2020-04-16 07:11:23 +08:00
|
|
|
"failed to put myself in foreground:", err)
|
|
|
|
}
|
2021-01-04 22:17:27 +08:00
|
|
|
}
|
2020-04-16 07:11:23 +08:00
|
|
|
}
|
2018-03-01 10:50:27 +08:00
|
|
|
}
|
|
|
|
|
2021-08-02 04:38:30 +08:00
|
|
|
func fillDefaultDummyPorts(ports []*Port) []*Port {
|
|
|
|
growPorts(&ports, 3)
|
|
|
|
if ports[0] == nil {
|
|
|
|
ports[0] = DummyInputPort
|
|
|
|
}
|
|
|
|
if ports[1] == nil {
|
|
|
|
ports[1] = DummyOutputPort
|
|
|
|
}
|
|
|
|
if ports[2] == nil {
|
|
|
|
ports[2] = DummyOutputPort
|
|
|
|
}
|
|
|
|
return ports
|
|
|
|
}
|
|
|
|
|
2022-12-11 22:21:09 +08:00
|
|
|
// Check checks the given source code for any parse error, autofixes, and
|
|
|
|
// compilation error. It always tries to compile the code even if there is a
|
|
|
|
// parse error. If w is not nil, deprecation messages are written to it.
|
|
|
|
func (ev *Evaler) Check(src parse.Source, w io.Writer) (error, []string, error) {
|
2021-02-01 22:17:57 +08:00
|
|
|
tree, parseErr := parse.Parse(src, parse.Config{WarningWriter: w})
|
2022-12-11 22:21:09 +08:00
|
|
|
autofixes, compileErr := ev.CheckTree(tree, w)
|
|
|
|
return parseErr, autofixes, compileErr
|
2021-01-03 23:20:31 +08:00
|
|
|
}
|
|
|
|
|
2022-12-11 22:21:09 +08:00
|
|
|
// CheckTree checks the given parsed source tree for autofixes and compilation
|
|
|
|
// errors. If w is not nil, deprecation messages are written to it.
|
|
|
|
func (ev *Evaler) CheckTree(tree parse.Tree, w io.Writer) ([]string, error) {
|
|
|
|
ev.mu.RLock()
|
|
|
|
b, g, m := ev.builtin, ev.global, ev.modules
|
|
|
|
ev.mu.RUnlock()
|
|
|
|
_, autofixes, compileErr := compile(b.static(), g.static(), mapKeys(m), tree, w)
|
|
|
|
return autofixes, compileErr
|
2016-02-06 07:25:13 +08:00
|
|
|
}
|