[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: __methcall
- From: Rici Lake <lua@...>
- Date: Thu, 4 Nov 2004 13:25:14 -0500
On 4-Nov-04, at 1:15 PM, Daniel Silverstone wrote:
do
local _gmt = getmetatable
local _smt = setmetatable
function getmetatable(foo)
if type(foo) == "userdata" then
error("Cannot fetch a metatable from a userdata")
end
return _gmt(foo)
end
function setmetatable(foo,mt)
if type(foo) == "userdata" then
error("Cannot set a metatable on a userdata")
end
return _smt(foo,mt)
end
end
Oh yeah? Watch this:
function type(foo) return "Hey, I'm an object" end
meta = getmetatable(someUserdata)
:)
Of course, in real life I would change getmetatable in the startup C
code. It is not necessary to change setmetatable, by the way, but it is
interesting to note that the Lua basic library prevents Lua code from
setting a userdata's metatable, but not from getting a userdata's
metatable.
However, if you cannot get a userdata's metatable, setting the
metatable is not actually dangerous, in the sense that it might disable
the object's functionality, but it cannot be used to forge a self
argument to a metamethod. So there is actually an argument for allowing
setmetatable and forbidding getmetatable.
I know there are ways of doing it (I use the closure technique a lot);
but it is worth thinking about whether there are ways that the language
can just guarantee it for you. Maybe not. Maybe so.