runtime.go 904 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Package runtime implements the runtime module.
  2. package runtime
  3. import (
  4. "os"
  5. "src.elv.sh/pkg/eval"
  6. "src.elv.sh/pkg/eval/vals"
  7. "src.elv.sh/pkg/eval/vars"
  8. )
  9. var osExecutable = os.Executable
  10. // Ns returns the namespace for the runtime: module.
  11. //
  12. // All the public properties of the Evaler should be set before this function is
  13. // called.
  14. func Ns(ev *eval.Evaler) *eval.Ns {
  15. elvishPath, err := osExecutable()
  16. if err != nil {
  17. elvishPath = ""
  18. }
  19. return eval.BuildNsNamed("runtime").
  20. AddVars(map[string]vars.Var{
  21. "elvish-path": vars.NewReadOnly(nonEmptyOrNil(elvishPath)),
  22. "lib-dirs": vars.NewReadOnly(vals.MakeListSlice(ev.LibDirs)),
  23. "rc-path": vars.NewReadOnly(nonEmptyOrNil(ev.RcPath)),
  24. "effective-rc-path": vars.NewReadOnly(nonEmptyOrNil(ev.EffectiveRcPath)),
  25. }).Ns()
  26. }
  27. func nonEmptyOrNil(s string) any {
  28. if s == "" {
  29. return nil
  30. }
  31. return s
  32. }