|
@@ -206,22 +206,38 @@ static int str_char (lua_State *L) {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-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);
|
|
|
|
|
|
+/*
|
|
|
|
+** Buffer to store the result of 'string.dump'. It must be initialized
|
|
|
|
+** after the call to 'lua_dump', to ensure that the function is on the
|
|
|
|
+** top of the stack when 'lua_dump' is called. ('luaL_buffinit' might
|
|
|
|
+** push stuff.)
|
|
|
|
+*/
|
|
|
|
+struct str_Writer {
|
|
|
|
+ int init; /* true iff buffer has been initialized */
|
|
|
|
+ luaL_Buffer B;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int writer (lua_State *L, const void *b, size_t size, void *ud) {
|
|
|
|
+ struct str_Writer *state = (struct str_Writer *)ud;
|
|
|
|
+ if (!state->init) {
|
|
|
|
+ state->init = 1;
|
|
|
|
+ luaL_buffinit(L, &state->B);
|
|
|
|
+ }
|
|
|
|
+ luaL_addlstring(&state->B, (const char *)b, size);
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
static int str_dump (lua_State *L) {
|
|
static int str_dump (lua_State *L) {
|
|
- luaL_Buffer b;
|
|
|
|
|
|
+ struct str_Writer state;
|
|
int strip = lua_toboolean(L, 2);
|
|
int strip = lua_toboolean(L, 2);
|
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
|
luaL_checktype(L, 1, LUA_TFUNCTION);
|
|
- lua_settop(L, 1);
|
|
|
|
- luaL_buffinit(L,&b);
|
|
|
|
- if (lua_dump(L, writer, &b, strip) != 0)
|
|
|
|
|
|
+ lua_settop(L, 1); /* ensure function is on the top of the stack */
|
|
|
|
+ state.init = 0;
|
|
|
|
+ if (lua_dump(L, writer, &state, strip) != 0)
|
|
return luaL_error(L, "unable to dump given function");
|
|
return luaL_error(L, "unable to dump given function");
|
|
- luaL_pushresult(&b);
|
|
|
|
|
|
+ luaL_pushresult(&state.B);
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
|