My writer for lua_dump is this:
static int buf_writer( lua_State *L, const void* b, size_t n, void* B ) {
(void)L;
luaL_addlstring((luaL_Buffer*) B, (const char *)b, n);
return 0;
}
the replacement happens inside
LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
if (l > 0) { /* avoid 'memcpy' when 's' can be NULL */
char *b = prepbuffsize(B, l, -1);
memcpy(b, s, l * sizeof(char));
luaL_addsize(B, l);
}
}
when
prepbuffsize is called, because my stack contains the function being dumped at the top (since that's what lua_dump needs), and prepbufsize does:
else { /* no box yet */
lua_remove(L, boxidx); /* remove placeholder */
where boxidx == -1, which removes the function off the top of the stack. The light user data initially pushed by luaL_buffinit is just below.
Benoit.