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

Running files virtually but still in same shell

Started by dCode, 17 July 2017 - 02:09 AM
dCode #1
Posted 17 July 2017 - 04:09 AM
I have a function I like to call "require" because CC misses the require ability.

require.lua

if Global == nil then Global = {}; end

function require (mod, useExtenstion)
  __exports__, __name__ = nil, nil;
  local cDir = getDir();
  local path = cDir .. '/' .. mod;
  if useExtenstion ~= nil or not useExtenstion then path = path .. '.lua'; end
  if not fs.exists(path) then return false; end

  local fileHandle = fs.open(path, 'r');
  local mods = fileHandle.readAll();
  fileHandle.close();

  module = {};

  loadstring(mods)();

  if
    __name__ == nil or
    type(__name__) ~= 'string' or
    __exports__ == nil
  then
    print('module [' .. mod .. '] does not use module.* correctly');
    sleep(3);
    os.shutdown();
  end

  Global[__name__] = __exports__;
  Global['callid:' .. mod .. '/script'] = __name__;
  __exports__, __name__ = nil, nil;
  return Global[Global['callid:' .. mod .. '/script']];
end

But the module I try require is not run virtually inside the function it still have access to everything thats going around, as well as the Global variable defined in the start of the file. So I wonder how I can run the file virtually without having to start a new shell. So how do I run the code in a sandbox mode?
Bomb Bloke #2
Posted 17 July 2017 - 08:00 AM
ComputerCraft offers a version of loadfile() which accepts an environment table as a second argument. os.loadAPI() provides an example as to how you might want to use it here.

The 1.80WIP builds implement require within the shell. This may alternatively suit your purposes.
dCode #3
Posted 17 July 2017 - 05:00 PM
So theres still no way to execute this Lua script virtually?
CLNinja #4
Posted 17 July 2017 - 11:10 PM
Running code in a "sandbox" isnt too hard. If you want to sandbox to a folder, you have to make all fs calls resolve a folder you define as the root of the computer. Sandboxing environments is something that i personally have little experience with.
KingofGamesYami #5
Posted 17 July 2017 - 11:53 PM
Running code in a "sandbox" isnt too hard.

False. Sandboxing is actually rather difficult, as you have to override a lot of things to prevent damage, and be smart about how you do it. Otherwise, getfenv and setfenv can seriously break things, or your code will be included in the sandbox.
Bomb Bloke #6
Posted 18 July 2017 - 05:07 AM
So theres still no way to execute this Lua script virtually?

Now might be a good time for you to explain what you mean by "virtually". All Lua code runs in a VM. Twice, in the case of ComputerCraft, because you're going through LuaJ and the same is true of Java code. If you want to run Lua code "virtually" within Lua code then you're going to have to provide a Lua-based Lua interpreter!

Given that you mention sandboxing, I've been assuming you instead simply want control over the elements available within your loaded function's environment table. If this is the case then all you need to do is build a suitable table containing whatever elements you do want and pass that in through loadfile(). If you don't have a clue what I'm talking about then say so. If you want something else then elaborate.