[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Possible to add methods in Lua to metatables defined in C?
- From: pansz <pan.shizhu@...>
- Date: Fri, 4 May 2012 11:40:09 +0800
On Fri, May 4, 2012 at 3:04 AM, Andrew Budd <andrew.budd@gmail.com> wrote:
> Probably already obvious to most, but this is how I achieved the desired
> .prototype functionality
>
> lua_getglobal(L,"array");
> lua_pushstring(L,"prototype");
> luaL_getmetatable(L,"LuaBook.array");
> lua_settable(L,-3);
>
> at the bottom of the luaopen_array function
If you don't want to call a function, this is the obvious way to go.
But you really do not need to do getglobal(). Note that you need to
return 1 in your luaopen_array() function, this means the array table
is on the stack top.
when require "array", the global variable 'array' will be assigned
after luaopen_array() returned (with the return value in stack top),
it seems that you cannot get the global value of array inside luaopen_
function.