[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: luaL_loadstring error with dumped chunk
- From: Sean Conner <sean@...>
- Date: Fri, 10 Oct 2014 05:37:05 -0400
It was thus said that the Great Victor Bombi once stated:
> Once I had the same problem.
> Solution was given in the list:
> use luaL_loadbuffer instead of luaL_loadstring
What he said.
Another approach to use, instead of using lua_dump() is to use
string.dump(). A "proof-of-concept" (Lua 5.1, but should be easily
adaptable to 5.2 if that's what you are using):
#include <stdlib.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
int main(int argc,char *argv[])
{
lua_State *L;
lua_State *L2;
const char *origcode;
char *copy;
size_t size;
int rc;
if (argc == 1)
return EXIT_FAILURE;
L = luaL_newstate();
luaL_openlibs(L);
lua_settop(L,0);
/* luaL_loadstring() works here as well ... */
luaL_loadfile(L,argv[1]); /* -- func */
lua_getglobal(L,"string"); /* -- func tab-string */
lua_getfield(L,2,"dump"); /* -- func tab-string f-dump */
lua_pushvalue(L,1); /* -- func tab-string f-dump func */
lua_call(L,1,1); /* -- func tab-string bin-func */
origcode = luaL_checklstring(L,3,&size);
copy = malloc(size);
memcpy(copy,origcode,size);
lua_pop(L,2); /* -- func */
rc = lua_pcall(L,0,LUA_MULTRET,0);
if (rc != 0)
{
const char *err = lua_tostring(L,-1);
fprintf(stderr,"lua_pcall(L) = %s\n",err);
return EXIT_FAILURE;
}
L2 = luaL_newstate();
luaL_openlibs(L2);
rc = luaL_loadbuffer(L2,copy,size,"copy");
if (rc != 0)
{
fprintf(stderr,"luaL_loadstring(L2) = %d\n",rc);
return EXIT_FAILURE;
}
rc = lua_pcall(L2,0,LUA_MULTRET,0);
if (rc != 0)
{
const char *err = lua_tostring(L2,-1);
fprintf(stderr,"lua_pcall(L2) = %s\n",err);
return EXIT_FAILURE;
}
lua_close(L2);
lua_close(L);
return EXIT_SUCCESS;
}
-spc (string.dump() is there ... might as well use it)