[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Calling overloaded original functions
- From: "Greg Falcon" <veloso@...>
- Date: Fri, 9 May 2008 17:56:30 -0400
On Thu, May 8, 2008 at 4:31 PM, Till Harbaum / Lists <lists@harbaum.org> wrote:
> How is this done? And perhaps someone even knows how to do this from
> a c function?
>
> lua_pushcfunction(L, my_tostring);
> lua_setfield(L, LUA_GLOBALSINDEX, "tostring");
>
> Overwrites tostring, but how do i call the original function from my "my_tostring"?
To do the equivalent thing from the C API, instead of
lua_pushcfunction, use lua_pushcclosure. The latter takes an extra
integer argument, the number of values to pop off the stack and store
in the function object that gets created. So:
lua_getglobal(L, "tostring");
lua_pushcclosure(L, my_tostring, 1);
lua_setglobal(L, "tostring");
Then, inside my_tostring, you can access the original tostring
function with the pseudo index lua_upvalueindex(1).
That is, where you would have written
lua_getglobal(L, "tostring");
to access the original function object, you instead can write
lua_pushvalue(L, lua_upvalueindex(1));
Greg F