diff options
Diffstat (limited to 'lstrlib.c')
| -rw-r--r-- | lstrlib.c | 19 |
1 files changed, 12 insertions, 7 deletions
| @@ -225,7 +225,12 @@ static int writer (lua_State *L, const void *b, size_t size, void *ud) { | |||
| 225 | state->init = 1; | 225 | state->init = 1; |
| 226 | luaL_buffinit(L, &state->B); | 226 | luaL_buffinit(L, &state->B); |
| 227 | } | 227 | } |
| 228 | luaL_addlstring(&state->B, (const char *)b, size); | 228 | if (b == NULL) { /* finishing dump? */ |
| 229 | luaL_pushresult(&state->B); /* push result */ | ||
| 230 | lua_replace(L, 1); /* move it to reserved slot */ | ||
| 231 | } | ||
| 232 | else | ||
| 233 | luaL_addlstring(&state->B, (const char *)b, size); | ||
| 229 | return 0; | 234 | return 0; |
| 230 | } | 235 | } |
| 231 | 236 | ||
| @@ -233,13 +238,13 @@ static int writer (lua_State *L, const void *b, size_t size, void *ud) { | |||
| 233 | static int str_dump (lua_State *L) { | 238 | static int str_dump (lua_State *L) { |
| 234 | struct str_Writer state; | 239 | struct str_Writer state; |
| 235 | int strip = lua_toboolean(L, 2); | 240 | int strip = lua_toboolean(L, 2); |
| 236 | luaL_checktype(L, 1, LUA_TFUNCTION); | 241 | luaL_argcheck(L, lua_type(L, 1) == LUA_TFUNCTION && !lua_iscfunction(L, 1), |
| 237 | lua_settop(L, 1); /* ensure function is on the top of the stack */ | 242 | 1, "Lua function expected"); |
| 243 | /* ensure function is on the top of the stack and vacate slot 1 */ | ||
| 244 | lua_pushvalue(L, 1); | ||
| 238 | state.init = 0; | 245 | state.init = 0; |
| 239 | if (l_unlikely(lua_dump(L, writer, &state, strip) != 0)) | 246 | lua_dump(L, writer, &state, strip); |
| 240 | return luaL_error(L, "unable to dump given function"); | 247 | lua_settop(L, 1); /* leave final result on top */ |
| 241 | luaL_pushresult(&state.B); | ||
| 242 | lua_assert(lua_isfunction(L, 1)); /* lua_dump kept that value */ | ||
| 243 | return 1; | 248 | return 1; |
| 244 | } | 249 | } |
| 245 | 250 | ||
