[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Garbage Collector
- From: "Leandro Candido" <enclle@...>
- Date: Sun, 14 Dec 2003 17:32:40 -0200
Hello all,
This is a code for catchglobals:
-- begin code
function createo(n)
local t = { n = n }, mt
mt = { __gc = "gc" }
setmetatable(t,mt)
return t;
end
x = createo({})
function catchglobal( _function )
local tab = {}
local meta = {}
local catched = {}
function meta:__index( index )
local c = catched
local _G = _G
local i = ( rawget(c,index) or 0 ) + 1;
rawset(c,index,i);
return _G[index];
end
function meta:__newindex( index, value )
local c = catched
local _G = _G;
local i = ( rawget(c,index) or 0 ) + 1;
rawset(c,index,i);
_G[index] = value;
end
setmetatable(tab,meta);
return function(...)
local cat = catched;
local env = tab;
local func = _function;
local oldenv = getfenv(_func);
setfenv(func,env);
local result = { func(unpack(arg)) }
setfenv(func,oldenv);
return cat, unpack(result);
end
end
function test( HelloWorld )
helloworld = HelloWorld;-- forget local keyword
print(helloworld);
end
globalsvar = catchglobal ( test ) ("Hello World!")
print(type(globalsvar));
table.foreach(globalsvar,print);
globalsvar, result = catchglobal( createo ) ( {} );
table.foreach(globalsvar,print);
-- end code
Please, add indentation, etc... to the code, because when pasting the
code it lost format.
The God's Peace,
Leandro.
----- Original Message -----
From: "Leandro Candido" <enclle@click21.com.br>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Sunday, December 14, 2003 5:16 PM
Subject: Re: Garbage Collector
> Hello Edgar,
>
> See below.
>
> ----- Original Message -----
> From: "Edgar Toernig" <froese@gmx.de>
> To: "Lua list" <lua@bazar2.conectiva.com.br>
> Sent: Sunday, December 14, 2003 1:12 PM
> Subject: Re: Garbage Collector
>
>
> > Nicolas Noble wrote:
> > >
> > > How exactly does the __gc metamethod works ? It just
> > > seems I'm not using it the right way.
> > >[...]
> > > function createobj(n)
> > > local t = {n = n}, mt
> > > mt = {__gc = mygc}
> > > setmetatable(t, mt)
> > > return t
> > > end
> >
> > The __gc method is only for userdata objects. For other
> > types it's ignored.
> >
> > Btw, "local t = {n = n}, mt" looks wrong.
> >
> > Question: should Lua issue an error if there are
> > more expressions than variable names?
> ...
>
> Are you asking because the mt will be a global variable, and not a
> local?
> If yes, I think the best is to modify lua to allow the local keyword
> work for ,XXX too. I always make this error, and always need to rewrite
the
> code.
> If no, someone/you show me than what you are asking ;-).
>
> The God's Peace,
>
>
> Leandro.
>
>