lua_dump官方文档说明
[-0, +0, e]
int lua_dump (lua_State *L, lua_Writer writer, void *data, int strip);
Dumps a function as a binary chunk. Receives a Lua function on the top of the stack and produces a binary chunk that, if loaded again, results in a function equivalent to the one dumped. As it produces parts of the chunk, lua_dump
calls function writer
(see lua_Writer
) with the given data
to write them.
If strip
is true, the binary representation may not include all debug information about the function, to save space.
The value returned is the error code returned by the last call to the writer; 0 means no errors.
This function does not pop the Lua function from the stack.
下面是string扩展库中使用的例子
static int writer (lua_State *L, const void* b, size_t size, void* B) { (void)L; luaL_addlstring((luaL_Buffer*) B, (const char *)b, size); return 0; } static int str_dump (lua_State *L) { luaL_Buffer b; luaL_checktype(L, 1, LUA_TFUNCTION); lua_settop(L, 1); luaL_buffinit(L,&b); if (lua_dump(L, writer, &b) != 0) return luaL_error(L, "unable to dump given function"); luaL_pushresult(&b); return 1; }