lua-users home
lua-l archive

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



Well, you can always use the 'string.dump()' method taught to me right here a while back.

1. f is Lua function
2. s= string.dump(f) makes a string dump of it
3. g= loadstring(s) recreates the same function

Try on your own risk! May have side tastes... ;) And does not keep upvalues, either.

-asko


Graham Wakefield kirjoitti 27.2.2007 kello 6.18:

Ah, I should have said, a lua function, not a CFunction.

Just tried this and I get a EXC_BAD_ACCESS; which I guess is due to trying to convert a lua function into a lua_CFunction.

Nevermeind, and thanks anyway.

On Feb 26, 2007, at 7:53 PM, Mark Edgar wrote:

On 2/26/07, Graham Wakefield <lists@grahamwakefield.net> wrote:
Hi,

Is it possible to make a duplicate of a function in the C API?

I believe the following should work:

/* cfunction envtable -- newcfunction */
int makedupenv(lua_State * L)
{
 lua_CFunction f = lua_tocfunction(L, 1);
 luaL_argcheck(L, f != 0, 1, "expected C function");
 luaL_checktable(L, 2, LUA_TTABLE);
/* note that the copy will not have any upvalues, even if the original does */
 lua_pushcfunction(L, f);
 lua_pushvalue(L, 1);
 lua_setfenv(L, 1);
 return 1;
}

You can use lua_getupvalue() to duplicate the C function with all its upvalues.

    -Mark