lua-users home
lua-l archive

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


On Thu, 2004-11-04 at 13:08 -0500, Rici Lake wrote:
> "making safe" is important. A script, even a malicious one, ought not
> to be able to make Lua segfault or execute arbitrary C code. To prevent
> that, userdata methods must check the type of their self argument.

That is a good point

> Arguably, that is good programming style, but it is quite expensive to
> do on every method call. So if it is possible to guarantee that the
> type of the self argument is correct, that is probably better.

So create a C closure for each object/method pair and lock the object in
as an upvalue discarding the object at index 1 if it is the same as the
upvalue. (this is what I did for a while when I had decided I disliked
the : syntax, I've since changed back)

Another alternative if you're worried is to do this:

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

D.

-- 
Daniel Silverstone                         http://www.digital-scurf.org/
PGP mail accepted and encouraged.            Key Id: 2BC8 4016 2068 7895