[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lua-l
Subject:    RE: How to hook a lua variable accessing
From:       "Huang, Baiyan" <Baiyan.Huang () morganstanley ! com>
Date:       2013-01-23 8:29:04
Message-ID: 2A646AA4039F5344B7A8BCB791BABA0C0E65B5 () IAWEX0003N1 ! msad ! ms ! com
[Download RAW message or body]

Cool!  Steve, 
How could I neglect  the power of meta table:) Thanks!

-Baiyan   
      

-----Original Message-----
From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On Behalf Of \
                steve donovan
Sent: 23 January 2013 16:23
To: Lua mailing list
Subject: Re: How to hook a lua variable accessing

On Wed, Jan 23, 2013 at 10:10 AM, Huang, Baiyan
<Baiyan.Huang@morganstanley.com> wrote:
> Then I want to remove this global variable, but I want to give a buffer time
> for our user to off board, so I stage the change and firstly it will emit an
> warning if user is using this global variable.  Is there a way to achieve
> this without using LuaMacros?

Sure, but you have to attach a metatable to _G, that tracks
__newindex.  For this to fire on GLOBAL_VARIABLE this variable must
not be in _G already, so __index is defined to look it up.

Something like this:

local global_variable = 'boo'

local function message(writing)
   print 'you are still using GLOBAL_VARIABLE'
end

setmetatable(_G,{
   __index = function (t, k)
       if k == 'GLOBAL_VARIABLE' then
            message(false)
            return global_variable
        end
  end;
  __newindex = function(t,k,v)
     if k == 'GLOBAL_VARIABLE' then
         message(true)
         global_variable = v
     end
 end;
})

Easy to generalize for a whole bunch of 'globals' that you want to deprecate.

steve d.



--------------------------------------------------------------------------------

NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or views \
contained herein are not intended to be, and do not constitute, advice within the \
meaning of Section 975 of the Dodd-Frank Wall Street Reform and Consumer Protection \
Act. If you have received this communication in error, please destroy all electronic \
and paper copies and notify the sender immediately. Mistransmission is not intended \
to waive confidentiality or privilege. Morgan Stanley reserves the right, to the \
extent permitted under applicable law, to monitor electronic communications. This \
message is subject to terms available at the following link: \
http://www.morganstanley.com/disclaimers. If you cannot access these links, please \
notify us by reply message and we will send the contents to you. By messaging with \
Morgan Stanley you consent to the foregoing.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic