diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-12-14 11:41:57 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-12-14 11:41:57 -0300 |
| commit | 4eda1acafa1a69224b2d4f786cf1ec8f7a4d9ac5 (patch) | |
| tree | 474a56c1cbb1b109f945cb3765c6ebc918d35b37 /lstrlib.c | |
| parent | ad73b332240ef5b9bab1517517f63a1425dc7545 (diff) | |
| download | lua-4eda1acafa1a69224b2d4f786cf1ec8f7a4d9ac5.tar.gz lua-4eda1acafa1a69224b2d4f786cf1ec8f7a4d9ac5.tar.bz2 lua-4eda1acafa1a69224b2d4f786cf1ec8f7a4d9ac5.zip | |
Cleaner protocol between 'lua_dump' and writer function
'lua_dump' signals to the writer function the end of a dump, so that
is has more freedom when using the stack.
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 | ||
