Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the luash model can't work , please help me solve this problem #7

Open
TaoTaoFu opened this issue Jul 5, 2017 · 1 comment
Open

Comments

@TaoTaoFu
Copy link

TaoTaoFu commented Jul 5, 2017

th> local sh = require('sh')
                                                                      [0.0001s]
th> sh
function: 0x410888b0	
                                                                      [0.0001s]
th> local dockerbusybox = sh.command('docker', 'run', 'busybox')
[string "local dockerbusybox = sh.command('docker', 'r..."]:1: attempt to index global 'sh' (a function value)
stack traceback:
	[string "local dockerbusybox = sh.command('docker', 'r..."]:1: in main chunk
	[C]: in function 'xpcall'
	/home/tao/Datas/torch/install/share/lua/5.1/trepl/init.lua:679: in function 'repl'
	...atas/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:204: in main chunk
	[C]: at 0x004065c0	
                                                                      [0.0002s]
th> dockerbusybox('echo', 'hello')
sh: 1: dockerbusybox: not found

                                                                      [0.0027s]

When I installed the sh as the command

   "luarocks install --server=http://luarocks.org/dev luash "

It's successful , but when I run the example as above , the problem occured .

I don't know how to solve it, please help me sovle this problem. Thanks in advance ~ : )

@folknor
Copy link

folknor commented Aug 11, 2017

It's because of how lua variables are looked up in the lua shell vs during full script interpretation; so these two things will produce different results.

test.lua:
#!/usr/bin/lua
local sh = require("sh")
print(type(sh))

$ lua test.lua
table
$ lua
Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> local sh = require("sh")
> print(type(sh))
function

And the reason this happens is because luash changes _G's metatable, adding a __index that translates all attempts at accessing unset _G.xyz members as invoking a shell command of the same name (xyz).

So in your shell, type(sh) == "function" is not a defined function from the library, it's a function that in turn would have invoked this shell command for you:
$ sh

Here is a snippet of the relevant code from https://github.com/zserge/luash/blob/master/sh.lua

local mt = getmetatable(_G)
if mt == nil then
  mt = {}
  setmetatable(_G, mt)
end

-- set hook for undefined variables
mt.__index = function(t, cmd)
	return command(cmd)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants