|
On 13-Feb-07, at 10:42 PM, Wesley Smith wrote:
BTW, Is it common for environment tables to have metatables? In alot of the environment udata examples, I see lua_gettable(L, -2); when getting a field from the environment table as opposed to lua_rawget(). This makes sense for metatable access in the fallback case, but I can't think of a circumstance in which an environment metatable would be used.
My instinct is to always use lua_gettable rather than lua_rawget unless there is a really good reason to use lua_rawget, because it's more flexible. In this particular case, I had in mind that I might sometime want to set up a prototyping relationship between two objects, and using lua_rawget would preclude that.
In practice, there is very little speed difference between the two, particularly if the table has no metatable.
I don't know how common the use of userdata environment tables is, except in my own code -- where it's becoming quite common -- so I can't answer the question in general. The code I presented in that wiki page is my own, based on stuff I've been doing lately; it's not an "official" idiom, but I think it is quite handy.