src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/mods/runtime/runtime.go (about)

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