lua-users home
lua-l archive

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


2010/1/12 Dravonk <dravonk@yahoo.de>:
> Daniel Silverstone schrieb:
>> So this would be blah = loadin(pathtoscript, envtab) rather than blah =
>> loadfile(pathtoscript) setfenv(blah, envtab) ?  That seems emminently
>> reasonable and pleasant to use.
>
> It would be
>  file = assert (io.open (path))
>  source = file:read ("*all")
>  file:close ()
>  func = loadin (env, source, "@"..path)
>
> But in order to address the "reuse"-issue again, with loadin() it would
> only be easy to reuse the source and compile the function each time again.

I may be missing something, but does this solve the multiple reuse problem?

function multienv(funcstr)
  return loadstring([[
    return function(_G)
      in _G do
        return function()
          ]]..funcstr..[[
        end
      end
    end
    ]]
  )()
end

> funcwrapper = multienv("print(foo)")
> funcwrapper({print=print, foo="hello"})()
hello

Alex