lua-users home
lua-l archive

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


Hello,

I try to load a dumped chunk and get the error: binary string: truncated precompiled chunk
My C++ source code shows:

class CInterpreter 
{

	lua_State* m_interpreter;
	std::string m_binaryscript;


	CInterpreter( const std::string& p_script ) :
	m_interpreter(luaL_newstate()),
	m_binaryscript()
	{
    		luaL_openlibs(m_interpreter);
		luaL_loadstring(m_interpreter, p_script.c_str());

		m_binaryscript.clear();
		if (lua_dump( m_interpreter, CInterpreter::dump, &m_binaryscript))
			throw std::runtime_error(„dump error");
		
		lua_pcall(m_interpreter, 0, LUA_MULTRET, 0);
	}


	static int dump( lua_State* p_interpreter, const void* p_source, std::size_t p_size, void* p_target )
	{
		try {
    		static_cast< std::string* >(p_target)->append( static_cast<const char*>(p_source), p_size );
		} catch (…) { return 1; }
    		return 0;
	}
}

In my main I do something like

CInterpreter x("function abcd() print(‚test') end“)
CInterpreter y(x.m_binaryscript) <- This line creates the truncated chunk error

IMHO luaL_loadstring adds the compiled script on the stack top, my Lua dump function adds the full data into a std::string,
because on a std::string the chunk is ended with \0 (I have tested the call with change the std::string to std::vector<char> but I get equal errors).
The lua_pcall runs the script, which is on the stack top, so I don’t see my mistake at the moment why the chunk with should be stored in the
std::string is incomplete.

Can anybody help me to create a correct dumping process?

Thanks a lot

Phil