zserge / luash

Tiny lua module to write shell scripts with lua (inspired by Python's sh module)
MIT License
317 stars 31 forks source link

I think it is a bug #9

Open Freakwill opened 5 years ago

Freakwill commented 5 years ago

Let's see the following code. whenever a variable is nil, it will be a function if import sh.

require('sh')
print(type(x), type(nil))  -- function, nil
x = nil
print(type(x))   -- function, but x is set to be nil
t = {}
x = 'hello'
z = t[x]
print(type(t[x]), type(z))  -- nil, function, but z should be nil as usual.

I forgive the first one, since x may be set to be a function by sh, but how to explain next two?

mnemnion commented 4 years ago

This is the ordinary logic of Lua; setting a variable to nil is the same thing as never setting it in the first place.

So there's no difference between z = nil (and z = t[x] on an empty t is just a fancy way of saying z = nil) and not setting z in the first place. In both cases, the failed lookup on _G triggers the __newindex patch, causing a shell function to be returned.