This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LeDark Lua's profile picture

How does shell.run work?

Started by LeDark Lua, 13 July 2015 - 05:16 PM
LeDark Lua #1
Posted 13 July 2015 - 07:16 PM
Im trying to do the same thing in pure lua. But i can't. If there is something done in JLua let me know.
But if its possible wihtout JLua then how? I tried and failed. I looked at the shell script in CC but I dont get it at all. Well I just need an answer if this was done in JLua, if not I need an explanation how.

Thanks in advance,

LeDark Lua.
Edited on 13 July 2015 - 05:17 PM
Lupus590 #2
Posted 13 July 2015 - 07:54 PM
The shell "API" (It's not a true API) is made by CC, you won't find it outside CC.

Runs a file with arguments

shell.run(FilePath+FileName, arg1, arg2, arg3, ..., argN)
Edited on 13 July 2015 - 06:00 PM
LeDark Lua #3
Posted 13 July 2015 - 07:55 PM
Oh, but can you run programs without the shell api?

Well make your own api….??

EDIT: Ok but how can you make your own shell.run?
Edited on 13 July 2015 - 06:04 PM
Grim Reaper #4
Posted 13 July 2015 - 08:08 PM
Can you elaborate on the environment in which you're trying to execute Lua code? For example, are you able to run code but are having time running another file/program from your code? Or are you unable to run Lua at all?

If I understand what you're asking, then I'll try to give you my best answer.

The shell API is loading the contents of a file as Lua source into a function, then executing that function with a custom environment. The environment with which the loaded file is running contains the shell table itself, and its metatable has an __index metamethod that points to the _G (global) table. That way, the loaded function can use the shell API as well as access all of the items in the global namespace.

If you wanted to do your own version of a kind of 'run' function, this works for me, at least in CC.

local superAwesomeAPI = {
    -- Super awesome stuff goes here.
}

--[[
    Loads a file at a given path as a function and executes it with
    the given arguments. Once the function is loaded, it is given an
    environment which has access to the _G table via __index, as well
    as our super special API table.

    @param path -- The path of the file to be loaded and executed.
    @param ...  -- The arguments to pass to the loaded program.
]]
function run(path, ...)
    -- Uses the fs API from CC. You could just use the io API from native Lua if not operating
    -- within CC.
    local fileHandle = fs.open(path, 'r')

    -- Make sure the file was opened properly.
    if not fileHandle then
        return false
    end

    local program = loadstring(fileHandle.readAll())

    -- Program was not valid Lua (was not interpreted properly).
    if not program then
        return false
    end

    local environment = setmetatable({ superAwesomeAPI = superAwesomeAPI }, { __index = _G })
    program = setfenv(program, environment)

    if not program then
        return false
    end

    return pcall(program, ...)
end
Lupus590 #5
Posted 13 July 2015 - 08:09 PM
Grim Reaper knows more then me on thisIt would be something like this:

--#pseudo code
--#a recreation of shell.run
args = {}
filepath, args = ...
--#load the file
loadedFile = loadfile(filepath)
--#run the loaded file
loadedFile(unpack(args))
Edited on 13 July 2015 - 06:11 PM
LeDark Lua #6
Posted 13 July 2015 - 08:13 PM
Thanks, I will test it and see if this works!
LeDark Lua #7
Posted 13 July 2015 - 09:25 PM
Both of them worked, thanks!