[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: patching userdata metatable.... ouch!
- From: Edgar Toernig <froese@...>
- Date: Wed, 17 Dec 2003 19:59:00 +0100
Just some style comments...
> SetInheritance = function(obj, parent)
> local nmt = {}
> local omt = getmetatable(obj)
>
> nmt.__index = function(object, key)
> local omt = omt -- Correct use of upvalues
> local parent = parent -- to store the original
> -- metatable and parent?
These two locals are not necessary. Simply use omt and
parent directly. You only need to make local copies if
the function wants to change omt/parent and these changes
shouldn't be visible to the outer function (SetInheritance).
> local result
>
> if (omt) then
The parentheses are not necessary.
> -- There should be a simpler way to
> -- express this lookup.
> if (istable(omt.__index) then
Same as above.
> result = omt.__index[key]
> else
> result = omt.__index(object, key)
> end
> end
>
> if result then return result end
>
> return parent[key]
The last 3 lines are often written as
return result or parent[key]
> end
>
> -- Presumably we need a C function to call
> -- setmetatable for us because Lua code
> -- isn't allowed to set metatables for
> -- a userdata
> application:SetObjectMetatable(obj, nmt)
> end
Ciao, ET.