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