mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-12 17:27:50 +08:00
parent
bfd555808f
commit
f42c2dda61
|
@ -27,7 +27,6 @@ var (
|
|||
|
||||
func init() {
|
||||
addBuiltinFns(map[string]any{
|
||||
"nop": nop,
|
||||
"kind-of": kindOf,
|
||||
"constantly": constantly,
|
||||
|
||||
|
@ -69,6 +68,8 @@ func init() {
|
|||
// Etymology: Various languages, in particular NOP in
|
||||
// [assembly languages](https://en.wikipedia.org/wiki/NOP).
|
||||
|
||||
var nopGoFn = NewGoFn("nop", nop)
|
||||
|
||||
func nop(opts RawOptions, args ...any) {
|
||||
// Do nothing
|
||||
}
|
||||
|
|
|
@ -104,15 +104,16 @@ import (
|
|||
// @cf buildinfo
|
||||
|
||||
var builtinNs = BuildNsNamed("").AddVars(map[string]vars.Var{
|
||||
"_": vars.NewBlackhole(),
|
||||
"pid": vars.NewReadOnly(strconv.Itoa(syscall.Getpid())),
|
||||
"ok": vars.NewReadOnly(OK),
|
||||
"nil": vars.NewReadOnly(nil),
|
||||
"true": vars.NewReadOnly(true),
|
||||
"false": vars.NewReadOnly(false),
|
||||
"buildinfo": vars.NewReadOnly(buildinfo.Value),
|
||||
"version": vars.NewReadOnly(buildinfo.Value.Version),
|
||||
"paths": vars.NewEnvListVar("PATH"),
|
||||
"_": vars.NewBlackhole(),
|
||||
"pid": vars.NewReadOnly(strconv.Itoa(syscall.Getpid())),
|
||||
"ok": vars.NewReadOnly(OK),
|
||||
"nil": vars.NewReadOnly(nil),
|
||||
"true": vars.NewReadOnly(true),
|
||||
"false": vars.NewReadOnly(false),
|
||||
"buildinfo": vars.NewReadOnly(buildinfo.Value),
|
||||
"version": vars.NewReadOnly(buildinfo.Value.Version),
|
||||
"paths": vars.NewEnvListVar("PATH"),
|
||||
"nop" + FnSuffix: vars.NewReadOnly(nopGoFn),
|
||||
})
|
||||
|
||||
func addBuiltinFns(fns map[string]any) {
|
||||
|
|
|
@ -41,6 +41,7 @@ func TestVar(t *testing.T) {
|
|||
// Declaring a variable whose name ends in "~" initializes it to the
|
||||
// builtin nop function.
|
||||
That("var cmd~; cmd &ignored-opt ignored-arg").DoesNothing(),
|
||||
That("var cmd~; is $cmd~ $nop~").Puts(true),
|
||||
// Declaring multiple variables
|
||||
That("var x y", "put $x $y").Puts(nil, nil),
|
||||
// Declaring one variable with initial value
|
||||
|
|
|
@ -148,20 +148,15 @@ func (c *Closure) Call(fm *Frame, args []any, opts map[string]any) error {
|
|||
return exc
|
||||
}
|
||||
|
||||
var (
|
||||
fnDefault = NewGoFn("nop~", nop)
|
||||
nsDefault = &Ns{}
|
||||
)
|
||||
|
||||
// MakeVarFromName creates a Var with a suitable type constraint inferred from
|
||||
// the name.
|
||||
func MakeVarFromName(name string) vars.Var {
|
||||
switch {
|
||||
case strings.HasSuffix(name, FnSuffix):
|
||||
val := fnDefault
|
||||
val := nopGoFn
|
||||
return vars.FromPtr(&val)
|
||||
case strings.HasSuffix(name, NsSuffix):
|
||||
val := nsDefault
|
||||
val := &Ns{}
|
||||
return vars.FromPtr(&val)
|
||||
default:
|
||||
return vars.FromInit(nil)
|
||||
|
|
Loading…
Reference in New Issue
Block a user