lua-users home
lua-l archive

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

I think it might be most convenient to simply extend loadfile with an
optional first parameter, to stay consistent with the order of
loadin's parameters. Normal use of would be unaffected, but supplying
the environment as a parameter would have the additional effect of
using lua_setfenv() to change the file chunk's environment.

I wrote some pseudo-code earlier to demonstrate what I mean. It's
written here in Lua for clarity, but would be implemented in C so as
to have access to lua_setfenv().

function loadfile(env, filename)
  -- support loadfile(filename) form
  if filename == nil then
    filename = env
    env = nil

  local chunk = loadfile(filename)
  if env then
    setfenv(chunk, env) -- lua_setfenv in C

  return chunk

~Jonathan Castello