lua-users home
lua-l archive

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

I think Jeremy know this way and it is a wrong way.
the __index and __newindex is called only that there is no the global variable. For variable Foo, it exised in the _G, so the __index or __newindex was not called.

You can create a proxy table and put all global variabl in the proxy table, but you must replace _G's __index and __newindex metamethod at first. In your __index and __newindex metamethod, you call the readers and writers respectively, then get value from proxy table or change value in the proxy table. 

> Date: Wed, 29 Aug 2007 21:46:22 +0200
> Subject: Re: Global variable __index and __newindex?
> From:
> To:
> > I need a way to provide an __index and __newindex method for global
> > variables. Is there an easy way to go about this?
> >
> > What I mean is that if I have a variable (call it Foo) when I use it I
> > want
> > it to call readers and writers in my source code instead. I know I can
> > overwrite the default global handlers but that seems a bit of the wrong
> > way.
> >
> > --
> > Jeremy
> >
> > "Help I suffer from the oxymoron Corporate Security."
> >
> you can call lua_setmetatable with LUA_GLOBALSINDEX as stack index, it
> will set the metable for the global table. Just set the __index and
> __newindex field as usual to the meta table and it should call your method
> when accessing globals
> Julien Hamaide

不登录就能管理多个邮件帐户,试试 Windows Live Mail。 立即尝试!