lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Friday 07 February 2003 07:40, Bjorn wrote:
>
> Personally, I think that require should take a second
> argument which is the global table to be used for
> the required() filename. Of course, such a require could
> be written in Lua based on loadfile() but it would be nice
> to have it by default.

I would also like to see require have a optional second argument, 
but for now I use an include function written in Lua.

- Peter Shook

$ more try.lua incl.lua bob.lua bill.lua
::::::::::::::
try.lua
::::::::::::::

function include(filename, scope)
  local path = LUA_PATH or os.getenv'LUA_PATH' or '?.lua;?'
  for p in string.gfind(path, '[^;]+') do
    local f, err = loadfile(string.gsub(p, '?', filename))
    if f then
      setglobals(f, scope or getglobals(2))
      return f()
    end
    if not string.find(err, '^cannot read') then error(err, 2) end
  end
  error(string.format("cannot find '%s' in path '%s'", filename, path), 2)
end

local try_G = getglobals()
print('try_G =', try_G)

function whereami()
  local g = getglobals(2)
  local name
  for n,v in try_G do
    if v == g then
      name = n
      break
    end
  end
  print('! Now in', name or 'unknown', g)
end

whereami()

local function sep() print(string.rep('-',78)) end

sep()

print'hi'
include'bob'
print'wow'

sep()

sandbox = { include=include, print=print, whereami=whereami }
print('sandbox =', sandbox)

local src = [[
  include'incl.lua'
  b=2
  print('in sandbox: a=', a, 'b=', b)
  whereami()
]]

local code = assert(loadstring(src, 'source of code'))

print('before sandbox: a=', a, 'b=', b)
setglobals(code, sandbox)
code()  -- run code in sandbox
print('after sandbox: a=', a, 'b=', b)

sep()

include('bill', sandbox)

sep()

print'dump sandbox:'
for n,v in sandbox do print(n,v) end

::::::::::::::
incl.lua
::::::::::::::

print'this is incl'
whereami()
a = 1

::::::::::::::
bob.lua
::::::::::::::

print'this is bob'
whereami()

::::::::::::::
bill.lua
::::::::::::::

print'this is bill'
whereami()
bill = 33

$ lua try.lua
try_G = table: 0x8050528
! Now in        _G      table: 0x8050528
------------------------------------------------------------------------------
hi
this is bob
! Now in        _G      table: 0x8050528
wow
------------------------------------------------------------------------------
sandbox =       table: 0x80585e8
before sandbox: a=      nil     b=      nil
this is incl
! Now in        sandbox table: 0x80585e8
in sandbox: a=  1       b=      2
! Now in        sandbox table: 0x80585e8
after sandbox: a=       nil     b=      nil
------------------------------------------------------------------------------
this is bill
! Now in        sandbox table: 0x80585e8
------------------------------------------------------------------------------
dump sandbox:
a       1
whereami        function: 0x8056988
b       2
bill    33
print   function: 0x8050ec8
include function: 0x80537a0
$