From 4eda1acafa1a69224b2d4f786cf1ec8f7a4d9ac5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 14 Dec 2023 11:41:57 -0300 Subject: 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. --- lstrlib.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'lstrlib.c') diff --git a/lstrlib.c b/lstrlib.c index e29c09b9..a90c4fd1 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -225,7 +225,12 @@ static int writer (lua_State *L, const void *b, size_t size, void *ud) { state->init = 1; luaL_buffinit(L, &state->B); } - luaL_addlstring(&state->B, (const char *)b, size); + if (b == NULL) { /* finishing dump? */ + luaL_pushresult(&state->B); /* push result */ + lua_replace(L, 1); /* move it to reserved slot */ + } + else + luaL_addlstring(&state->B, (const char *)b, size); return 0; } @@ -233,13 +238,13 @@ static int writer (lua_State *L, const void *b, size_t size, void *ud) { static int str_dump (lua_State *L) { struct str_Writer state; int strip = lua_toboolean(L, 2); - luaL_checktype(L, 1, LUA_TFUNCTION); - lua_settop(L, 1); /* ensure function is on the top of the stack */ + luaL_argcheck(L, lua_type(L, 1) == LUA_TFUNCTION && !lua_iscfunction(L, 1), + 1, "Lua function expected"); + /* ensure function is on the top of the stack and vacate slot 1 */ + lua_pushvalue(L, 1); state.init = 0; - if (l_unlikely(lua_dump(L, writer, &state, strip) != 0)) - return luaL_error(L, "unable to dump given function"); - luaL_pushresult(&state.B); - lua_assert(lua_isfunction(L, 1)); /* lua_dump kept that value */ + lua_dump(L, writer, &state, strip); + lua_settop(L, 1); /* leave final result on top */ return 1; } -- cgit v1.2.3-55-g6feb