lua-users home
lua-l archive

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


Title: global variable conflict
See "sandboxing" in the list archives.
http://lua-users.org/cgi-bin/namazu.cgi?query=sandboxing&idxname=lua-l&max=20&result=normal&sort=date%3Alate
 
see also the "more modular scripts" thread from 2 days ago (July 13, 2004).
 
More simply since you have not mentioned being concerned about protecting variables introduced by one script from overwriting by another script, but only protecting specific variables introduced by the C code, simply setting/using __newindex and __index metamethods on the globals table may suffice.  In this scheme's simplest form <AFAIK> you would NOT set these protected values in the globals table, but rather add them to another table and set that table as the __index member of the globals table's metatable.  The __newindex metamethod would simply need to check if any "new" global variables already exist in the table of "protected globals", and if so issue your warning, if not then it would go ahead and actually add the variable to the globals table by way of a rawset operation.
 
Note that the above scheme will not protect "nested" members of tables, but you could apply the method to each sub-table to be protected.
 
----
 
I haven't read it myself, but I'm fairly certain that Roberto's "Programming in Lua" book covers this exact issue.
http://www.lua.org/pil/

 

 
 
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br]On Behalf Of BONNARD Cyril
Sent: Thursday, July 15, 2004 8:55 AM
To: lua@bazar2.conectiva.com.br
Subject: global variable conflict

Hello everybody,

i am new in Lua world, so i may ask some stupid question...

 I am facing a problem with variable names. I have a set of global variables defined in C and passed to the LUA stack via the C API. This stack is left intact and reused for running some LUA script. The problem is that, in the scripts, some variables could have the same name than variables retreived from C code. This yield the erasement of the initial variables. Is there any easy way to solve the problem, such as overloading the behaviour of the assignment operator "=" to test if the current variable name is already in use and warn the user of the conflict.

Any remark is obviously welcome!

Regards, cyril.