simplification
Download
140 lines 3.8 KiBLuaModified 2026-11-09 raw
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
local new_instance, NOMSU_DIRECTORY
do
  local _obj_0 = require("nomsu_environment")
  new_instance, NOMSU_DIRECTORY = _obj_0.new_instance, _obj_0.NOMSU_DIRECTORY
end
local Errhand = require("error_handling")
local file_loader, table_loader, chain_loaders
do
  local _obj_0 = require("nomsu_loaders")
  file_loader, table_loader, chain_loaders = _obj_0.file_loader, _obj_0.table_loader, _obj_0.chain_loaders
end
local make_sandbox
make_sandbox = require("nomsu_sandbox").make_sandbox
local run_limited
run_limited = require("nomsu_limits").run_limited
local MAX_COPY_NODES = 1000000
local plain_copy
plain_copy = function(value, _seen, _count)
  if _seen == nil then
    _seen = { }
  end
  if _count == nil then
    _count = {
      n = 0
    }
  end
  local t = type(value)
  local _exp_0 = t
  if "string" == _exp_0 or "number" == _exp_0 or "boolean" == _exp_0 or "nil" == _exp_0 then
    return value
  elseif "table" == _exp_0 then
    if not (getmetatable(value) == nil) then
      error("A sandbox value can only be a plain table (one with no metatable).", 0)
    end
    if _seen[value] then
      error("A sandbox value can't contain a cycle.", 0)
    end
    _seen[value] = true
    local result = { }
    for k, v in pairs(value) do
      _count.n = _count.n + 1
      if _count.n > MAX_COPY_NODES then
        error("Sandbox value is too big to copy.", 0)
      end
      result[plain_copy(k, _seen, _count)] = plain_copy(v, _seen, _count)
    end
    _seen[value] = nil
    return result
  else
    return error("A sandbox value can only be plain data, not a " .. tostring(t) .. ".", 0)
  end
end
local Runtime
do
  local _class_0
  local _base_0 = {
    run = function(self, code)
      local run
      run = function()
        local ok, value = xpcall((function()
          return self.environment:run(code)
        end), Errhand.enhance_error)
        if ok and self.sealed then
          ok, value = pcall(plain_copy, value)
        end
        return ok, value
      end
      return run_limited(self.limits, run)
    end,
    seal = function(self)
      if self.sealed then
        return 
      end
      self.sealed = true
      self.environment = make_sandbox(self.environment, self.host_names)
    end
  }
  _base_0.__index = _base_0
  _class_0 = setmetatable({
    __init = function(self, config)
      if config == nil then
        config = { }
      end
      local modules = config.modules or {
        "."
      }
      local directories = {
        tostring(NOMSU_DIRECTORY) .. "/lib"
      }
      for _index_0 = 1, #modules do
        local dir = modules[_index_0]
        table.insert(directories, dir)
      end
      local loader = chain_loaders(table_loader((function()
        local _tbl_0 = { }
        for k, v in pairs(modules) do
          if type(k) == "string" then
            _tbl_0[k] = v
          end
        end
        return _tbl_0
      end)()), file_loader(directories))
      self.root = new_instance({
        loader = loader,
        print = config.print,
        input = config.input,
        exit = Errhand.exit_signal
      })
      for k, v in pairs(config.environment or { }) do
        self.root[k] = v
      end
      self.root:export("core")
      self.environment = self.root.new_environment()
      self.environment.MODULE_NAME = "main"
      do
        local _tbl_0 = { }
        for k in pairs(config.environment or { }) do
          _tbl_0[k] = true
        end
        self.host_names = _tbl_0
      end
      self.limits = config.limits
    end,
    __base = _base_0,
    __name = "Runtime"
  }, {
    __index = _base_0,
    __call = function(cls, ...)
      local _self_0 = setmetatable({}, _base_0)
      cls.__init(_self_0, ...)
      return _self_0
    end
  })
  _base_0.__class = _class_0
  Runtime = _class_0
end
return {
  Runtime = Runtime,
  plain_copy = plain_copy
}