lua-users home
lua-l archive

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


Or better yet something like array.prototype?

On Thu, May 3, 2012 at 1:53 PM, Andrew Budd <andrew.budd@gmail.com> wrote:
Ahh ok that works.  Is there any way to do that other than calling a method that would return something with the appropriate metatable?  Could I stuff a reference to the metatable (what is coming back from getmetatable) into the global variable space from C when the lib is loaded?


On Thu, May 3, 2012 at 1:49 PM, joao lobato <btnfdp.lobato@gmail.com> wrote:
On 5/3/12, Andrew Budd <andrew.budd@gmail.com> wrote:
> That's how I thought it should work, so if for the sake of an example I
> have something like this:
>
>
>     int luaopen_array (lua_State *L) {
>       luaL_newmetatable(L, "LuaBook.array");
>
>       lua_pushstring(L, "__index");
>       lua_pushvalue(L, -2);  /* pushes the metatable */
>       lua_settable(L, -3);  /* metatable.__index = metatable */
>
>       luaL_openlib(L, NULL, arraylib_m, 0);
>
>       luaL_openlib(L, "array", arraylib_f, 0);
>       return 1;
>     }
>
>
> (from online reference here: http://www.lua.org/pil/28.3.html)
>
> how would I add a method on the lua side?
>
> getmetatable(LuaBook.array) throws a attempt to index global 'LuaBook'
> (a nil value)
>
> what am I missing?
>

You are missing the fact that "LuaBook.array" is only a unique key to
the Registry.

Following that example, I believe what you want is:

mt = getmetatable(array.new(0))

that is, you want to get the metatable shared by all instances of the type.