lua-users home
lua-l archive

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


Am 08.10.2014 um 05:19 schrieb Tim Hill <drtimhill@gmail.com>:

> 
> On Oct 7, 2014, at 11:51 AM, Philipp Kraus <philipp.kraus@tu-clausthal.de> wrote:
> 
>> 
>> Sorry for my bad description, my Lua script ist added by the user, so I have got a script
>> with a lot of global functions, so on the dump process, I need to get all non-default functions.
>> I must do something like:
>> 
>> std::vector< std::string > function = lua_getglobalfunction();
>> for(std::size_t i=0; i < function.size(); ++i
>> {
>>    lua_getglobal( L, function[i].c_str() );
>>    lua_dump
>> }
>> 
> 
> Can you clarify your intent here? Are you trying to clone the *current state* of an existing lua_State, so that when cloned it can continue from the point of the clone, or are you trying to clone the original chunk of Lua code that was launched by the source state? Some of your posts seem to imply this.
> 
> If you just wish to clone the script and re-start it, there should be no need for a deep copy unless you have loaded scripts externally at run-time. Otherwise, as the other posters have noted, you will end up in a complete mess trying to find out what state to copy.
> 
> What is the ultimate goal of cloning the Lua state anyway? Why do you need a copy constructor?

I have the lua_state* pointer encapsulate by a class and with the class ctor the Lua script is loaded in the Lua state.
I would like to overload the Copy-Ctor and =-operator in a way, that the lua_state* is point to a new Lua state (lua_newstate is called) and the data from the right-hand-side is copy to the new state (in this case the Lua script is pushed into the new lua state), but I load the the Lua script with luaL_loadstring, because the script is set by a database call. My current code excerpt of the =-operator:

m_interpreter is the lua_state* class member

CInterpreter& CInterpreter::operator=(const CInterpreter& p_rhs)
{
	//luaL_checktype( p_rhs.m_interpreter, 1, LUA_TFUNCTION);
    	//lua_settop( p_rhs.m_interpreter, 1);

    	std::vector<char> l_buffer;

    	if (lua_dump( p_rhs.m_interpreter, CInterpreter::dump, &l_buffer))
        	throw std::runtime_error("dump error");

    	lua_close(m_interpreter);
    	m_interpreter = luaL_newstate();
    	luaL_openlibs(m_interpreter);
    	luaL_loadstring(m_interpreter, &l_buffer[0]);

    	return *this;
}

int CInterpreter::dump( lua_State* p_interpreter, const void* p_source, std::size_t p_size, void* p_target )
{
	std::vector<char>* l_target    = static_cast< std::vector<char>* >(p_target);
	const std::size_t l_beginindex = l_target->size();

	try {
		l_target->resize(l_beginindex + p_size);
		memcpy( &((*l_target)[l_beginindex]), p_source, p_size );
	} catch (...) { return 1; }

    return 0;
}

Hops that help to understand the problem

Thanks

Phil