lua-users home
lua-l archive

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


> Is this "getglobals" of a *number* a good idea? It sounds rather
dangerous
> (especially since it is prey to optimisation). =:-O

> Would people not just be better off passing in the global table that they
> wanted? Eg:
>     MyFunc(arg1,arg2,getglobals())
> or does this not work for some reason?

Conceivably, yes. But it's not as convenient.

One reason (for people addicted to globals) is that you do something like
this:

register_CGI_variables()
if SERVER_NAME == myhost then

and then you have access to those variables as "local" globals". That is,
register_CGI_variables creates a new globals table for the calling
function,
and puts the CGI variables in there.

I would have preferred:

local CGI = session:CGI()
if CGI.SERVER_NAME == myhost then
...

People who didn't like solution 2 would also not like having to say
register_CGI_variables(getglobals())  :)

The main reason to getglobals(L, 2) from C is to be able to feed it
into a newly created Lua closure. For example, loadfile() running in
a sandbox (a Lua function with restricted globals) should give the
Lua function it loads the same sandbox. Here you definitely do not
want to rely on the caller to provide a globals table, because they
might be able to supply the global (unsandboxed) globals table, and
anyway having to add getglobals() to every call to loadfile is a bit
ugly.

> This "getglobals" of a number makes me nervous :-O.

I agree. But what is the solution?

R.