lua-users home
lua-l archive

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

On 12/01/2010 15:55, Jerome Vuarand wrote:

One way to restrict such abusive uses, which can easily become a mess
(I guess all agree on that), would have been to restrict
setfenv/getfenv so they no longer can take their integer parameter,
ie. they can only modified a function that is passed directly as a
reference rather than a call stack index.

I agree about setfenv not able to take call stack indexes, but how about getfenv? It allows me to run some function in the context of the caller.

Like this code to fix 'dofile' so the code it executes does not pollute _G with its globals.

How can this be rewritten as a general purpose function without getfenv?

local __old_dofile = dofile
function dofile(name)
	local f,e = loadfile(name)
	if not f then error(e, 2) end
	setfenv(f, getfenv(2))
	return f()

P.S. Is this behaviour of dofile going to change?