lua-users home
lua-l archive

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

Gert wrote:
> * On 2009-12-11 Michal Kottman <> wrote  :
>> I have tried to achieve the same thing, and basically ended up
>> collectiong all the locals and upvalues using functions from debug
>> table, 
> Yes, I was afraid I would end up using the debug library. I just
> realized locals are not part of the environment - which would be my
> first intuitive guess - but live in registers instead.
> Ok, nothing I can do about it then, thanks for your help!

Well, if you know where you're loading the string then you might know
the local variables that are likely to be used by the loaded code. You
can put them in a table whose its metatable has an __index set to the
global table, like:

   var1 = "Hello"
   local function one()
      local var2 = "world"
      local code = "print(var1, var2)"
      chunk = loadstring(code)
      local locals = { var2 = var2, }
      setfenv(chunk, setmetatable(locals, { __index = _G }))


But that would require a little bit of maintenance as the code changes.


The information contained in this email transmission may contain proprietary and business 
sensitive information.  If you are not the intended recipient, you are hereby notified that 
any review, dissemination, distribution or duplication of this communication is strictly 
prohibited.  Unauthorized interception of this e-mail is a violation of law.  If you are not 
the intended recipient, please contact the sender by reply email and immediately destroy all 
copies of the original message.

Any technical data and/or information provided with or in this email may be subject to U.S. 
export controls law.  Export, diversion or disclosure contrary to U.S. law is prohibited.  
Such technical data or information is not to be exported from the U.S. or given to any foreign
person in the U.S. without prior written authorization of Elbit Systems of America and the 
appropriate U.S. Government agency.