lua-users home
lua-l archive

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


That's because, I believe, loadfile and loadstring apply their own
environment to the loaded chunk, not the lexical environment.
Functions retain the environment they had when they were defined.

On Thu, Feb 18, 2010 at 11:55 PM, steve donovan
<steve.j.donovan@gmail.com> wrote:
> On Fri, Feb 19, 2010 at 9:47 AM, Geoff Leyland
> <geoff_leyland@fastmail.fm> wrote:
>> Oh, right, well that's ok then.  Thanks!
>
> Like so:
>
> Lua 5.2.0 (work2)  Copyright (C) 1994-2010 Lua.org, PUC-Rio
>> fn = loadin({x=1,y=2},"return x+y")
>> = fn()
> 3
>
>> Sandbox?  Maybe I'm naive in thinking that most of the work I do is unlikely to suffer security issues, but in that particular case, if you can change the the config file, you can just go and change the script that's reading it.
>
> Sorry, I was picking up on someone remarking on the io dependency
> needed. Point being that you can write a version of dofile that works
> as expected.
>
> Now, here is something that I expected to work, but no:
>
> -- test1.lua
> print(x+y)
>
> -- dofile.lua
> local loadfile = loadfile
> local fun
> in {x=1,y=2,print=print} do
>    fun = loadfile "test1.lua"
> end
> print(fun())
>
> But (alas) the created function is not given the environment!
>
> steve d.
>