lua-users home
lua-l archive

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


Hi,

Is it possible to make a duplicate of a function in the C API? I'd like to be able to duplicate a function in order to set and retain a different environment on each copy.

function f() print(foo) end
tab1 = { foo = 1 }
tab2 = { foo  = 2 }
ftab1 = makedupenv(f, tab1) -- where makedupenv is a lua_CFunction as below
ftab2 = makedupenv(f, tab2)	
ftab1()	--> 	1
ftab2()	--> 	2


// arg 1 is a function
// arg 2 is a table
int makedupenv(lua_State * L)
{
	// Q? how to I make a copy of function f ?	

	lua_pushvalue(L, 2);	// push table
lua_setfenv(L, 1); // set environment of f // this should setfenv on a COPY of f.

	return 1;
}


Thanks in advance!