Make init value of $f~ actually identical to $nop~.

This fixes #1519.
This commit is contained in:
Qi Xiao 2022-06-07 20:20:00 +01:00
parent bfd555808f
commit f42c2dda61
4 changed files with 15 additions and 17 deletions

View File

@ -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
}

View File

@ -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) {

View File

@ -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

View File

@ -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)