lua-users home
lua-l archive

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


> If it bothers you though, it's easy enough to redefine loadstring to
> use the caller environment.

> do
>     local ls = loadstring
>     function loadstring(s, n)
>         return setfenv(ls(s, n), getfenv(2))
>     end
> end

(note: setfenv does not return its first argument, in contrast to
setmetatable)

It is not so easy: try the following example

do
    local ls = loadstring
    function loadstring(s, n)
        local c = ls(s, n)
        setfenv(c, getfenv(2))
        return c
    end
end

function oops()
    print "testing my loadstring..."
    return loadstring "print(100)"
end

oops()()


--
Wim