lua-users home
lua-l archive

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


On Sun, Aug 8, 2010 at 2:00 PM, Mark Hamburg <mark@grubmah.com> wrote:
> Why not the following function instead:
>
> function newproxy2( meta, env ) -- returns a userdata with the given metatable and environment (user value in 5.2)
>
> The existing approach of creating a userdata with a metatable and then using getmetatable to access it for further modification seems rather convoluted and also doesn't allow one to exploit the ability to hang a Lua table off of the userdata.
>
> Mark
>

You just have to use the debug library and this can be implemented manually:

----
function newproxy2(meta, env)
  local ud = newproxy(false)
  if meta then debug.setmetatable(ud, meta) end
  if env then debug.setfenv(ud, env) end
  return ud
end
----

I'm not sure about passing an env in, because userdata automatically
get one (at least that's what debug.getfenv(newproxy(false)) shows),
and the ease of sharing envs seems like a bad idea to me. I use the
env for object-local data usually.

-- 
~Jonathan